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 | * 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 | |
06b69b18 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * $Id$ |
37 | * | |
38 | */ | |
39 | class CRM_Core_State { | |
40 | ||
41 | /** | |
100fef9d | 42 | * State name |
6a488035 TO |
43 | * @var string |
44 | */ | |
45 | protected $_name; | |
46 | ||
47 | /** | |
100fef9d | 48 | * This is a combination "OR" of the STATE_* constants defined below |
6a488035 TO |
49 | * @var int |
50 | */ | |
51 | protected $_type; | |
52 | ||
53 | /** | |
100fef9d | 54 | * The state that precedes this state |
c490a46a | 55 | * @var CRM_Core_State |
6a488035 TO |
56 | */ |
57 | protected $_back; | |
58 | ||
59 | /** | |
100fef9d | 60 | * The state that succeeds this state |
c490a46a | 61 | * @var CRM_Core_State |
6a488035 TO |
62 | */ |
63 | protected $_next; | |
64 | ||
65 | /** | |
66 | * The state machine that this state is part of | |
c490a46a | 67 | * @var CRM_Core_StateMachine |
6a488035 TO |
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 | */ | |
7da04cde | 77 | const START = 1, FINISH = 2, SIMPLE = 4; |
6a488035 TO |
78 | |
79 | /** | |
100fef9d | 80 | * Constructor |
6a488035 | 81 | * |
6a0b768e TO |
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. | |
6a488035 | 92 | * |
c490a46a | 93 | * @return CRM_Core_State |
6a488035 | 94 | */ |
00be9182 | 95 | public function __construct($name, $type, $back, $next, &$stateMachine) { |
6a488035 TO |
96 | $this->_name = $name; |
97 | $this->_type = $type; | |
98 | $this->_back = $back; | |
99 | $this->_next = $next; | |
100 | ||
101 | $this->_stateMachine = &$stateMachine; | |
102 | } | |
103 | ||
00be9182 | 104 | public function debugPrint() { |
6a488035 TO |
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 does a jump to the back state | |
6a488035 | 114 | */ |
00be9182 | 115 | public function handleBackState(&$page) { |
6a488035 TO |
116 | if ($this->_type & self::START) { |
117 | $page->handle('display'); | |
118 | } | |
119 | else { | |
120 | $back = &$page->controller->getPage($this->_back); | |
121 | return $back->handle('jump'); | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
126 | * Given an CRM Form, jump to the next page | |
127 | * | |
128 | * @param object the CRM_Core_Form element under consideration | |
129 | * | |
130 | * @return mixed does a jump to the nextstate | |
6a488035 | 131 | */ |
00be9182 | 132 | public function handleNextState(&$page) { |
6a488035 TO |
133 | if ($this->_type & self::FINISH) { |
134 | $page->handle('process'); | |
135 | } | |
136 | else { | |
137 | $next = &$page->controller->getPage($this->_next); | |
138 | return $next->handle('jump'); | |
139 | } | |
140 | } | |
141 | ||
142 | /** | |
143 | * Determine the name of the next state. This is useful when we want | |
144 | * to display the navigation labels or potential path | |
145 | * | |
146 | * @return string | |
6a488035 | 147 | */ |
00be9182 | 148 | public function getNextState() { |
6a488035 TO |
149 | if ($this->_type & self::FINISH) { |
150 | return NULL; | |
151 | } | |
152 | else { | |
153 | $next = &$page->controller->getPage($this->_next); | |
154 | return $next; | |
155 | } | |
156 | } | |
157 | ||
158 | /** | |
159 | * Mark this page as valid for the QFC framework. This is needed as | |
160 | * we build more advanced functionality into the StateMachine | |
161 | * | |
162 | * @param object the QFC data container | |
163 | * | |
164 | * @return void | |
6a488035 | 165 | */ |
00be9182 | 166 | public function validate(&$data) { |
6a488035 TO |
167 | $data['valid'][$this->_name] = TRUE; |
168 | } | |
169 | ||
170 | /** | |
171 | * Mark this page as invalid for the QFC framework. This is needed as | |
172 | * we build more advanced functionality into the StateMachine | |
173 | * | |
174 | * @param object the QFC data container | |
175 | * | |
176 | * @return void | |
6a488035 | 177 | */ |
00be9182 | 178 | public function invalidate(&$data) { |
6a488035 TO |
179 | $data['valid'][$this->_name] = NULL; |
180 | } | |
181 | ||
182 | /** | |
100fef9d | 183 | * Getter for name |
6a488035 TO |
184 | * |
185 | * @return string | |
6a488035 | 186 | */ |
00be9182 | 187 | public function getName() { |
6a488035 TO |
188 | return $this->_name; |
189 | } | |
190 | ||
191 | /** | |
100fef9d | 192 | * Setter for name |
6a488035 TO |
193 | * |
194 | * @param string | |
195 | * | |
196 | * @return void | |
6a488035 | 197 | */ |
00be9182 | 198 | public function setName($name) { |
6a488035 TO |
199 | $this->_name = $name; |
200 | } | |
201 | ||
202 | /** | |
100fef9d | 203 | * Getter for type |
6a488035 TO |
204 | * |
205 | * @return int | |
6a488035 | 206 | */ |
00be9182 | 207 | public function getType() { |
6a488035 TO |
208 | return $this->_type; |
209 | } | |
210 | } |