Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Core StateMachine. All statemachines subclass for the core one | |
38 | * for functionality specific to their needs. | |
39 | * | |
40 | * A statemachine keeps track of differnt states and forms for a | |
41 | * html quickform controller. | |
42 | * | |
43 | */ | |
44 | class CRM_Core_StateMachine { | |
45 | ||
46 | /** | |
f9e31d7f | 47 | * The controller of this state machine. |
6a488035 TO |
48 | * @var object |
49 | */ | |
50 | protected $_controller; | |
51 | ||
52 | /** | |
f9e31d7f | 53 | * The list of states that belong to this state machine. |
6a488035 TO |
54 | * @var array |
55 | */ | |
56 | protected $_states; | |
57 | ||
58 | /** | |
100fef9d | 59 | * The list of pages that belong to this state machine. Note |
6a488035 TO |
60 | * that a state and a form have a 1 <-> 1 relationship. so u |
61 | * can always derive one from the other | |
62 | * @var array | |
63 | */ | |
64 | protected $_pages; | |
65 | ||
66 | /** | |
f9e31d7f | 67 | * The names of the pages. |
6a488035 TO |
68 | * |
69 | * @var array | |
70 | */ | |
71 | protected $_pageNames; | |
72 | ||
73 | /** | |
f9e31d7f | 74 | * The mode that the state machine is operating in. |
6a488035 TO |
75 | * @var int |
76 | */ | |
77 | protected $_action = NULL; | |
78 | ||
79 | /** | |
f9e31d7f | 80 | * The display name for this machine. |
6a488035 TO |
81 | * @var string |
82 | */ | |
83 | protected $_name = NULL; | |
84 | ||
85 | /** | |
f9e31d7f | 86 | * Class constructor. |
6a488035 | 87 | * |
6a0b768e TO |
88 | * @param object $controller |
89 | * The controller for this state machine. | |
6a488035 | 90 | * |
da3c7979 | 91 | * @param \const|int $action |
dd244018 EM |
92 | * |
93 | * @return \CRM_Core_StateMachine | |
6a488035 | 94 | */ |
00be9182 | 95 | public function __construct(&$controller, $action = CRM_Core_Action::NONE) { |
6a488035 TO |
96 | $this->_controller = &$controller; |
97 | $this->_action = $action; | |
98 | ||
99 | $this->_states = array(); | |
100 | } | |
101 | ||
102 | /** | |
2e2605fe | 103 | * Getter for name. |
6a488035 TO |
104 | * |
105 | * @return string | |
6a488035 TO |
106 | */ |
107 | public function getName() { | |
108 | return $this->_name; | |
109 | } | |
110 | ||
111 | /** | |
2e2605fe | 112 | * Setter for name. |
6a488035 | 113 | * |
389bcebf | 114 | * @param string $name |
6a488035 TO |
115 | * |
116 | * @return void | |
6a488035 TO |
117 | */ |
118 | public function setName($name) { | |
119 | $this->_name = $name; | |
120 | } | |
121 | ||
122 | /** | |
2e2605fe EM |
123 | * Do a state transition jump. |
124 | * | |
125 | * Currently only supported types are | |
6a488035 TO |
126 | * Next and Back. The other actions (Cancel, Done, Submit etc) do |
127 | * not need the state machine to figure out where to go | |
128 | * | |
6a0b768e TO |
129 | * @param CRM_Core_Form $page |
130 | * The current form-page. | |
131 | * @param string $actionName | |
132 | * Current action name, as one Action object can serve multiple actions. | |
133 | * @param string $type | |
134 | * The type of transition being requested (Next or Back). | |
6a488035 TO |
135 | * |
136 | * @return void | |
6a488035 | 137 | */ |
00be9182 | 138 | public function perform(&$page, $actionName, $type = 'Next') { |
6a488035 TO |
139 | // save the form values and validation status to the session |
140 | $page->isFormBuilt() or $page->buildForm(); | |
141 | ||
142 | $pageName = $page->getAttribute('name'); | |
143 | $data = &$page->controller->container(); | |
144 | ||
145 | $data['values'][$pageName] = $page->exportValues(); | |
146 | $data['valid'][$pageName] = $page->validate(); | |
147 | ||
148 | // if we are going to the next state | |
149 | // Modal form and page is invalid: don't go further | |
150 | if ($type == 'Next' && !$data['valid'][$pageName]) { | |
151 | return $page->handle('display'); | |
152 | } | |
153 | ||
154 | $state = &$this->_states[$pageName]; | |
155 | ||
156 | // dont know how or why we landed here so abort and display | |
157 | // current page | |
158 | if (empty($state)) { | |
159 | return $page->handle('display'); | |
160 | } | |
161 | ||
162 | // the page is valid, process it if we are jumping to the next state | |
163 | if ($type == 'Next') { | |
e6a0a301 | 164 | $page->mainProcess(); |
6a488035 TO |
165 | // we get the state again, since postProcess might have changed it |
166 | // this bug took me forever to find :) Lobo | |
167 | $state = &$this->_states[$pageName]; | |
168 | $state->handleNextState($page); | |
169 | } | |
170 | else { | |
171 | $state->handleBackState($page); | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
2e2605fe | 176 | * Helper function to add a State to the state machine. |
6a488035 | 177 | * |
6a0b768e TO |
178 | * @param string $name |
179 | * The internal name. | |
180 | * @param int $type | |
181 | * The type of state (START|FINISH|SIMPLE). | |
182 | * @param object $prev | |
183 | * The previous page if any. | |
184 | * @param object $next | |
185 | * The next page if any. | |
6a488035 TO |
186 | * |
187 | * @return void | |
6a488035 | 188 | */ |
00be9182 | 189 | public function addState($name, $type, $prev, $next) { |
6a488035 TO |
190 | $this->_states[$name] = new CRM_Core_State($name, $type, $prev, $next, $this); |
191 | } | |
192 | ||
193 | /** | |
2e2605fe | 194 | * Given a name find the corresponding state. |
6a488035 | 195 | * |
6a0b768e TO |
196 | * @param string $name |
197 | * The state name. | |
6a488035 | 198 | * |
a6c01b45 CW |
199 | * @return object |
200 | * the state object | |
6a488035 | 201 | */ |
00be9182 | 202 | public function find($name) { |
6a488035 TO |
203 | if (array_key_exists($name, $this->_states)) { |
204 | return $this->_states[$name]; | |
205 | } | |
206 | else { | |
207 | return NULL; | |
208 | } | |
209 | } | |
210 | ||
211 | /** | |
2e2605fe | 212 | * Return the list of state objects. |
6a488035 | 213 | * |
a6c01b45 CW |
214 | * @return array |
215 | * array of states in the state machine | |
6a488035 | 216 | */ |
00be9182 | 217 | public function getStates() { |
6a488035 TO |
218 | return $this->_states; |
219 | } | |
220 | ||
221 | /** | |
f9e31d7f | 222 | * Return the state object corresponding to the name. |
6a488035 | 223 | * |
6a0b768e TO |
224 | * @param string $name |
225 | * Name of page. | |
6a488035 | 226 | * |
16b10e64 CW |
227 | * @return CRM_Core_State |
228 | * state object matching the name | |
6a488035 | 229 | */ |
00be9182 | 230 | public function &getState($name) { |
6a488035 | 231 | if (isset($this->_states[$name])) { |
2aa397bc TO |
232 | return $this->_states[$name]; |
233 | } | |
6a488035 TO |
234 | |
235 | /* | |
236 | * This is a gross hack for ajax driven requests where | |
237 | * we change the form name to allow multiple edits to happen | |
238 | * We need a cleaner way of doing this going forward | |
239 | */ | |
481a74f4 | 240 | foreach ($this->_states as $n => $s) { |
6a488035 TO |
241 | if (substr($name, 0, strlen($n)) == $n) { |
242 | return $s; | |
243 | } | |
244 | } | |
245 | ||
2aa397bc | 246 | return NULL; |
6a488035 TO |
247 | } |
248 | ||
249 | /** | |
f9e31d7f | 250 | * Return the list of form objects. |
6a488035 | 251 | * |
a6c01b45 CW |
252 | * @return array |
253 | * array of pages in the state machine | |
6a488035 | 254 | */ |
00be9182 | 255 | public function getPages() { |
6a488035 TO |
256 | return $this->_pages; |
257 | } | |
258 | ||
259 | /** | |
2e2605fe | 260 | * Add sequential pages. |
6a488035 | 261 | * |
2e2605fe | 262 | * Meta level function to create a simple wizard for a state machine that is completely sequential. |
6a488035 | 263 | * |
6a0b768e TO |
264 | * @param array $pages |
265 | * (reference ) the array of page objects. | |
6a488035 | 266 | */ |
00be9182 | 267 | public function addSequentialPages(&$pages) { |
6a488035 TO |
268 | $this->_pages = &$pages; |
269 | $numPages = count($pages); | |
270 | ||
271 | $this->_pageNames = array(); | |
272 | foreach ($pages as $tempName => $value) { | |
a7488080 | 273 | if (!empty($value['className'])) { |
6a488035 TO |
274 | $this->_pageNames[] = $tempName; |
275 | } | |
276 | else { | |
277 | $this->_pageNames[] = CRM_Utils_String::getClassName($tempName); | |
278 | } | |
279 | } | |
280 | ||
281 | $i = 0; | |
282 | foreach ($pages as $tempName => $value) { | |
283 | $name = $this->_pageNames[$i]; | |
284 | ||
285 | $className = CRM_Utils_Array::value('className', | |
286 | $value, | |
287 | $tempName | |
288 | ); | |
289 | $classPath = str_replace('_', '/', $className) . '.php'; | |
290 | if ($numPages == 1) { | |
291 | $prev = $next = NULL; | |
292 | $type = CRM_Core_State::START | CRM_Core_State::FINISH; | |
293 | } | |
294 | elseif ($i == 0) { | |
295 | // start state | |
296 | $prev = NULL; | |
297 | $next = $this->_pageNames[$i + 1]; | |
298 | $type = CRM_Core_State::START; | |
299 | } | |
300 | elseif ($i == $numPages - 1) { | |
301 | // finish state | |
302 | $prev = $this->_pageNames[$i - 1]; | |
303 | $next = NULL; | |
304 | $type = CRM_Core_State::FINISH; | |
305 | } | |
306 | else { | |
307 | // in between simple state | |
308 | $prev = $this->_pageNames[$i - 1]; | |
309 | $next = $this->_pageNames[$i + 1]; | |
310 | $type = CRM_Core_State::SIMPLE; | |
311 | } | |
312 | ||
313 | $this->addState($name, $type, $prev, $next); | |
314 | ||
315 | $i++; | |
316 | } | |
317 | } | |
318 | ||
319 | /** | |
f9e31d7f | 320 | * Reset the state machine. |
6a488035 TO |
321 | * |
322 | * @return void | |
6a488035 | 323 | */ |
00be9182 | 324 | public function reset() { |
6a488035 TO |
325 | $this->_controller->reset(); |
326 | } | |
327 | ||
328 | /** | |
f9e31d7f | 329 | * Getter for action. |
6a488035 TO |
330 | * |
331 | * @return int | |
6a488035 | 332 | */ |
00be9182 | 333 | public function getAction() { |
6a488035 TO |
334 | return $this->_action; |
335 | } | |
336 | ||
337 | /** | |
f9e31d7f | 338 | * Setter for content. |
6a488035 | 339 | * |
6a0b768e TO |
340 | * @param string $content |
341 | * The content generated by this state machine. | |
6a488035 TO |
342 | * |
343 | * @return void | |
6a488035 | 344 | */ |
00be9182 | 345 | public function setContent(&$content) { |
6a488035 TO |
346 | $this->_controller->setContent($content); |
347 | } | |
348 | ||
349 | /** | |
f9e31d7f | 350 | * Getter for content. |
6a488035 TO |
351 | * |
352 | * @return string | |
6a488035 | 353 | */ |
00be9182 | 354 | public function &getContent() { |
6a488035 TO |
355 | return $this->_controller->getContent(); |
356 | } | |
357 | ||
a0ee3941 EM |
358 | /** |
359 | * @return mixed | |
360 | */ | |
00be9182 | 361 | public function getDestination() { |
6a488035 TO |
362 | return $this->_controller->getDestination(); |
363 | } | |
364 | ||
a0ee3941 EM |
365 | /** |
366 | * @return mixed | |
367 | */ | |
00be9182 | 368 | public function getSkipRedirection() { |
6a488035 TO |
369 | return $this->_controller->getSkipRedirection(); |
370 | } | |
371 | ||
a0ee3941 EM |
372 | /** |
373 | * @return mixed | |
374 | */ | |
00be9182 | 375 | public function fini() { |
6a488035 TO |
376 | return $this->_controller->fini(); |
377 | } | |
378 | ||
a0ee3941 EM |
379 | /** |
380 | * @return mixed | |
381 | */ | |
00be9182 | 382 | public function cancelAction() { |
6a488035 TO |
383 | return $this->_controller->cancelAction(); |
384 | } | |
385 | ||
386 | /** | |
387 | * Should the controller reset the session | |
388 | * In some cases, specifically search we want to remember | |
389 | * state across various actions and want to go back to the | |
390 | * beginning from the final state, but retain the same session | |
391 | * values | |
392 | * | |
389bcebf | 393 | * @return bool |
6a488035 | 394 | */ |
00be9182 | 395 | public function shouldReset() { |
6a488035 | 396 | return TRUE; |
2aa397bc | 397 | } |
6a488035 TO |
398 | |
399 | } |