Merge pull request #16931 from civicrm/5.24
[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 * @var array
50 * @deprecated
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 * @deprecated
71 */
72 private static $pcPage;
73
74 /**
75 * Get all the n events
76 *
77 *
78 * @param int $id
79 * @param bool $all
80 * @param null $condition
81 *
82 * @return array
83 * array of all events if any
84 */
85 public static function event($id = NULL, $all = FALSE, $condition = NULL) {
86 $key = "{$id}_{$all}_{$condition}";
87
88 if (!isset(self::$event[$key])) {
89 self::$event[$key] = [];
90 }
91
92 if (!self::$event[$key]) {
93 CRM_Core_PseudoConstant::populate(self::$event[$key],
94 'CRM_Event_DAO_Event',
95 $all, 'title', 'is_active', $condition, NULL
96 );
97 }
98
99 if ($id) {
100 if (array_key_exists($id, self::$event[$key])) {
101 return self::$event[$key][$id];
102 }
103 else {
104 return NULL;
105 }
106 }
107 return self::$event[$key];
108 }
109
110 /**
111 * Get all the n participant statuses.
112 *
113 *
114 * @param int $id
115 * @param null $cond
116 * @param string $retColumn
117 * Tells populate() whether to return 'name' (default) or 'label' values.
118 *
119 * @return array
120 * array reference of all participant statuses if any
121 */
122 public static function &participantStatus($id = NULL, $cond = NULL, $retColumn = 'name') {
123 if (self::$participantStatus === NULL) {
124 self::$participantStatus = [];
125 }
126
127 $index = $cond ? $cond : 'No Condition';
128 $index = "{$index}_{$retColumn}";
129 if (empty(self::$participantStatus[$index])) {
130 self::$participantStatus[$index] = [];
131 CRM_Core_PseudoConstant::populate(self::$participantStatus[$index],
132 'CRM_Event_DAO_ParticipantStatusType',
133 FALSE, $retColumn, 'is_active', $cond, 'weight'
134 );
135 }
136
137 if ($id) {
138 return self::$participantStatus[$index][$id];
139 }
140
141 return self::$participantStatus[$index];
142 }
143
144 /**
145 * Get participant status class options.
146 *
147 * @return array
148 */
149 public static function participantStatusClassOptions() {
150 return [
151 'Positive' => ts('Positive'),
152 'Pending' => ts('Pending'),
153 'Waiting' => ts('Waiting'),
154 'Negative' => ts('Negative'),
155 ];
156 }
157
158 /**
159 * Return a status-type-keyed array of status classes
160 *
161 * @return array
162 * Array of status classes, keyed by status type
163 */
164 public static function &participantStatusClass() {
165 static $statusClasses = NULL;
166
167 if ($statusClasses === NULL) {
168 self::populate($statusClasses, 'CRM_Event_DAO_ParticipantStatusType', TRUE, 'class');
169 }
170
171 return $statusClasses;
172 }
173
174 /**
175 * Get all the n participant roles.
176 *
177 *
178 * @param int $id
179 * @param null $cond
180 *
181 * @return array|string
182 * array reference of all participant roles if any
183 */
184 public static function &participantRole($id = NULL, $cond = NULL) {
185 $index = $cond ? $cond : 'No Condition';
186 if (empty(self::$participantRole[$index])) {
187 self::$participantRole[$index] = [];
188
189 $condition = NULL;
190
191 if ($cond) {
192 $condition = "AND $cond";
193 }
194
195 self::$participantRole[$index] = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE,
196 FALSE, $condition
197 );
198 }
199
200 if ($id) {
201 return self::$participantRole[$index][$id];
202 }
203 return self::$participantRole[$index];
204 }
205
206 /**
207 * Get all the participant listings.
208 *
209 * @deprecated
210 * @param int $id
211 * @return array|string
212 * array reference of all participant listings if any
213 */
214 public static function &participantListing($id = NULL) {
215 CRM_Core_Error::deprecatedFunctionWarning('Function participantListing will be removed');
216 if (!self::$participantListing) {
217 self::$participantListing = [];
218 self::$participantListing = CRM_Core_OptionGroup::values('participant_listing');
219 }
220
221 if ($id) {
222 return self::$participantListing[$id];
223 }
224
225 return self::$participantListing;
226 }
227
228 /**
229 * Get all event types.
230 *
231 *
232 * @param int $id
233 * @return array|string
234 * array reference of all event types.
235 */
236 public static function &eventType($id = NULL) {
237 if (!self::$eventType) {
238 self::$eventType = [];
239 self::$eventType = CRM_Core_OptionGroup::values('event_type');
240 }
241
242 if ($id) {
243 return self::$eventType[$id];
244 }
245
246 return self::$eventType;
247 }
248
249 /**
250 * Get event template titles.
251 *
252 * @param int $id
253 *
254 * @return array
255 * Array of event id → template title pairs
256 */
257 public static function &eventTemplates($id = NULL) {
258 if (!self::$eventTemplates) {
259 CRM_Core_PseudoConstant::populate(self::$eventTemplates,
260 'CRM_Event_DAO_Event',
261 FALSE,
262 'template_title',
263 'is_active',
264 'is_template = 1'
265 );
266 }
267 if ($id) {
268 return self::$eventTemplates[$id];
269 }
270 return self::$eventTemplates;
271 }
272
273 /**
274 * Flush given pseudoconstant so it can be reread from db
275 * nex time it's requested.
276 *
277 *
278 * @param bool|string $name pseudoconstant to be flushed
279 */
280 public static function flush($name = 'cache') {
281 if (isset(self::$$name)) {
282 self::$$name = NULL;
283 }
284 }
285
286 /**
287 * Get all the Personal campaign pages.
288 *
289 * @deprecated
290 * @param int $id
291 * @return array
292 * array reference of all pcp if any
293 */
294 public static function &pcPage($id = NULL) {
295 CRM_Core_Error::deprecatedFunctionWarning('Function pcPage will be removed');
296 if (!self::$pcPage) {
297 CRM_Core_PseudoConstant::populate(self::$pcPage,
298 'CRM_PCP_DAO_PCP',
299 FALSE, 'title'
300 );
301 }
302 if ($id) {
303 return self::$pcPage[$id] ?? NULL;
304 }
305 return self::$pcPage;
306 }
307
308 }