Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
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 | |
e7112fa7 | 35 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
36 | */ |
37 | class CRM_Core_State { | |
38 | ||
39 | /** | |
f9e31d7f | 40 | * State name. |
6a488035 TO |
41 | * @var string |
42 | */ | |
43 | protected $_name; | |
44 | ||
45 | /** | |
100fef9d | 46 | * This is a combination "OR" of the STATE_* constants defined below |
6a488035 TO |
47 | * @var int |
48 | */ | |
49 | protected $_type; | |
50 | ||
51 | /** | |
f9e31d7f | 52 | * The state that precedes this state. |
c490a46a | 53 | * @var CRM_Core_State |
6a488035 TO |
54 | */ |
55 | protected $_back; | |
56 | ||
57 | /** | |
f9e31d7f | 58 | * The state that succeeds this state. |
c490a46a | 59 | * @var CRM_Core_State |
6a488035 TO |
60 | */ |
61 | protected $_next; | |
62 | ||
63 | /** | |
f9e31d7f | 64 | * The state machine that this state is part of. |
c490a46a | 65 | * @var CRM_Core_StateMachine |
6a488035 TO |
66 | */ |
67 | protected $_stateMachine; | |
68 | ||
69 | /** | |
70 | * The different types of states. As we flush out the framework more | |
71 | * we will introduce other conditional / looping states which will | |
72 | * bring in more complexity to the framework. For now, lets keep it simple | |
73 | * @var int | |
74 | */ | |
7da04cde | 75 | const START = 1, FINISH = 2, SIMPLE = 4; |
6a488035 TO |
76 | |
77 | /** | |
f9e31d7f | 78 | * Constructor. |
6a488035 | 79 | * |
6a0b768e TO |
80 | * @param string $name |
81 | * Internal name of the state. | |
82 | * @param int $type | |
83 | * State type. | |
84 | * @param CRM_Core_State $back | |
85 | * State that precedes this state. | |
86 | * @param CRM_Core_State $next | |
87 | * State that follows this state. | |
88 | * @param CRM_Core_StateMachine $stateMachine | |
89 | * Statemachine that this states belongs to. | |
6a488035 | 90 | * |
c490a46a | 91 | * @return CRM_Core_State |
6a488035 | 92 | */ |
00be9182 | 93 | public function __construct($name, $type, $back, $next, &$stateMachine) { |
6a488035 TO |
94 | $this->_name = $name; |
95 | $this->_type = $type; | |
96 | $this->_back = $back; | |
97 | $this->_next = $next; | |
98 | ||
99 | $this->_stateMachine = &$stateMachine; | |
100 | } | |
101 | ||
00be9182 | 102 | public function debugPrint() { |
6a488035 TO |
103 | CRM_Core_Error::debug("{$this->_name}, {$this->_type}", "{$this->_back}, {$this->_next}"); |
104 | } | |
105 | ||
106 | /** | |
ad37ac8e | 107 | * Given an CRM Form, jump to the previous page. |
108 | * | |
109 | * @param CRM_Core_Page $page | |
6a488035 | 110 | * |
72b3a70c CW |
111 | * @return mixed |
112 | * does a jump to the back state | |
6a488035 | 113 | */ |
00be9182 | 114 | public function handleBackState(&$page) { |
6a488035 TO |
115 | if ($this->_type & self::START) { |
116 | $page->handle('display'); | |
117 | } | |
118 | else { | |
119 | $back = &$page->controller->getPage($this->_back); | |
120 | return $back->handle('jump'); | |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
ad37ac8e | 125 | * Given an CRM Form, jump to the next page. |
126 | * | |
127 | * @param CRM_Core_Page $page | |
6a488035 | 128 | * |
72b3a70c | 129 | * @return mixed |
ad37ac8e | 130 | * 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 | /** | |
ad37ac8e | 143 | * Determine the name of the next state. |
144 | * | |
145 | * This is useful when we want to display the navigation labels or potential path. | |
6a488035 TO |
146 | * |
147 | * @return string | |
6a488035 | 148 | */ |
00be9182 | 149 | public function getNextState() { |
6a488035 TO |
150 | if ($this->_type & self::FINISH) { |
151 | return NULL; | |
152 | } | |
153 | else { | |
154 | $next = &$page->controller->getPage($this->_next); | |
155 | return $next; | |
156 | } | |
157 | } | |
158 | ||
159 | /** | |
8eedd10a | 160 | * Mark this page as valid for the QFC framework. |
ad37ac8e | 161 | * |
162 | * @param array $data | |
6a488035 | 163 | */ |
00be9182 | 164 | public function validate(&$data) { |
6a488035 TO |
165 | $data['valid'][$this->_name] = TRUE; |
166 | } | |
167 | ||
168 | /** | |
8eedd10a | 169 | * Mark this page as invalid for the QFC framework. |
ad37ac8e | 170 | * |
171 | * @param array $data | |
6a488035 | 172 | */ |
00be9182 | 173 | public function invalidate(&$data) { |
6a488035 TO |
174 | $data['valid'][$this->_name] = NULL; |
175 | } | |
176 | ||
177 | /** | |
f9e31d7f | 178 | * Getter for name. |
6a488035 TO |
179 | * |
180 | * @return string | |
6a488035 | 181 | */ |
00be9182 | 182 | public function getName() { |
6a488035 TO |
183 | return $this->_name; |
184 | } | |
185 | ||
186 | /** | |
f9e31d7f | 187 | * Setter for name. |
54957108 | 188 | * |
189 | * @param string $name | |
6a488035 | 190 | */ |
00be9182 | 191 | public function setName($name) { |
6a488035 TO |
192 | $this->_name = $name; |
193 | } | |
194 | ||
195 | /** | |
f9e31d7f | 196 | * Getter for type. |
6a488035 TO |
197 | * |
198 | * @return int | |
6a488035 | 199 | */ |
00be9182 | 200 | public function getType() { |
6a488035 TO |
201 | return $this->_type; |
202 | } | |
96025800 | 203 | |
6a488035 | 204 | } |