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