ManagedEntities - Refactor class variable $declarations to local variable
[civicrm-core.git] / CRM / Core / State.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
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
19 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 */
21 class CRM_Core_State {
22
23 /**
24 * State name.
25 * @var string
26 */
27 protected $_name;
28
29 /**
30 * This is a combination "OR" of the STATE_* constants defined below
31 * @var int
32 */
33 protected $_type;
34
35 /**
36 * The state that precedes this state.
37 * @var CRM_Core_State
38 */
39 protected $_back;
40
41 /**
42 * The state that succeeds this state.
43 * @var CRM_Core_State
44 */
45 protected $_next;
46
47 /**
48 * The state machine that this state is part of.
49 * @var CRM_Core_StateMachine
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 */
59 const START = 1, FINISH = 2, SIMPLE = 4;
60
61 /**
62 * Constructor.
63 *
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.
74 *
75 * @return CRM_Core_State
76 */
77 public function __construct($name, $type, $back, $next, &$stateMachine) {
78 $this->_name = $name;
79 $this->_type = $type;
80 $this->_back = $back;
81 $this->_next = $next;
82
83 $this->_stateMachine = &$stateMachine;
84 }
85
86 /**
87 * Given an CRM Form, jump to the previous page.
88 *
89 * @param CRM_Core_Form $page
90 *
91 * @return mixed
92 * does a jump to the back state
93 */
94 public function handleBackState(&$page) {
95 if ($this->_type & self::START) {
96 $page->handle('display');
97 }
98 else {
99 $back = &$page->controller->getPage($this->_back);
100 return $back->handle('jump');
101 }
102 }
103
104 /**
105 * Given an CRM Form, jump to the next page.
106 *
107 * @param CRM_Core_Form $page
108 *
109 * @return mixed
110 * Does a jump to the nextstate
111 */
112 public function handleNextState(&$page) {
113 if ($this->_type & self::FINISH) {
114 $page->handle('process');
115 }
116 else {
117 $next = &$page->controller->getPage($this->_next);
118 return $next->handle('jump');
119 }
120 }
121
122 /**
123 * Mark this page as valid for the QFC framework.
124 *
125 * @param array $data
126 */
127 public function validate(&$data) {
128 $data['valid'][$this->_name] = TRUE;
129 }
130
131 /**
132 * Mark this page as invalid for the QFC framework.
133 *
134 * @param array $data
135 */
136 public function invalidate(&$data) {
137 $data['valid'][$this->_name] = NULL;
138 }
139
140 /**
141 * Getter for name.
142 *
143 * @return string
144 */
145 public function getName() {
146 return $this->_name;
147 }
148
149 /**
150 * Setter for name.
151 *
152 * @param string $name
153 */
154 public function setName($name) {
155 $this->_name = $name;
156 }
157
158 /**
159 * Getter for type.
160 *
161 * @return int
162 */
163 public function getType() {
164 return $this->_type;
165 }
166
167 }