26110dc06abcc9d266b947b4c615611048c7aa77
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 $financialAccount;
52
53 /**
54 * Contribution pages
55 * @var array
56 */
57 private static $contributionPageActive = NULL;
58
59 /**
60 * Contribution pages
61 * @var array
62 */
63 private static $contributionPageAll = NULL;
64
65 /**
66 * Payment instruments
67 *
68 * @var array
69 */
70 private static $paymentInstrument;
71
72 /**
73 * Contribution status
74 *
75 * @var array
76 */
77 private static $contributionStatus;
78
79 /**
80 * Personal campaign pages
81 * @var array
82 */
83 private static $pcPage;
84
85 /**
86 * Status of personal campaign page
87 * @var array
88 */
89 private static $pcpStatus;
90
91 /**
92 * Contribution / financial batches
93 * @var array
94 */
95 private static $batch;
96
97 /**
98 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
99 *
100 * Get all the financial types
101 *
102 *
103 * @param int $id
104 * @param bool $includeDisabled
105 *
106 * @return array
107 * array reference of all financial types if any
108 */
109 public static function &financialType($id = NULL, $includeDisabled = FALSE) {
110 if (!self::$financialType) {
111 $condition = "";
112 if (!$includeDisabled) {
113 $condition = " is_active = 1 ";
114 }
115 CRM_Core_PseudoConstant::populate(
116 self::$financialType,
117 'CRM_Financial_DAO_FinancialType',
118 TRUE,
119 'name',
120 NULL,
121 $condition
122 );
123 }
124
125 if ($id) {
126 $result = CRM_Utils_Array::value($id, self::$financialType);
127 return $result;
128 }
129 return self::$financialType;
130 }
131
132 /**
133 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
134 *
135 * Get all the financial Accounts
136 *
137 *
138 * @param int $id
139 * @param int $financialAccountTypeId
140 * @param string $retrieveColumn
141 * @param string $key
142 *
143 * @return array
144 * array reference of all financial accounts if any
145 */
146 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
147 $condition = NULL;
148 if ($financialAccountTypeId) {
149 $condition = " financial_account_type_id = " . $financialAccountTypeId;
150 }
151 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
152 if (!isset(self::$financialAccount[$cacheKey])) {
153 CRM_Core_PseudoConstant::populate(
154 self::$financialAccount[$cacheKey],
155 'CRM_Financial_DAO_FinancialAccount',
156 TRUE,
157 $retrieveColumn,
158 'is_active',
159 $condition,
160 NULL,
161 $key
162 );
163
164 }
165 if ($id) {
166 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
167 return $result;
168 }
169 return self::$financialAccount[$cacheKey];
170 }
171
172 /**
173 * Flush given pseudoconstant so it can be reread from db
174 * nex time it's requested.
175 *
176 *
177 * @param bool|string $name pseudoconstant to be flushed
178 */
179 public static function flush($name = 'cache') {
180 if (isset(self::$$name)) {
181 self::$$name = NULL;
182 }
183 }
184
185 /**
186 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
187 *
188 * Get all the contribution pages
189 *
190 * @param int $id
191 * Id of the contribution page.
192 * @param bool $all
193 * Do we want all pages or only active pages.
194 *
195 *
196 * @return array
197 * array reference of all contribution pages if any
198 */
199 public static function &contributionPage($id = NULL, $all = FALSE) {
200 if ($all) {
201 $cacheVarToUse = &self::$contributionPageAll;
202 }
203 else {
204 $cacheVarToUse = &self::$contributionPageActive;
205 }
206
207 if (!$cacheVarToUse) {
208 CRM_Core_PseudoConstant::populate($cacheVarToUse,
209 'CRM_Contribute_DAO_ContributionPage',
210 $all, 'title'
211 );
212 }
213 if ($id) {
214 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
215 return $pageTitle;
216 }
217 return $cacheVarToUse;
218 }
219
220 /**
221 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
222 *
223 * Get all the payment instruments
224 *
225 *
226 * @param string $columnName
227 *
228 * @return array
229 * array reference of all payment instruments if any
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 *
245 * @return array
246 * array reference of all payment instruments if any
247 */
248 public static function &creditCard() {
249 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
250 }
251
252 /**
253 * Get all premiums.
254 *
255 *
256 * @param int $pageID
257 * @return array
258 * array of all Premiums if any
259 */
260 public static function products($pageID = NULL) {
261 $products = array();
262 $dao = new CRM_Contribute_DAO_Product();
263 $dao->is_active = 1;
264 $dao->orderBy('id');
265 $dao->find();
266
267 while ($dao->fetch()) {
268 $products[$dao->id] = $dao->name;
269 }
270 if ($pageID) {
271 $dao = new CRM_Contribute_DAO_Premium();
272 $dao->entity_table = 'civicrm_contribution_page';
273 $dao->entity_id = $pageID;
274 $dao->find(TRUE);
275 $premiumID = $dao->id;
276
277 $productID = array();
278
279 $dao = new CRM_Contribute_DAO_PremiumsProduct();
280 $dao->premiums_id = $premiumID;
281 $dao->find();
282 while ($dao->fetch()) {
283 $productID[$dao->product_id] = $dao->product_id;
284 }
285
286 $tempProduct = array();
287 foreach ($products as $key => $value) {
288 if (!array_key_exists($key, $productID)) {
289 $tempProduct[$key] = $value;
290 }
291 }
292
293 return $tempProduct;
294 }
295
296 return $products;
297 }
298
299 /**
300 * Get all the contribution statuses.
301 *
302 *
303 * @param int $id
304 * @param string $columnName
305 * @return array
306 * array reference of all contribution statuses
307 */
308 public static function &contributionStatus($id = NULL, $columnName = 'label') {
309 $cacheKey = $columnName;
310 if (!isset(self::$contributionStatus[$cacheKey])) {
311 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
312 FALSE, FALSE, FALSE, NULL, $columnName
313 );
314 }
315 $result = self::$contributionStatus[$cacheKey];
316 if ($id) {
317 $result = CRM_Utils_Array::value($id, $result);
318 }
319
320 return $result;
321 }
322
323 /**
324 * Get all the Personal campaign pages.
325 *
326 *
327 * @param null $pageType
328 * @param int $id
329 *
330 * @return array
331 * array reference of all pcp if any
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 *
360 * @param string $column
361 * @return array
362 * 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 financial account for a Financial type.
380 *
381 *
382 * @param int $entityId
383 * @param string $accountRelationType
384 * @param string $entityTable
385 * @param string $returnField
386 * @return int
387 */
388 public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') {
389 $params = array(
390 'return' => array($returnField),
391 'entity_table' => $entityTable,
392 'entity_id' => $entityId,
393 );
394 if ($accountRelationType) {
395 $params['account_relationship.name'] = $accountRelationType;
396 }
397 $result = civicrm_api3('EntityFinancialAccount', 'get', $params);
398 if (!$result['count']) {
399 return NULL;
400 }
401 return $result['values'][$result['id']][$returnField];
402 }
403
404 /**
405 * Get all batches.
406 *
407 *
408 * @param int $id
409 * @return array
410 * array reference of all batches if any
411 */
412 public static function &batch($id = NULL) {
413 if (!self::$batch) {
414 $orderBy = " id DESC ";
415 CRM_Core_PseudoConstant::populate(
416 self::$batch,
417 'CRM_Batch_DAO_Batch',
418 TRUE,
419 'title',
420 NULL,
421 NULL,
422 $orderBy
423 );
424 }
425
426 if ($id) {
427 $result = CRM_Utils_Array::value($id, self::$batch);
428 return $result;
429 }
430 return self::$batch;
431 }
432
433 }