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