Merge pull request #140 from dlobo/CRM-12111
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * credit card
88 *
89 * @var array
90 * @static
91 */
92 private static $creditCard;
93
94 /**
95 * contribution status
96 *
97 * @var array
98 * @static
99 */
100 private static $contributionStatus;
101
102 /**
103 * Personal campaign pages
104 * @var array
105 * @static
106 */
107 private static $pcPage;
108
109 /**
110 * status of personal campaign page
111 * @var array
112 * @static
113 */
114 private static $pcpStatus;
115
116 /**
117 * Get all the financial types
118 *
119 * @access public
120 *
121 * @return array - array reference of all financial types if any
122 * @static
123 */
124 public static function &financialType($id = NULL) {
125 if (!self::$financialType) {
126 $condition = " is_active = 1 ";
127 CRM_Core_PseudoConstant::populate(
128 self::$financialType,
129 'CRM_Financial_DAO_FinancialType',
130 TRUE,
131 'name',
132 NULL,
133 $condition
134 );
135 }
136
137 if ($id) {
138 $result = CRM_Utils_Array::value($id, self::$financialType);
139 return $result;
140 }
141 return self::$financialType;
142 }
143
144 /**
145 * Get all the financial Accounts
146 *
147 * @access public
148 * @return array - array reference of all financial accounts if any
149 * @static
150 */
151 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
152 $condition = NUll;
153 if ($financialAccountTypeId) {
154 $condition = " financial_account_type_id = ". $financialAccountTypeId;
155 }
156 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
157 if (!isset(self::$financialAccount[$cacheKey])) {
158 CRM_Core_PseudoConstant::populate(
159 self::$financialAccount[$cacheKey],
160 'CRM_Financial_DAO_FinancialAccount',
161 TRUE,
162 $retrieveColumn,
163 'is_active',
164 $condition,
165 NULL,
166 $key
167 );
168
169 }
170 if ($id) {
171 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
172 return $result;
173 }
174 return self::$financialAccount[$cacheKey];
175 }
176
177 /**
178 * Flush given pseudoconstant so it can be reread from db
179 * nex time it's requested.
180 *
181 * @access public
182 * @static
183 *
184 * @param boolean $name pseudoconstant to be flushed
185 *
186 */
187 public static function flush($name) {
188 self::$$name = NULL;
189 }
190
191 /**
192 * Get all the contribution pages
193 *
194 * @param integer $id id of the contribution page
195 * @param boolean $all do we want all pages or only active pages
196 *
197 * @access public
198 *
199 * @return array - array reference of all contribution pages if any
200 * @static
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 * Get all the payment instruments
225 *
226 * @access public
227 *
228 * @return array - array reference of all payment instruments if any
229 * @static
230 */
231 public static function &paymentInstrument($columnName = 'label') {
232 if (!isset(self::$paymentInstrument[$columnName])) {
233 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
234 FALSE, FALSE, FALSE, NULL, $columnName
235 );
236 }
237
238 return self::$paymentInstrument[$columnName];
239 }
240
241 /**
242 * Get all the valid accepted credit cards
243 *
244 * @access public
245 *
246 * @return array - array reference of all payment instruments if any
247 * @static
248 */
249 public static function &creditCard() {
250 $acceptCreditCard = array();
251 $creditCard = CRM_Core_OptionGroup::values('accept_creditcard');
252
253 if (!$creditCard) {
254 $creditCard = array();
255 }
256 foreach ($creditCard as $key => $value) {
257 $acceptCreditCard[$value] = $value;
258 }
259 return $acceptCreditCard;
260 }
261
262 /**
263 * Get all premiums
264 *
265 * @access public
266 *
267 * @return array - array of all Premiums if any
268 * @static
269 */
270 public static function products($pageID = NULL) {
271 $products = array();
272 $dao = new CRM_Contribute_DAO_Product();
273 $dao->is_active = 1;
274 $dao->orderBy('id');
275 $dao->find();
276
277 while ($dao->fetch()) {
278 $products[$dao->id] = $dao->name;
279 }
280 if ($pageID) {
281 $dao = new CRM_Contribute_DAO_Premium();
282 $dao->entity_table = 'civicrm_contribution_page';
283 $dao->entity_id = $pageID;
284 $dao->find(TRUE);
285 $premiumID = $dao->id;
286
287 $productID = array();
288
289 $dao = new CRM_Contribute_DAO_PremiumsProduct();
290 $dao->premiums_id = $premiumID;
291 $dao->find();
292 while ($dao->fetch()) {
293 $productID[$dao->product_id] = $dao->product_id;
294 }
295
296 $tempProduct = array();
297 foreach ($products as $key => $value) {
298 if (!array_key_exists($key, $productID)) {
299 $tempProduct[$key] = $value;
300 }
301 }
302
303 return $tempProduct;
304 }
305
306 return $products;
307 }
308
309 /**
310 * Get all the contribution statuses
311 *
312 * @access public
313 *
314 * @return array - array reference of all contribution statuses
315 * @static
316 */
317 public static function &contributionStatus($id = NULL, $columnName = 'label') {
318 $cacheKey = $columnName;
319 if (!isset(self::$contributionStatus[$cacheKey])) {
320 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
321 FALSE, FALSE, FALSE, NULL, $columnName
322 );
323 }
324 $result = self::$contributionStatus[$cacheKey];
325 if ($id) {
326 $result = CRM_Utils_Array::value($id, $result);
327 }
328
329 return $result;
330 }
331
332 /**
333 * Get all the Personal campaign pages
334 *
335 * @access public
336 *
337 * @return array - array reference of all pcp if any
338 * @static
339 */
340 public static function &pcPage($pageType = NULL, $id = NULL) {
341 if (!isset(self::$pcPage[$pageType])) {
342 if ($pageType) {
343 $params = "page_type='{$pageType}'";
344 }
345 else {
346 $params = '';
347 }
348 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
349 'CRM_PCP_DAO_PCP',
350 FALSE, 'title', 'is_active', $params
351 );
352 }
353 $result = self::$pcPage[$pageType];
354 if ($id) {
355 return $result = CRM_Utils_Array::value($id, $result);
356 }
357
358 return $result;
359 }
360
361 /**
362 * Get all PCP Statuses.
363 *
364 * The static array pcpStatus is returned
365 *
366 * @access public
367 * @static
368 *
369 * @return array - array reference of all PCP activity statuses
370 */
371 public static function &pcpStatus($column = 'label') {
372 if (NULL === self::$pcpStatus) {
373 self::$pcpStatus = array();
374 }
375 if (!array_key_exists($column, self::$pcpStatus)) {
376 self::$pcpStatus[$column] = array();
377
378 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
379 FALSE, FALSE, NULL, $column
380 );
381 }
382 return self::$pcpStatus[$column];
383 }
384
385 /**
386 * Get all financial accounts for a Financial type.
387 *
388 * The static array $financialTypeAccount is returned
389 *
390 * @access public
391 * @static
392 *
393 * @return array - array reference of all financial accounts for a Financial type
394 */
395 public static function financialAccountType($financialTypeId, $relationTypeId = NULL) {
396 if (!CRM_Utils_Array::value($financialTypeId, self::$financialTypeAccount)) {
397 $condition = " entity_id = $financialTypeId ";
398 CRM_Core_PseudoConstant::populate(
399 self::$financialTypeAccount[$financialTypeId],
400 'CRM_Financial_DAO_EntityFinancialAccount',
401 $all = true,
402 $retrieve = 'financial_account_id',
403 $filter = NULL,
404 $condition,
405 NULL,
406 'account_relationship'
407 );
408 }
409
410 if ($relationTypeId) {
411 return CRM_Utils_Array::value($relationTypeId, self::$financialTypeAccount[$financialTypeId]);
412 }
413
414 return self::$financialTypeAccount[$financialTypeId];
415 }
416 }
417