1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Sesha_Forms_Property extends Horde_Form
12: {
13: function __construct($vars)
14: {
15: parent::__construct($vars);
16:
17: $this->appendButtons(_("Save Property"));
18:
19: $types = array();
20: $datatypes = $GLOBALS['conf']['datatypes']['types'];
21: foreach ($datatypes as $d) {
22: $types[$d] = $d;
23: }
24:
25: $priorities = array();
26: for ($i = 0; $i < 100; $i++) {
27: $priorities[] = $i;
28: }
29:
30: $this->addHidden('', 'actionID', 'text', false, false, null);
31: $this->addHidden('', 'property_id', 'text', false, false, null);
32: $this->addVariable(_("Property Name"), 'property', 'text', true);
33:
34: $action = Horde_Form_Action::factory('submit');
35: $v = $this->addVariable(_("Data Type"), 'datatype', 'enum', true, false, null, array($types, true));
36: $v->setAction($action);
37: $v->setOption('trackchange', true);
38:
39: $this->addVariable(_("Unit"), 'unit', 'text', false);
40: $this->addVariable(_("Description"), 'description', 'longtext', false);
41: $this->addVariable(_("Sort Weight"), 'priority', 'enum', false, false, _("When properties are displayed, they will be shown in weight order from highest to lowest"), array($priorities));
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56:
57: public function validate($vars, $canAutoFill = false)
58: {
59: $this->_addParameters($vars);
60: return parent::validate($vars, $canAutoFill);
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
77:
78: public function renderActive($renderer, $vars, $action, $method = 'get', $enctype = null, $focus = true)
79: {
80: if ($vars->get('old_datatype') === null) {
81: $this->_addParameters($vars);
82: }
83: parent::renderActive($renderer, $vars, $action, $method, $enctype, $focus);
84: }
85:
86: protected function _addParameters($vars)
87: {
88: $dataType = $vars->get('datatype');
89: $className = $this->_buildTypeClassname($dataType);
90: if (empty($dataType)) {
91:
92: } elseif (!$className) {
93: $GLOBALS['notification']->push(sprintf(_("The form field type \"%s\" doesn't exist."), $dataType), 'horde.error');
94: } else {
95: $params = call_user_func(array($className, 'about'));
96: if (isset($params['params'])) {
97: foreach ($params['params'] as $name => $param) {
98: $field_id = 'parameters[' . $name . ']';
99: $param['required'] = isset($param['required'])
100: ? $param['required']
101: : null;
102: $param['readonly'] = isset($param['readonly'])
103: ? $param['readonly']
104: : null;
105: $param['desc'] = isset($param['desc'])
106: ? $param['desc']
107: : null;
108: $this->insertVariableBefore('unit', $param['label'],
109: $field_id, $param['type'],
110: $param['required'],
111: $param['readonly'],
112: $param['desc']);
113: $vars->set('old_datatype', $dataType);
114: }
115: }
116: }
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127:
128:
129: protected function _buildTypeClassname($dataType)
130: {
131: if (class_exists('Horde_Form_Type_' . $dataType)) {
132: return 'Horde_Form_Type_' . $dataType;
133: } elseif (class_exists('Sesha_Forms_Type_' . ucfirst($dataType))) {
134: return 'Sesha_Forms_Type_' . ucfirst($dataType);
135: } else {
136: return '';
137: }
138: }
139: }
140: