1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
14:
15: 16: 17: 18: 19: 20: 21:
22: class PifaLeftBottomPage extends cGuiPage {
23:
24: 25: 26: 27: 28:
29: protected $typeId;
30:
31: 32: 33:
34: public function __construct() {
35:
36: 37: 38: 39:
40: global $action;
41:
42: 43: 44: 45:
46: global $idform;
47:
48: parent::__construct('left_bottom', Pifa::getName());
49:
50:
51: $typeCollection = new cApiTypeCollection();
52: $typeCollection->select('type = "CMS_PIFAFORM"');
53: $type = $typeCollection->next();
54: $this->typeId = $type->get('idtype');
55:
56: $this->addScript('form_assistant.js');
57: $this->addScript('left_bottom.js');
58:
59: $this->set('s', 'dialog_title', Pifa::i18n('INUSE_DIALOG_TITLE'));
60: $this->set('s', 'menu', $this->_getMenu());
61:
62:
63: $this->set('s', 'I18N', json_encode(array(
64: 'confirm_delete_form' => Pifa::i18n('CONFIRM_DELETE_FORM')
65: )));
66: }
67:
68: 69: 70:
71: private function () {
72: global $area;
73:
74: $cfg = cRegistry::getConfig();
75: $client = cRegistry::getClientId();
76: $lang = cRegistry::getLanguageId();
77:
78:
79: $forms = PifaFormCollection::getByClientAndLang($client, $lang);
80: if (false === $forms) {
81: return '<!-- no forms for current client/language -->';
82: }
83:
84:
85:
86:
87: $contentCollection = new cApiContentCollection();
88:
89: $formContent = $contentCollection->getFieldsByWhereClause(array(
90: 'idartlang',
91: 'value'
92: ), 'idtype = "' . $this->typeId . '"');
93:
94: $assignedForms = array();
95: foreach ($formContent as $formRow) {
96:
97: $formRow['value'] = conHtmlEntityDecode($formRow['value']);
98: $formRow['value'] = utf8_encode($formRow['value']);
99: $settings = cXmlBase::xmlStringToArray($formRow['value']);
100:
101: if ($settings['idform'] != '') {
102: if (is_array($assignedForms[$settings['idform']])) {
103: $assignedForms[$settings['idform']][] = new cApiArticleLanguage($formRow['idartlang']);
104: } else {
105: $assignedForms[$settings['idform']] = array(
106: new cApiArticleLanguage($formRow['idartlang'])
107: );
108: }
109: }
110: }
111:
112:
113: $menu = new cGuiMenu();
114: while (false !== $form = $forms->next()) {
115:
116: $idform = $form->get('idform');
117: $formName = $form->get('name');
118:
119: $menu->setTitle($idform, $formName);
120:
121:
122: $link = new cHTMLLink();
123: $link->setMultiLink($area, '', $area, PifaRightBottomFormPage::SHOW_FORM);
124: $link->setCustom('idform', $idform);
125: $link->setAttribute('title', 'idform: ' . $idform);
126: $menu->setLink($idform, $link);
127:
128:
129: if (isset($assignedForms[$idform])) {
130:
131: $link = new cHTMLLink();
132:
133: $link->setClass('in_use_link');
134: $dialogText = Pifa::i18n("FOLLOWING_LIST_USES_FORM") . "<br><br>";
135: foreach($assignedForms[$idform] as $article) {
136: $dialogText .= '<b>' . $article->get('title') . '</b> - (' . $article->get('idart') . ')<br>';
137: }
138: $link->setAttribute("data-dialog-text", $dialogText);
139: $link->setLink('javascript://');
140:
141: $image = new cHTMLImage();
142: $image->setSrc($cfg['path']['images'] . 'exclamation.gif');
143: $link->setContent($image->render());
144:
145: $menu->setActions($idform, 'inuse', $link);
146: }
147:
148:
149: if (cRegistry::getPerm()->have_perm_area_action('form', PifaRightBottomFormPage::DELETE_FORM)) {
150: $link = new cHTMLLink();
151: $link->setMultiLink($area, PifaRightBottomFormPage::DELETE_FORM, $area, PifaRightBottomFormPage::DELETE_FORM);
152: $link->setCustom('idform', $idform);
153: $link->setClass('pifa-icon-delete-form');
154: $deleteForm = Pifa::i18n('DELETE_FORM');
155: $link->setAlt($deleteForm);
156: $link->setContent('<img src="' . $cfg['path']['images'] . 'delete.gif" title="' . $deleteForm . '" alt="' . $deleteForm . '">');
157:
158: $menu->setActions($idform, 'delete', $link);
159: } else {
160: $menu->setActions($idform, 'delete', '<img src="' . $cfg['path']['images'] . 'delete_inact.gif" title="' . $deleteForm . '" alt="' . $deleteForm . '">');
161: }
162: }
163:
164: return $menu->render(false);
165: }
166:
167: }
168:
169: 170: 171: 172: 173:
174: class PifaRightBottomFormPage extends cGuiPage {
175:
176: 177: 178: 179: 180:
181: const SHOW_FORM = 'pifa_show_form';
182:
183: 184: 185: 186: 187:
188: const STORE_FORM = 'pifa_store_form';
189:
190: 191: 192: 193: 194:
195: const DELETE_FORM = 'pifa_delete_form';
196:
197: 198: 199: 200: 201:
202: private $_pifaForm = NULL;
203:
204: 205: 206: 207: 208: 209: 210: 211: 212:
213: public function __construct() {
214:
215: 216: 217: 218:
219: global $action;
220:
221: 222: 223: 224:
225: global $idform;
226:
227: 228: 229: 230:
231: global $idfield;
232:
233: parent::__construct('right_bottom', Pifa::getName());
234:
235: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
236: $this->addStyle('right_bottom.css');
237: $this->addScript('form_assistant.js');
238: $this->addScript('right_bottom.js');
239:
240:
241: $this->_pifaForm = new PifaForm();
242:
243:
244: $idform = cSecurity::toInteger($idform);
245: if (0 < $idform) {
246: if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
247: $msg = Pifa::i18n('FORM_LOAD_ERROR');
248: throw new PifaException($msg);
249: }
250: }
251:
252:
253: $this->set('s', 'I18N', json_encode(array(
254: 'cancel' => Pifa::i18n('CANCEL'),
255: 'save' => Pifa::i18n('SAVE')
256: )));
257:
258:
259: try {
260: $this->_dispatch($action);
261: } catch (PifaException $e) {
262: $cGuiNotification = new cGuiNotification();
263: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
264: $this->set('s', 'notification', $notification);
265: $this->set('s', 'content', '');
266: }
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276:
277: protected function _dispatch($action, $notification = '') {
278: global $area;
279:
280:
281: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
282: $msg = Pifa::i18n('NO_PERMISSIONS');
283: throw new PifaIllegalStateException($msg);
284: }
285:
286: if (NULL === $action) {
287: $cGuiNotification = new cGuiNotification();
288: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, Pifa::i18n('please select a form'));
289: $this->set('s', 'notification', $notification);
290: $this->set('s', 'content', '');
291: return;
292: }
293:
294:
295: switch ($action) {
296: case self::SHOW_FORM:
297: $this->set('s', 'notification', $notification);
298: try {
299: $this->set('s', 'content', $this->_showForm());
300: } catch (SmartyCompilerException $e) {
301: $this->set('s', 'content', Pifa::notifyException($e));
302: }
303: break;
304:
305: case self::STORE_FORM:
306: $notification = '';
307: try {
308: $this->_storeForm();
309: $this->setReload();
310:
311: $idform = $this->_pifaForm->get('idform');
312: $url = "main.php?area=form&frame=3&idform=$idform&action=" . PifaRightBottomFormPage::SHOW_FORM;
313: $url = cRegistry::getSession()->url($url);
314: $this->addScript("<script type=\"text/javascript\">
315: Con.getFrame('right_top').location.href = '$url';
316: </script>");
317: } catch (Exception $e) {
318: $notification = Pifa::notifyException($e);
319: }
320: $this->_dispatch(self::SHOW_FORM, $notification);
321: break;
322:
323: case self::DELETE_FORM:
324: $notification = '';
325: try {
326: $this->_deleteForm();
327: $cGuiNotification = new cGuiNotification();
328: $this->set('s', 'notification', $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, Pifa::i18n('FORM_DELETED')));
329: $this->set('s', 'content', '');
330: $this->setReload();
331: } catch (Exception $e) {
332: $notification = Pifa::notifyException($e);
333: }
334: break;
335:
336: default:
337: $msg = Pifa::i18n('UNKNOWN_ACTION');
338: throw new PifaException($msg);
339: }
340: }
341:
342: 343: 344: 345: 346:
347: private function _showForm() {
348: global $area;
349:
350: $cfg = cRegistry::getConfig();
351:
352:
353: $formAction = '';
354: if (cRegistry::getPerm()->have_perm_area_action('form', self::STORE_FORM)) {
355: $formAction = new cHTMLLink();
356: $formAction->setCLink($area, 4, self::STORE_FORM);
357: $formAction = $formAction->getHref();
358: }
359:
360:
361: if (!is_null($this->_pifaForm) && $this->_pifaForm->isLoaded()) {
362: $idform = $this->_pifaForm->get('idform');
363: $nameValue = $this->_pifaForm->get('name');
364: $dataTableValue = $this->_pifaForm->get('data_table');
365: $methodValue = $this->_pifaForm->get('method');
366: $withTimestampValue = (bool) $this->_pifaForm->get('with_timestamp');
367: } else {
368: $idform = NULL;
369:
370:
371: $nameValue = empty($_POST['name'])? '' : $_POST['name'];
372: $nameValue = cSecurity::unescapeDB($nameValue);
373: $nameValue = cSecurity::toString($nameValue);
374: $nameValue = trim($nameValue);
375:
376: $dataTableValue = empty($_POST['data_table'])? '' : $_POST['data_table'];
377: $dataTableValue = trim($dataTableValue);
378: $dataTableValue = strtolower($dataTableValue);
379: $dataTableValue = preg_replace('/[^a-z0-9_]/', '_', $dataTableValue);
380:
381: $methodValue = '';
382: $withTimestampValue = true;
383: }
384:
385: $tpl = cSmartyBackend::getInstance(true);
386: $tpl->assign('formAction', $formAction);
387: $tpl->assign('idform', $idform);
388: $tpl->assign('nameValue', $nameValue);
389: $tpl->assign('dataTableValue', $dataTableValue);
390: $tpl->assign('methodValue', strtoupper($methodValue));
391: $tpl->assign('withTimestampValue', $withTimestampValue);
392: $tpl->assign('hasWithTimestamp', Pifa::TIMESTAMP_BYFORM === Pifa::getTimestampSetting());
393: $tpl->assign('trans', array(
394: 'legend' => Pifa::i18n('form'),
395: 'name' => Pifa::i18n('form name'),
396: 'dataTable' => Pifa::i18n('data table'),
397: 'method' => Pifa::i18n('method'),
398: 'withTimestamp' => Pifa::i18n('with timestamp'),
399: 'pleaseChoose' => Pifa::i18n('please choose'),
400: 'saveForm' => Pifa::i18n('save form')
401: ));
402:
403: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_form']);
404:
405: return $out;
406: }
407:
408: 409: 410: 411: 412:
413: private function _storeForm() {
414:
415:
416: $isLoaded = $this->_pifaForm->isLoaded();
417:
418:
419: $name = $_POST['name'];
420: $name = cSecurity::unescapeDB($name);
421: $name = cSecurity::toString($name);
422: $name = trim($name);
423:
424: $dataTable = $_POST['data_table'];
425: $dataTable = trim($dataTable);
426: $dataTable = strtolower($dataTable);
427: $dataTable = preg_replace('/[^a-z0-9_]/', '_', $dataTable);
428:
429: $method = $_POST['method'];
430: $method = trim($method);
431: $method = strtoupper($method);
432:
433: switch (Pifa::getTimestampSetting()) {
434: case Pifa::TIMESTAMP_NEVER:
435: $withTimestamp = false;
436: break;
437: case Pifa::TIMESTAMP_BYFORM:
438: $withTimestamp = 'on' === $_POST['with_timestamp'];
439: break;
440: case Pifa::TIMESTAMP_ALWAYS:
441: $withTimestamp = true;
442: break;
443: }
444:
445:
446: if (0 === strlen($name)) {
447: $msg = Pifa::i18n('EMPTY_FORMNAME_ERROR');
448: throw new PifaException($msg);
449: }
450: if (0 === strlen($dataTable)) {
451: $msg = Pifa::i18n('EMPTY_DATETABLENAME_ERROR');
452: throw new PifaException($msg);
453: }
454: if (!in_array($method, array(
455: 'GET',
456: 'POST'
457: ))) {
458: $msg = Pifa::i18n('FORM_METHOD_ERROR');
459: throw new PifaException($msg);
460: }
461:
462: if ($isLoaded) {
463:
464: $oldDataTable = $this->_pifaForm->get('data_table');
465: $oldWithTimestamp = (bool) $this->_pifaForm->get('with_timestamp');
466: } else {
467:
468: $pifaFormCollection = new PifaFormCollection();
469: $this->_pifaForm = $pifaFormCollection->createNewItem(array(
470: 'idclient' => cRegistry::getClientId(),
471: 'idlang' => cRegistry::getLanguageId()
472: ));
473: }
474:
475:
476:
477:
478:
479:
480: if ($name !== $this->_pifaForm->get('name')) {
481: $this->_pifaForm->set('name', $name);
482: }
483: if ($dataTable !== $this->_pifaForm->get('data_table')) {
484: $this->_pifaForm->set('data_table', $dataTable);
485: }
486: if (0 !== strcasecmp($method, $this->_pifaForm->get('method'))) {
487: $this->_pifaForm->set('method', $method);
488: }
489: if ($withTimestamp !== (bool) $this->_pifaForm->get('with_timestamp')) {
490: $this->_pifaForm->set('with_timestamp', $withTimestamp);
491: }
492:
493: if ($isLoaded) {
494:
495:
496:
497:
498: $this->_pifaForm->alterTable($oldDataTable, $oldWithTimestamp);
499: } else {
500:
501: $this->_pifaForm->createTable($withTimestamp);
502: }
503:
504:
505: if (false === $this->_pifaForm->store()) {
506: $msg = Pifa::i18n('FORM_STORE_ERROR');
507: $msg = sprintf($msg, $this->_pifaForm->getLastError());
508: throw new PifaException($msg);
509: }
510: }
511:
512: 513:
514: private function _deleteForm() {
515: $this->_pifaForm->delete();
516: $this->_pifaForm = NULL;
517: }
518:
519: }
520:
521: 522: 523: 524: 525:
526: class PifaRightBottomFormFieldsPage extends cGuiPage {
527:
528: 529: 530: 531: 532:
533: const SHOW_FIELDS = 'pifa_show_fields';
534:
535: 536: 537: 538: 539:
540: private $_pifaForm = NULL;
541:
542: 543: 544: 545: 546: 547: 548: 549: 550:
551: public function __construct() {
552:
553: 554: 555: 556:
557: global $action;
558:
559: 560: 561: 562:
563: global $idform;
564:
565: 566: 567: 568:
569: global $idfield;
570:
571: parent::__construct('right_bottom', Pifa::getName());
572:
573: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
574: $this->addStyle('right_bottom.css');
575: $this->addScript('form_assistant.js');
576: $this->addScript('right_bottom.js');
577:
578:
579: $this->_pifaForm = new PifaForm();
580:
581:
582: $idform = cSecurity::toInteger($idform);
583: if (0 < $idform) {
584: $ret = $this->_pifaForm->loadByPrimaryKey($idform);
585: if (false === $ret) {
586: $msg = Pifa::i18n('FORM_LOAD_ERROR');
587: throw new PifaException($msg);
588: }
589: }
590:
591:
592: $this->set('s', 'I18N', json_encode(array(
593: 'cancel' => Pifa::i18n('CANCEL'),
594: 'save' => Pifa::i18n('SAVE'),
595: 'confirm_delete_field' => Pifa::i18n('CONFIRM_DELETE_FIELD')
596: )));
597:
598:
599: try {
600: $this->_dispatch($action);
601: } catch (PifaException $e) {
602: $cGuiNotification = new cGuiNotification();
603: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
604: $this->set('s', 'notification', $notification);
605: $this->set('s', 'content', '');
606: }
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616:
617: protected function _dispatch($action, $notification = '') {
618: global $area;
619:
620:
621: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
622: $msg = Pifa::i18n('NO_PERMISSIONS');
623: throw new PifaIllegalStateException($msg);
624: }
625:
626: if (NULL === $action) {
627: $this->set('s', 'notification', Pifa::i18n('please select a form'));
628: $this->set('s', 'content', '');
629: return;
630: }
631:
632:
633: switch ($action) {
634:
635: case self::SHOW_FIELDS:
636: $this->set('s', 'notification', $notification);
637: try {
638: $this->set('s', 'content', $this->_showFields());
639: } catch (SmartyCompilerException $e) {
640: $this->set('s', 'content', Pifa::notifyException($e));
641: }
642: break;
643:
644: default:
645: $msg = Pifa::i18n('UNKNOWN_ACTION');
646: throw new PifaException($msg);
647: }
648: }
649:
650: 651:
652: private function _showFields() {
653: global $area;
654:
655: $cfg = cRegistry::getConfig();
656:
657: $idform = $idfield = NULL;
658: $fields = NULL;
659: $editField = $deleteField = NULL;
660: if ($this->_pifaForm->isLoaded()) {
661:
662: $idform = $this->_pifaForm->get('idform');
663: $idfield = $_GET['idfield'];
664:
665: $fields = $this->_pifaForm->getFields();
666:
667: if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::GET_FIELD_FORM)) {
668: $editField = new cHTMLLink();
669: $editField->setCLink('form_ajax', 4, PifaAjaxHandler::GET_FIELD_FORM);
670: $editField->setCustom('idform', $idform);
671: $editField = $editField->getHref();
672: }
673:
674: if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::DELETE_FIELD)) {
675: $deleteField = new cHTMLLink();
676: $deleteField->setCLink('form_ajax', 4, PifaAjaxHandler::DELETE_FIELD);
677: $deleteField->setCustom('idform', $idform);
678: $deleteField = $deleteField->getHref();
679: }
680: }
681:
682:
683: $tpl = cSmartyBackend::getInstance(true);
684:
685: $columnNames = array();
686: foreach ($this->_pifaForm->getFields() as $field) {
687: $columnNames[] = $field->get('column_name');
688: }
689:
690:
691: if (!in_array('email', $columnNames)) {
692: $cGuiNotification = new cGuiNotification();
693: $email_notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_WARNING, Pifa::i18n('Currently there is no field called "email" in this form. Sending mails - if configured - to the user which entered the form data may not work!'));
694: $tpl->assign('email_notification', $email_notification);
695: }
696:
697:
698: $tpl->assign('trans', array(
699: 'legend' => Pifa::i18n('fields'),
700: 'pleaseSaveFirst' => Pifa::i18n('please save first'),
701: 'dialogTitle' => Pifa::i18n('edit field'),
702: 'edit' => Pifa::i18n('EDIT'),
703: 'delete' => Pifa::i18n('DELETE'),
704: 'obligatory' => Pifa::i18n('OBLIGATORY')
705: ));
706:
707:
708: $tpl->assign('ajaxParams', implode('&', array(
709: 'area=form_ajax',
710: 'frame=4',
711: 'contenido=' . cRegistry::getBackendSessionId()
712: )));
713: if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::GET_FIELD_FORM)) {
714: $tpl->assign('dragParams', implode('&', array(
715: 'area=form_ajax',
716: 'frame=4',
717: 'contenido=' . cRegistry::getBackendSessionId(),
718: 'action=' . PifaAjaxHandler::GET_FIELD_FORM,
719: 'idform=' . $idform
720: )));
721: }
722: if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::REORDER_FIELDS)) {
723: $tpl->assign('sortParams', implode('&', array(
724: 'area=form_ajax',
725: 'frame=4',
726: 'contenido=' . cRegistry::getBackendSessionId(),
727: 'action=' . PifaAjaxHandler::REORDER_FIELDS,
728: 'idform=' . $this->_pifaForm->get('idform')
729: )));
730: }
731:
732:
733: $tpl->assign('idform', $idform);
734: $tpl->assign('idfield', $idfield);
735:
736: $tpl->assign('fields', $fields);
737: $tpl->assign('fieldTypes', PifaField::getFieldTypeNames());
738:
739:
740: $tpl->assign('editField', $editField);
741: $tpl->assign('deleteField', $deleteField);
742:
743: $tpl->assign('partialFieldRow', $cfg['templates']['pifa_ajax_field_row']);
744:
745: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_fields']);
746:
747: return $out;
748: }
749:
750: }
751:
752: 753: 754: 755: 756:
757: class PifaRightBottomFormDataPage extends cGuiPage {
758:
759: 760: 761: 762: 763:
764: const SHOW_DATA = 'pifa_show_data';
765:
766: 767: 768: 769: 770:
771: private $_pifaForm = NULL;
772:
773: 774: 775: 776: 777: 778: 779: 780: 781:
782: public function __construct() {
783:
784: 785: 786: 787:
788: global $action;
789:
790: 791: 792: 793:
794: global $idform;
795:
796: 797: 798: 799:
800: global $idfield;
801:
802: parent::__construct('right_bottom', Pifa::getName());
803:
804: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
805: $this->addStyle('right_bottom.css');
806: $this->addScript('form_assistant.js');
807: $this->addScript('right_bottom.js');
808:
809:
810: $this->_pifaForm = new PifaForm();
811:
812:
813: $idform = cSecurity::toInteger($idform);
814: if (0 < $idform) {
815: if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
816: $msg = Pifa::i18n('FORM_LOAD_ERROR');
817: throw new PifaException($msg);
818: }
819: }
820:
821:
822: $this->set('s', 'I18N', json_encode(array(
823: 'cancel' => Pifa::i18n('CANCEL'),
824: 'save' => Pifa::i18n('SAVE')
825: )));
826:
827:
828: try {
829: $this->_dispatch($action);
830: } catch (PifaException $e) {
831: $cGuiNotification = new cGuiNotification();
832: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
833: $this->set('s', 'notification', $notification);
834: $this->set('s', 'content', '');
835: }
836: }
837:
838: 839: 840: 841: 842: 843: 844: 845:
846: protected function _dispatch($action, $notification = '') {
847: global $area;
848:
849:
850: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
851: $msg = Pifa::i18n('NO_PERMISSIONS');
852: throw new PifaIllegalStateException($msg);
853: }
854:
855: if (NULL === $action) {
856: $this->set('s', 'notification', Pifa::i18n('please select a form'));
857: $this->set('s', 'content', '');
858: return;
859: }
860:
861:
862: switch ($action) {
863:
864: case self::SHOW_DATA:
865: $this->set('s', 'notification', $notification);
866: try {
867: $this->set('s', 'content', $this->_showData());
868: } catch (SmartyCompilerException $e) {
869: $this->set('s', 'content', Pifa::notifyException($e));
870: }
871: break;
872:
873: default:
874: $msg = Pifa::i18n('UNKNOWN_ACTION');
875: throw new PifaException($msg);
876: }
877: }
878:
879: 880:
881: private function _showData() {
882: $cfg = cRegistry::getConfig();
883:
884: $tpl = cSmartyBackend::getInstance(true);
885:
886:
887: $tpl->assign('trans', array(
888: 'legend' => Pifa::i18n('data'),
889: 'nodata' => Pifa::i18n('NODATA'),
890: 'pleaseSaveFirst' => Pifa::i18n('please save first'),
891: 'export' => Pifa::i18n('download data as CSV')
892: ));
893:
894: $tpl->assign('form', $this->_pifaForm);
895: $tpl->assign('getFileUrl', 'main.php?' . implode('&', array(
896: 'area=form_ajax',
897: 'frame=4',
898: 'contenido=' . cRegistry::getBackendSessionId(),
899: 'action=' . PifaAjaxHandler::GET_FILE
900: )));
901:
902: try {
903: $tpl->assign('fields', $this->_pifaForm->getFields());
904: } catch (Exception $e) {
905: $tpl->assign('fields', Pifa::notifyException($e));
906: }
907:
908: $tpl->assign('withTimestamp', (bool) $this->_pifaForm->get('with_timestamp'));
909:
910: try {
911: $data = $this->_pifaForm->getData();
912: $tpl->assign('data', $data);
913: if (!empty($data) && cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::EXPORT_DATA)) {
914: $tpl->assign('exportUrl', 'main.php?' . implode('&', array(
915: 'area=form_ajax',
916: 'frame=4',
917: 'contenido=' . cRegistry::getBackendSessionId(),
918: 'action=' . PifaAjaxHandler::EXPORT_DATA,
919: 'idform=' . $this->_pifaForm->get('idform')
920: )));
921: }
922: } catch (Exception $e) {
923: $tpl->assign('data', Pifa::notifyException($e));
924: }
925:
926: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_data']);
927:
928: return $out;
929: }
930:
931: }
932:
933: 934: 935: 936: 937: 938:
939: class PifaRightBottomFormExportPage extends cGuiPage {
940:
941: 942: 943: 944: 945:
946: const EXPORT_FORM = 'pifa_export_form';
947:
948: 949: 950: 951: 952:
953: private $_pifaForm = NULL;
954:
955: 956: 957: 958: 959: 960: 961: 962:
963: public function __construct() {
964:
965: 966: 967: 968:
969: global $action;
970:
971: 972: 973: 974:
975: global $idform;
976:
977: parent::__construct('right_bottom', Pifa::getName());
978:
979: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
980: $this->addStyle('right_bottom.css');
981: $this->addScript('form_assistant.js');
982: $this->addScript('right_bottom.js');
983:
984:
985: $this->set('s', 'I18N', json_encode(array(
986: 'cancel' => Pifa::i18n('CANCEL'),
987: 'save' => Pifa::i18n('SAVE')
988: )));
989:
990:
991: $this->_pifaForm = new PifaForm();
992:
993:
994: $idform = cSecurity::toInteger($idform);
995: if (0 < $idform) {
996: if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
997: $msg = Pifa::i18n('FORM_LOAD_ERROR');
998: throw new PifaException($msg);
999: }
1000: }
1001:
1002:
1003: try {
1004: $this->_dispatch($action);
1005: } catch (PifaException $e) {
1006: $cGuiNotification = new cGuiNotification();
1007: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
1008: $this->set('s', 'notification', $notification);
1009: $this->set('s', 'content', '');
1010: }
1011: }
1012:
1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020:
1021: protected function _dispatch($action, $notification = '') {
1022:
1023:
1024: switch ($action) {
1025:
1026: case self::EXPORT_FORM:
1027:
1028:
1029: if (!cRegistry::getPerm()->have_perm_area_action('form_ajax', $action)) {
1030: $msg = Pifa::i18n('NO_PERMISSIONS');
1031: throw new PifaIllegalStateException($msg);
1032: }
1033:
1034: $this->set('s', 'notification', $notification);
1035: try {
1036: $this->set('s', 'content', $this->_exportForm());
1037: } catch (SmartyCompilerException $e) {
1038: $this->set('s', 'content', Pifa::notifyException($e));
1039: }
1040: break;
1041:
1042: default:
1043: $msg = Pifa::i18n('UNKNOWN_ACTION');
1044: throw new PifaException($msg);
1045: }
1046: }
1047:
1048: 1049:
1050: private function _exportForm() {
1051: $cfg = cRegistry::getConfig();
1052:
1053: $tpl = cSmartyBackend::getInstance(true);
1054:
1055:
1056: $tpl->assign('trans', array(
1057: 'legend' => Pifa::i18n('pifa_export_form'),
1058: 'withData' => Pifa::i18n('WITH_DATA'),
1059: 'export' => Pifa::i18n('EXPORT')
1060: ));
1061:
1062: $tpl->assign('formAction', 'main.php?' . implode('&', array(
1063: 'area=form_ajax',
1064: 'frame=4',
1065: 'contenido=' . cRegistry::getBackendSessionId(),
1066: 'action=' . PifaAjaxHandler::EXPORT_FORM,
1067: 'idform=' . $this->_pifaForm->get('idform')
1068: )));
1069:
1070: $tpl->assign('idform', $this->_pifaForm->get('idform'));
1071:
1072: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_export']);
1073:
1074: return $out;
1075: }
1076:
1077: }
1078:
1079: 1080: 1081: 1082: 1083: 1084:
1085: class PifaRightBottomFormImportPage extends cGuiPage {
1086:
1087: 1088: 1089: 1090: 1091:
1092: const IMPORT_FORM = 'pifa_import_form';
1093:
1094: 1095: 1096: 1097:
1098: public function __construct() {
1099:
1100: 1101: 1102: 1103:
1104: global $action;
1105:
1106: parent::__construct('right_bottom', Pifa::getName());
1107:
1108: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
1109: $this->addStyle('right_bottom.css');
1110: $this->addScript('form_assistant.js');
1111: $this->addScript('right_bottom.js');
1112:
1113:
1114: $this->set('s', 'I18N', json_encode(array(
1115: 'cancel' => Pifa::i18n('CANCEL'),
1116: 'save' => Pifa::i18n('SAVE')
1117: )));
1118:
1119:
1120: try {
1121: $this->_dispatch($action);
1122: } catch (PifaException $e) {
1123: $cGuiNotification = new cGuiNotification();
1124: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
1125: $this->set('s', 'notification', $notification);
1126: $this->set('s', 'content', '');
1127: }
1128: }
1129:
1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137:
1138: protected function _dispatch($action, $notification = '') {
1139: global $area;
1140:
1141:
1142: if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
1143: $msg = Pifa::i18n('NO_PERMISSIONS');
1144: throw new PifaIllegalStateException($msg);
1145: }
1146:
1147:
1148: switch ($action) {
1149:
1150: case self::IMPORT_FORM:
1151: $this->set('s', 'notification', $notification);
1152: try {
1153:
1154: switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
1155: case 'GET':
1156: $this->set('s', 'content', $this->_importFormGet());
1157: break;
1158:
1159: case 'POST':
1160: $this->set('s', 'content', $this->_importFormPost());
1161: break;
1162: }
1163:
1164: $this->setReload();
1165: } catch (SmartyCompilerException $e) {
1166: $this->set('s', 'content', Pifa::notifyException($e));
1167: }
1168: break;
1169:
1170: default:
1171: $msg = Pifa::i18n('UNKNOWN_ACTION');
1172: throw new PifaException($msg);
1173: }
1174: }
1175:
1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185:
1186: private function _importFormGet($showTableNameField = false) {
1187: $cfg = cRegistry::getConfig();
1188:
1189: $tpl = cSmartyBackend::getInstance(true);
1190:
1191:
1192: $tpl->assign('trans', array(
1193: 'legend' => Pifa::i18n('pifa_import_form'),
1194: 'xml' => Pifa::i18n('XML'),
1195: 'used_table_name_error' => Pifa::i18n('USED_TABLE_NAME_ERROR'),
1196: 'table_name' => Pifa::i18n('data table'),
1197: 'export' => Pifa::i18n('IMPORT')
1198: ));
1199:
1200: $tpl->assign('formAction', 'main.php?' . implode('&', array(
1201: 'area=form_import',
1202: 'frame=4',
1203: 'contenido=' . cRegistry::getBackendSessionId(),
1204: 'action=' . self::IMPORT_FORM
1205: )));
1206:
1207: $tpl->assign('showTableNameField', $showTableNameField);
1208:
1209: $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_import']);
1210:
1211: return $out;
1212: }
1213:
1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223:
1224: private function _importFormPost() {
1225: $cGuiNotification = new cGuiNotification();
1226:
1227:
1228: $xml = file_get_contents($_FILES['xml']['tmp_name']);
1229: $tableName = isset($_POST['table_name'])? $_POST['table_name'] : NULL;
1230:
1231:
1232: if (false === $xml) {
1233: $note = Pifa::i18n('READ_XML_ERROR');
1234: return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $note);
1235: }
1236:
1237: try {
1238:
1239:
1240: $pifaImporter = new PifaImporter();
1241: $pifaImporter->setTableName($tableName);
1242: $pifaImporter->import($xml);
1243:
1244:
1245: $note = Pifa::i18n('IMPORT_SUCCESS');
1246: $out = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, $note);
1247: } catch (PifaDatabaseException $e) {
1248: $out = $this->_importFormGet(true);
1249: }
1250:
1251: return $out;
1252: }
1253:
1254: }
1255: