Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 object | |
56 | */ | |
57 | protected $_back; | |
58 | ||
59 | /** | |
60 | * the state that succeeds this state | |
61 | * @var object | |
62 | */ | |
63 | protected $_next; | |
64 | ||
65 | /** | |
66 | * The state machine that this state is part of | |
67 | * @var object | |
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 the internal name of the state | |
83 | * @param int the state type | |
84 | * @param object the state that precedes this state | |
85 | * @param object the state that follows this state | |
86 | * @param object the statemachine that this states belongs to | |
87 | * | |
88 | * @return object | |
89 | * @access public | |
90 | */ | |
91 | function __construct($name, $type, $back, $next, &$stateMachine) { | |
92 | $this->_name = $name; | |
93 | $this->_type = $type; | |
94 | $this->_back = $back; | |
95 | $this->_next = $next; | |
96 | ||
97 | $this->_stateMachine = &$stateMachine; | |
98 | } | |
99 | ||
100 | function debugPrint() { | |
101 | CRM_Core_Error::debug("{$this->_name}, {$this->_type}", "{$this->_back}, {$this->_next}"); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Given an CRM Form, jump to the previous page | |
106 | * | |
107 | * @param object the CRM_Core_Form element under consideration | |
108 | * | |
109 | * @return mixed does a jump to the back state | |
110 | * @access public | |
111 | */ | |
112 | function handleBackState(&$page) { | |
113 | if ($this->_type & self::START) { | |
114 | $page->handle('display'); | |
115 | } | |
116 | else { | |
117 | $back = &$page->controller->getPage($this->_back); | |
118 | return $back->handle('jump'); | |
119 | } | |
120 | } | |
121 | ||
122 | /** | |
123 | * Given an CRM Form, jump to the next page | |
124 | * | |
125 | * @param object the CRM_Core_Form element under consideration | |
126 | * | |
127 | * @return mixed does a jump to the nextstate | |
128 | * @access public | |
129 | */ | |
130 | function handleNextState(&$page) { | |
131 | if ($this->_type & self::FINISH) { | |
132 | $page->handle('process'); | |
133 | } | |
134 | else { | |
135 | $next = &$page->controller->getPage($this->_next); | |
136 | return $next->handle('jump'); | |
137 | } | |
138 | } | |
139 | ||
140 | /** | |
141 | * Determine the name of the next state. This is useful when we want | |
142 | * to display the navigation labels or potential path | |
143 | * | |
144 | * @return string | |
145 | * @access public | |
146 | */ | |
147 | function getNextState() { | |
148 | if ($this->_type & self::FINISH) { | |
149 | return NULL; | |
150 | } | |
151 | else { | |
152 | $next = &$page->controller->getPage($this->_next); | |
153 | return $next; | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
158 | * Mark this page as valid for the QFC framework. This is needed as | |
159 | * we build more advanced functionality into the StateMachine | |
160 | * | |
161 | * @param object the QFC data container | |
162 | * | |
163 | * @return void | |
164 | * @access public | |
165 | */ | |
166 | function validate(&$data) { | |
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 | |
177 | * @access public | |
178 | */ | |
179 | function invalidate(&$data) { | |
180 | $data['valid'][$this->_name] = NULL; | |
181 | } | |
182 | ||
183 | /** | |
184 | * getter for name | |
185 | * | |
186 | * @return string | |
187 | * @access public | |
188 | */ | |
189 | function getName() { | |
190 | return $this->_name; | |
191 | } | |
192 | ||
193 | /** | |
194 | * setter for name | |
195 | * | |
196 | * @param string | |
197 | * | |
198 | * @return void | |
199 | * @access public | |
200 | */ | |
201 | function setName($name) { | |
202 | $this->_name = $name; | |
203 | } | |
204 | ||
205 | /** | |
206 | * getter for type | |
207 | * | |
208 | * @return int | |
209 | * @access public | |
210 | */ | |
211 | function getType() { | |
212 | return $this->_type; | |
213 | } | |
214 | } | |
215 |