Example #1
0
 /**
  * Generates the configuration file items for a part of the configuration
  * tree.
  *
  * @param array $section             An associative array containing the
  *                                   part of the traversed XML
  *                                   configuration tree that should be
  *                                   processed.
  * @param string $prefix             A configuration prefix determining
  *                                   the current position inside the
  *                                   configuration file. This prefix will
  *                                   be translated to keys of the $conf
  *                                   array in the generated configuration
  *                                   file.
  * @param Horde_Variables $formvars  The processed configuration form
  *                                   data.
  */
 protected function _generatePHPConfig($section, $prefix, $formvars)
 {
     if (!is_array($section)) {
         return;
     }
     foreach ($section as $name => $configitem) {
         if (is_array($configitem) && isset($configitem['tab'])) {
             continue;
         }
         $prefixedname = empty($prefix) ? $name : $prefix . '|' . $name;
         $configname = str_replace('|', '__', $prefixedname);
         $quote = !isset($configitem['quote']) || $configitem['quote'] !== false;
         if ($configitem == 'placeholder') {
             $this->_phpConfig .= '$conf[\'' . str_replace('|', '\'][\'', $prefix) . "'] = array();\n";
         } elseif (isset($configitem['switch'])) {
             $val = $formvars->getExists($configname, $wasset);
             if (!$wasset) {
                 $val = isset($configitem['default']) ? $configitem['default'] : null;
             }
             if (isset($configitem['switch'][$val])) {
                 $value = $val;
                 if ($quote && $value != 'true' && $value != 'false') {
                     $value = "'" . $value . "'";
                 }
                 $this->_generatePHPConfig($configitem['switch'][$val]['fields'], $prefix, $formvars);
             }
         } elseif (isset($configitem['_type'])) {
             $val = $formvars->getExists($configname, $wasset);
             if (!$wasset && (array_key_exists('is_default', $configitem) && $configitem['is_default'] || !array_key_exists('is_default', $configitem))) {
                 $val = isset($configitem['default']) ? $configitem['default'] : null;
             }
             $type = $configitem['_type'];
             switch ($type) {
                 case 'multienum':
                     if (is_array($val)) {
                         $encvals = array();
                         foreach ($val as $v) {
                             $encvals[] = $this->_quote($v);
                         }
                         $arrayval = "'" . implode('\', \'', $encvals) . "'";
                         if ($arrayval == "''") {
                             $arrayval = '';
                         }
                     } else {
                         $arrayval = '';
                     }
                     $value = 'array(' . $arrayval . ')';
                     break;
                 case 'boolean':
                     if (is_bool($val)) {
                         $value = $val ? 'true' : 'false';
                     } else {
                         $value = $val == 'on' ? 'true' : 'false';
                     }
                     break;
                 case 'stringlist':
                     $values = explode(',', $val);
                     if (!is_array($values)) {
                         $value = "array('" . $this->_quote(trim($values)) . "')";
                     } else {
                         $encvals = array();
                         foreach ($values as $v) {
                             $encvals[] = $this->_quote(trim($v));
                         }
                         $arrayval = "'" . implode('\', \'', $encvals) . "'";
                         if ($arrayval == "''") {
                             $arrayval = '';
                         }
                         $value = 'array(' . $arrayval . ')';
                     }
                     break;
                 case 'int':
                     if (strlen($val)) {
                         $value = (int) $val;
                     }
                     break;
                 case 'octal':
                     $value = sprintf('0%o', octdec($val));
                     break;
                 case 'header':
                 case 'description':
                     break;
                 default:
                     if ($val != '') {
                         $value = $val;
                         if ($quote && $value != 'true' && $value != 'false') {
                             $value = "'" . $this->_quote($value) . "'";
                         }
                     }
                     break;
             }
         } else {
             $this->_generatePHPConfig($configitem, $prefixedname, $formvars);
         }
         if (isset($value)) {
             $this->_phpConfig .= '$conf[\'' . str_replace('__', '\'][\'', $configname) . '\'] = ' . $value . ";\n";
         }
         unset($value);
     }
 }
Example #2
0
 /**
  * Builds the form based on the specified level of the configuration tree.
  *
  * @param array $config   The portion of the configuration tree for that
  *                        the form fields should be created.
  * @param string $prefix  A string representing the current position
  *                        inside the configuration tree.
  */
 protected function _buildVariables($config, $prefix = '')
 {
     if (!is_array($config)) {
         return;
     }
     foreach ($config as $name => $configitem) {
         $prefixedname = empty($prefix) ? $name : $prefix . '|' . $name;
         $varname = str_replace('|', '__', $prefixedname);
         if ($configitem == 'placeholder') {
             continue;
         } elseif (isset($configitem['tab'])) {
             $this->setSection($configitem['tab'], $configitem['desc']);
         } elseif (isset($configitem['switch'])) {
             $selected = $this->_vars->getExists($varname, $wasset);
             $var_params = array();
             $select_option = true;
             if (is_bool($configitem['default'])) {
                 $configitem['default'] = $configitem['default'] ? 'true' : 'false';
             }
             foreach ($configitem['switch'] as $option => $case) {
                 $var_params[$option] = $case['desc'];
                 if ($option == $configitem['default']) {
                     $select_option = false;
                     if (!$wasset) {
                         $selected = $option;
                     }
                 }
             }
             $name = '$conf[' . implode('][', explode('|', $prefixedname)) . ']';
             $desc = $configitem['desc'];
             $v =& $this->addVariable($name, $varname, 'enum', true, false, $desc, array($var_params, $select_option));
             if (array_key_exists('default', $configitem)) {
                 $v->setDefault($configitem['default']);
                 if ($this->_fillvars) {
                     $this->_vars->set($varname, $configitem['default']);
                 }
             }
             if (!empty($configitem['is_default'])) {
                 $v->_new = true;
             }
             $v_action = Horde_Form_Action::factory('reload');
             $v->setAction($v_action);
             if (isset($selected) && isset($configitem['switch'][$selected])) {
                 $this->_buildVariables($configitem['switch'][$selected]['fields'], $prefix);
             }
         } elseif (isset($configitem['_type'])) {
             $required = isset($configitem['required']) ? $configitem['required'] : true;
             $type = $configitem['_type'];
             if ($type == 'header' || $type == 'description') {
                 $required = false;
             }
             $var_params = $type == 'multienum' || $type == 'enum' ? array($configitem['values']) : array();
             if ($type == 'header' || $type == 'description') {
                 $name = $configitem['desc'];
                 $desc = null;
             } else {
                 $name = '$conf[' . implode('][', explode('|', $prefixedname)) . ']';
                 $desc = $configitem['desc'];
                 if ($type == 'php') {
                     $type = 'text';
                     $desc .= "\nEnter a valid PHP expression.";
                 }
             }
             $v =& $this->addVariable($name, $varname, $type, $required, false, $desc, $var_params);
             if (isset($configitem['default'])) {
                 $v->setDefault($configitem['default']);
                 if ($this->_fillvars) {
                     switch ($type) {
                         case 'boolean':
                             if ($configitem['default']) {
                                 $this->_vars->set($varname, 'on');
                             }
                             break;
                         case 'int':
                             $this->_vars->set($varname, (string) $configitem['default']);
                             break;
                         default:
                             $this->_vars->set($varname, $configitem['default']);
                             break;
                     }
                 }
             }
             if (!empty($configitem['is_default'])) {
                 $v->_new = true;
             }
         } else {
             $this->_buildVariables($configitem, $prefixedname);
         }
     }
 }