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 | /** | |
100fef9d | 47 | * The controller of this state machine |
6a488035 TO |
48 | * @var object |
49 | */ | |
50 | protected $_controller; | |
51 | ||
52 | /** | |
100fef9d | 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 | /** | |
67 | * The names of the pages | |
68 | * | |
69 | * @var array | |
70 | */ | |
71 | protected $_pageNames; | |
72 | ||
73 | /** | |
100fef9d | 74 | * The mode that the state machine is operating in |
6a488035 TO |
75 | * @var int |
76 | */ | |
77 | protected $_action = NULL; | |
78 | ||
79 | /** | |
80 | * The display name for this machine | |
81 | * @var string | |
82 | */ | |
83 | protected $_name = NULL; | |
84 | ||
85 | /** | |
100fef9d | 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 | /** | |
100fef9d | 103 | * Getter for name |
6a488035 TO |
104 | * |
105 | * @return string | |
6a488035 TO |
106 | */ |
107 | public function getName() { | |
108 | return $this->_name; | |
109 | } | |
110 | ||
111 | /** | |
100fef9d | 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 | /** | |
100fef9d | 123 | * Do a state transition jump. Currently only supported types are |
6a488035 TO |
124 | * Next and Back. The other actions (Cancel, Done, Submit etc) do |
125 | * not need the state machine to figure out where to go | |
126 | * | |
6a0b768e TO |
127 | * @param CRM_Core_Form $page |
128 | * The current form-page. | |
129 | * @param string $actionName | |
130 | * Current action name, as one Action object can serve multiple actions. | |
131 | * @param string $type | |
132 | * The type of transition being requested (Next or Back). | |
6a488035 TO |
133 | * |
134 | * @return void | |
6a488035 | 135 | */ |
00be9182 | 136 | public function perform(&$page, $actionName, $type = 'Next') { |
6a488035 TO |
137 | // save the form values and validation status to the session |
138 | $page->isFormBuilt() or $page->buildForm(); | |
139 | ||
140 | $pageName = $page->getAttribute('name'); | |
141 | $data = &$page->controller->container(); | |
142 | ||
143 | $data['values'][$pageName] = $page->exportValues(); | |
144 | $data['valid'][$pageName] = $page->validate(); | |
145 | ||
146 | // if we are going to the next state | |
147 | // Modal form and page is invalid: don't go further | |
148 | if ($type == 'Next' && !$data['valid'][$pageName]) { | |
149 | return $page->handle('display'); | |
150 | } | |
151 | ||
152 | $state = &$this->_states[$pageName]; | |
153 | ||
154 | // dont know how or why we landed here so abort and display | |
155 | // current page | |
156 | if (empty($state)) { | |
157 | return $page->handle('display'); | |
158 | } | |
159 | ||
160 | // the page is valid, process it if we are jumping to the next state | |
161 | if ($type == 'Next') { | |
e6a0a301 | 162 | $page->mainProcess(); |
6a488035 TO |
163 | // we get the state again, since postProcess might have changed it |
164 | // this bug took me forever to find :) Lobo | |
165 | $state = &$this->_states[$pageName]; | |
166 | $state->handleNextState($page); | |
167 | } | |
168 | else { | |
169 | $state->handleBackState($page); | |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
100fef9d | 174 | * Helper function to add a State to the state machine |
6a488035 | 175 | * |
6a0b768e TO |
176 | * @param string $name |
177 | * The internal name. | |
178 | * @param int $type | |
179 | * The type of state (START|FINISH|SIMPLE). | |
180 | * @param object $prev | |
181 | * The previous page if any. | |
182 | * @param object $next | |
183 | * The next page if any. | |
6a488035 TO |
184 | * |
185 | * @return void | |
6a488035 | 186 | */ |
00be9182 | 187 | public function addState($name, $type, $prev, $next) { |
6a488035 TO |
188 | $this->_states[$name] = new CRM_Core_State($name, $type, $prev, $next, $this); |
189 | } | |
190 | ||
191 | /** | |
192 | * Given a name find the corresponding state | |
193 | * | |
6a0b768e TO |
194 | * @param string $name |
195 | * The state name. | |
6a488035 | 196 | * |
a6c01b45 CW |
197 | * @return object |
198 | * the state object | |
6a488035 | 199 | */ |
00be9182 | 200 | public function find($name) { |
6a488035 TO |
201 | if (array_key_exists($name, $this->_states)) { |
202 | return $this->_states[$name]; | |
203 | } | |
204 | else { | |
205 | return NULL; | |
206 | } | |
207 | } | |
208 | ||
209 | /** | |
100fef9d | 210 | * Return the list of state objects |
6a488035 | 211 | * |
a6c01b45 CW |
212 | * @return array |
213 | * array of states in the state machine | |
6a488035 | 214 | */ |
00be9182 | 215 | public function getStates() { |
6a488035 TO |
216 | return $this->_states; |
217 | } | |
218 | ||
219 | /** | |
100fef9d | 220 | * Return the state object corresponding to the name |
6a488035 | 221 | * |
6a0b768e TO |
222 | * @param string $name |
223 | * Name of page. | |
6a488035 | 224 | * |
16b10e64 CW |
225 | * @return CRM_Core_State |
226 | * state object matching the name | |
6a488035 | 227 | */ |
00be9182 | 228 | public function &getState($name) { |
6a488035 | 229 | if (isset($this->_states[$name])) { |
2aa397bc TO |
230 | return $this->_states[$name]; |
231 | } | |
6a488035 TO |
232 | |
233 | /* | |
234 | * This is a gross hack for ajax driven requests where | |
235 | * we change the form name to allow multiple edits to happen | |
236 | * We need a cleaner way of doing this going forward | |
237 | */ | |
481a74f4 | 238 | foreach ($this->_states as $n => $s) { |
6a488035 TO |
239 | if (substr($name, 0, strlen($n)) == $n) { |
240 | return $s; | |
241 | } | |
242 | } | |
243 | ||
2aa397bc | 244 | return NULL; |
6a488035 TO |
245 | } |
246 | ||
247 | /** | |
100fef9d | 248 | * Return the list of form objects |
6a488035 | 249 | * |
a6c01b45 CW |
250 | * @return array |
251 | * array of pages in the state machine | |
6a488035 | 252 | */ |
00be9182 | 253 | public function getPages() { |
6a488035 TO |
254 | return $this->_pages; |
255 | } | |
256 | ||
257 | /** | |
100fef9d | 258 | * AddSequentialStates: meta level function to create a simple |
6a488035 TO |
259 | * wizard for a state machine that is completely sequential. |
260 | * | |
6a488035 | 261 | * |
6a0b768e TO |
262 | * @param array $pages |
263 | * (reference ) the array of page objects. | |
fd31fa4c EM |
264 | * |
265 | * @internal param array $states states is an array of arrays. Each element | |
6a488035 TO |
266 | * of the top level array describes a state. Each state description |
267 | * includes the name, the display name and the class name | |
268 | * | |
6a488035 TO |
269 | * @return void |
270 | */ | |
00be9182 | 271 | public function addSequentialPages(&$pages) { |
6a488035 TO |
272 | $this->_pages = &$pages; |
273 | $numPages = count($pages); | |
274 | ||
275 | $this->_pageNames = array(); | |
276 | foreach ($pages as $tempName => $value) { | |
a7488080 | 277 | if (!empty($value['className'])) { |
6a488035 TO |
278 | $this->_pageNames[] = $tempName; |
279 | } | |
280 | else { | |
281 | $this->_pageNames[] = CRM_Utils_String::getClassName($tempName); | |
282 | } | |
283 | } | |
284 | ||
285 | $i = 0; | |
286 | foreach ($pages as $tempName => $value) { | |
287 | $name = $this->_pageNames[$i]; | |
288 | ||
289 | $className = CRM_Utils_Array::value('className', | |
290 | $value, | |
291 | $tempName | |
292 | ); | |
293 | $classPath = str_replace('_', '/', $className) . '.php'; | |
294 | if ($numPages == 1) { | |
295 | $prev = $next = NULL; | |
296 | $type = CRM_Core_State::START | CRM_Core_State::FINISH; | |
297 | } | |
298 | elseif ($i == 0) { | |
299 | // start state | |
300 | $prev = NULL; | |
301 | $next = $this->_pageNames[$i + 1]; | |
302 | $type = CRM_Core_State::START; | |
303 | } | |
304 | elseif ($i == $numPages - 1) { | |
305 | // finish state | |
306 | $prev = $this->_pageNames[$i - 1]; | |
307 | $next = NULL; | |
308 | $type = CRM_Core_State::FINISH; | |
309 | } | |
310 | else { | |
311 | // in between simple state | |
312 | $prev = $this->_pageNames[$i - 1]; | |
313 | $next = $this->_pageNames[$i + 1]; | |
314 | $type = CRM_Core_State::SIMPLE; | |
315 | } | |
316 | ||
317 | $this->addState($name, $type, $prev, $next); | |
318 | ||
319 | $i++; | |
320 | } | |
321 | } | |
322 | ||
323 | /** | |
100fef9d | 324 | * Reset the state machine |
6a488035 TO |
325 | * |
326 | * @return void | |
6a488035 | 327 | */ |
00be9182 | 328 | public function reset() { |
6a488035 TO |
329 | $this->_controller->reset(); |
330 | } | |
331 | ||
332 | /** | |
100fef9d | 333 | * Getter for action |
6a488035 TO |
334 | * |
335 | * @return int | |
6a488035 | 336 | */ |
00be9182 | 337 | public function getAction() { |
6a488035 TO |
338 | return $this->_action; |
339 | } | |
340 | ||
341 | /** | |
100fef9d | 342 | * Setter for content |
6a488035 | 343 | * |
6a0b768e TO |
344 | * @param string $content |
345 | * The content generated by this state machine. | |
6a488035 TO |
346 | * |
347 | * @return void | |
6a488035 | 348 | */ |
00be9182 | 349 | public function setContent(&$content) { |
6a488035 TO |
350 | $this->_controller->setContent($content); |
351 | } | |
352 | ||
353 | /** | |
100fef9d | 354 | * Getter for content |
6a488035 TO |
355 | * |
356 | * @return string | |
6a488035 | 357 | */ |
00be9182 | 358 | public function &getContent() { |
6a488035 TO |
359 | return $this->_controller->getContent(); |
360 | } | |
361 | ||
a0ee3941 EM |
362 | /** |
363 | * @return mixed | |
364 | */ | |
00be9182 | 365 | public function getDestination() { |
6a488035 TO |
366 | return $this->_controller->getDestination(); |
367 | } | |
368 | ||
a0ee3941 EM |
369 | /** |
370 | * @return mixed | |
371 | */ | |
00be9182 | 372 | public function getSkipRedirection() { |
6a488035 TO |
373 | return $this->_controller->getSkipRedirection(); |
374 | } | |
375 | ||
a0ee3941 EM |
376 | /** |
377 | * @return mixed | |
378 | */ | |
00be9182 | 379 | public function fini() { |
6a488035 TO |
380 | return $this->_controller->fini(); |
381 | } | |
382 | ||
a0ee3941 EM |
383 | /** |
384 | * @return mixed | |
385 | */ | |
00be9182 | 386 | public function cancelAction() { |
6a488035 TO |
387 | return $this->_controller->cancelAction(); |
388 | } | |
389 | ||
390 | /** | |
391 | * Should the controller reset the session | |
392 | * In some cases, specifically search we want to remember | |
393 | * state across various actions and want to go back to the | |
394 | * beginning from the final state, but retain the same session | |
395 | * values | |
396 | * | |
389bcebf | 397 | * @return bool |
6a488035 | 398 | */ |
00be9182 | 399 | public function shouldReset() { |
6a488035 | 400 | return TRUE; |
2aa397bc | 401 | } |
6a488035 TO |
402 | |
403 | } |