1: <?php
2:
3: /**
4: *
5: * @package Plugin
6: * @subpackage FormAssistant
7: * @author Marcus Gnaß <marcus.gnass@4fb.de>
8: * @copyright four for business AG
9: * @link http://www.4fb.de
10: */
11:
12: // assert CONTENIDO framework
13: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
14:
15: /**
16: * Abstract base class for all classes that are used as PIFA form module.
17: *
18: * In order for an extension class to be displayed in the CMS_PIFAFORM's editor
19: * as module class it has to extend this class and implement its abstract
20: * methods doGet() & doPost().
21: *
22: * @author Marcus Gnaß <marcus.gnass@4fb.de>
23: */
24: abstract class PifaAbstractFormModule {
25:
26: /**
27: * The HTTP GET request method.
28: *
29: * @var string
30: */
31: const GET = 'GET';
32:
33: /**
34: * The HTTP POST request method.
35: *
36: * @var string
37: */
38: const POST = 'POST';
39:
40: /**
41: * Array of settings as defined for a content type CMS_PIFAFORM.
42: *
43: * @var array
44: */
45: protected $_settings = array();
46:
47: /**
48: * The unique ID of the form to be displayed and processed by this module.
49: * This ID is read from the given settings (pifaform_idform).
50: *
51: * @var int
52: */
53: private $_idform = 0;
54:
55: /**
56: * The current template name to be used when displaying the form.
57: * This name usually depends upon the request method to be used.
58: * These names are read from the given settings.
59: *
60: * @var string
61: */
62: private $_templateName = '';
63:
64: /**
65: *
66: * @var cSmartyFrontend
67: */
68: private $_tpl = NULL;
69:
70: /**
71: *
72: * @param array $settings as defined for cContentTypePifaForm
73: */
74: public function __construct(array $settings = NULL) {
75: $this->_settings = $settings;
76: $this->_idform = cSecurity::toInteger($this->_settings['pifaform_idform']);
77: $this->_tpl = cSmartyFrontend::getInstance(true);
78: }
79:
80: /**
81: *
82: * @return array
83: */
84: public function getSettings() {
85: return $this->_settings;
86: }
87:
88: /**
89: *
90: * @param string $key
91: * @return mixed
92: */
93: public function getSetting($key) {
94: return $this->_settings[$key];
95: }
96:
97: /**
98: *
99: * @param array $_settings
100: */
101: public function setSettings(array $_settings) {
102: $this->_settings = $_settings;
103: }
104:
105: /**
106: *
107: * @return int
108: */
109: public function getIdform() {
110: return $this->_idform;
111: }
112:
113: /**
114: *
115: * @param int $_idform
116: */
117: public function setIdform($_idform) {
118: $this->_idform = $_idform;
119: }
120:
121: /**
122: *
123: * @return string
124: */
125: public function getTemplateName() {
126: return $this->_templateName;
127: }
128:
129: /**
130: *
131: * @param string $_templateName
132: */
133: public function setTemplateName($_templateName) {
134: $this->_templateName = $_templateName;
135: }
136:
137: /**
138: *
139: * @return cSmartyFrontend
140: */
141: public function getTpl() {
142: return $this->_tpl;
143: }
144:
145: /**
146: *
147: * @param cSmartyFrontend $_tpl
148: */
149: public function setTpl(cSmartyFrontend $_tpl) {
150: $this->_tpl = $_tpl;
151: }
152:
153: /**
154: * Helper method to determine the current request method.
155: * The request method is returned as uppercase string.
156: *
157: * @return string
158: */
159: protected function _getRequestMethod() {
160: $requestMethod = $_SERVER['REQUEST_METHOD'];
161: $requestMethod = strtoupper($requestMethod);
162:
163: return $requestMethod;
164: }
165:
166: /**
167: *
168: * @param bool $return
169: * @throws PifaException if request method is unknown
170: */
171: public function render($return = false) {
172:
173: // dispatch request method
174: switch ($this->_getRequestMethod()) {
175: case self::GET:
176:
177: $this->doGet();
178: break;
179:
180: case self::POST:
181:
182: // always handle POST method in backend edit mode as GET action
183: if (cRegistry::isBackendEditMode()) {
184: $this->doGet();
185: break;
186: }
187:
188: // execute POST only if current form has been submitted
189: // and just GET form if POST has another reason (other form etc.)
190: if (isset($_POST['idform']) && $_POST['idform'] != $this->getSetting('pifaform_idform')) {
191: $this->doGet();
192: break;
193: }
194:
195: // handle form as if it were posted
196: $this->doPost();
197: break;
198:
199: default:
200: $msg = Pifa::i18n('UNKNOWN_REQUEST_METHOD');
201: throw new PifaException($msg);
202: }
203:
204: // fetch || display template
205: $clientConfig = cRegistry::getClientConfig(cRegistry::getClientId());
206: $path = $clientConfig['template']['path'];
207: if (true === $return) {
208: return $this->_tpl->fetch($path . $this->getTemplateName());
209: } else {
210: $this->_tpl->display($path . $this->getTemplateName());
211: }
212: }
213:
214: /**
215: * Handle GET request.
216: *
217: * @param array $values
218: * @param array $errors
219: */
220: abstract protected function doGet(array $values = array(), array $errors = array());
221:
222: /**
223: * Handle POST request.
224: */
225: abstract protected function doPost();
226: }
227: