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