3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * Core StateMachine. All statemachines subclass for the core one
38 * for functionality specific to their needs.
40 * A statemachine keeps track of different 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.
125 * Currently only supported types are
126 * Next and Back. The other actions (Cancel, Done, Submit etc) do
127 * not need the state machine to figure out where to go
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).
138 public function perform(&$page, $actionName, $type = 'Next') {
139 // save the form values and validation status to the session
140 $page->isFormBuilt() or $page->buildForm();
142 $pageName = $page->getAttribute('name');
143 $data = &$page->controller
->container();
145 $data['values'][$pageName] = $page->exportValues();
146 $data['valid'][$pageName] = $page->validate();
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');
154 $state = &$this->_states
[$pageName];
156 // dont know how or why we landed here so abort and display
159 return $page->handle('display');
162 // the page is valid, process it if we are jumping to the next state
163 if ($type == 'Next') {
164 $page->mainProcess();
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);
171 $state->handleBackState($page);
176 * Helper function to add a State to the state machine.
178 * @param string $name
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.
189 public function addState($name, $type, $prev, $next) {
190 $this->_states
[$name] = new CRM_Core_State($name, $type, $prev, $next, $this);
194 * Given a name find the corresponding state.
196 * @param string $name
202 public function find($name) {
203 if (array_key_exists($name, $this->_states
)) {
204 return $this->_states
[$name];
212 * Return the list of state objects.
215 * array of states in the state machine
217 public function getStates() {
218 return $this->_states
;
222 * Return the state object corresponding to the name.
224 * @param string $name
227 * @return CRM_Core_State
228 * state object matching the name
230 public function &getState($name) {
231 if (isset($this->_states
[$name])) {
232 return $this->_states
[$name];
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
240 foreach ($this->_states
as $n => $s) {
241 if (substr($name, 0, strlen($n)) == $n) {
250 * Return the list of form objects.
253 * array of pages in the state machine
255 public function getPages() {
256 return $this->_pages
;
260 * Add sequential pages.
262 * Meta level function to create a simple wizard for a state machine that is completely sequential.
264 * @param array $pages
265 * (reference ) the array of page objects.
267 public function addSequentialPages(&$pages) {
268 $this->_pages
= &$pages;
269 $numPages = count($pages);
271 $this->_pageNames
= array();
272 foreach ($pages as $tempName => $value) {
273 if (!empty($value['className'])) {
274 $this->_pageNames
[] = $tempName;
277 $this->_pageNames
[] = CRM_Utils_String
::getClassName($tempName);
282 foreach ($pages as $tempName => $value) {
283 $name = $this->_pageNames
[$i];
285 $className = CRM_Utils_Array
::value('className',
289 $classPath = str_replace('_', '/', $className) . '.php';
290 if ($numPages == 1) {
291 $prev = $next = NULL;
292 $type = CRM_Core_State
::START | CRM_Core_State
::FINISH
;
297 $next = $this->_pageNames
[$i +
1];
298 $type = CRM_Core_State
::START
;
300 elseif ($i == $numPages - 1) {
302 $prev = $this->_pageNames
[$i - 1];
304 $type = CRM_Core_State
::FINISH
;
307 // in between simple state
308 $prev = $this->_pageNames
[$i - 1];
309 $next = $this->_pageNames
[$i +
1];
310 $type = CRM_Core_State
::SIMPLE
;
313 $this->addState($name, $type, $prev, $next);
320 * Reset the state machine.
324 public function reset() {
325 $this->_controller
->reset();
333 public function getAction() {
334 return $this->_action
;
338 * Setter for content.
340 * @param string $content
341 * The content generated by this state machine.
345 public function setContent(&$content) {
346 $this->_controller
->setContent($content);
350 * Getter for content.
354 public function &getContent() {
355 return $this->_controller
->getContent();
361 public function getDestination() {
362 return $this->_controller
->getDestination();
368 public function getSkipRedirection() {
369 return $this->_controller
->getSkipRedirection();
375 public function fini() {
376 return $this->_controller
->fini();
382 public function cancelAction() {
383 return $this->_controller
->cancelAction();
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
395 public function shouldReset() {