Merge pull request #15958 from civicrm/5.20
[civicrm-core.git] / CRM / Event / PseudoConstant.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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class holds all the Pseudo constants that are specific to Event. This avoids
22 * polluting the core class and isolates the Event
23 */
24 class CRM_Event_PseudoConstant extends CRM_Core_PseudoConstant {
25
26 /**
27 * Event
28 *
29 * @var array
30 */
31 private static $event;
32
33 /**
34 * Participant Status
35 *
36 * @var array
37 */
38 private static $participantStatus;
39
40 /**
41 * Participant Role
42 *
43 * @var array
44 */
45 private static $participantRole;
46
47 /**
48 * Participant Listing
49 *
50 * @var array
51 */
52 private static $participantListing;
53
54 /**
55 * Event Type.
56 *
57 * @var array
58 */
59 private static $eventType;
60
61 /**
62 * Event template titles
63 * @var array
64 */
65 private static $eventTemplates;
66
67 /**
68 * Personal campaign pages
69 * @var array
70 */
71 private static $pcPage;
72
73 /**
74 * Get all the n events
75 *
76 *
77 * @param int $id
78 * @param bool $all
79 * @param null $condition
80 *
81 * @return array
82 * array of all events if any
83 */
84 public static function event($id = NULL, $all = FALSE, $condition = NULL) {
85 $key = "{$id}_{$all}_{$condition}";
86
87 if (!isset(self::$event[$key])) {
88 self::$event[$key] = [];
89 }
90
91 if (!self::$event[$key]) {
92 CRM_Core_PseudoConstant::populate(self::$event[$key],
93 'CRM_Event_DAO_Event',
94 $all, 'title', 'is_active', $condition, NULL
95 );
96 }
97
98 if ($id) {
99 if (array_key_exists($id, self::$event[$key])) {
100 return self::$event[$key][$id];
101 }
102 else {
103 return NULL;
104 }
105 }
106 return self::$event[$key];
107 }
108
109 /**
110 * Get all the n participant statuses.
111 *
112 *
113 * @param int $id
114 * @param null $cond
115 * @param string $retColumn
116 * Tells populate() whether to return 'name' (default) or 'label' values.
117 *
118 * @return array
119 * array reference of all participant statuses if any
120 */
121 public static function &participantStatus($id = NULL, $cond = NULL, $retColumn = 'name') {
122 if (self::$participantStatus === NULL) {
123 self::$participantStatus = [];
124 }
125
126 $index = $cond ? $cond : 'No Condition';
127 $index = "{$index}_{$retColumn}";
128 if (!CRM_Utils_Array::value($index, self::$participantStatus)) {
129 self::$participantStatus[$index] = [];
130 CRM_Core_PseudoConstant::populate(self::$participantStatus[$index],
131 'CRM_Event_DAO_ParticipantStatusType',
132 FALSE, $retColumn, 'is_active', $cond, 'weight'
133 );
134 }
135
136 if ($id) {
137 return self::$participantStatus[$index][$id];
138 }
139
140 return self::$participantStatus[$index];
141 }
142
143 /**
144 * Get participant status class options.
145 *
146 * @return array
147 */
148 public static function participantStatusClassOptions() {
149 return [
150 'Positive' => ts('Positive'),
151 'Pending' => ts('Pending'),
152 'Waiting' => ts('Waiting'),
153 'Negative' => ts('Negative'),
154 ];
155 }
156
157 /**
158 * Return a status-type-keyed array of status classes
159 *
160 * @return array
161 * Array of status classes, keyed by status type
162 */
163 public static function &participantStatusClass() {
164 static $statusClasses = NULL;
165
166 if ($statusClasses === NULL) {
167 self::populate($statusClasses, 'CRM_Event_DAO_ParticipantStatusType', TRUE, 'class');
168 }
169
170 return $statusClasses;
171 }
172
173 /**
174 * Get all the n participant roles.
175 *
176 *
177 * @param int $id
178 * @param null $cond
179 *
180 * @return array|string
181 * array reference of all participant roles if any
182 */
183 public static function &participantRole($id = NULL, $cond = NULL) {
184 $index = $cond ? $cond : 'No Condition';
185 if (!CRM_Utils_Array::value($index, self::$participantRole)) {
186 self::$participantRole[$index] = [];
187
188 $condition = NULL;
189
190 if ($cond) {
191 $condition = "AND $cond";
192 }
193
194 self::$participantRole[$index] = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE,
195 FALSE, $condition
196 );
197 }
198
199 if ($id) {
200 return self::$participantRole[$index][$id];
201 }
202 return self::$participantRole[$index];
203 }
204
205 /**
206 * Get all the participant listings.
207 *
208 *
209 * @param int $id
210 *
211 * @return array|string
212 * array reference of all participant listings if any
213 */
214 public static function &participantListing($id = NULL) {
215 if (!self::$participantListing) {
216 self::$participantListing = [];
217 self::$participantListing = CRM_Core_OptionGroup::values('participant_listing');
218 }
219
220 if ($id) {
221 return self::$participantListing[$id];
222 }
223
224 return self::$participantListing;
225 }
226
227 /**
228 * Get all event types.
229 *
230 *
231 * @param int $id
232 * @return array|string
233 * array reference of all event types.
234 */
235 public static function &eventType($id = NULL) {
236 if (!self::$eventType) {
237 self::$eventType = [];
238 self::$eventType = CRM_Core_OptionGroup::values('event_type');
239 }
240
241 if ($id) {
242 return self::$eventType[$id];
243 }
244
245 return self::$eventType;
246 }
247
248 /**
249 * Get event template titles.
250 *
251 * @param int $id
252 *
253 * @return array
254 * Array of event id → template title pairs
255 */
256 public static function &eventTemplates($id = NULL) {
257 if (!self::$eventTemplates) {
258 CRM_Core_PseudoConstant::populate(self::$eventTemplates,
259 'CRM_Event_DAO_Event',
260 FALSE,
261 'template_title',
262 'is_active',
263 'is_template = 1'
264 );
265 }
266 if ($id) {
267 return self::$eventTemplates[$id];
268 }
269 return self::$eventTemplates;
270 }
271
272 /**
273 * Flush given pseudoconstant so it can be reread from db
274 * nex time it's requested.
275 *
276 *
277 * @param bool|string $name pseudoconstant to be flushed
278 */
279 public static function flush($name = 'cache') {
280 if (isset(self::$$name)) {
281 self::$$name = NULL;
282 }
283 }
284
285 /**
286 * Get all the Personal campaign pages.
287 *
288 *
289 * @param int $id
290 * @return array
291 * array reference of all pcp if any
292 */
293 public static function &pcPage($id = NULL) {
294 if (!self::$pcPage) {
295 CRM_Core_PseudoConstant::populate(self::$pcPage,
296 'CRM_PCP_DAO_PCP',
297 FALSE, 'title'
298 );
299 }
300 if ($id) {
301 return CRM_Utils_Array::value($id, self::$pcPage);
302 }
303 return self::$pcPage;
304 }
305
306 }