1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Ulaform_Api extends Horde_Registry_Api
16: {
17: 18: 19:
20: public function getForms()
21: {
22: return $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getForms();
23: }
24:
25: 26: 27: 28: 29:
30: public function getFormFields($form_id)
31: {
32: try {
33: $fields = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getFields($form_id);
34: } catch (Ulaform_Exception $e) {
35: throw new Ulaform_Exception($e->getMessage());
36: }
37:
38: foreach ($fields as $id => $field) {
39:
40: if ($field['field_type'] == 'link' ||
41: $field['field_type'] == 'enum' ||
42: $field['field_type'] == 'multienum' ||
43: $field['field_type'] == 'radio' ||
44: $field['field_type'] == 'set' ||
45: $field['field_type'] == 'sorter') {
46: $fields[$id]['field_params']['values'] = Ulaform::getStringlistArray($field['field_params']['values']);
47: }
48: }
49:
50: return $fields;
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function display($form_id, $target_url = null)
60: {
61:
62: try {
63: $form_info = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getForm($form_id, Horde_Perms::READ);
64: } catch (Horde_Exception $e) {
65: throw new Ulaform_Exception($e->getMessage());
66: }
67:
68: if (!empty($form_info['form_params']['language'])) {
69: Horde_Nls::setLanguageEnvironment($form_info['form_params']['language']);
70: }
71:
72: $vars = Horde_Variables::getDefaultVariables();
73: $vars->set('form_id', $form_id);
74:
75: $form = new Horde_Form($vars);
76: $form->addHidden('', 'form_id', 'int', false);
77: $form->addHidden('', 'user_uid', 'text', false);
78: $form->addHidden('', 'email', 'email', false);
79: $vars->set('user_uid', $GLOBALS['registry']->getAuth());
80: $vars->set('email', $GLOBALS['prefs']->getValue('from_addr'));
81:
82: try {
83: $fields = $GLOBALS['injector']->getInstance('Ulaform_Factory_Driver')->create()->getFields($form_id);
84: } catch (Ulaform_Exception $e) {
85: throw new Ulaform_Exception($e->getMessage());
86: }
87:
88: foreach ($fields as $field) {
89:
90: if ($field['field_type'] == 'link' ||
91: $field['field_type'] == 'enum' ||
92: $field['field_type'] == 'multienum' ||
93: $field['field_type'] == 'mlenum' ||
94: $field['field_type'] == 'radio' ||
95: $field['field_type'] == 'set' ||
96: $field['field_type'] == 'sorter') {
97: $field['field_params']['values'] = Ulaform::getStringlistArray($field['field_params']['values']);
98: }
99:
100:
101: $form->addVariable($field['field_label'], $field['field_name'], $field['field_type'], $field['field_required'], $field['field_readonly'], $field['field_desc'], $field['field_params']);
102: }
103:
104:
105: $result = array('title' => $form_info['form_name']);
106: if ($form->validate()) {
107: $form->getInfo(null, $info);
108: try {
109: $GLOBALS['ulaform_driver']->submitForm($info);
110: return true;
111: } catch (Horde_Exception $e) {
112: throw new Ulaform_Exception(sprintf(_("Error submitting form. %s."), $e->getMessage()));
113: }
114: }
115:
116: if (is_null($target_url)) {
117: $target_url = Horde::selfUrl(true);
118: }
119:
120: Horde::startBuffer();
121: $form->renderActive(null, null, $target_url, 'post', 'multipart/form-data');
122: return array('title' => $form_info['form_name'],
123: 'form' => Horde::endBuffer());
124: }
125:
126: }