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