1: <?php
2:
3: /**
4: *
5: * @package Plugin
6: * @subpackage FormAssistant
7: * @version SVN Revision $Rev:$
8: * @author marcus.gnass
9: * @copyright four for business AG
10: * @link http://www.4fb.de
11: */
12:
13: /**
14: * This class is the default implementation for PIFA form modules.
15: * On a GET request it displays a form. On a POST request the posted data is
16: * processed. If an error occurs the form is displayed again only that now all
17: * valid data is displayed in the form and error messages (if defined) are
18: * displayed for all invalid form fields.
19: *
20: * @author marcus.gnass
21: */
22: class DefaultFormModule extends PifaAbstractFormModule {
23:
24: /**
25: * Handle GET request.
26: *
27: * @param array $values to be displayed in form
28: * @param array $errors to be displayed in form
29: * @throws Exception if form could not be loaded
30: * @see PifaAbstractFormModule::doGet()
31: */
32: protected function doGet(array $values = array(), array $errors = array()) {
33:
34: // set template to use
35: $this->setTemplateName($this->getSetting('pifaform_template_get'));
36:
37: // create and load form
38: $pifaForm = new PifaForm($this->getIdform());
39:
40: // catch error
41: if (true !== $pifaForm->isLoaded()) {
42: $msg = Pifa::i18n('FORM_LOAD_ERROR');
43: throw new PifaException($msg);
44: }
45:
46: // set values (keep default values if NULL!)
47: $pifaForm->setValues($values);
48:
49: // set errors
50: $pifaForm->setErrors($errors);
51:
52: $actionPath = cUri::getInstance()->build(array(
53: 'idart' => cRegistry::getArticleId(),
54: 'lang' => cRegistry::getLanguageId()
55: ), true);
56:
57: if (Pifa::isHttps()) {
58: $actionPath = str_replace('http://', 'https://', $actionPath);
59: }
60:
61: // assign rendered form
62: $this->getTpl()->assign('form', $pifaForm->toHtml(array(
63: 'action' => $actionPath
64: )));
65: }
66:
67: /**
68: * Handle POST request.
69: *
70: * @see PifaAbstractFormModule::doPost()
71: */
72: protected function doPost() {
73:
74: // set template to use
75: $this->setTemplateName($this->getSetting('pifaform_template_post'));
76:
77: try {
78:
79: // get name of processor class
80: $processorClass = $this->getSetting('pifaform_processor');
81:
82: // get name of file in which processor class could be found
83: $filename = Pifa::fromCamelCase($processorClass);
84: $filename = "extensions/class.pifa.$filename.php";
85: if (false === file_exists(Pifa::getPath() . $filename)) {
86: $msg = Pifa::i18n('MISSING_PROCESSOR_FILE');
87: $msg = sprintf($msg, $filename);
88: throw new PifaException($msg);
89: }
90: plugin_include(Pifa::getName(), $filename);
91: if (false === class_exists($processorClass)) {
92: $msg = Pifa::i18n('MISSING_PROCESSOR_CLASS');
93: $msg = sprintf($msg, $processorClass);
94: throw new PifaException($msg);
95: }
96:
97: // create processor instance
98: // pass module in order to access its settings
99: // processorClass is subclass of PifaAbstractFormProcessor
100: $postProcessor = new $processorClass($this);
101: $postProcessor->process();
102:
103: // assign reply to post template
104: $this->getTpl()->assign('reply', array(
105: 'headline' => mi18n("REPLY_HEADLINE"),
106: 'text' => mi18n("REPLY_TEXT")
107: ));
108: } catch (PifaValidationException $e) {
109:
110: // display form with valid values again
111: $this->doGet($postProcessor->getForm()->getValues(), $e->getErrors());
112:
113: // store validation state as (global) application so another module
114: // can show a reply text but when validation is successfull
115: cRegistry::setAppVar('pifaFormValidity', 'invalid');
116: } catch (Exception $e) {
117:
118: Pifa::logException($e);
119: Pifa::displayException($e);
120: }
121: }
122: }
123: