Merge pull request #3211 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @access public
121 *
122 * @return array - array reference of all financial types if any
123 * @static
124 */
125 public static function &financialType($id = NULL) {
126 if (!self::$financialType) {
127 $condition = " is_active = 1 ";
128 CRM_Core_PseudoConstant::populate(
129 self::$financialType,
130 'CRM_Financial_DAO_FinancialType',
131 TRUE,
132 'name',
133 NULL,
134 $condition
135 );
136 }
137
138 if ($id) {
139 $result = CRM_Utils_Array::value($id, self::$financialType);
140 return $result;
141 }
142 return self::$financialType;
143 }
144
145 /**
146 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
147 *
148 * Get all the financial Accounts
149 *
150 * @access public
151 *
152 * @param null $id
153 * @param null $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 * @access public
191 * @static
192 *
193 * @param boolean $name pseudoconstant to be flushed
194 *
195 */
196 public static function flush($name = 'cache') {
197 if (isset(self::$$name)) {
198 self::$$name = NULL;
199 }
200 }
201
202 /**
203 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
204 *
205 * Get all the contribution pages
206 *
207 * @param integer $id id of the contribution page
208 * @param boolean $all do we want all pages or only active pages
209 *
210 * @access public
211 *
212 * @return array - array reference of all contribution pages if any
213 * @static
214 */
215 public static function &contributionPage($id = NULL, $all = FALSE) {
216 if ($all) {
217 $cacheVarToUse = &self::$contributionPageAll;
218 }
219 else {
220 $cacheVarToUse = &self::$contributionPageActive;
221 }
222
223 if (!$cacheVarToUse) {
224 CRM_Core_PseudoConstant::populate($cacheVarToUse,
225 'CRM_Contribute_DAO_ContributionPage',
226 $all, 'title'
227 );
228 }
229 if ($id) {
230 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
231 return $pageTitle;
232 }
233 return $cacheVarToUse;
234 }
235
236 /**
237 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
238 *
239 * Get all the payment instruments
240 *
241 * @access public
242 *
243 * @return array - array reference of all payment instruments if any
244 * @static
245 */
246 public static function &paymentInstrument($columnName = 'label') {
247 if (!isset(self::$paymentInstrument[$columnName])) {
248 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
249 FALSE, FALSE, FALSE, NULL, $columnName
250 );
251 }
252
253 return self::$paymentInstrument[$columnName];
254 }
255
256 /**
257 * Get all the valid accepted credit cards
258 *
259 * @access public
260 *
261 * @return array - array reference of all payment instruments if any
262 * @static
263 */
264 public static function &creditCard() {
265 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
266 }
267
268 /**
269 * Get all premiums
270 *
271 * @access public
272 *
273 * @return array - array of all Premiums if any
274 * @static
275 */
276 public static function products($pageID = NULL) {
277 $products = array();
278 $dao = new CRM_Contribute_DAO_Product();
279 $dao->is_active = 1;
280 $dao->orderBy('id');
281 $dao->find();
282
283 while ($dao->fetch()) {
284 $products[$dao->id] = $dao->name;
285 }
286 if ($pageID) {
287 $dao = new CRM_Contribute_DAO_Premium();
288 $dao->entity_table = 'civicrm_contribution_page';
289 $dao->entity_id = $pageID;
290 $dao->find(TRUE);
291 $premiumID = $dao->id;
292
293 $productID = array();
294
295 $dao = new CRM_Contribute_DAO_PremiumsProduct();
296 $dao->premiums_id = $premiumID;
297 $dao->find();
298 while ($dao->fetch()) {
299 $productID[$dao->product_id] = $dao->product_id;
300 }
301
302 $tempProduct = array();
303 foreach ($products as $key => $value) {
304 if (!array_key_exists($key, $productID)) {
305 $tempProduct[$key] = $value;
306 }
307 }
308
309 return $tempProduct;
310 }
311
312 return $products;
313 }
314
315 /**
316 * Get all the contribution statuses
317 *
318 * @access public
319 *
320 * @return array - array reference of all contribution statuses
321 * @static
322 */
323 public static function &contributionStatus($id = NULL, $columnName = 'label') {
324 $cacheKey = $columnName;
325 if (!isset(self::$contributionStatus[$cacheKey])) {
326 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
327 FALSE, FALSE, FALSE, NULL, $columnName
328 );
329 }
330 $result = self::$contributionStatus[$cacheKey];
331 if ($id) {
332 $result = CRM_Utils_Array::value($id, $result);
333 }
334
335 return $result;
336 }
337
338 /**
339 * Get all the Personal campaign pages
340 *
341 * @access public
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 * @access public
373 * @static
374 *
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 * @access public
397 * @static
398 *
399 * @return array - array reference of all financial accounts for a Financial type
400 */
401 public static function financialAccountType($financialTypeId, $relationTypeId = NULL) {
402 if (!CRM_Utils_Array::value($financialTypeId, self::$financialTypeAccount)) {
403 $condition = " entity_id = $financialTypeId ";
404 CRM_Core_PseudoConstant::populate(
405 self::$financialTypeAccount[$financialTypeId],
406 'CRM_Financial_DAO_EntityFinancialAccount',
407 $all = true,
408 $retrieve = 'financial_account_id',
409 $filter = NULL,
410 $condition,
411 NULL,
412 'account_relationship'
413 );
414 }
415
416 if ($relationTypeId) {
417 return CRM_Utils_Array::value($relationTypeId, self::$financialTypeAccount[$financialTypeId]);
418 }
419
420 return self::$financialTypeAccount[$financialTypeId];
421 }
422
423 /**
424 * Get all batches
425 *
426 * @access public
427 *
428 * @return array - array reference of all batches if any
429 * @static
430 */
431 public static function &batch($id = NULL) {
432 if (!self::$batch) {
433 $orderBy = " id DESC ";
434 CRM_Core_PseudoConstant::populate(
435 self::$batch,
436 'CRM_Batch_DAO_Batch',
437 TRUE,
438 'title',
439 NULL,
440 NULL,
441 $orderBy
442 );
443 }
444
445 if ($id) {
446 $result = CRM_Utils_Array::value($id, self::$batch);
447 return $result;
448 }
449 return self::$batch;
450 }
451 }
452