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