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