Fix for CRM-16157
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 |
006389de
TO
25 +--------------------------------------------------------------------+
26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 */
40class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant {
41
42 /**
100fef9d 43 * Financial types
6a488035 44 * @var array
6a488035
TO
45 */
46 private static $financialType;
47
48 /**
100fef9d 49 * Financial types
6a488035 50 * @var array
6a488035
TO
51 */
52 private static $financialTypeAccount;
53
54
55 /**
100fef9d 56 * Financial types
6a488035 57 * @var array
6a488035
TO
58 */
59 private static $financialAccount;
60
874c9be7 61 /**
353ffa53
TO
62 * Contribution pages
63 * @var array
353ffa53 64 */
6a488035
TO
65 private static $contributionPageActive = NULL;
66
67 /**
100fef9d 68 * Contribution pages
6a488035 69 * @var array
6a488035
TO
70 */
71 private static $contributionPageAll = NULL;
72
73 /**
100fef9d 74 * Payment instruments
6a488035
TO
75 *
76 * @var array
6a488035
TO
77 */
78 private static $paymentInstrument;
79
6a488035 80 /**
100fef9d 81 * Contribution status
6a488035
TO
82 *
83 * @var array
6a488035
TO
84 */
85 private static $contributionStatus;
86
87 /**
88 * Personal campaign pages
89 * @var array
6a488035
TO
90 */
91 private static $pcPage;
92
93 /**
100fef9d 94 * Status of personal campaign page
6a488035 95 * @var array
6a488035
TO
96 */
97 private static $pcpStatus;
98
91aff94c 99 /**
100fef9d 100 * Contribution / financial batches
91aff94c 101 * @var array
91aff94c 102 */
103 private static $batch;
104
6a488035 105 /**
3f3a3ba0
CW
106 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
107 *
6a488035
TO
108 * Get all the financial types
109 *
6a488035 110 *
100fef9d 111 * @param int $id
da6b46f4 112 *
a6c01b45
CW
113 * @return array
114 * array reference of all financial types if any
6a488035
TO
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 /**
3f3a3ba0
CW
137 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
138 *
6a488035
TO
139 * Get all the financial Accounts
140 *
dd244018 141 *
100fef9d
CW
142 * @param int $id
143 * @param int $financialAccountTypeId
dd244018
EM
144 * @param string $retrieveColumn
145 * @param string $key
146 *
a6c01b45
CW
147 * @return array
148 * array reference of all financial accounts if any
6a488035 149 */
086ca649 150 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
874c9be7 151 $condition = NULL;
6a488035 152 if ($financialAccountTypeId) {
86bfa4f6 153 $condition = " financial_account_type_id = " . $financialAccountTypeId;
6a488035 154 }
086ca649 155 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
6a488035
TO
156 if (!isset(self::$financialAccount[$cacheKey])) {
157 CRM_Core_PseudoConstant::populate(
158 self::$financialAccount[$cacheKey],
159 'CRM_Financial_DAO_FinancialAccount',
160 TRUE,
086ca649 161 $retrieveColumn,
6a488035 162 'is_active',
086ca649
PN
163 $condition,
164 NULL,
165 $key
6a488035
TO
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 *
6a488035 180 *
da6b46f4 181 * @param bool|string $name pseudoconstant to be flushed
6a488035 182 */
3fb36592 183 public static function flush($name = 'cache') {
874c9be7 184 if (isset(self::$$name)) {
fa56270d 185 self::$$name = NULL;
353ffa53 186 }
6a488035
TO
187 }
188
189 /**
3f3a3ba0
CW
190 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
191 *
6a488035
TO
192 * Get all the contribution pages
193 *
014c4014
TO
194 * @param int $id
195 * Id of the contribution page.
196 * @param bool $all
197 * Do we want all pages or only active pages.
6a488035 198 *
6a488035 199 *
a6c01b45
CW
200 * @return array
201 * array reference of all contribution pages if any
6a488035
TO
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 /**
3f3a3ba0
CW
225 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
226 *
6a488035
TO
227 * Get all the payment instruments
228 *
6a488035 229 *
fd31fa4c
EM
230 * @param string $columnName
231 *
a6c01b45
CW
232 * @return array
233 * array reference of all payment instruments if any
6a488035
TO
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 /**
fe482240 246 * Get all the valid accepted credit cards.
6a488035 247 *
6a488035 248 *
a6c01b45
CW
249 * @return array
250 * array reference of all payment instruments if any
6a488035
TO
251 */
252 public static function &creditCard() {
3f3a3ba0 253 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
6a488035
TO
254 }
255
256 /**
fe482240 257 * Get all premiums.
6a488035 258 *
6a488035 259 *
100fef9d 260 * @param int $pageID
a6c01b45
CW
261 * @return array
262 * array of all Premiums if any
6a488035
TO
263 */
264 public static function products($pageID = NULL) {
353ffa53
TO
265 $products = array();
266 $dao = new CRM_Contribute_DAO_Product();
6a488035
TO
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) {
353ffa53 275 $dao = new CRM_Contribute_DAO_Premium();
6a488035 276 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 277 $dao->entity_id = $pageID;
6a488035
TO
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 /**
fe482240 304 * Get all the contribution statuses.
6a488035 305 *
6a488035 306 *
100fef9d 307 * @param int $id
da6b46f4 308 * @param string $columnName
a6c01b45
CW
309 * @return array
310 * array reference of all contribution statuses
6a488035
TO
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 /**
fe482240 328 * Get all the Personal campaign pages.
6a488035 329 *
6a488035 330 *
2a6da8d7 331 * @param null $pageType
100fef9d 332 * @param int $id
2a6da8d7 333 *
a6c01b45
CW
334 * @return array
335 * array reference of all pcp if any
6a488035
TO
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 *
6a488035 363 *
2a6da8d7 364 * @param string $column
a6c01b45
CW
365 * @return array
366 * array reference of all PCP activity statuses
6a488035
TO
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 *
6a488035 387 *
100fef9d
CW
388 * @param int $financialTypeId
389 * @param int $relationTypeId
a6c01b45
CW
390 * @return array
391 * array reference of all financial accounts for a Financial type
6a488035
TO
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',
874c9be7 399 $all = TRUE,
6a488035
TO
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 }
91aff94c 414
415 /**
fe482240 416 * Get all batches.
91aff94c 417 *
91aff94c 418 *
100fef9d 419 * @param int $id
a6c01b45
CW
420 * @return array
421 * array reference of all batches if any
91aff94c 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 }
96025800 443
6a488035 444}