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: // assert CONTENIDO framework
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * This form post helper should simplify the implementation of typical form
18: * processing.
19: * Its main method process() wraps the steps read data from request,
20: * validate data and persist data in database. In order to make this approach
21: * more customizable three protected "event handler" were defined that can be
22: * implemented by a concrete implementation of this class. These allow to
23: * preprocess the data read from the request before it's going to be validated
24: * and to be postprocessed after it's been persisted into the database.
25: *
26: * @author marcus.gnass
27: */
28: abstract class PifaAbstractFormProcessor {
29:
30: /**
31: *
32: * @var PifaAbstractFormModule
33: */
34: private $_module = NULL;
35:
36: /**
37: *
38: * @todo Should be private instead of protected as it can be accessed via
39: * getForm()
40: * @var PifaForm
41: */
42: protected $_form = NULL;
43:
44: /**
45: * Instantiates the processor.
46: * The processor aggregates the form module and the form.
47: * The idform is read from the given modules settings.
48: *
49: * In former implementations of the processor the modules had no settings
50: * and thus no idform. Thats why optionally the idform gan be given
51: * explicitly. This shoud be removed when all processors are refactored.
52: *
53: * @param int $idform
54: * @throws PifaException if id of form could not be determined from module
55: * or param
56: * @throws PifaException if form could not be loaded
57: */
58: public function __construct(PifaAbstractFormModule $module = NULL, $idform = NULL) {
59: $this->_module = $module;
60:
61: // for backward compatibility
62: if (NULL !== $this->_module) {
63: $idform = $this->_module->getSetting('pifaform_idform');
64: }
65:
66: // assure $idform to be an integer
67: $idform = cSecurity::toInteger($idform);
68: if (0 === $idform) {
69: $msg = Pifa::i18n('MISSING_IDFORM');
70: throw new PifaException($msg);
71: }
72:
73: // load form
74: $this->_form = new PifaForm($idform);
75: if (false === $this->_form->isLoaded()) {
76: $msg = Pifa::i18n('FORM_LOAD_ERROR');
77: throw new PifaException($msg);
78: }
79: }
80:
81: /**
82: *
83: * @return PifaAbstractFormModule
84: */
85: public function getModule() {
86: return $this->_module;
87: }
88:
89: /**
90: *
91: * @param PifaAbstractFormModule $_module
92: */
93: public function setModule($_module) {
94: $this->_module = $_module;
95: }
96:
97: /**
98: *
99: * @return PifaForm
100: */
101: public function getForm() {
102: return $this->_form;
103: }
104:
105: /**
106: *
107: * @param PifaForm $_form
108: */
109: public function setForm($_form) {
110: $this->_form = $_form;
111: }
112:
113: /**
114: * Template method to postprocess data that has just been read from request.
115: * This can be usefull e.g. to remove default values for certain form
116: * fields.
117: */
118: abstract protected function _processReadData();
119:
120: /**
121: * Template method to postprocess data that has just been validated.
122: * I cannot yet imagine a situatio where this could be useful but added
123: * this method for completeness' sake.
124: */
125: abstract protected function _processValidatedData();
126:
127: /**
128: * Template method to postprocess data that has just been stored to
129: * database.
130: * This can be usefull e.g. to send form values via email or process them
131: * in another way.
132: */
133: abstract protected function _processStoredData();
134:
135: /**
136: * Processes a form.
137: * Therefor the forms values are read from the appropriate request,
138: * validated and written to database.
139: * After each step a method is called that allows to postprocess the forms
140: * data or even the form itself. This postprocessing is optional and can be
141: * implemented in concrete implementations of this abstratc class
142: *
143: * @throws PifaException if there is no form to process
144: * @throws PifaValidationException if data is invalid
145: * @throws PifaDatabaseException if data could not be stored
146: */
147: public function process() {
148:
149: // assert there is a form to process
150: if (NULL === $this->_form) {
151: $msg = Pifa::i18n('MISSING_IDFORM');
152: throw new PifaException($msg);
153: }
154:
155: // perform steps as described in documentation
156:
157: $this->_form->fromForm();
158: $this->_processReadData();
159:
160: $this->_form->validate();
161: $this->_processValidatedData();
162:
163: $this->_form->storeData();
164: $this->_processStoredData();
165: }
166: }
167: