1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19:
20: class PifaAjaxHandler {
21:
22: 23: 24: 25: 26:
27: const GET_FIELD_FORM = 'pifa_get_field_form';
28:
29: 30: 31: 32: 33:
34: const POST_FIELD_FORM = 'pifa_post_field_form';
35:
36: 37: 38: 39:
40: const REORDER_FIELDS = 'pifa_reorder_fields';
41:
42: 43: 44: 45:
46: const EXPORT_DATA = 'pifa_export_data';
47:
48: 49: 50: 51:
52: const GET_FILE = 'pifa_get_file';
53:
54: 55: 56: 57:
58: const DELETE_FIELD = 'pifa_delete_field';
59:
60: 61: 62: 63:
64: const GET_OPTION_ROW = 'pifa_get_option_row';
65:
66: 67: 68: 69:
70: function dispatch($action) {
71: switch ($action) {
72:
73: case self::GET_FIELD_FORM:
74:
75: $idform = cSecurity::toInteger($_GET['idform']);
76: $idfield = cSecurity::toInteger($_GET['idfield']);
77: $fieldType = cSecurity::toInteger($_GET['field_type']);
78: $this->_getFieldForm($idform, $idfield, $fieldType);
79: break;
80:
81: case self::POST_FIELD_FORM:
82:
83: $idform = cSecurity::toInteger($_POST['idform']);
84: $idfield = cSecurity::toInteger($_POST['idfield']);
85:
86: $this->_postFieldForm($idform, $idfield);
87: break;
88:
89: case self::DELETE_FIELD:
90: $idfield = cSecurity::toInteger($_GET['idfield']);
91: $this->_deleteField($idfield);
92: break;
93:
94: case self::REORDER_FIELDS:
95: $idform = cSecurity::toInteger($_POST['idform']);
96: $idfields = implode(',', array_map('cSecurity::toInteger', explode(',', $_POST['idfields'])));
97: $this->_reorderFields($idform, $idfields);
98: break;
99:
100: case self::EXPORT_DATA:
101: $idform = cSecurity::toInteger($_GET['idform']);
102: $this->_exportData($idform);
103: break;
104:
105: case self::GET_FILE:
106: $name = cSecurity::toString($_GET['name']);
107: $file = cSecurity::toString($_GET['file']);
108: $this->_getFile($name, $file);
109: break;
110:
111: case self::GET_OPTION_ROW:
112: $index = cSecurity::toInteger($_GET['index']);
113: $this->_getOptionRow($index);
114: break;
115:
116: default:
117: $msg = Pifa::i18n('UNKNOWN_ACTION');
118: throw new Exception($msg);
119: }
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129:
130: private function _getFieldForm($idform, $idfield, $fieldType) {
131: $cfg = cRegistry::getConfig();
132:
133:
134: if (0 < $idfield) {
135:
136: $field = new PifaField();
137: $field->loadByPrimaryKey($idfield);
138: } elseif (0 < $fieldType) {
139:
140: $field = new PifaField();
141: $field->loadByRecordSet(array(
142: 'field_type' => $fieldType
143: ));
144: } else {
145:
146: $msg = Pifa::i18n('FORM_CREATE_ERROR');
147: throw new Exception($msg);
148: }
149:
150:
151: $optionClasses = Pifa::getExtensionClasses('PifaExternalOptionsDatasourceInterface');
152: array_unshift($optionClasses, array(
153: 'value' => '',
154: 'label' => Pifa::i18n('none')
155: ));
156:
157:
158: $tpl = Contenido_SmartyWrapper::getInstance(true);
159:
160:
161: $tpl->assign('trans', array(
162: 'idfield' => Pifa::i18n('ID'),
163: 'fieldRank' => Pifa::i18n('RANK'),
164: 'fieldType' => Pifa::i18n('FIELD_TYPE'),
165: 'columnName' => Pifa::i18n('COLUMN_NAME'),
166: 'label' => Pifa::i18n('LABEL'),
167: 'displayLabel' => Pifa::i18n('DISPLAY_LABEL'),
168: 'defaultValue' => Pifa::i18n('DEFAULT_VALUE'),
169: 'helpText' => Pifa::i18n('HELP_TEXT'),
170: 'rule' => Pifa::i18n('VALIDATION_RULE'),
171: 'errorMessage' => Pifa::i18n('ERROR_MESSAGE'),
172: 'database' => Pifa::i18n('DATABASE'),
173: 'options' => Pifa::i18n('OPTIONS'),
174: 'general' => Pifa::i18n('GENERAL'),
175: 'obligatory' => Pifa::i18n('OBLIGATORY'),
176: 'value' => Pifa::i18n('VALUE'),
177: 'addOption' => Pifa::i18n('ADD_OPTION'),
178: 'submitValue' => Pifa::i18n('SAVE'),
179: 'styling' => Pifa::i18n('STYLING'),
180: 'cssClass' => Pifa::i18n('CSS_CLASS'),
181: 'externalOptionsDatasource' => Pifa::i18n('EXTERNAL_OPTIONS_DATASOURCE')
182: ));
183:
184:
185: $tpl->assign('contenido', cRegistry::getBackendSessionId());
186: $tpl->assign('action', self::POST_FIELD_FORM);
187: $tpl->assign('idform', $idform);
188:
189:
190: $tpl->assign('field', $field);
191:
192:
193: $tpl->assign('cssClasses', explode(',', getEffectiveSetting('pifa', 'field-css-classes', 'half-row,full-row,line-bottom,line-top')));
194:
195:
196: $tpl->assign('optionClasses', $optionClasses);
197:
198:
199: $tpl->assign('hrefAddOption', 'main.php?' . implode('&', array(
200: 'area=form_ajax',
201: 'frame=4',
202: 'contenido=' . cRegistry::getBackendSessionId(),
203: 'action=' . PifaAjaxHandler::GET_OPTION_ROW
204: )));
205:
206:
207: $tpl->assign('partialOptionRow', $cfg['templates']['pifa_ajax_option_row']);
208:
209: $tpl->display($cfg['templates']['pifa_ajax_field_form']);
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: private function _postFieldForm($idform, $idfield) {
220: $string_cast_deep = create_function('$value', '
221: $value = cSecurity::unescapeDB($value);
222: $value = cSecurity::toString($value);
223: $value = trim($value);
224: // replace comma by comma entity
225: $value = str_replace(\',\', \',\', $value);
226: return $value;
227: ');
228:
229: global $area;
230: $cfg = cRegistry::getConfig();
231:
232:
233: if (0 < $idfield) {
234:
235: $pifaField = new PifaField($idfield);
236: if (!$pifaField->isLoaded()) {
237: $msg = Pifa::i18n('FIELD_LOAD_ERROR');
238: throw new Exception($msg);
239: }
240: $isFieldCreated = false;
241: } else {
242:
243: $fieldType = $_POST['field_type'];
244: $fieldType = cSecurity::toInteger($fieldType);
245:
246: $collection = new PifaFieldCollection();
247: $pifaField = $collection->createNewItem(array(
248: 'idform' => $idform,
249: 'field_type' => $fieldType
250: ));
251: $isFieldCreated = true;
252: }
253:
254:
255:
256: $oldColumnName = $pifaField->get('column_name');
257:
258:
259: $fieldRank = $_POST['field_rank'];
260: $fieldRank = cSecurity::toInteger($fieldRank);
261: if ($fieldRank !== $pifaField->get('field_rank')) {
262: $pifaField->set('field_rank', $fieldRank);
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272:
273:
274:
275:
276: if ($pifaField->showField('column_name')) {
277: $columnName = $_POST['column_name'];
278: $columnName = cSecurity::unescapeDB($columnName);
279: $columnName = cSecurity::toString($columnName);
280: $columnName = trim($columnName);
281: $columnName = strtolower($columnName);
282:
283:
284: $columnName = preg_replace('/[^a-z0-9_]/', '_', $columnName);
285: $columnName = substr($columnName, 0, 64);
286: if ($columnName !== $pifaField->get('column_name')) {
287: $pifaField->set('column_name', $columnName);
288: }
289: }
290:
291: if ($pifaField->showField('label')) {
292: $label = $_POST['label'];
293: $label = cSecurity::unescapeDB($label);
294: $label = cSecurity::toString($label);
295: $label = trim($label);
296: $label = substr($label, 0, 1023);
297: if ($label !== $pifaField->get('label')) {
298: $pifaField->set('label', $label);
299: }
300: }
301:
302: if ($pifaField->showField('display_label')) {
303: $displayLabel = $_POST['display_label'];
304: $displayLabel = cSecurity::unescapeDB($displayLabel);
305: $displayLabel = cSecurity::toString($displayLabel);
306: $displayLabel = trim($displayLabel);
307: $displayLabel = 'on' === $displayLabel? 1 : 0;
308: if ($displayLabel !== $pifaField->get('display_label')) {
309: $pifaField->set('display_label', $displayLabel);
310: }
311: }
312:
313: if ($pifaField->showField('default_value')) {
314: $defaultValue = $_POST['default_value'];
315: $defaultValue = cSecurity::unescapeDB($defaultValue);
316: $defaultValue = cSecurity::toString($defaultValue);
317: $defaultValue = trim($defaultValue);
318: $defaultValue = substr($defaultValue, 0, 1023);
319: if ($defaultValue !== $pifaField->get('default_value')) {
320: $pifaField->set('default_value', $defaultValue);
321: }
322: }
323:
324: if ($pifaField->showField('option_labels')) {
325: if (array_key_exists('option_labels', $_POST) && is_array($_POST['option_labels'])) {
326: $optionLabels = implode(',', array_map($string_cast_deep, $_POST['option_labels']));
327: $optionLabels = substr($optionLabels, 0, 1023);
328: }
329: if ($optionLabels !== $pifaField->get('option_labels')) {
330: $pifaField->set('option_labels', $optionLabels);
331: }
332: }
333:
334: if ($pifaField->showField('option_values')) {
335: if (array_key_exists('option_values', $_POST) && is_array($_POST['option_values'])) {
336: $optionValues = implode(',', array_map($string_cast_deep, $_POST['option_values']));
337: $optionValues = substr($optionValues, 0, 1023);
338: }
339: if ($optionValues !== $pifaField->get('option_values')) {
340: $pifaField->set('option_values', $optionValues);
341: }
342: }
343:
344: if ($pifaField->showField('help_text')) {
345: $helpText = $_POST['help_text'];
346: $helpText = cSecurity::unescapeDB($helpText);
347: $helpText = cSecurity::toString($helpText);
348: $helpText = trim($helpText);
349: if ($helpText !== $pifaField->get('help_text')) {
350: $pifaField->set('help_text', $helpText);
351: }
352: }
353:
354: if ($pifaField->showField('obligatory')) {
355: $obligatory = $_POST['obligatory'];
356: $obligatory = cSecurity::unescapeDB($obligatory);
357: $obligatory = cSecurity::toString($obligatory);
358: $obligatory = trim($obligatory);
359: $obligatory = 'on' === $obligatory? 1 : 0;
360: if ($obligatory !== $pifaField->get('obligatory')) {
361: $pifaField->set('obligatory', $obligatory);
362: }
363: }
364:
365: if ($pifaField->showField('rule')) {
366: $rule = $_POST['rule'];
367: $rule = cSecurity::unescapeDB($rule);
368: $rule = cSecurity::toString($rule);
369: $rule = trim($rule);
370: $rule = substr($rule, 0, 1023);
371:
372: if (0 === strlen($rule)) {
373: $pifaField->set('rule', $rule);
374: } else if (false === @preg_match($rule, 'And always remember: the world is an orange!')) {
375:
376: } else if ($rule === $pifaField->get('rule')) {
377:
378: } else {
379: $pifaField->set('rule', $rule);
380: }
381: }
382:
383: if ($pifaField->showField('error_message')) {
384: $errorMessage = $_POST['error_message'];
385: $errorMessage = cSecurity::unescapeDB($errorMessage);
386: $errorMessage = cSecurity::toString($errorMessage);
387: $errorMessage = trim($errorMessage);
388: $errorMessage = substr($errorMessage, 0, 1023);
389: if ($errorMessage !== $pifaField->get('error_message')) {
390: $pifaField->set('error_message', $errorMessage);
391: }
392: }
393:
394: if ($pifaField->showField('css_class') && array_key_exists('css_class', $_POST) && is_array($_POST['css_class'])) {
395: $cssClass = implode(',', array_map($string_cast_deep, $_POST['css_class']));
396: $cssClass = substr($cssClass, 0, 1023);
397: if ($cssClass !== $pifaField->get('css_class')) {
398: $pifaField->set('css_class', $cssClass);
399: }
400: }
401:
402: if ($pifaField->showField('option_class')) {
403: $optionClass = $_POST['option_class'];
404: $optionClass = cSecurity::unescapeDB($optionClass);
405: $optionClass = cSecurity::toString($optionClass);
406: $optionClass = trim($optionClass);
407: $optionClass = substr($optionClass, 0, 1023);
408: if ($optionClass !== $pifaField->get('option_class')) {
409: $pifaField->set('option_class', $optionClass);
410: }
411: }
412:
413:
414: $pifaForm = new PifaForm($idform);
415: try {
416: $pifaForm->storeColumn($pifaField, $oldColumnName);
417: } catch (PifaException $e) {
418:
419: if ($isFieldCreated) {
420:
421: $pifaField->delete();
422: } else {
423:
424: $pifaField->set('column_name', $oldColumnName);
425: }
426: throw $e;
427: }
428:
429:
430: if (false === $pifaField->store()) {
431: $msg = Pifa::i18n('FIELD_STORE_ERROR');
432: $msg = sprintf($msg, $pifaField->getLastError());
433: throw new PifaException($msg);
434: }
435:
436:
437:
438: if (true === $isFieldCreated) {
439:
440:
441: $sql = "-- PifaAjaxHandler->_editFieldFormKK()
442: UPDATE
443: " . cRegistry::getDbTableName('pifa_field') . "
444: SET
445: field_rank = field_rank + 1
446: WHERE
447: idform = " . cSecurity::toInteger($idform) . "
448: AND field_rank >= " . cSecurity::toInteger($fieldRank) . "
449: AND idfield <> " . cSecurity::toInteger($pifaField->get('idfield')) . "
450: ;";
451:
452: $db = cRegistry::getDb();
453: if (false === $db->query($sql)) {
454:
455:
456:
457: }
458: }
459:
460:
461: $editField = new cHTMLLink();
462: $editField->setCLink($area, 4, self::GET_FIELD_FORM);
463: $editField->setCustom('idform', $idform);
464: $editField = $editField->getHref();
465:
466: $deleteField = new cHTMLLink();
467: $deleteField->setCLink($area, 4, self::DELETE_FIELD);
468: $deleteField->setCustom('idform', $idform);
469: $deleteField = $deleteField->getHref();
470:
471: $tpl = Contenido_SmartyWrapper::getInstance(true);
472:
473:
474: $tpl->assign('trans', array(
475: 'edit' => Pifa::i18n('EDIT'),
476: 'delete' => Pifa::i18n('DELETE'),
477: 'obligatory' => Pifa::i18n('OBLIGATORY')
478: ));
479:
480:
481: $tpl->assign('field', $pifaField);
482:
483: $tpl->assign('editField', $editField);
484: $tpl->assign('deleteField', $deleteField);
485:
486: $tpl->display($cfg['templates']['pifa_ajax_field_row']);
487: }
488:
489: 490: 491: 492: 493:
494: private function _deleteField($idfield) {
495: if (0 == $idfield) {
496: $msg = Pifa::i18n('MISSING_IDFIELD');
497: throw new Exception($msg);
498: }
499:
500: $pifaField = new PifaField($idfield);
501: $pifaField->delete();
502: }
503:
504: 505: 506: 507: 508: 509:
510: private function _reorderFields($idform, $idfields) {
511: PifaFieldCollection::reorder($idform, $idfields);
512: }
513:
514: 515: 516: 517:
518: private function _exportData($idform) {
519:
520:
521: $pifaForm = new PifaForm($idform);
522: $filename = $pifaForm->get('data_table') . date('_Y_m_t_H_i_s') . '.csv';
523: $data = $pifaForm->getDataAsCsv();
524:
525:
526: session_cache_limiter('private');
527: session_cache_limiter('must-revalidate');
528:
529:
530: header('Pragma: cache');
531: header('Expires: 0');
532: header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
533: header('Cache-Control: private');
534: header('Content-Type: text/csv');
535: header('Content-Length: ' . strlen($data));
536: header('Content-Disposition: attachment; filename="' . $filename . '"');
537: header('Content-Transfer-Encoding: binary');
538:
539:
540: echo $data;
541: }
542:
543: 544: 545: 546: 547:
548: private function _getFile($name, $file) {
549: $cfg = cRegistry::getConfig();
550:
551: $path = $cfg['path']['contenido_cache'] . 'form_assistant/';
552:
553: $file = basename($file);
554:
555: header('Pragma: cache');
556: header('Expires: 0');
557: header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
558: header('Cache-Control: private');
559:
560: 561: 562: 563: 564:
565: header('Content-Type: application/octet-stream');
566:
567: header('Content-Length: ' . filesize($path . $file));
568: header('Content-Disposition: attachment; filename="' . $name . '"');
569: header('Content-Transfer-Encoding: binary');
570:
571: $buffer = '';
572: $handle = fopen($path . $file, 'rb');
573: if (false === $handle) {
574: return false;
575: }
576: while (!feof($handle)) {
577: print fread($handle, 1 * (1024 * 1024));
578: ob_flush();
579: flush();
580: }
581: fclose($handle);
582: }
583:
584: 585: 586: 587:
588: private function _getOptionRow($index) {
589: $cfg = cRegistry::getConfig();
590:
591: $tpl = Contenido_SmartyWrapper::getInstance(true);
592:
593:
594: $tpl->assign('trans', array(
595: 'label' => Pifa::i18n('LABEL'),
596: 'value' => Pifa::i18n('VALUE')
597: ));
598:
599: $tpl->assign('i', $index);
600:
601:
602: $tpl->assign('option', array(
603: 'label' => '',
604: 'value' => ''
605: ));
606:
607: $tpl->display($cfg['templates']['pifa_ajax_option_row']);
608: }
609: }
610:
611: ?>