1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Config_Form extends Horde_Form
16: {
17: 18: 19: 20: 21: 22: 23:
24: protected $_useFormToken = false;
25:
26: 27: 28: 29: 30:
31: protected $_xmlConfig;
32:
33: 34: 35: 36: 37:
38: protected $_vars;
39:
40: 41: 42: 43: 44: 45:
46: protected $_fillvars;
47:
48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function __construct(&$vars, $app, $fillvars = false)
59: {
60: parent::__construct($vars);
61:
62: $this->_xmlConfig = new Horde_Config($app);
63: $this->_vars = &$vars;
64: $this->_fillvars = $fillvars;
65: if ($fillvars) {
66: $vars->app = $app;
67: }
68: $config = $this->_xmlConfig->readXMLConfig();
69: $this->addHidden('', 'app', 'text', true);
70: $this->_buildVariables($config);
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80:
81: protected function _buildVariables($config, $prefix = '')
82: {
83: if (!is_array($config)) {
84: return;
85: }
86:
87: foreach ($config as $name => $configitem) {
88: $prefixedname = empty($prefix) ? $name : $prefix . '|' . $name;
89: $varname = str_replace('|', '__', $prefixedname);
90: if ($configitem == 'placeholder') {
91: continue;
92: } elseif (isset($configitem['tab'])) {
93: $this->setSection($configitem['tab'], $configitem['desc']);
94: } elseif (isset($configitem['switch'])) {
95: $selected = $this->_vars->getExists($varname, $wasset);
96: $var_params = array();
97: $select_option = true;
98: if (is_bool($configitem['default'])) {
99: $configitem['default'] = $configitem['default'] ? 'true' : 'false';
100: }
101: foreach ($configitem['switch'] as $option => $case) {
102: $var_params[$option] = $case['desc'];
103: if ($option == $configitem['default']) {
104: $select_option = false;
105: if (!$wasset) {
106: $selected = $option;
107: }
108: }
109: }
110:
111: $name = '$conf[' . implode('][', explode('|', $prefixedname)) . ']';
112: $desc = $configitem['desc'];
113:
114: $v = &$this->addVariable($name, $varname, 'enum', true, false, $desc, array($var_params, $select_option));
115: if (array_key_exists('default', $configitem)) {
116: $v->setDefault($configitem['default']);
117: if ($this->_fillvars) {
118: $this->_vars->set($varname, $configitem['default']);
119: }
120: }
121: if (!empty($configitem['is_default'])) {
122: $v->_new = true;
123: }
124: $v_action = Horde_Form_Action::factory('reload');
125: $v->setAction($v_action);
126: if (isset($selected) && isset($configitem['switch'][$selected])) {
127: $this->_buildVariables($configitem['switch'][$selected]['fields'], $prefix);
128: }
129: } elseif (isset($configitem['_type'])) {
130: $required = (isset($configitem['required'])) ? $configitem['required'] : true;
131: $type = $configitem['_type'];
132:
133: if (($type == 'header') || ($type == 'description')) {
134: $required = false;
135: }
136:
137: $var_params = ($type == 'multienum' || $type == 'enum')
138: ? array($configitem['values'])
139: : array();
140:
141: if ($type == 'header' || $type == 'description') {
142: $name = $configitem['desc'];
143: $desc = null;
144: } else {
145: $name = '$conf[' . implode('][', explode('|', $prefixedname)) . ']';
146: $desc = $configitem['desc'];
147: if ($type == 'php') {
148: $type = 'text';
149: $desc .= "\nEnter a valid PHP expression.";
150: }
151: }
152:
153: $v = &$this->addVariable($name, $varname, $type, $required, false, $desc, $var_params);
154: if (isset($configitem['default'])) {
155: $v->setDefault($configitem['default']);
156: if ($this->_fillvars) {
157: switch ($type) {
158: case 'boolean':
159: if ($configitem['default']) {
160: $this->_vars->set($varname, 'on');
161: }
162: break;
163: case 'int':
164: $this->_vars->set($varname, (string)$configitem['default']);
165: break;
166: default:
167: $this->_vars->set($varname, $configitem['default']);
168: break;
169: }
170: }
171: }
172: if (!empty($configitem['is_default'])) {
173: $v->_new = true;
174: }
175: } else {
176: $this->_buildVariables($configitem, $prefixedname);
177: }
178: }
179: }
180:
181: }
182: