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 PifaLeftBottomPage extends cGuiPage {
22:
23: 24: 25: 26:
27: public function __construct() {
28:
29: 30: 31: 32:
33: global $action;
34:
35: 36: 37: 38:
39: global $idform;
40:
41: parent::__construct('left_bottom', Pifa::getName());
42:
43: $this->addScript('../plugins/form_assistant/scripts/left_bottom.js');
44:
45: $this->set('s', 'menu', $this->_getMenu());
46:
47:
48: $this->set('s', 'I18N', json_encode(array(
49: 'confirm_delete_form' => Pifa::i18n('CONFIRM_DELETE_FORM')
50: )));
51: }
52:
53: 54: 55:
56: private function () {
57: global $area;
58:
59: $cfg = cRegistry::getConfig();
60: $client = cRegistry::getClientId();
61: $lang = cRegistry::getLanguageId();
62:
63:
64: $forms = PifaFormCollection::getByClientAndLang($client, $lang);
65: if (false === $forms) {
66: return '<!-- no forms for current client/language -->';
67: }
68:
69:
70:
71:
72: $menu = new cGuiMenu();
73: while (false !== $form = $forms->next()) {
74:
75: $idform = $form->get('idform');
76: $formName = $form->get('name');
77:
78: $menu->setTitle($idform, $formName);
79:
80:
81: $link = new cHTMLLink();
82: $link->setMultiLink($area, '', $area, PifaRightBottomFormPage::SHOW_FORM);
83: $link->setCustom('idform', $idform);
84: $menu->setLink($idform, $link);
85:
86:
87: $link = new cHTMLLink();
88: $link->setMultiLink($area, PifaRightBottomFormPage::DELETE_FORM, $area, PifaRightBottomFormPage::DELETE_FORM);
89: $link->setCustom('idform', $idform);
90: $link->setClass('pifa-icon-delete-form');
91: $deleteForm = Pifa::i18n('DELETE_FORM');
92: $link->setAlt($deleteForm);
93: $link->setContent('<img src="' . $cfg['path']['images'] . 'delete.gif" title="' . $deleteForm . '" alt="' . $deleteForm . '">');
94:
95: $menu->setActions($idform, 'delete', $link);
96: }
97:
98: return $menu->render(false);
99: }
100: }
101:
102: 103: 104: 105: 106:
107: class PifaRightBottomFormPage extends cGuiPage {
108:
109: 110: 111: 112:
113: const SHOW_FORM = 'pifa_show_form';
114:
115: 116: 117: 118:
119: const STORE_FORM = 'pifa_store_form';
120:
121: 122: 123: 124:
125: const DELETE_FORM = 'pifa_delete_form';
126:
127: 128: 129: 130: 131:
132: private $_pifaForm = NULL;
133:
134: 135: 136: 137: 138: 139: 140: 141: 142:
143: public function __construct() {
144:
145: 146: 147: 148:
149: global $action;
150:
151: 152: 153: 154:
155: global $idform;
156:
157: 158: 159: 160:
161: global $idfield;
162:
163: parent::__construct('right_bottom', Pifa::getName());
164:
165: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/smoothness/jquery-ui-1.8.20.custom.css');
166: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/right_bottom.css');
167: $this->addScript('../plugins/' . Pifa::getName() . '/scripts/right_bottom.js');
168:
169:
170: $this->_pifaForm = new PifaForm();
171:
172:
173: $idform = cSecurity::toInteger($idform);
174: if (0 < $idform) {
175: if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
176: $msg = Pifa::i18n('FORM_LOAD_ERROR');
177: throw new Exception($msg);
178: }
179: }
180:
181:
182: try {
183: $this->_dispatch($action);
184: } catch (InvalidArgumentException $e) {
185: $cGuiNotification = new cGuiNotification();
186: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
187: $this->set('s', 'notification', $notification);
188: }
189:
190:
191: $this->set('s', 'I18N', json_encode(array(
192: 'cancel' => Pifa::i18n('CANCEL'),
193: 'save' => Pifa::i18n('SAVE')
194: )));
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: protected function _dispatch($action, $notification = '') {
205: global $area;
206:
207:
208: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
209: throw new IllegalStateException('no permissions');
210: }
211:
212: if (NULL === $action) {
213: $cGuiNotification = new cGuiNotification();
214: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, Pifa::i18n('please select a form'));
215: $this->set('s', 'notification', $notification);
216: $this->set('s', 'content', '');
217: return;
218: }
219:
220:
221: switch ($action) {
222: case PifaRightBottomFormPage::SHOW_FORM:
223: $this->set('s', 'notification', $notification);
224: try {
225: $this->set('s', 'content', $this->_showForm());
226: } catch (SmartyCompilerException $e) {
227: $this->set('s', 'content', Pifa::notifyException($e));
228: }
229: break;
230:
231: case PifaRightBottomFormPage::STORE_FORM:
232: $notification = '';
233: try {
234: $this->_storeForm();
235: $this->setReload();
236:
237: $idform = $this->_pifaForm->get('idform');
238: $url = "main.php?area=form&frame=3&idform=$idform&action=" . PifaRightBottomFormPage::SHOW_FORM;
239: $url = cRegistry::getSession()->url($url);
240: $this->addScript("<script type=\"text/javascript\">
241: parent.parent.frames['right'].frames['right_top'].location.href = '$url';
242: </script>");
243: } catch (Exception $e) {
244: $notification = Pifa::notifyException($e);
245: }
246: $this->_dispatch(PifaRightBottomFormPage::SHOW_FORM, $notification);
247: break;
248:
249: case PifaRightBottomFormPage::DELETE_FORM:
250: $notification = '';
251: try {
252: $this->_deleteForm();
253: $cGuiNotification = new cGuiNotification();
254: $this->set('s', 'notification', $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, Pifa::i18n('FORM_DELETED')));
255: $this->set('s', 'content', '');
256: $this->setReload();
257: } catch (Exception $e) {
258: $notification = Pifa::notifyException($e);
259: }
260: break;
261:
262: default:
263: $msg = Pifa::i18n('UNKNOWN_ACTION');
264: throw new InvalidArgumentException($msg);
265: }
266: }
267:
268: 269: 270: 271: 272:
273: private function _showForm() {
274: global $area;
275:
276: $cfg = cRegistry::getConfig();
277:
278:
279: $formAction = new cHTMLLink();
280: $formAction->setCLink($area, 4, PifaRightBottomFormPage::STORE_FORM);
281: $formAction = $formAction->getHref();
282:
283:
284: if (!is_null($this->_pifaForm) && $this->_pifaForm->isLoaded()) {
285: $idform = $this->_pifaForm->get('idform');
286: $nameValue = $this->_pifaForm->get('name');
287: $dataTableValue = $this->_pifaForm->get('data_table');
288: $methodValue = $this->_pifaForm->get('method');
289: $withTimestampValue = (bool) $this->_pifaForm->get('with_timestamp');
290: } else {
291: $idform = NULL;
292: $nameValue = '';
293: $dataTableValue = '';
294: $methodValue = '';
295: $withTimestampValue = true;
296: }
297:
298: $tpl = Contenido_SmartyWrapper::getInstance(true);
299: $tpl->assign('formAction', $formAction);
300: $tpl->assign('idform', $idform);
301: $tpl->assign('nameValue', $nameValue);
302: $tpl->assign('dataTableValue', $dataTableValue);
303: $tpl->assign('methodValue', strtoupper($methodValue));
304: $tpl->assign('withTimestampValue', $withTimestampValue);
305: $tpl->assign('hasWithTimestamp', Pifa::TIMESTAMP_BYFORM === Pifa::getTimestampSetting());
306: $tpl->assign('trans', array(
307: 'legend' => Pifa::i18n('form'),
308: 'name' => Pifa::i18n('form name'),
309: 'dataTable' => Pifa::i18n('data table'),
310: 'method' => Pifa::i18n('method'),
311: 'withTimestamp' => Pifa::i18n('with timestamp'),
312: 'pleaseChoose' => Pifa::i18n('please choose'),
313: 'saveForm' => Pifa::i18n('save form')
314: ));
315:
316: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_form']);
317:
318: return $out;
319: }
320:
321: 322: 323:
324: private function _storeForm() {
325:
326:
327: $isLoaded = $this->_pifaForm->isLoaded();
328:
329:
330: $name = $_POST['name'];
331: $name = cSecurity::unescapeDB($name);
332: $name = cSecurity::toString($name);
333: $name = trim($name);
334:
335: $dataTable = $_POST['data_table'];
336: $dataTable = trim($dataTable);
337: $dataTable = strtolower($dataTable);
338: $dataTable = preg_replace('/[^a-z0-9_]/', '_', $dataTable);
339:
340: $method = $_POST['method'];
341: $method = trim($method);
342: $method = strtoupper($method);
343:
344: switch (Pifa::getTimestampSetting()) {
345: case Pifa::TIMESTAMP_NEVER:
346: $withTimestamp = false;
347: break;
348: case Pifa::TIMESTAMP_BYFORM:
349: $withTimestamp = 'on' === $_POST['with_timestamp'];
350: break;
351: case Pifa::TIMESTAMP_ALWAYS:
352: $withTimestamp = true;
353: break;
354: }
355:
356:
357: if (0 === strlen($name)) {
358: $msg = Pifa::i18n('EMPTY_FORMNAME_ERROR');
359: throw new Exception($msg);
360: }
361: if (0 === strlen($dataTable)) {
362: $msg = Pifa::i18n('EMPTY_DATETABLENAME_ERROR');
363: throw new Exception($msg);
364: }
365: if (!in_array($method, array(
366: 'GET',
367: 'POST'
368: ))) {
369: $msg = Pifa::i18n('FORM_METHOD_ERROR');
370: throw new Exception($msg);
371: }
372:
373: if ($isLoaded) {
374:
375: $oldDataTable = $this->_pifaForm->get('data_table');
376: $oldWithTimestamp = (bool) $this->_pifaForm->get('with_timestamp');
377: } else {
378:
379: $pifaFormCollection = new PifaFormCollection();
380: $this->_pifaForm = $pifaFormCollection->createNewItem(array(
381: 'idclient' => cRegistry::getClientId(),
382: 'idlang' => cRegistry::getLanguageId()
383: ));
384: }
385:
386:
387:
388:
389:
390:
391: if ($name !== $this->_pifaForm->get('name')) {
392: $this->_pifaForm->set('name', $name);
393: }
394: if ($dataTable !== $this->_pifaForm->get('data_table')) {
395: $this->_pifaForm->set('data_table', $dataTable);
396: }
397: if (0 !== strcasecmp($method, $this->_pifaForm->get('method'))) {
398: $this->_pifaForm->set('method', $method);
399: }
400: if ($withTimestamp !== (bool) $this->_pifaForm->get('with_timestamp')) {
401: $this->_pifaForm->set('with_timestamp', $withTimestamp);
402: }
403:
404:
405: if (false === $this->_pifaForm->store()) {
406: $msg = Pifa::i18n('FORM_STORE_ERROR');
407: $msg = sprintf($msg, $this->_pifaForm->getLastError());
408: throw new Exception($msg);
409: }
410:
411: if ($isLoaded) {
412:
413:
414:
415:
416: $this->_pifaForm->alterTable($oldDataTable, $oldWithTimestamp);
417: } else {
418:
419: $this->_pifaForm->createTable($withTimestamp);
420: }
421: }
422:
423: 424:
425: private function _deleteForm() {
426: $this->_pifaForm->delete();
427: $this->_pifaForm = NULL;
428: }
429: }
430:
431: 432: 433: 434: 435:
436: class PifaRightBottomFormFieldsPage extends cGuiPage {
437:
438: 439: 440: 441:
442: const SHOW_FIELDS = 'pifa_show_fields';
443:
444: 445: 446: 447: 448:
449: private $_pifaForm = NULL;
450:
451: 452: 453: 454: 455: 456: 457: 458: 459:
460: public function __construct() {
461:
462: 463: 464: 465:
466: global $action;
467:
468: 469: 470: 471:
472: global $idform;
473:
474: 475: 476: 477:
478: global $idfield;
479:
480: parent::__construct('right_bottom', Pifa::getName());
481:
482: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/smoothness/jquery-ui-1.8.20.custom.css');
483: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/right_bottom.css');
484: $this->addScript('../plugins/' . Pifa::getName() . '/scripts/right_bottom.js');
485:
486:
487: $this->_pifaForm = new PifaForm();
488:
489:
490: $idform = cSecurity::toInteger($idform);
491: if (0 < $idform) {
492: $ret = $this->_pifaForm->loadByPrimaryKey($idform);
493: if (false === $ret) {
494: $msg = Pifa::i18n('FORM_LOAD_ERROR');
495: throw new Exception($msg);
496: }
497: }
498:
499:
500: try {
501: $this->_dispatch($action);
502: } catch (InvalidArgumentException $e) {
503: $cGuiNotification = new cGuiNotification();
504: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
505: $this->set('s', 'notification', $notification);
506: }
507:
508:
509: $this->set('s', 'I18N', json_encode(array(
510: 'cancel' => Pifa::i18n('CANCEL'),
511: 'save' => Pifa::i18n('SAVE'),
512: 'confirm_delete_field' => Pifa::i18n('CONFIRM_DELETE_FIELD')
513: )));
514: }
515:
516: 517: 518: 519: 520: 521: 522:
523: protected function _dispatch($action, $notification = '') {
524: global $area;
525:
526:
527: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
528: $msg = Pifa::i18n('NO_PERMISSIONS');
529: throw new IllegalStateException($msg);
530: }
531:
532: if (NULL === $action) {
533: $this->set('s', 'notification', Pifa::i18n('please select a form'));
534: $this->set('s', 'content', '');
535: return;
536: }
537:
538:
539: switch ($action) {
540:
541: case PifaRightBottomFormFieldsPage::SHOW_FIELDS:
542: $this->set('s', 'notification', $notification);
543: try {
544: $this->set('s', 'content', $this->_showFields());
545: } catch (SmartyCompilerException $e) {
546: $this->set('s', 'content', Pifa::notifyException($e));
547: }
548: break;
549:
550: default:
551: $msg = Pifa::i18n('UNKNOWN_ACTION');
552: throw new InvalidArgumentException($msg);
553:
554: }
555: }
556:
557: 558:
559: private function _showFields() {
560: global $area;
561:
562: $cfg = cRegistry::getConfig();
563:
564: if ($this->_pifaForm->isLoaded()) {
565:
566: $idform = $this->_pifaForm->get('idform');
567: $idfield = $_GET['idfield'];
568:
569: $fieldTypes = $this->_getFieldTypes();
570: $fields = $this->_pifaForm->getFields();
571:
572: $editField = new cHTMLLink();
573: $editField->setCLink('form_ajax', 4, PifaAjaxHandler::GET_FIELD_FORM);
574: $editField->setCustom('idform', $idform);
575: $editField = $editField->getHref();
576:
577: $deleteField = new cHTMLLink();
578: $deleteField->setCLink('form_ajax', 4, PifaAjaxHandler::DELETE_FIELD);
579: $deleteField->setCustom('idform', $idform);
580: $deleteField = $deleteField->getHref();
581: } else {
582:
583: $idform = NULL;
584: $idfield = NULL;
585:
586: $fieldTypes = NULL;
587: $fields = NULL;
588:
589: $editField = NULL;
590: $deleteField = NULL;
591: }
592:
593:
594: $tpl = Contenido_SmartyWrapper::getInstance(true);
595:
596:
597: $tpl->assign('trans', array(
598: 'legend' => Pifa::i18n('fields'),
599: 'pleaseSaveFirst' => Pifa::i18n('please save first'),
600: 'dialogTitle' => Pifa::i18n('edit field'),
601: 'edit' => Pifa::i18n('EDIT'),
602: 'delete' => Pifa::i18n('DELETE'),
603: 'obligatory' => Pifa::i18n('OBLIGATORY')
604: ));
605:
606:
607: $tpl->assign('ajaxParams', implode('&', array(
608: 'area=form_ajax',
609: 'frame=4',
610: 'contenido=' . cRegistry::getBackendSessionId()
611: )));
612: $tpl->assign('dragParams', implode('&', array(
613: 'area=form_ajax',
614: 'frame=4',
615: 'contenido=' . cRegistry::getBackendSessionId(),
616: 'action=' . PifaAjaxHandler::GET_FIELD_FORM,
617: 'idform=' . $idform
618: )));
619: $tpl->assign('sortParams', implode('&', array(
620: 'area=form_ajax',
621: 'frame=4',
622: 'contenido=' . cRegistry::getBackendSessionId(),
623: 'action=' . PifaAjaxHandler::REORDER_FIELDS,
624: 'idform=' . $this->_pifaForm->get('idform')
625: )));
626:
627:
628: $tpl->assign('idform', $idform);
629: $tpl->assign('idfield', $idfield);
630:
631: $tpl->assign('fields', $fields);
632:
633: $tpl->assign('fieldTypes', PifaField::getFieldTypeNames());
634:
635:
636: $tpl->assign('editField', $editField);
637: $tpl->assign('deleteField', $deleteField);
638:
639: $tpl->assign('partialFieldRow', $cfg['templates']['pifa_ajax_field_row']);
640:
641: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_fields']);
642:
643: return $out;
644: }
645:
646: 647: 648: 649: 650: 651:
652: private function _getFieldTypes() {
653: $fieldTypes = array();
654: foreach (PifaField::getFieldTypeIds() as $fieldTypeId) {
655: $fieldTypes[$fieldTypeId] = array();
656: $fieldTypes[$fieldTypeId]['id'] = $fieldTypeId;
657: $fieldTypes[$fieldTypeId]['label'] = PifaField::getFieldTypeName($fieldTypeId);
658:
659: $fieldTypes[$fieldTypeId]['icon'] = PifaField::getFieldTypeIcon($fieldTypeId);
660: }
661:
662: return $fieldTypes;
663: }
664: }
665:
666: 667: 668: 669: 670:
671: class PifaRightBottomFormDataPage extends cGuiPage {
672:
673: 674: 675: 676:
677: const SHOW_DATA = 'pifa_show_data';
678:
679: 680: 681: 682: 683:
684: private $_pifaForm = NULL;
685:
686: 687: 688: 689: 690: 691: 692: 693: 694:
695: public function __construct() {
696:
697: 698: 699: 700:
701: global $action;
702:
703: 704: 705: 706:
707: global $idform;
708:
709: 710: 711: 712:
713: global $idfield;
714:
715: parent::__construct('right_bottom', Pifa::getName());
716:
717: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/smoothness/jquery-ui-1.8.20.custom.css');
718: $this->addStyle('../plugins/' . Pifa::getName() . '/styles/right_bottom.css');
719: $this->addScript('../plugins/' . Pifa::getName() . '/scripts/right_bottom.js');
720:
721:
722: $this->_pifaForm = new PifaForm();
723:
724:
725: $idform = cSecurity::toInteger($idform);
726: if (0 < $idform) {
727: if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
728: $msg = Pifa::i18n('FORM_LOAD_ERROR');
729: throw new Exception($msg);
730: }
731: }
732:
733:
734: try {
735: $this->_dispatch($action);
736: } catch (InvalidArgumentException $e) {
737: $cGuiNotification = new cGuiNotification();
738: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
739: $this->set('s', 'notification', $notification);
740: }
741:
742:
743: $this->set('s', 'I18N', json_encode(array(
744: 'cancel' => Pifa::i18n('CANCEL'),
745: 'save' => Pifa::i18n('SAVE')
746: )));
747: }
748:
749: 750: 751: 752: 753: 754: 755:
756: protected function _dispatch($action, $notification = '') {
757: global $area;
758:
759:
760: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
761: $msg = Pifa::i18n('NO_PERMISSIONS');
762: throw new IllegalStateException($msg);
763: }
764:
765: if (NULL === $action) {
766: $this->set('s', 'notification', Pifa::i18n('please select a form'));
767: $this->set('s', 'content', '');
768: return;
769: }
770:
771:
772: switch ($action) {
773:
774: case PifaRightBottomFormDataPage::SHOW_DATA:
775: $this->set('s', 'notification', $notification);
776: try {
777: $this->set('s', 'content', $this->_showData());
778: } catch (SmartyCompilerException $e) {
779: $this->set('s', 'content', Pifa::notifyException($e));
780: }
781: break;
782:
783: default:
784: $msg = Pifa::i18n('UNKNOWN_ACTION');
785: throw new InvalidArgumentException($msg);
786:
787: }
788: }
789:
790: 791:
792: private function _showData() {
793: $cfg = cRegistry::getConfig();
794:
795: $tpl = Contenido_SmartyWrapper::getInstance(true);
796:
797:
798: $tpl->assign('trans', array(
799: 'legend' => Pifa::i18n('data'),
800: 'pleaseSaveFirst' => Pifa::i18n('please save first'),
801: 'export' => Pifa::i18n('download data as CSV')
802: ));
803:
804: $tpl->assign('exportUrl', 'main.php?' . implode('&', array(
805: 'area=form_ajax',
806: 'frame=4',
807: 'contenido=' . cRegistry::getBackendSessionId(),
808: 'action=' . PifaAjaxHandler::EXPORT_DATA,
809: 'idform=' . $this->_pifaForm->get('idform')
810: )));
811: $tpl->assign('form', $this->_pifaForm);
812: $tpl->assign('getFileUrl', 'main.php?' . implode('&', array(
813: 'area=form_ajax',
814: 'frame=4',
815: 'contenido=' . cRegistry::getBackendSessionId(),
816: 'action=' . PifaAjaxHandler::GET_FILE
817: )));
818:
819: try {
820: $tpl->assign('fields', $this->_pifaForm->getFields());
821: } catch (Exception $e) {
822: $tpl->assign('fields', Pifa::notifyException($e));
823: }
824:
825: $tpl->assign('withTimestamp', (bool) $this->_pifaForm->get('with_timestamp'));
826:
827: try {
828: $tpl->assign('data', $this->_pifaForm->getData());
829: } catch (Exception $e) {
830: $tpl->assign('data', Pifa::notifyException($e));
831: }
832:
833: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_data']);
834:
835: return $out;
836: }
837: }
838: