Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * The basic state element. Each state element is linked to a form and | |
14 | * represents the form in the transition diagram. We use the state to | |
15 | * determine what action to take on various user input. Actions include | |
16 | * things like going back / stepping forward / process etc | |
17 | * | |
18 | * @package CRM | |
ca5cec67 | 19 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
20 | */ |
21 | class CRM_Core_State { | |
22 | ||
23 | /** | |
f9e31d7f | 24 | * State name. |
6a488035 TO |
25 | * @var string |
26 | */ | |
27 | protected $_name; | |
28 | ||
29 | /** | |
100fef9d | 30 | * This is a combination "OR" of the STATE_* constants defined below |
6a488035 TO |
31 | * @var int |
32 | */ | |
33 | protected $_type; | |
34 | ||
35 | /** | |
f9e31d7f | 36 | * The state that precedes this state. |
c490a46a | 37 | * @var CRM_Core_State |
6a488035 TO |
38 | */ |
39 | protected $_back; | |
40 | ||
41 | /** | |
f9e31d7f | 42 | * The state that succeeds this state. |
c490a46a | 43 | * @var CRM_Core_State |
6a488035 TO |
44 | */ |
45 | protected $_next; | |
46 | ||
47 | /** | |
f9e31d7f | 48 | * The state machine that this state is part of. |
c490a46a | 49 | * @var CRM_Core_StateMachine |
6a488035 TO |
50 | */ |
51 | protected $_stateMachine; | |
52 | ||
53 | /** | |
54 | * The different types of states. As we flush out the framework more | |
55 | * we will introduce other conditional / looping states which will | |
56 | * bring in more complexity to the framework. For now, lets keep it simple | |
57 | * @var int | |
58 | */ | |
7da04cde | 59 | const START = 1, FINISH = 2, SIMPLE = 4; |
6a488035 TO |
60 | |
61 | /** | |
f9e31d7f | 62 | * Constructor. |
6a488035 | 63 | * |
6a0b768e TO |
64 | * @param string $name |
65 | * Internal name of the state. | |
66 | * @param int $type | |
67 | * State type. | |
68 | * @param CRM_Core_State $back | |
69 | * State that precedes this state. | |
70 | * @param CRM_Core_State $next | |
71 | * State that follows this state. | |
72 | * @param CRM_Core_StateMachine $stateMachine | |
73 | * Statemachine that this states belongs to. | |
6a488035 | 74 | * |
c490a46a | 75 | * @return CRM_Core_State |
6a488035 | 76 | */ |
00be9182 | 77 | public function __construct($name, $type, $back, $next, &$stateMachine) { |
6a488035 TO |
78 | $this->_name = $name; |
79 | $this->_type = $type; | |
80 | $this->_back = $back; | |
81 | $this->_next = $next; | |
82 | ||
83 | $this->_stateMachine = &$stateMachine; | |
84 | } | |
85 | ||
00be9182 | 86 | public function debugPrint() { |
6a488035 TO |
87 | CRM_Core_Error::debug("{$this->_name}, {$this->_type}", "{$this->_back}, {$this->_next}"); |
88 | } | |
89 | ||
90 | /** | |
ad37ac8e | 91 | * Given an CRM Form, jump to the previous page. |
92 | * | |
93 | * @param CRM_Core_Page $page | |
6a488035 | 94 | * |
72b3a70c CW |
95 | * @return mixed |
96 | * does a jump to the back state | |
6a488035 | 97 | */ |
00be9182 | 98 | public function handleBackState(&$page) { |
6a488035 TO |
99 | if ($this->_type & self::START) { |
100 | $page->handle('display'); | |
101 | } | |
102 | else { | |
103 | $back = &$page->controller->getPage($this->_back); | |
104 | return $back->handle('jump'); | |
105 | } | |
106 | } | |
107 | ||
108 | /** | |
ad37ac8e | 109 | * Given an CRM Form, jump to the next page. |
110 | * | |
111 | * @param CRM_Core_Page $page | |
6a488035 | 112 | * |
72b3a70c | 113 | * @return mixed |
ad37ac8e | 114 | * Does a jump to the nextstate |
6a488035 | 115 | */ |
00be9182 | 116 | public function handleNextState(&$page) { |
6a488035 TO |
117 | if ($this->_type & self::FINISH) { |
118 | $page->handle('process'); | |
119 | } | |
120 | else { | |
121 | $next = &$page->controller->getPage($this->_next); | |
122 | return $next->handle('jump'); | |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
ad37ac8e | 127 | * Determine the name of the next state. |
128 | * | |
129 | * This is useful when we want to display the navigation labels or potential path. | |
6a488035 TO |
130 | * |
131 | * @return string | |
6a488035 | 132 | */ |
00be9182 | 133 | public function getNextState() { |
6a488035 TO |
134 | if ($this->_type & self::FINISH) { |
135 | return NULL; | |
136 | } | |
137 | else { | |
138 | $next = &$page->controller->getPage($this->_next); | |
139 | return $next; | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
8eedd10a | 144 | * Mark this page as valid for the QFC framework. |
ad37ac8e | 145 | * |
146 | * @param array $data | |
6a488035 | 147 | */ |
00be9182 | 148 | public function validate(&$data) { |
6a488035 TO |
149 | $data['valid'][$this->_name] = TRUE; |
150 | } | |
151 | ||
152 | /** | |
8eedd10a | 153 | * Mark this page as invalid for the QFC framework. |
ad37ac8e | 154 | * |
155 | * @param array $data | |
6a488035 | 156 | */ |
00be9182 | 157 | public function invalidate(&$data) { |
6a488035 TO |
158 | $data['valid'][$this->_name] = NULL; |
159 | } | |
160 | ||
161 | /** | |
f9e31d7f | 162 | * Getter for name. |
6a488035 TO |
163 | * |
164 | * @return string | |
6a488035 | 165 | */ |
00be9182 | 166 | public function getName() { |
6a488035 TO |
167 | return $this->_name; |
168 | } | |
169 | ||
170 | /** | |
f9e31d7f | 171 | * Setter for name. |
54957108 | 172 | * |
173 | * @param string $name | |
6a488035 | 174 | */ |
00be9182 | 175 | public function setName($name) { |
6a488035 TO |
176 | $this->_name = $name; |
177 | } | |
178 | ||
179 | /** | |
f9e31d7f | 180 | * Getter for type. |
6a488035 TO |
181 | * |
182 | * @return int | |
6a488035 | 183 | */ |
00be9182 | 184 | public function getType() { |
6a488035 TO |
185 | return $this->_type; |
186 | } | |
96025800 | 187 | |
6a488035 | 188 | } |