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