Merge pull request #23972 from colemanw/primaryLocationWrite
[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 events
74 *
75 * @param int|null $id
76 * @param bool $all
77 * @param string|null $condition
78 * Optional SQL where condition
79 *
80 * @return array|string|null
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 event participant statuses.
110 *
111 *
112 * @param int|null $id
113 * Return the specified participant status, or null to return all
114 * @param string|null $cond
115 * Optional SQL where condition
116 * @param string $retColumn
117 * Tells populate() whether to return 'name' (default) or 'label' values.
118 *
119 * @return array|string
120 * array reference of all participant statuses if any, or single value if $id was passed
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 string|null $cond
180 * Optional SQL where condition
181 *
182 * @return array|string
183 * array reference of all participant roles if any
184 */
185 public static function &participantRole($id = NULL, $cond = NULL) {
186 $index = $cond ? $cond : 'No Condition';
187 if (empty(self::$participantRole[$index])) {
188 self::$participantRole[$index] = [];
189
190 $condition = NULL;
191
192 if ($cond) {
193 $condition = "AND $cond";
194 }
195
196 self::$participantRole[$index] = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE,
197 FALSE, $condition
198 );
199 }
200
201 if ($id) {
202 return self::$participantRole[$index][$id];
203 }
204 return self::$participantRole[$index];
205 }
206
207 /**
208 * Get all the participant listings.
209 *
210 * @deprecated
211 * @param int $id
212 * @return array|string
213 * array reference of all participant listings if any
214 */
215 public static function &participantListing($id = NULL) {
216 CRM_Core_Error::deprecatedFunctionWarning('Function participantListing will be removed');
217 if (!self::$participantListing) {
218 self::$participantListing = [];
219 self::$participantListing = CRM_Core_OptionGroup::values('participant_listing');
220 }
221
222 if ($id) {
223 return self::$participantListing[$id];
224 }
225
226 return self::$participantListing;
227 }
228
229 /**
230 * Get all event types.
231 *
232 *
233 * @param int $id
234 * @return array|string
235 * array reference of all event types.
236 */
237 public static function &eventType($id = NULL) {
238 if (!self::$eventType) {
239 self::$eventType = [];
240 self::$eventType = CRM_Core_OptionGroup::values('event_type');
241 }
242
243 if ($id) {
244 return self::$eventType[$id];
245 }
246
247 return self::$eventType;
248 }
249
250 /**
251 * Get event template titles.
252 *
253 * @param int $id
254 *
255 * @return array
256 * Array of event id → template title pairs
257 */
258 public static function &eventTemplates($id = NULL) {
259 if (!self::$eventTemplates) {
260 CRM_Core_PseudoConstant::populate(self::$eventTemplates,
261 'CRM_Event_DAO_Event',
262 FALSE,
263 'template_title',
264 'is_active',
265 'is_template = 1'
266 );
267 }
268 if ($id) {
269 return self::$eventTemplates[$id];
270 }
271 return self::$eventTemplates;
272 }
273
274 /**
275 * Flush given pseudoconstant so it can be reread from db
276 * next time it's requested.
277 *
278 *
279 * @param bool|string $name pseudoconstant to be flushed
280 */
281 public static function flush($name = 'cache') {
282 if (isset(self::$$name)) {
283 self::$$name = NULL;
284 }
285 }
286
287 /**
288 * Get all the Personal campaign pages.
289 *
290 * @deprecated
291 * @param int $id
292 * @return array
293 * array reference of all pcp if any
294 */
295 public static function &pcPage($id = NULL) {
296 CRM_Core_Error::deprecatedFunctionWarning('Function pcPage will be removed');
297 if (!self::$pcPage) {
298 CRM_Core_PseudoConstant::populate(self::$pcPage,
299 'CRM_PCP_DAO_PCP',
300 FALSE, 'title'
301 );
302 }
303 if ($id) {
304 return self::$pcPage[$id] ?? NULL;
305 }
306 return self::$pcPage;
307 }
308
309 }