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