1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: 
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: cInclude('includes', 'functions.con.php');
 18: cInclude('includes', 'functions.upl.php');
 19: 
 20:  21:  22:  23:  24:  25:  26:  27:  28:  29: 
 30: class cContentTypePifaForm extends cContentTypeAbstractTabbed {
 31: 
 32:      33:  34:  35:  36:  37:  38:  39: 
 40:     public static function sortByLabel($a, $b) {
 41:         return ($a['label'] == $b['label']) ? 0 : (($a['label'] < $b['label']) ? -1 : 1);
 42:     }
 43: 
 44:      45:  46:  47:  48:  49:  50:  51:  52:  53: 
 54:     function __construct($rawSettings, $id, array $contentTypes) {
 55: 
 56:         
 57:         $this->_type = 'CMS_PIFAFORM';
 58:         $this->_prefix = 'pifaform';
 59:         $this->_settingsType = self::SETTINGS_TYPE_XML;
 60:         $this->_formFields = array(
 61:             'pifaform_idform',
 62:             'pifaform_module',
 63:             'pifaform_processor',
 64:             'pifaform_template_get',
 65:             'pifaform_template_post',
 66:             'pifaform_mail_client_template',
 67:             'pifaform_mail_client_from_email',
 68:             'pifaform_mail_client_from_name',
 69:             'pifaform_mail_client_subject',
 70:             'pifaform_mail_system_template',
 71:             'pifaform_mail_system_from_email',
 72:             'pifaform_mail_system_from_name',
 73:             'pifaform_mail_system_recipient_email',
 74:             'pifaform_mail_system_subject'
 75:         );
 76: 
 77:         parent::__construct($rawSettings, $id, $contentTypes);
 78: 
 79:         
 80:         
 81:         
 82:         $action = isset($_POST['pifaform_action']) ? $_POST['pifaform_action'] : NULL;
 83:         $id = isset($_POST['pifaform_id']) ? $_POST['pifaform_id'] : NULL;
 84:         if ('store' === $action && $this->_id == $id) {
 85:             $this->_storeSettings();
 86:         }
 87:     }
 88: 
 89:      90:  91:  92:  93: 
 94:     public function generateEditCode() {
 95: 
 96:         
 97:         $tplTop = new cTemplate();
 98:         $tplTop->set('s', 'ICON', 'plugins/form_assistant/images/icon_form.png');
 99:         $tplTop->set('s', 'ID', $this->_id);
100:         $tplTop->set('s', 'PREFIX', $this->_prefix);
101:         $tplTop->set('s', 'HEADLINE', Pifa::i18n('form'));
102:         $codeTop = $tplTop->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_abstract_tabbed_edit_top.html', true);
103: 
104:         
105:         $tabMenu = array(
106:             'base' => Pifa::i18n('form')
107:         );
108: 
109:         
110:         $tplPanel = new cTemplate();
111:         $tplPanel->set('s', 'PREFIX', $this->_prefix);
112:         $tplPanel->set('d', 'TAB_ID', 'base');
113:         $tplPanel->set('d', 'TAB_CLASS', 'base');
114:         $tplPanel->set('d', 'TAB_CONTENT', $this->_getPanel());
115:         $tplPanel->next();
116:         $codePanel = $tplPanel->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_abstract_tabbed_edit_tabs.html', true);
117: 
118:         
119:         $tplBottom = new cTemplate();
120:         $tplBottom->set('s', 'PATH_FRONTEND', $this->_cfgClient[$this->_client]['path']['htmlpath']);
121:         $tplBottom->set('s', 'ID', $this->_id);
122:         $tplBottom->set('s', 'PREFIX', $this->_prefix);
123:         $tplBottom->set('s', 'IDARTLANG', $this->_idArtLang);
124:         $tplBottom->set('s', 'FIELDS', "'" . implode("','", $this->_formFields) . "'");
125:         
126:         
127:         $tplBottom->set('s', 'SETTINGS', json_encode(str_replace('$', '$', $this->_settings)));
128:         $tplBottom->set('s', 'JS_CLASS_SCRIPT', Pifa::getUrl() . 'scripts/cmsPifaform.js');
129:         $tplBottom->set('s', 'JS_CLASS_NAME', get_class($this));
130: 
131:         $codeBottom = $tplBottom->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_abstract_tabbed_edit_bottom.html', true);
132: 
133:         
134:         $code = $this->_encodeForOutput($codeTop);
135:         
136:         $code .= $this->_encodeForOutput($codePanel);
137:         $code .= $this->_generateActionCode();
138:         $code .= $this->_encodeForOutput($codeBottom);
139:         $code .= $this->generateViewCode();
140: 
141:         return $code;
142:     }
143: 
144:     145: 146: 147: 148: 
149:     private function _getPanel() {
150:         $wrapper = new cHTMLDiv(array(
151: 
152:             
153:             $this->_getSelectForm(),
154: 
155:             
156:             new cHTMLFieldset(array(
157:                 new cHTMLLegend(Pifa::i18n('classes & templates')),
158:                 $this->_getSelectModule(),
159:                 $this->_getSelectProcessor(),
160:                 $this->_getSelectTemplateGet(),
161:                 $this->_getSelectTemplatePost()
162:             )),
163: 
164:             
165:             new cHTMLFieldset(array(
166:                 new cHTMLLegend(Pifa::i18n('client mail')),
167:                 $this->_getSelectMailClientTemplate(),
168:                 $this->_getInputMailClientFromEmail(),
169:                 $this->_getInputMailClientFromName(),
170:                 $this->_getInputMailClientSubject()
171:             )),
172: 
173:             
174:             new cHTMLFieldset(array(
175:                 new cHTMLLegend(Pifa::i18n('system mail')),
176:                 $this->_getSelectMailSystemTemplate(),
177:                 $this->_getInputMailSystemFromEmail(),
178:                 $this->_getInputMailSystemFromName(),
179:                 $this->_getInputMailSystemRecipientEmail(),
180:                 $this->_getInputMailSystemSubject()
181:             ))
182:         ), $this->_prefix . '_panel_base', $this->_prefix . '_panel_base_' . $this->_id);
183:         $wrapper->setStyle('clear:both');
184: 
185:         return $wrapper->render();
186:     }
187: 
188:     189: 190: 191: 192: 193: 
194:     private function _getSelectForm() {
195: 
196:         
197:         $id = 'pifaform_idform_' . $this->_id;
198: 
199:         
200:         $label = new cHTMLLabel(Pifa::i18n('form'), $id);
201: 
202:         
203:         $select = new cHTMLSelectElement($id, '', $id);
204:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
205: 
206:         
207:         $idclient = cRegistry::getClientId();
208:         $forms = PifaFormCollection::getByClient($idclient);
209:         if (false === $forms) {
210:             return $select;
211:         }
212: 
213:         
214:         while (false !== $form = $forms->next()) {
215: 
216:             
217:             $title = $form->get('name');
218:             $value = $form->get('idform');
219:             $selected = $form->get('idform') == $this->_settings['pifaform_idform'];
220: 
221:             
222:             $option = new cHTMLOptionElement($title, $value, $selected);
223: 
224:             
225:             $select->addOptionElement(++$index, $option);
226:         }
227: 
228:         
229:         $div = new cHTMLDiv(array(
230:             $label,
231:             $select
232:         ));
233: 
234:         
235:         return $div;
236:     }
237: 
238:     239: 240: 241: 242: 243: 
244:     private function _getSelectModule() {
245: 
246:         
247:         $id = 'pifaform_module_' . $this->_id;
248: 
249:         
250:         $label = new cHTMLLabel(Pifa::i18n('module'), $id);
251: 
252:         
253:         $select = new cHTMLSelectElement($id, '', $id);
254:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
255: 
256:         
257:         $modules = Pifa::getExtensionClasses('PifaAbstractFormModule');
258: 
259:         
260:         usort($modules, 'cContentTypePifaForm::sortByLabel');
261: 
262:         
263:         foreach ($modules as $module) {
264: 
265:             
266:             $title = $module['label'];
267:             $value = $module['value'];
268:             $selected = $module['value'] == $this->_settings['pifaform_module'];
269: 
270:             
271:             $option = new cHTMLOptionElement($title, $value, $selected);
272: 
273:             
274:             $select->addOptionElement(++$index, $option);
275:         }
276: 
277:         
278:         $div = new cHTMLDiv(array(
279:             $label,
280:             $select
281:         ));
282: 
283:         
284:         return $div;
285:     }
286: 
287:     288: 289: 290: 291: 292: 
293:     private function _getSelectProcessor() {
294: 
295:         
296:         $id = 'pifaform_processor_' . $this->_id;
297: 
298:         
299:         $label = new cHTMLLabel(Pifa::i18n('processor'), $id);
300: 
301:         
302:         $select = new cHTMLSelectElement($id, '', $id);
303:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
304: 
305:         
306:         $processors = Pifa::getExtensionClasses('PifaAbstractFormProcessor');
307: 
308:         
309:         usort($processors, 'cContentTypePifaForm::sortByLabel');
310: 
311:         
312:         foreach ($processors as $processor) {
313: 
314:             
315:             $title = $processor['label'];
316:             $value = $processor['value'];
317:             $selected = $processor['value'] == $this->_settings['pifaform_processor'];
318: 
319:             
320:             $option = new cHTMLOptionElement($title, $value, $selected);
321: 
322:             
323:             $select->addOptionElement(++$index, $option);
324:         }
325: 
326:         
327:         $div = new cHTMLDiv(array(
328:             $label,
329:             $select
330:         ));
331: 
332:         
333:         return $div;
334:     }
335: 
336:     337: 338: 339: 340: 341: 
342:     private function _getSelectTemplateGet() {
343: 
344:         
345:         $id = 'pifaform_template_get_' . $this->_id;
346: 
347:         
348:         $label = new cHTMLLabel(Pifa::i18n('template') . ' – ' . Pifa::i18n('GET'), $id);
349: 
350:         
351:         $select = new cHTMLSelectElement($id, '', $id);
352:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
353: 
354:         
355:         $templates = Pifa::getTemplates('/cms_pifaform_[^\.]+_get\.tpl/');
356: 
357:         
358:         usort($templates, 'cContentTypePifaForm::sortByLabel');
359: 
360:         
361:         foreach ($templates as $template) {
362: 
363:             
364:             $title = $template['label'];
365:             $value = $template['value'];
366:             $selected = $template['value'] == $this->_settings['pifaform_template_get'];
367: 
368:             
369:             $option = new cHTMLOptionElement($title, $value, $selected);
370: 
371:             
372:             $select->addOptionElement(++$index, $option);
373:         }
374: 
375:         
376:         $div = new cHTMLDiv(array(
377:             $label,
378:             $select
379:         ));
380: 
381:         
382:         return $div;
383:     }
384: 
385:     386: 387: 388: 389: 390: 
391:     private function _getSelectTemplatePost() {
392: 
393:         
394:         $id = 'pifaform_template_post_' . $this->_id;
395: 
396:         
397:         $label = new cHTMLLabel(Pifa::i18n('template') . ' – ' . Pifa::i18n('POST'), $id);
398: 
399:         
400:         $select = new cHTMLSelectElement($id, '', $id);
401:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
402: 
403:         
404:         $templates = Pifa::getTemplates('/cms_pifaform_[^\.]+_post\.tpl/');
405: 
406:         
407:         usort($templates, 'cContentTypePifaForm::sortByLabel');
408: 
409:         
410:         foreach ($templates as $template) {
411: 
412:             
413:             $title = $template['label'];
414:             $value = $template['value'];
415:             $selected = $template['value'] == $this->_settings['pifaform_template_post'];
416: 
417:             
418:             $option = new cHTMLOptionElement($title, $value, $selected);
419: 
420:             
421:             $select->addOptionElement(++$index, $option);
422:         }
423: 
424:         
425:         $div = new cHTMLDiv(array(
426:             $label,
427:             $select
428:         ));
429: 
430:         
431:         return $div;
432:     }
433: 
434:     435: 436: 437: 438: 439: 440: 
441:     private function _getSelectMailClientTemplate() {
442: 
443:         
444:         $id = 'pifaform_mail_client_template_' . $this->_id;
445: 
446:         
447:         $label = new cHTMLLabel(Pifa::i18n('template'), $id);
448: 
449:         
450:         $select = new cHTMLSelectElement($id, '', $id);
451:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
452: 
453:         
454:         $templates = Pifa::getTemplates('/cms_pifaform_[^\.]+_mail_client\.tpl/');
455: 
456:         
457:         usort($templates, 'cContentTypePifaForm::sortByLabel');
458: 
459:         
460:         foreach ($templates as $template) {
461: 
462:             
463:             $title = $template['label'];
464:             $value = $template['value'];
465:             $selected = $template['value'] == $this->_settings['pifaform_mail_client_template'];
466: 
467:             
468:             $option = new cHTMLOptionElement($title, $value, $selected);
469: 
470:             
471:             $select->addOptionElement(++$index, $option);
472:         }
473: 
474:         
475:         $div = new cHTMLDiv(array(
476:             $label,
477:             $select
478:         ));
479: 
480:         
481:         return $div;
482:     }
483: 
484:     485: 486: 487: 488: 
489:     private function _getInputMailClientFromEmail() {
490: 
491:         
492:         $label = Pifa::i18n('sender email');
493:         $id = 'pifaform_mail_client_from_email_' . $this->_id;
494:         $value = $this->_settings['pifaform_mail_client_from_email'];
495: 
496:         
497:         $div = new cHTMLDiv(array(
498:             new cHTMLLabel($label, $id),
499:             new cHTMLTextbox($id, $value, '', '', $id)
500:         ));
501: 
502:         
503:         return $div;
504:     }
505: 
506:     507: 508: 509: 510: 
511:     private function _getInputMailClientFromName() {
512: 
513:         
514:         $label = Pifa::i18n('sender name');
515:         $id = 'pifaform_mail_client_from_name_' . $this->_id;
516:         $value = $this->_settings['pifaform_mail_client_from_name'];
517: 
518:         
519:         $div = new cHTMLDiv(array(
520:             new cHTMLLabel($label, $id),
521:             new cHTMLTextbox($id, $value, '', '', $id)
522:         ));
523: 
524:         
525:         return $div;
526:     }
527: 
528:     529: 530: 531: 532: 
533:     private function _getInputMailClientSubject() {
534: 
535:         
536:         $label = Pifa::i18n('subject');
537:         $id = 'pifaform_mail_client_subject_' . $this->_id;
538:         $value = $this->_settings['pifaform_mail_client_subject'];
539:         
540:         
541:         $value = str_replace('$', '$', $value);
542: 
543:         
544:         $div = new cHTMLDiv(array(
545:             new cHTMLLabel($label, $id),
546:             new cHTMLTextbox($id, $value, '', '', $id)
547:         ));
548: 
549:         
550:         return $div;
551:     }
552: 
553:     554: 555: 556: 557: 558: 
559:     private function _getSelectMailSystemTemplate() {
560: 
561:         
562:         $id = 'pifaform_mail_system_template_' . $this->_id;
563: 
564:         
565:         $label = new cHTMLLabel(Pifa::i18n('template'), $id);
566: 
567:         
568:         $select = new cHTMLSelectElement($id, '', $id);
569:         $select->addOptionElement($index = 0, new cHTMLOptionElement(Pifa::i18n('none'), ''));
570: 
571:         
572:         $templates = Pifa::getTemplates('/cms_pifaform_[^\.]+_mail_system\.tpl/');
573: 
574:         
575:         usort($templates, 'cContentTypePifaForm::sortByLabel');
576: 
577:         
578:         foreach ($templates as $template) {
579: 
580:             
581:             $title = $template['label'];
582:             $value = $template['value'];
583:             $selected = $template['value'] == $this->_settings['pifaform_mail_system_template'];
584: 
585:             
586:             $option = new cHTMLOptionElement($title, $value, $selected);
587: 
588:             
589:             $select->addOptionElement(++$index, $option);
590:         }
591: 
592:         
593:         $div = new cHTMLDiv(array(
594:             $label,
595:             $select
596:         ));
597: 
598:         
599:         return $div;
600:     }
601: 
602:     603: 604: 605: 606: 
607:     private function _getInputMailSystemFromEmail() {
608: 
609:         
610:         $label = Pifa::i18n('sender email');
611:         $id = 'pifaform_mail_system_from_email_' . $this->_id;
612:         $value = $this->_settings['pifaform_mail_system_from_email'];
613: 
614:         
615:         $div = new cHTMLDiv(array(
616:             new cHTMLLabel($label, $id),
617:             new cHTMLTextbox($id, $value, '', '', $id)
618:         ));
619: 
620:         
621:         return $div;
622:     }
623: 
624:     625: 626: 627: 628: 
629:     private function _getInputMailSystemFromName() {
630: 
631:         
632:         $label = Pifa::i18n('sender name');
633:         $id = 'pifaform_mail_system_from_name_' . $this->_id;
634:         $value = $this->_settings['pifaform_mail_system_from_name'];
635: 
636:         
637:         $div = new cHTMLDiv(array(
638:             new cHTMLLabel($label, $id),
639:             new cHTMLTextbox($id, $value, '', '', $id)
640:         ));
641: 
642:         
643:         return $div;
644:     }
645: 
646:     647: 648: 649: 650: 651: 
652:     private function _getInputMailSystemRecipientEmail() {
653: 
654:         
655:         $label = Pifa::i18n('Recipient email');
656:         $id = 'pifaform_mail_system_recipient_email_' . $this->_id;
657:         $value = $this->_settings['pifaform_mail_system_recipient_email'];
658: 
659:         
660:         $div = new cHTMLDiv(array(
661:             new cHTMLLabel($label, $id),
662:             new cHTMLTextbox($id, $value, '', '', $id)
663:         ));
664: 
665:         
666:         return $div;
667:     }
668: 
669:     670: 671: 672: 673: 
674:     private function _getInputMailSystemSubject() {
675: 
676:         
677:         $label = Pifa::i18n('subject');
678:         $id = 'pifaform_mail_system_subject_' . $this->_id;
679:         $value = $this->_settings['pifaform_mail_system_subject'];
680:         
681:         
682:         $value = str_replace('$', '$', $value);
683: 
684:         
685:         $div = new cHTMLDiv(array(
686:             new cHTMLLabel($label, $id),
687:             new cHTMLTextbox($id, $value, '', '', $id)
688:         ));
689: 
690:         
691:         return $div;
692:     }
693: 
694:     695: 696: 697: 698: 699: 700: 701: 702: 703: 
704:     public function generateViewCode() {
705:         $code = '";?' . '><' . '?php $form = new %s(\'%s\', %s, %s); echo $form->buildCode(); ?' . '><' . '?php echo "';
706:         $code = sprintf($code, get_class($this), $this->_rawSettings, $this->_id, 'array()');
707: 
708:         return $code;
709:     }
710: 
711:     712: 713: 714: 715: 716: 
717:     public function buildCode() {
718:         $out = '';
719:         if (0 === cSecurity::toInteger($this->_settings['pifaform_idform'])) {
720:             
721:         } else if (0 === strlen(trim($this->_settings['pifaform_module']))) {
722:             
723:         } else {
724:             $moduleClass = trim($this->_settings['pifaform_module']);
725:             try {
726:                 $filename = Pifa::fromCamelCase($moduleClass);
727:                 $filename = "extensions/class.pifa.$filename.php";
728:                 if (false === file_exists(Pifa::getPath() . $filename)) {
729:                     $msg = sprintf(Pifa::i18n('MISSING_MODULE_FILE'), $filename);
730:                     throw new PifaException($msg);
731:                 }
732:                 plugin_include(Pifa::getName(), $filename);
733:                 if (false === class_exists($moduleClass)) {
734:                     $msg = sprintf(Pifa::i18n('MISSING_MODULE_CLASS'), $moduleClass);
735:                     throw new PifaException($msg);
736:                 }
737:                 $mod = new $moduleClass($this->_settings);
738:                 $out = $mod->render(true);
739:             } catch (Exception $e) {
740:                 Pifa::logException($e);
741:                 
742:             }
743:         }
744: 
745:         
746:         
747: 
748:         return $out;
749:     }
750: }
751: