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