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