Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-10-12-16-00-15
[civicrm-core.git] / CRM / Core / State.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 */
37 class CRM_Core_State {
38
39 /**
40 * State name.
41 * @var string
42 */
43 protected $_name;
44
45 /**
46 * This is a combination "OR" of the STATE_* constants defined below
47 * @var int
48 */
49 protected $_type;
50
51 /**
52 * The state that precedes this state.
53 * @var CRM_Core_State
54 */
55 protected $_back;
56
57 /**
58 * The state that succeeds this state.
59 * @var CRM_Core_State
60 */
61 protected $_next;
62
63 /**
64 * The state machine that this state is part of.
65 * @var CRM_Core_StateMachine
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 */
75 const START = 1, FINISH = 2, SIMPLE = 4;
76
77 /**
78 * Constructor.
79 *
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.
90 *
91 * @return CRM_Core_State
92 */
93 public function __construct($name, $type, $back, $next, &$stateMachine) {
94 $this->_name = $name;
95 $this->_type = $type;
96 $this->_back = $back;
97 $this->_next = $next;
98
99 $this->_stateMachine = &$stateMachine;
100 }
101
102 public function debugPrint() {
103 CRM_Core_Error::debug("{$this->_name}, {$this->_type}", "{$this->_back}, {$this->_next}");
104 }
105
106 /**
107 * Given an CRM Form, jump to the previous page
108 *
109 * @return mixed
110 * does a jump to the back state
111 */
112 public 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 * @return mixed
126 * does a jump to the nextstate
127 */
128 public function handleNextState(&$page) {
129 if ($this->_type & self::FINISH) {
130 $page->handle('process');
131 }
132 else {
133 $next = &$page->controller->getPage($this->_next);
134 return $next->handle('jump');
135 }
136 }
137
138 /**
139 * Determine the name of the next state. This is useful when we want
140 * to display the navigation labels or potential path
141 *
142 * @return string
143 */
144 public function getNextState() {
145 if ($this->_type & self::FINISH) {
146 return NULL;
147 }
148 else {
149 $next = &$page->controller->getPage($this->_next);
150 return $next;
151 }
152 }
153
154 /**
155 * Mark this page as valid for the QFC framework.
156 */
157 public function validate(&$data) {
158 $data['valid'][$this->_name] = TRUE;
159 }
160
161 /**
162 * Mark this page as invalid for the QFC framework.
163 */
164 public function invalidate(&$data) {
165 $data['valid'][$this->_name] = NULL;
166 }
167
168 /**
169 * Getter for name.
170 *
171 * @return string
172 */
173 public function getName() {
174 return $this->_name;
175 }
176
177 /**
178 * Setter for name.
179 */
180 public function setName($name) {
181 $this->_name = $name;
182 }
183
184 /**
185 * Getter for type.
186 *
187 * @return int
188 */
189 public function getType() {
190 return $this->_type;
191 }
192
193 }