Merge pull request #5120 from eileenmcnaughton/CRM-15938
[civicrm-core.git] / CRM / Contribute / 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 Contributions. This avoids
38 * polluting the core class and isolates the mass mailer class
39 */
40 class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant {
41
42 /**
43 * Financial types
44 * @var array
45 */
46 private static $financialType;
47
48 /**
49 * Financial types
50 * @var array
51 */
52 private static $financialTypeAccount;
53
54
55 /**
56 * Financial types
57 * @var array
58 */
59 private static $financialAccount;
60
61 /**
62 * Contribution pages
63 * @var array
64 */
65 private static $contributionPageActive = NULL;
66
67 /**
68 * Contribution pages
69 * @var array
70 */
71 private static $contributionPageAll = NULL;
72
73 /**
74 * Payment instruments
75 *
76 * @var array
77 */
78 private static $paymentInstrument;
79
80 /**
81 * Contribution status
82 *
83 * @var array
84 */
85 private static $contributionStatus;
86
87 /**
88 * Personal campaign pages
89 * @var array
90 */
91 private static $pcPage;
92
93 /**
94 * Status of personal campaign page
95 * @var array
96 */
97 private static $pcpStatus;
98
99 /**
100 * Contribution / financial batches
101 * @var array
102 */
103 private static $batch;
104
105 /**
106 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
107 *
108 * Get all the financial types
109 *
110 *
111 * @param int $id
112 *
113 * @return array
114 * array reference of all financial types if any
115 */
116 public static function &financialType($id = NULL) {
117 if (!self::$financialType) {
118 $condition = " is_active = 1 ";
119 CRM_Core_PseudoConstant::populate(
120 self::$financialType,
121 'CRM_Financial_DAO_FinancialType',
122 TRUE,
123 'name',
124 NULL,
125 $condition
126 );
127 }
128
129 if ($id) {
130 $result = CRM_Utils_Array::value($id, self::$financialType);
131 return $result;
132 }
133 return self::$financialType;
134 }
135
136 /**
137 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
138 *
139 * Get all the financial Accounts
140 *
141 *
142 * @param int $id
143 * @param int $financialAccountTypeId
144 * @param string $retrieveColumn
145 * @param string $key
146 *
147 * @return array
148 * array reference of all financial accounts if any
149 */
150 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
151 $condition = NULL;
152 if ($financialAccountTypeId) {
153 $condition = " financial_account_type_id = " . $financialAccountTypeId;
154 }
155 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
156 if (!isset(self::$financialAccount[$cacheKey])) {
157 CRM_Core_PseudoConstant::populate(
158 self::$financialAccount[$cacheKey],
159 'CRM_Financial_DAO_FinancialAccount',
160 TRUE,
161 $retrieveColumn,
162 'is_active',
163 $condition,
164 NULL,
165 $key
166 );
167
168 }
169 if ($id) {
170 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
171 return $result;
172 }
173 return self::$financialAccount[$cacheKey];
174 }
175
176 /**
177 * Flush given pseudoconstant so it can be reread from db
178 * nex time it's requested.
179 *
180 *
181 * @param bool|string $name pseudoconstant to be flushed
182 */
183 public static function flush($name = 'cache') {
184 if (isset(self::$$name)) {
185 self::$$name = NULL;
186 }
187 }
188
189 /**
190 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
191 *
192 * Get all the contribution pages
193 *
194 * @param int $id
195 * Id of the contribution page.
196 * @param bool $all
197 * Do we want all pages or only active pages.
198 *
199 *
200 * @return array
201 * array reference of all contribution pages if any
202 */
203 public static function &contributionPage($id = NULL, $all = FALSE) {
204 if ($all) {
205 $cacheVarToUse = &self::$contributionPageAll;
206 }
207 else {
208 $cacheVarToUse = &self::$contributionPageActive;
209 }
210
211 if (!$cacheVarToUse) {
212 CRM_Core_PseudoConstant::populate($cacheVarToUse,
213 'CRM_Contribute_DAO_ContributionPage',
214 $all, 'title'
215 );
216 }
217 if ($id) {
218 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
219 return $pageTitle;
220 }
221 return $cacheVarToUse;
222 }
223
224 /**
225 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
226 *
227 * Get all the payment instruments
228 *
229 *
230 * @param string $columnName
231 *
232 * @return array
233 * array reference of all payment instruments if any
234 */
235 public static function &paymentInstrument($columnName = 'label') {
236 if (!isset(self::$paymentInstrument[$columnName])) {
237 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
238 FALSE, FALSE, FALSE, NULL, $columnName
239 );
240 }
241
242 return self::$paymentInstrument[$columnName];
243 }
244
245 /**
246 * Get all the valid accepted credit cards.
247 *
248 *
249 * @return array
250 * array reference of all payment instruments if any
251 */
252 public static function &creditCard() {
253 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
254 }
255
256 /**
257 * Get all premiums.
258 *
259 *
260 * @param int $pageID
261 * @return array
262 * array of all Premiums if any
263 */
264 public static function products($pageID = NULL) {
265 $products = array();
266 $dao = new CRM_Contribute_DAO_Product();
267 $dao->is_active = 1;
268 $dao->orderBy('id');
269 $dao->find();
270
271 while ($dao->fetch()) {
272 $products[$dao->id] = $dao->name;
273 }
274 if ($pageID) {
275 $dao = new CRM_Contribute_DAO_Premium();
276 $dao->entity_table = 'civicrm_contribution_page';
277 $dao->entity_id = $pageID;
278 $dao->find(TRUE);
279 $premiumID = $dao->id;
280
281 $productID = array();
282
283 $dao = new CRM_Contribute_DAO_PremiumsProduct();
284 $dao->premiums_id = $premiumID;
285 $dao->find();
286 while ($dao->fetch()) {
287 $productID[$dao->product_id] = $dao->product_id;
288 }
289
290 $tempProduct = array();
291 foreach ($products as $key => $value) {
292 if (!array_key_exists($key, $productID)) {
293 $tempProduct[$key] = $value;
294 }
295 }
296
297 return $tempProduct;
298 }
299
300 return $products;
301 }
302
303 /**
304 * Get all the contribution statuses.
305 *
306 *
307 * @param int $id
308 * @param string $columnName
309 * @return array
310 * array reference of all contribution statuses
311 */
312 public static function &contributionStatus($id = NULL, $columnName = 'label') {
313 $cacheKey = $columnName;
314 if (!isset(self::$contributionStatus[$cacheKey])) {
315 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
316 FALSE, FALSE, FALSE, NULL, $columnName
317 );
318 }
319 $result = self::$contributionStatus[$cacheKey];
320 if ($id) {
321 $result = CRM_Utils_Array::value($id, $result);
322 }
323
324 return $result;
325 }
326
327 /**
328 * Get all the Personal campaign pages.
329 *
330 *
331 * @param null $pageType
332 * @param int $id
333 *
334 * @return array
335 * array reference of all pcp if any
336 */
337 public static function &pcPage($pageType = NULL, $id = NULL) {
338 if (!isset(self::$pcPage[$pageType])) {
339 if ($pageType) {
340 $params = "page_type='{$pageType}'";
341 }
342 else {
343 $params = '';
344 }
345 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
346 'CRM_PCP_DAO_PCP',
347 FALSE, 'title', 'is_active', $params
348 );
349 }
350 $result = self::$pcPage[$pageType];
351 if ($id) {
352 return $result = CRM_Utils_Array::value($id, $result);
353 }
354
355 return $result;
356 }
357
358 /**
359 * Get all PCP Statuses.
360 *
361 * The static array pcpStatus is returned
362 *
363 *
364 * @param string $column
365 * @return array
366 * array reference of all PCP activity statuses
367 */
368 public static function &pcpStatus($column = 'label') {
369 if (NULL === self::$pcpStatus) {
370 self::$pcpStatus = array();
371 }
372 if (!array_key_exists($column, self::$pcpStatus)) {
373 self::$pcpStatus[$column] = array();
374
375 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
376 FALSE, FALSE, NULL, $column
377 );
378 }
379 return self::$pcpStatus[$column];
380 }
381
382 /**
383 * Get all financial accounts for a Financial type.
384 *
385 * The static array $financialTypeAccount is returned
386 *
387 *
388 * @param int $financialTypeId
389 * @param int $relationTypeId
390 * @return array
391 * array reference of all financial accounts for a Financial type
392 */
393 public static function financialAccountType($financialTypeId, $relationTypeId = NULL) {
394 if (!CRM_Utils_Array::value($financialTypeId, self::$financialTypeAccount)) {
395 $condition = " entity_id = $financialTypeId ";
396 CRM_Core_PseudoConstant::populate(
397 self::$financialTypeAccount[$financialTypeId],
398 'CRM_Financial_DAO_EntityFinancialAccount',
399 $all = TRUE,
400 $retrieve = 'financial_account_id',
401 $filter = NULL,
402 $condition,
403 NULL,
404 'account_relationship'
405 );
406 }
407
408 if ($relationTypeId) {
409 return CRM_Utils_Array::value($relationTypeId, self::$financialTypeAccount[$financialTypeId]);
410 }
411
412 return self::$financialTypeAccount[$financialTypeId];
413 }
414
415 /**
416 * Get all batches.
417 *
418 *
419 * @param int $id
420 * @return array
421 * array reference of all batches if any
422 */
423 public static function &batch($id = NULL) {
424 if (!self::$batch) {
425 $orderBy = " id DESC ";
426 CRM_Core_PseudoConstant::populate(
427 self::$batch,
428 'CRM_Batch_DAO_Batch',
429 TRUE,
430 'title',
431 NULL,
432 NULL,
433 $orderBy
434 );
435 }
436
437 if ($id) {
438 $result = CRM_Utils_Array::value($id, self::$batch);
439 return $result;
440 }
441 return self::$batch;
442 }
443
444 }