1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Ulaform_Driver_Sql extends Ulaform_Driver {
15:
16: 17: 18: 19: 20:
21: protected $_db;
22:
23: 24: 25: 26: 27:
28: protected $_charset;
29:
30: 31: 32: 33: 34: 35: 36:
37: public function __construct($params = array())
38: {
39: if (empty($params['db'])) {
40: throw new InvalidArgumentException('Missing required connection parameter(s).');
41: }
42: $this->_db = $params['db'];
43: $this->_charset = $params['charset'];
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function saveForm(&$info)
57: {
58: $values = array();
59: if (!empty($info['form_id'])) {
60: $values[] = (int)$info['form_id'];
61: }
62:
63:
64: $info['form_params'] = Horde_Serialize::serialize($info['form_params'], Horde_Serialize::UTF7_BASIC);
65:
66: array_unshift($values,
67: $GLOBALS['registry']->getAuth(),
68: Horde_String::convertCharset($info['form_name'], 'UTF-8', $this->_charset),
69: $info['form_action'],
70: Horde_String::convertCharset($info['form_params'], 'UTF-8', $this->_charset),
71: $info['form_onsubmit']);
72:
73: if (empty($info['form_id'])) {
74: $sql = 'INSERT INTO ulaform_forms (user_uid, form_name, form_action, form_params, form_onsubmit) VALUES (?, ?, ?, ?, ?)';
75: try {
76: $info['form_id'] = $this->_db->insert($sql, $values);
77: } catch (Horde_Db_Exception $e) {
78: throw new Ulaform_Exception($e->getMessage());
79: }
80: } else {
81: $sql = 'UPDATE ulaform_forms SET user_uid = ?, form_name = ?, form_action = ?, form_params = ?, form_onsubmit = ? WHERE form_id = ?';
82: try {
83: $this->_db->execute($sql, $values);
84: } catch (Horde_Db_Exception $e) {
85: throw new Ulaform_Exception($e->getMessage());
86: }
87: }
88:
89: return $info['form_id'];
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103:
104: public function saveField(&$info)
105: {
106: if (empty($info['form_id'])) {
107: throw new Horde_Exception_NotFound(_("Missing form"));
108: }
109:
110: $values = array();
111: if (!empty($info['field_id'])) {
112: $values[] = $info['field_id'];
113: } else {
114: if (empty($info['field_order'])) {
115: $info['field_order'] = $this->_nextFieldOrder($info['form_id']);
116: }
117: }
118:
119:
120: $info['field_required'] = ($info['field_required'] ? 1 : 0);
121: $info['field_readonly'] = ($info['field_readonly'] ? 1 : 0);
122:
123: if (!empty($info['field_params'])) {
124: $info['field_params'] = Horde_Serialize::serialize($info['field_params'], Horde_Serialize::UTF7_BASIC);
125: } else {
126: $info['field_params'] = null;
127: }
128:
129: array_unshift($values,
130: $info['form_id'],
131: Horde_String::convertCharset($info['field_name'], 'UTF-8', $this->_charset),
132: Horde_String::convertCharset($info['field_label'], 'UTF-8', $this->_charset),
133: $info['field_type'],
134: Horde_String::convertCharset($info['field_params'], 'UTF-8', $this->_charset),
135: $info['field_required'],
136: $info['field_readonly'],
137: Horde_String::convertCharset($info['field_desc'], 'UTF-8', $this->_charset),
138: $info['field_order']);
139:
140: if (empty($info['field_id'])) {
141: $sql = 'INSERT INTO ulaform_fields (form_id, field_name, field_label, field_type, field_params, field_required, field_readonly, field_desc, field_order) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
142: try {
143: $this->_db->execute($sql, $values);
144: } catch (Horde_Db_Exception $e) {
145: throw new Ulaform_Exception($e->getMessage());
146: }
147: } else {
148: $sql = 'UPDATE ulaform_fields SET form_id = ?, field_name = ?, field_label = ?, field_type = ?, field_params = ?, field_required = ?, field_readonly = ?, field_desc = ?, field_order = ? WHERE field_id = ?';
149: try {
150: $this->_db->execute($sql, $values);
151: } catch (Horde_Db_Exception $e) {
152: throw new Ulaform_Exception($e->getMessage());
153: }
154: }
155:
156: return true;
157: }
158:
159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
169: public function sortFields(&$info)
170: {
171: if (empty($info['form_id'])) {
172: throw new Horde_Exception_NotFound(_("Missing form"));
173: }
174:
175: foreach ($info['field_order'] as $field_order => $field_id) {
176: $sql = 'UPDATE ulaform_fields
177: SET field_order = ?
178: WHERE field_id = ?';
179: try {
180: $this->_db->execute($sql, array((int)$field_order, (int)$field_id));
181: } catch (Horde_Db_Exception $e) {
182: throw new Ulaform_Exception($e->getMessage());
183: }
184: }
185:
186: return true;
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function getForms($form_id = null)
196: {
197: $wsql = '';
198: $values = array();
199: if (!is_null($form_id)) {
200: $wsql = ' WHERE form_id = ?';
201: $values[] = (int)$form_id;
202: }
203:
204:
205: $sql = 'SELECT form_id, user_uid, form_name, form_action, form_params,'
206: . ' form_onsubmit FROM ulaform_forms' . $wsql;
207: try {
208: $result = $this->_db->selectAll($sql, $values);
209: } catch (Horde_Db_Exception $e) {
210: throw new Ulaform_Exception($e->getMessage());
211: }
212:
213: return Ulaform::checkPermissions($result, 'form', Horde_Perms::SHOW, 'form_id');
214: }
215:
216: 217: 218: 219: 220: 221:
222: public function getAvailableForms()
223: {
224:
225: $sql = 'SELECT form_id, user_uid, form_name, form_action, form_params,'
226: . ' form_onsubmit FROM ulaform_forms';
227: try {
228: return $this->_db->selectAll($sql);
229: } catch (Horde_Db_Exception $e) {
230: throw new Ulaform_Exception($e->getMessage());
231: }
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241: 242: 243:
244: public function getForm($form_id, $permission = Horde_Perms::SHOW)
245: {
246:
247: if (!parent::hasPermission($permission, $form_id)) {
248: throw new Horde_Exception_PermissionDenied(_("You don't have the right permission to access this form."));
249: }
250:
251:
252: $sql = 'SELECT form_id, user_uid, form_name, form_action, form_params,'
253: . ' form_onsubmit FROM ulaform_forms WHERE form_id = ?';
254: try {
255: $form = $this->_db->selectOne($sql, array((int)$form_id));
256: } catch (Horde_Db_Exception $e) {
257: throw new Ulaform_Exception($e->getMessage());
258: }
259:
260:
261: if (empty($form)) {
262: throw new Horde_Exception_NotFound(sprintf(_("No such form ID \"%s\"."), $form_id));
263: }
264:
265:
266: $form['form_params'] = Horde_Serialize::unserialize($form['form_params'], Horde_Serialize::UTF7_BASIC);
267:
268: return $form;
269: }
270:
271: 272: 273: 274: 275: 276: 277: 278:
279: public function getFields($form_id, $field_id = null)
280: {
281: $values = array($form_id);
282: $sql = 'SELECT field_id, form_id, field_name, field_order, field_label, field_type, '
283: . ' field_params, field_required, field_readonly, field_desc FROM ulaform_fields '
284: . ' WHERE form_id = ?';
285:
286: if (!is_null($field_id)) {
287: $sql .= ' AND field_id = ?';
288: $values[] = (int)$field_id;
289: }
290: $sql .= ' ORDER BY field_order';
291:
292: try {
293: $results = $this->_db->selectAll($sql, $values);
294: } catch (Horde_Db_Exception $e) {
295: throw new Ulaform_Exception($e);
296: }
297:
298: $fields = array();
299: foreach ($results as $field) {
300:
301: if (empty($field['field_name'])) {
302: $field['field_name'] = 'field_' . $field['field_id'];
303: }
304:
305:
306: if (!empty($field['field_params'])) {
307: $field['field_params'] = Horde_Serialize::unserialize($field['field_params'], Horde_Serialize::UTF7_BASIC);
308: } else {
309: $field['field_params'] = null;
310: }
311: $fields[] = $field;
312: }
313:
314: return $fields;
315: }
316:
317: 318: 319: 320: 321: 322: 323: 324:
325: public function deleteForm($form_id)
326: {
327:
328: $sql = 'DELETE FROM ulaform_forms WHERE form_id = ?';
329: try {
330: $this->_db->execute($sql, array((int)$form_id));
331: } catch (Horde_Db_Exception $e) {
332: throw new Ulaform_Exception($e->getMessage());
333: }
334:
335:
336: $sql = 'DELETE FROM ulaform_fields WHERE form_id = ?';
337: try {
338: $this->_db->execute($sql, array((int)$form_id));
339: } catch (Horde_Db_Exception $e) {
340: throw new Ulaform_Exception($e->getMessage());
341: }
342:
343: return true;
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: public function deleteField($field_id)
355: {
356:
357: $sql = 'DELETE FROM ulaform_fields WHERE field_id = ?';
358: try {
359: $this->_db->execute($sql, array((int)$field_id));
360: } catch (Horde_Db_Exception $e) {
361: throw new Ulaform_Exception($e->getMessage());
362: }
363:
364: return true;
365: }
366:
367: 368: 369: 370: 371: 372: 373: 374:
375: protected function _nextFieldOrder($form_id)
376: {
377: $sql = 'SELECT MAX(field_order) FROM ulaform_fields WHERE form_id = ?';
378: try {
379: return $this->_db->selectValue($sql, array($form_id)) + 1;
380: } catch (Horde_Db_Exception $e) {
381: throw new Ulaform_Exception($e->getMessage);
382: }
383: }
384:
385: }
386: