Merge pull request #911 from agh1/membership-dash-counts-new
[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 * 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 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
110 *
111 * Get all the financial types
112 *
113 * @access public
114 *
115 * @return array - array reference of all financial types if any
116 * @static
117 */
118 public static function &financialType($id = NULL) {
119 if (!self::$financialType) {
120 $condition = " is_active = 1 ";
121 CRM_Core_PseudoConstant::populate(
122 self::$financialType,
123 'CRM_Financial_DAO_FinancialType',
124 TRUE,
125 'name',
126 NULL,
127 $condition
128 );
129 }
130
131 if ($id) {
132 $result = CRM_Utils_Array::value($id, self::$financialType);
133 return $result;
134 }
135 return self::$financialType;
136 }
137
138 /**
139 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
140 *
141 * Get all the financial Accounts
142 *
143 * @access public
144 * @return array - array reference of all financial accounts if any
145 * @static
146 */
147 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
148 $condition = NUll;
149 if ($financialAccountTypeId) {
150 $condition = " financial_account_type_id = ". $financialAccountTypeId;
151 }
152 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
153 if (!isset(self::$financialAccount[$cacheKey])) {
154 CRM_Core_PseudoConstant::populate(
155 self::$financialAccount[$cacheKey],
156 'CRM_Financial_DAO_FinancialAccount',
157 TRUE,
158 $retrieveColumn,
159 'is_active',
160 $condition,
161 NULL,
162 $key
163 );
164
165 }
166 if ($id) {
167 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
168 return $result;
169 }
170 return self::$financialAccount[$cacheKey];
171 }
172
173 /**
174 * Flush given pseudoconstant so it can be reread from db
175 * nex time it's requested.
176 *
177 * @access public
178 * @static
179 *
180 * @param boolean $name pseudoconstant to be flushed
181 *
182 */
183 public static function flush($name = 'cache') {
184 if (isset(self::$$name)) {
185 self::$$name = NULL;
186 }
187 }
188
189 /**
190 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
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 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
225 *
226 * Get all the payment instruments
227 *
228 * @access public
229 *
230 * @return array - array reference of all payment instruments if any
231 * @static
232 */
233 public static function &paymentInstrument($columnName = 'label') {
234 if (!isset(self::$paymentInstrument[$columnName])) {
235 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
236 FALSE, FALSE, FALSE, NULL, $columnName
237 );
238 }
239
240 return self::$paymentInstrument[$columnName];
241 }
242
243 /**
244 * Get all the valid accepted credit cards
245 *
246 * @access public
247 *
248 * @return array - array reference of all payment instruments if any
249 * @static
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 * @access public
259 *
260 * @return array - array of all Premiums if any
261 * @static
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 * @access public
306 *
307 * @return array - array reference of all contribution statuses
308 * @static
309 */
310 public static function &contributionStatus($id = NULL, $columnName = 'label') {
311 $cacheKey = $columnName;
312 if (!isset(self::$contributionStatus[$cacheKey])) {
313 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
314 FALSE, FALSE, FALSE, NULL, $columnName
315 );
316 }
317 $result = self::$contributionStatus[$cacheKey];
318 if ($id) {
319 $result = CRM_Utils_Array::value($id, $result);
320 }
321
322 return $result;
323 }
324
325 /**
326 * Get all the Personal campaign pages
327 *
328 * @access public
329 *
330 * @return array - array reference of all pcp if any
331 * @static
332 */
333 public static function &pcPage($pageType = NULL, $id = NULL) {
334 if (!isset(self::$pcPage[$pageType])) {
335 if ($pageType) {
336 $params = "page_type='{$pageType}'";
337 }
338 else {
339 $params = '';
340 }
341 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
342 'CRM_PCP_DAO_PCP',
343 FALSE, 'title', 'is_active', $params
344 );
345 }
346 $result = self::$pcPage[$pageType];
347 if ($id) {
348 return $result = CRM_Utils_Array::value($id, $result);
349 }
350
351 return $result;
352 }
353
354 /**
355 * Get all PCP Statuses.
356 *
357 * The static array pcpStatus is returned
358 *
359 * @access public
360 * @static
361 *
362 * @return array - array reference of all PCP activity statuses
363 */
364 public static function &pcpStatus($column = 'label') {
365 if (NULL === self::$pcpStatus) {
366 self::$pcpStatus = array();
367 }
368 if (!array_key_exists($column, self::$pcpStatus)) {
369 self::$pcpStatus[$column] = array();
370
371 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
372 FALSE, FALSE, NULL, $column
373 );
374 }
375 return self::$pcpStatus[$column];
376 }
377
378 /**
379 * Get all financial accounts for a Financial type.
380 *
381 * The static array $financialTypeAccount is returned
382 *
383 * @access public
384 * @static
385 *
386 * @return array - array reference of all financial accounts for a Financial type
387 */
388 public static function financialAccountType($financialTypeId, $relationTypeId = NULL) {
389 if (!CRM_Utils_Array::value($financialTypeId, self::$financialTypeAccount)) {
390 $condition = " entity_id = $financialTypeId ";
391 CRM_Core_PseudoConstant::populate(
392 self::$financialTypeAccount[$financialTypeId],
393 'CRM_Financial_DAO_EntityFinancialAccount',
394 $all = true,
395 $retrieve = 'financial_account_id',
396 $filter = NULL,
397 $condition,
398 NULL,
399 'account_relationship'
400 );
401 }
402
403 if ($relationTypeId) {
404 return CRM_Utils_Array::value($relationTypeId, self::$financialTypeAccount[$financialTypeId]);
405 }
406
407 return self::$financialTypeAccount[$financialTypeId];
408 }
409 }
410