Merge pull request #4875 from civicrm/minor-fix
[civicrm-core.git] / CRM / Core / State.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * The basic state element. Each state element is linked to a form and
30 * represents the form in the transition diagram. We use the state to
31 * determine what action to take on various user input. Actions include
32 * things like going back / stepping forward / process etc
33 *
34 * @package CRM
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * $Id$
37 *
38 */
39 class CRM_Core_State {
40
41 /**
42 * State name
43 * @var string
44 */
45 protected $_name;
46
47 /**
48 * This is a combination "OR" of the STATE_* constants defined below
49 * @var int
50 */
51 protected $_type;
52
53 /**
54 * The state that precedes this state
55 * @var CRM_Core_State
56 */
57 protected $_back;
58
59 /**
60 * The state that succeeds this state
61 * @var CRM_Core_State
62 */
63 protected $_next;
64
65 /**
66 * The state machine that this state is part of
67 * @var CRM_Core_StateMachine
68 */
69 protected $_stateMachine;
70
71 /**
72 * The different types of states. As we flush out the framework more
73 * we will introduce other conditional / looping states which will
74 * bring in more complexity to the framework. For now, lets keep it simple
75 * @var int
76 */
77 const START = 1, FINISH = 2, SIMPLE = 4;
78
79 /**
80 * Constructor
81 *
82 * @param string $name
83 * Internal name of the state.
84 * @param int $type
85 * State type.
86 * @param CRM_Core_State $back
87 * State that precedes this state.
88 * @param CRM_Core_State $next
89 * State that follows this state.
90 * @param CRM_Core_StateMachine $stateMachine
91 * Statemachine that this states belongs to.
92 *
93 * @return CRM_Core_State
94 */
95 public function __construct($name, $type, $back, $next, &$stateMachine) {
96 $this->_name = $name;
97 $this->_type = $type;
98 $this->_back = $back;
99 $this->_next = $next;
100
101 $this->_stateMachine = &$stateMachine;
102 }
103
104 public function debugPrint() {
105 CRM_Core_Error::debug("{$this->_name}, {$this->_type}", "{$this->_back}, {$this->_next}");
106 }
107
108 /**
109 * Given an CRM Form, jump to the previous page
110 *
111 * @param object the CRM_Core_Form element under consideration
112 *
113 * @return mixed
114 * does a jump to the back state
115 */
116 public function handleBackState(&$page) {
117 if ($this->_type & self::START) {
118 $page->handle('display');
119 }
120 else {
121 $back = &$page->controller->getPage($this->_back);
122 return $back->handle('jump');
123 }
124 }
125
126 /**
127 * Given an CRM Form, jump to the next page
128 *
129 * @param object the CRM_Core_Form element under consideration
130 *
131 * @return mixed
132 * does a jump to the nextstate
133 */
134 public function handleNextState(&$page) {
135 if ($this->_type & self::FINISH) {
136 $page->handle('process');
137 }
138 else {
139 $next = &$page->controller->getPage($this->_next);
140 return $next->handle('jump');
141 }
142 }
143
144 /**
145 * Determine the name of the next state. This is useful when we want
146 * to display the navigation labels or potential path
147 *
148 * @return string
149 */
150 public function getNextState() {
151 if ($this->_type & self::FINISH) {
152 return NULL;
153 }
154 else {
155 $next = &$page->controller->getPage($this->_next);
156 return $next;
157 }
158 }
159
160 /**
161 * Mark this page as valid for the QFC framework. This is needed as
162 * we build more advanced functionality into the StateMachine
163 *
164 * @param object the QFC data container
165 *
166 * @return void
167 */
168 public function validate(&$data) {
169 $data['valid'][$this->_name] = TRUE;
170 }
171
172 /**
173 * Mark this page as invalid for the QFC framework. This is needed as
174 * we build more advanced functionality into the StateMachine
175 *
176 * @param object the QFC data container
177 *
178 * @return void
179 */
180 public function invalidate(&$data) {
181 $data['valid'][$this->_name] = NULL;
182 }
183
184 /**
185 * Getter for name
186 *
187 * @return string
188 */
189 public function getName() {
190 return $this->_name;
191 }
192
193 /**
194 * Setter for name
195 *
196 * @param string
197 *
198 * @return void
199 */
200 public function setName($name) {
201 $this->_name = $name;
202 }
203
204 /**
205 * Getter for type
206 *
207 * @return int
208 */
209 public function getType() {
210 return $this->_type;
211 }
212 }