3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Core StateMachine. All statemachines subclass for the core one
38 * for functionality specific to their needs.
40 * A statemachine keeps track of differnt states and forms for a
41 * html quickform controller.
44 class CRM_Core_StateMachine
{
47 * The controller of this state machine
50 protected $_controller;
53 * The list of states that belong to this state machine
59 * The list of pages that belong to this state machine. Note
60 * that a state and a form have a 1 <-> 1 relationship. so u
61 * can always derive one from the other
67 * The names of the pages
71 protected $_pageNames;
74 * The mode that the state machine is operating in
77 protected $_action = NULL;
80 * The display name for this machine
83 protected $_name = NULL;
88 * @param object $controller
89 * The controller for this state machine.
91 * @param \const|int $action
93 * @return \CRM_Core_StateMachine
95 public function __construct(&$controller, $action = CRM_Core_Action
::NONE
) {
96 $this->_controller
= &$controller;
97 $this->_action
= $action;
99 $this->_states
= array();
107 public function getName() {
114 * @param string $name
118 public function setName($name) {
119 $this->_name
= $name;
123 * Do a state transition jump. Currently only supported types are
124 * Next and Back. The other actions (Cancel, Done, Submit etc) do
125 * not need the state machine to figure out where to go
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).
136 public function perform(&$page, $actionName, $type = 'Next') {
137 // save the form values and validation status to the session
138 $page->isFormBuilt() or $page->buildForm();
140 $pageName = $page->getAttribute('name');
141 $data = &$page->controller
->container();
143 $data['values'][$pageName] = $page->exportValues();
144 $data['valid'][$pageName] = $page->validate();
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');
152 $state = &$this->_states
[$pageName];
154 // dont know how or why we landed here so abort and display
157 return $page->handle('display');
160 // the page is valid, process it if we are jumping to the next state
161 if ($type == 'Next') {
162 $page->mainProcess();
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);
169 $state->handleBackState($page);
174 * Helper function to add a State to the state machine
176 * @param string $name
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.
187 public function addState($name, $type, $prev, $next) {
188 $this->_states
[$name] = new CRM_Core_State($name, $type, $prev, $next, $this);
192 * Given a name find the corresponding state
194 * @param string $name
200 public function find($name) {
201 if (array_key_exists($name, $this->_states
)) {
202 return $this->_states
[$name];
210 * Return the list of state objects
213 * array of states in the state machine
215 public function getStates() {
216 return $this->_states
;
220 * Return the state object corresponding to the name
222 * @param string $name
225 * @return CRM_Core_State
226 * state object matching the name
228 public function &getState($name) {
229 if (isset($this->_states
[$name])) {
230 return $this->_states
[$name];
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
238 foreach ($this->_states
as $n => $s) {
239 if (substr($name, 0, strlen($n)) == $n) {
248 * Return the list of form objects
251 * array of pages in the state machine
253 public function getPages() {
254 return $this->_pages
;
258 * AddSequentialStates: meta level function to create a simple
259 * wizard for a state machine that is completely sequential.
262 * @param array $pages
263 * (reference ) the array of page objects.
265 * @internal param array $states states is an array of arrays. Each element
266 * of the top level array describes a state. Each state description
267 * includes the name, the display name and the class name
271 public function addSequentialPages(&$pages) {
272 $this->_pages
= &$pages;
273 $numPages = count($pages);
275 $this->_pageNames
= array();
276 foreach ($pages as $tempName => $value) {
277 if (!empty($value['className'])) {
278 $this->_pageNames
[] = $tempName;
281 $this->_pageNames
[] = CRM_Utils_String
::getClassName($tempName);
286 foreach ($pages as $tempName => $value) {
287 $name = $this->_pageNames
[$i];
289 $className = CRM_Utils_Array
::value('className',
293 $classPath = str_replace('_', '/', $className) . '.php';
294 if ($numPages == 1) {
295 $prev = $next = NULL;
296 $type = CRM_Core_State
::START | CRM_Core_State
::FINISH
;
301 $next = $this->_pageNames
[$i +
1];
302 $type = CRM_Core_State
::START
;
304 elseif ($i == $numPages - 1) {
306 $prev = $this->_pageNames
[$i - 1];
308 $type = CRM_Core_State
::FINISH
;
311 // in between simple state
312 $prev = $this->_pageNames
[$i - 1];
313 $next = $this->_pageNames
[$i +
1];
314 $type = CRM_Core_State
::SIMPLE
;
317 $this->addState($name, $type, $prev, $next);
324 * Reset the state machine
328 public function reset() {
329 $this->_controller
->reset();
337 public function getAction() {
338 return $this->_action
;
344 * @param string $content
345 * The content generated by this state machine.
349 public function setContent(&$content) {
350 $this->_controller
->setContent($content);
358 public function &getContent() {
359 return $this->_controller
->getContent();
365 public function getDestination() {
366 return $this->_controller
->getDestination();
372 public function getSkipRedirection() {
373 return $this->_controller
->getSkipRedirection();
379 public function fini() {
380 return $this->_controller
->fini();
386 public function cancelAction() {
387 return $this->_controller
->cancelAction();
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
399 public function shouldReset() {