From b7629a40efd996ed7e0464856cd4cadda15d0af7 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 20 Oct 2013 14:26:29 +0000 Subject: [PATCH] reduced code --- lam/lib/baseModule.inc | 19 +++++---- lam/lib/modules/freeRadius.inc | 66 +++---------------------------- lam/lib/modules/hostObject.inc | 49 +---------------------- lam/lib/modules/ieee802device.inc | 62 +++-------------------------- 4 files changed, 23 insertions(+), 173 deletions(-) diff --git a/lam/lib/baseModule.inc b/lam/lib/baseModule.inc index 9e6c88fe..71ab37f6 100644 --- a/lam/lib/baseModule.inc +++ b/lam/lib/baseModule.inc @@ -1230,7 +1230,8 @@ abstract class baseModule { $labelTextOut = new htmlOutputText($label); $labelTextOut->alignment = htmlElement::ALIGN_TOP; $container->addElement($labelTextOut); - $subContainer = new htmlGroup(); + $subContainer = new htmlTable(); + $subContainer->alignment = htmlElement::ALIGN_TOP; for ($i = 0; $i < sizeof($values); $i++) { if (!$isTextArea) { $input = new htmlInputField($attrName . '_' . $i, $values[$i]); @@ -1246,13 +1247,11 @@ abstract class baseModule { } $subContainer->addElement(new htmlInputTextarea($attrName . '_' . $i, $values[$i], $cols, 3)); } - if ($i > 0) { - $subContainer->addElement(new htmlOutputText('
', false)); - } - else { + $subContainer->addElement(new htmlButton('del_' . $attrName . '_' . $i, 'del.png', true)); + if ($i == 0) { $subContainer->addElement(new htmlButton('add_' . $attrName, 'add.png', true)); - $subContainer->addElement(new htmlOutputText('
', false)); } + $subContainer->addNewLine(); } $container->addElement($subContainer); $help = new htmlHelpLink($attrName); @@ -1273,12 +1272,12 @@ abstract class baseModule { $counter = 0; while (isset($_POST[$attrName . '_' . $counter])) { $this->attributes[$attrName][$counter] = $_POST[$attrName . '_' . $counter]; - if (($validationID != null) && ($this->attributes[$attrName][$counter] != '') && !get_preg($this->attributes[$attrName][$counter], $validationID)) { - $errors[] = $this->messages[$attrName][0]; - } - if ($this->attributes[$attrName][$counter] == '') { + if (($this->attributes[$attrName][$counter] == '') || isset($_POST['del_' . $attrName . '_' . $counter])) { unset($this->attributes[$attrName][$counter]); } + elseif (($validationID != null) && ($this->attributes[$attrName][$counter] != '') && !get_preg($this->attributes[$attrName][$counter], $validationID)) { + $errors[] = $this->messages[$attrName][0]; + } $counter++; } if (isset($_POST['add_' . $attrName])) { diff --git a/lam/lib/modules/freeRadius.inc b/lam/lib/modules/freeRadius.inc index fc787cc8..58d7d77e 100644 --- a/lam/lib/modules/freeRadius.inc +++ b/lam/lib/modules/freeRadius.inc @@ -263,64 +263,24 @@ class freeRadius extends baseModule { if (in_array('radiusprofile', $this->attributes['objectClass'])) { // realm if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusRealm')) { - $radiusRealm = ''; - if (isset($this->attributes['radiusRealm'][0])) { - $radiusRealm = $this->attributes['radiusRealm'][0]; - } - $return->addElement(new htmlTableExtendedInputField(_('Realm'), 'radiusRealm', $radiusRealm, 'radiusRealm'), true); + $this->addSimpleInputTextField($return, 'radiusRealm', _('Realm')); } // group names if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName')) { - $groups = array(); - if (isset($this->attributes['radiusGroupName'][0])) { - $groups = $this->attributes['radiusGroupName']; - } - if (sizeof($groups) == 0) { - $groups[] = ''; - } - $groupLabel = new htmlOutputText(_('Group names')); - $groupLabel->alignment = htmlElement::ALIGN_TOP; - $return->addElement($groupLabel); - $groupContainer = new htmlGroup(); - for ($i = 0; $i < sizeof($groups); $i++) { - $groupContainer->addElement(new htmlInputField('radiusGroupName' . $i, $groups[$i])); - if ($i < (sizeof($groups) - 1)) { - $groupContainer->addElement(new htmlOutputText('
', false)); - } - else { - $groupContainer->addElement(new htmlButton('addRadiusGroupName', 'add.png', true)); - } - } - $return->addElement($groupContainer); - $groupHelp = new htmlHelpLink('radiusGroupName'); - $groupHelp->alignment = htmlElement::ALIGN_TOP; - $return->addElement($groupHelp, true); + $this->addMultiValueInputTextField($return, 'radiusGroupName', _('Group names')); } // IP address if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusFramedIPAddress')) { - $radiusFramedIPAddress = ''; - if (isset($this->attributes['radiusFramedIPAddress'][0])) { - $radiusFramedIPAddress = $this->attributes['radiusFramedIPAddress'][0]; - } - $return->addElement(new htmlTableExtendedInputField(_('IP address'), 'radiusFramedIPAddress', $radiusFramedIPAddress, 'radiusFramedIPAddress'), true); + $this->addSimpleInputTextField($return, 'radiusFramedIPAddress', _('IP address')); } // net mask if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusFramedIPNetmask')) { - $radiusFramedIPNetmask = ''; - if (isset($this->attributes['radiusFramedIPNetmask'][0])) { - $radiusFramedIPNetmask = $this->attributes['radiusFramedIPNetmask'][0]; - } - $return->addElement(new htmlTableExtendedInputField(_('Net mask'), 'radiusFramedIPNetmask', $radiusFramedIPNetmask, 'radiusFramedIPNetmask'), true); + $this->addSimpleInputTextField($return, 'radiusFramedIPNetmask', _('Net mask')); } // idle timeout if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusIdleTimeout')) { - $radiusIdleTimeout = ''; - if (isset($this->attributes['radiusIdleTimeout'][0])) { - $radiusIdleTimeout = $this->attributes['radiusIdleTimeout'][0]; - } - $radiusIdleTimeoutInput = new htmlTableExtendedInputField(_('Idle timeout'), 'radiusIdleTimeout', $radiusIdleTimeout, 'radiusIdleTimeout'); + $radiusIdleTimeoutInput = $this->addSimpleInputTextField($return, 'radiusIdleTimeout', _('Idle timeout')); $radiusIdleTimeoutInput->setValidationRule(htmlElement::VALIDATE_NUMERIC); - $return->addElement($radiusIdleTimeoutInput, true); } // expiration date if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusExpiration')) { @@ -395,21 +355,7 @@ class freeRadius extends baseModule { } // group names if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusGroupName')) { - $groupCounter = 0; - while (isset($_POST['radiusGroupName' . $groupCounter])) { - $this->attributes['radiusGroupName'][$groupCounter] = $_POST['radiusGroupName' . $groupCounter]; - if ($this->attributes['radiusGroupName'][$groupCounter] == '') { - unset($this->attributes['radiusGroupName'][$groupCounter]); - } - elseif (!get_preg($this->attributes['radiusGroupName'][$groupCounter], 'groupname')) { - $errors[] = $this->messages['radiusGroupName'][0]; - } - $groupCounter++; - } - if (isset($_POST['addRadiusGroupName'])) { - $this->attributes['radiusGroupName'][] = ''; - } - $this->attributes['radiusGroupName'] = array_values(array_unique($this->attributes['radiusGroupName'])); + $this->processMultiValueInputTextField('radiusGroupName', $errors, 'groupname'); } // idle timeout if (!$this->isBooleanConfigOptionSet('freeRadius_hideRadiusIdleTimeout')) { diff --git a/lam/lib/modules/hostObject.inc b/lam/lib/modules/hostObject.inc index 4d6a1dfe..ad1bae79 100644 --- a/lam/lib/modules/hostObject.inc +++ b/lam/lib/modules/hostObject.inc @@ -135,24 +135,7 @@ class hostObject extends baseModule { } $return = new htmlTable(); if (in_array('hostObject', $this->attributes['objectClass'])) { - $hostCount = 0; - // list current hosts - if (isset($this->attributes['host'])) { - $hostCount = sizeof($this->attributes['host']); - for ($i = 0; $i < sizeof($this->attributes['host']); $i++) { - $return->addElement(new htmlOutputText(_('Host'))); - $return->addElement(new htmlInputField('host' . $i, $this->attributes['host'][$i])); - $return->addElement(new htmlButton('delHost' . $i, 'del.png', true)); - $return->addElement(new htmlHelpLink('host'), true); - } - } - // input box for new host - $return->addElement(new htmlOutputText(_('New host'))); - $return->addElement(new htmlInputField('host', '')); - $return->addElement(new htmlButton('addHost', 'add.png', true)); - $return->addElement(new htmlHelpLink('host')); - $return->addElement(new htmlHiddenInput('host_number', $hostCount), true); - + $this->addMultiValueInputTextField($return, 'host', _('Host')); $return->addElement(new htmlSpacer(null, '10px'), true); $remButton = new htmlButton('remObjectClass', _('Remove host extension')); $remButton->colspan = 4; @@ -180,35 +163,7 @@ class hostObject extends baseModule { return array(); } $errors = array(); - $this->attributes['host'] = array(); - // check old hosts - if (isset($_POST['host_number'])) { - for ($i = 0; $i < $_POST['host_number']; $i++) { - if (isset($_POST['delHost' . $i])) continue; - if (isset($_POST['host' . $i]) && ($_POST['host' . $i] != "")) { - // check if host has correct format - if (!get_preg($_POST['host' . $i], 'hostObject')) { - $message = $this->messages['host'][0]; - $message[] = $_POST['host' . $i]; - $errors[] = $message; - } - $this->attributes['host'][] = $_POST['host' . $i]; - } - } - } - // check new host - if (isset($_POST['host']) && ($_POST['host'] != "")) { - // check if host has correct format - if (get_preg($_POST['host'], 'hostObject')) { - $this->attributes['host'][] = $_POST['host']; - } - else { - $message = $this->messages['host'][0]; - $message[] = $_POST['host']; - $errors[] = $message; - } - } - $this->attributes['host'] = array_unique($this->attributes['host']); + $this->processMultiValueInputTextField('host', $errors, 'hostObject'); return $errors; } diff --git a/lam/lib/modules/ieee802device.inc b/lam/lib/modules/ieee802device.inc index f84772dc..c14c4008 100644 --- a/lam/lib/modules/ieee802device.inc +++ b/lam/lib/modules/ieee802device.inc @@ -57,7 +57,7 @@ class ieee802device extends baseModule { $return['attributes'] = array('macAddress'); // help Entries $return['help'] = array( - 'mac' => array( + 'macAddress' => array( "Headline" => _("MAC address"), 'attr' => 'macAddress', "Text" => _("This is the MAC address of the network card of the device (e.g. 00:01:02:DE:EF:18).") ), @@ -85,8 +85,8 @@ class ieee802device extends baseModule { * This function fills the error message array with messages */ function load_Messages() { - $this->messages['mac'][0] = array('ERROR', 'MAC address is invalid!'); // third array value is set dynamically - $this->messages['mac'][1] = array('ERROR', _('Account %s:') . ' ieee802Device_mac', 'MAC address is invalid!'); + $this->messages['macAddress'][0] = array('ERROR', 'MAC address is invalid!'); // third array value is set dynamically + $this->messages['macAddress'][1] = array('ERROR', _('Account %s:') . ' ieee802Device_mac', 'MAC address is invalid!'); } /** @@ -96,29 +96,7 @@ class ieee802device extends baseModule { */ function display_html_attributes() { $return = new htmlTable(); - $macCount = 0; - // list current MACs - if (isset($this->attributes['macAddress'])) { - $macCount = sizeof($this->attributes['macAddress']); - for ($i = 0; $i < sizeof($this->attributes['macAddress']); $i++) { - $return->addElement(new htmlOutputText(_('MAC address'))); - $macInput = new htmlInputField('macAddress' . $i, $this->attributes['macAddress'][$i]); - $macInput->setFieldSize(17); - $macInput->setFieldMaxLength(17); - $return->addElement($macInput); - $return->addElement(new htmlButton('delMAC' . $i, 'del.png', true)); - $return->addElement(new htmlHelpLink('mac'), true); - } - } - // input box for new MAC - $return->addElement(new htmlOutputText(_('New MAC address'))); - $newMacInput = new htmlInputField('macAddress', ''); - $newMacInput->setFieldSize(17); - $newMacInput->setFieldMaxLength(17); - $return->addElement($newMacInput); - $return->addElement(new htmlButton('addMAC', 'add.png', true)); - $return->addElement(new htmlHelpLink('mac')); - $return->addElement(new htmlHiddenInput('mac_number', $macCount)); + $this->addMultiValueInputTextField($return, 'macAddress', _('MAC address'), false, 17); return $return; } @@ -130,35 +108,7 @@ class ieee802device extends baseModule { */ function process_attributes() { $errors = array(); - $this->attributes['macAddress'] = array(); - // check old MACs - if (isset($_POST['mac_number'])) { - for ($i = 0; $i < $_POST['mac_number']; $i++) { - if (isset($_POST['delMAC' . $i])) continue; - if (isset($_POST['macAddress' . $i]) && ($_POST['macAddress' . $i] != "")) { - // check if address has correct format - if (!get_preg($_POST['macAddress' . $i], 'macAddress')) { - $message = $this->messages['mac'][0]; - $message[] = $_POST['macAddress' . $i]; - $errors[] = $message; - } - $this->attributes['macAddress'][] = $_POST['macAddress' . $i]; - } - } - } - // check new MAC - if (isset($_POST['macAddress']) && ($_POST['macAddress'] != "")) { - // check if address has correct format - if (get_preg($_POST['macAddress'], 'macAddress')) { - $this->attributes['macAddress'][] = $_POST['macAddress']; - } - else { - $message = $this->messages['mac'][0]; - $message[] = $_POST['macAddress']; - $errors[] = $message; - } - } - $this->attributes['macAddress'] = array_unique($this->attributes['macAddress']); + $this->processMultiValueInputTextField('macAddress', $errors, 'macAddress'); return $errors; } @@ -185,7 +135,7 @@ class ieee802device extends baseModule { $partialAccounts[$i]['macAddress'][] = $macs[$m]; } else { - $errMsg = $this->messages['mac'][1]; + $errMsg = $this->messages['macAddress'][1]; array_push($errMsg, array($i)); $messages[] = $errMsg; }