[NFC] Simplify method of getting logged in contact ID
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 |
006389de
TO
25 +--------------------------------------------------------------------+
26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
347e061b 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.
6a488035
TO
38 */
39class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant {
40
41 /**
100fef9d 42 * Financial types
6a488035 43 * @var array
6a488035
TO
44 */
45 private static $financialType;
46
6a488035 47 /**
100fef9d 48 * Financial types
6a488035 49 * @var array
6a488035
TO
50 */
51 private static $financialAccount;
52
874c9be7 53 /**
353ffa53
TO
54 * Contribution pages
55 * @var array
353ffa53 56 */
6a488035
TO
57 private static $contributionPageActive = NULL;
58
59 /**
100fef9d 60 * Contribution pages
6a488035 61 * @var array
6a488035
TO
62 */
63 private static $contributionPageAll = NULL;
64
65 /**
100fef9d 66 * Payment instruments
6a488035
TO
67 *
68 * @var array
6a488035
TO
69 */
70 private static $paymentInstrument;
71
6a488035 72 /**
100fef9d 73 * Contribution status
6a488035
TO
74 *
75 * @var array
6a488035
TO
76 */
77 private static $contributionStatus;
78
79 /**
80 * Personal campaign pages
81 * @var array
6a488035
TO
82 */
83 private static $pcPage;
84
85 /**
100fef9d 86 * Status of personal campaign page
6a488035 87 * @var array
6a488035
TO
88 */
89 private static $pcpStatus;
90
91aff94c 91 /**
100fef9d 92 * Contribution / financial batches
91aff94c 93 * @var array
91aff94c 94 */
95 private static $batch;
96
6a488035 97 /**
69af118f
MW
98 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
99 *
3f3a3ba0 100 *
6a488035
TO
101 * Get all the financial types
102 *
6a488035 103 *
100fef9d 104 * @param int $id
d73f286e 105 * @param bool $includeDisabled
da6b46f4 106 *
a6c01b45
CW
107 * @return array
108 * array reference of all financial types if any
6a488035 109 */
d73f286e 110 public static function &financialType($id = NULL, $includeDisabled = FALSE) {
6a488035 111 if (!self::$financialType) {
d73f286e
SL
112 $condition = "";
113 if (!$includeDisabled) {
114 $condition = " is_active = 1 ";
115 }
6a488035
TO
116 CRM_Core_PseudoConstant::populate(
117 self::$financialType,
118 'CRM_Financial_DAO_FinancialType',
119 TRUE,
120 'name',
121 NULL,
122 $condition
123 );
124 }
125
126 if ($id) {
127 $result = CRM_Utils_Array::value($id, self::$financialType);
128 return $result;
129 }
130 return self::$financialType;
131 }
132
133 /**
69af118f 134 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
3f3a3ba0 135 *
6a488035
TO
136 * Get all the financial Accounts
137 *
dd244018 138 *
100fef9d
CW
139 * @param int $id
140 * @param int $financialAccountTypeId
dd244018
EM
141 * @param string $retrieveColumn
142 * @param string $key
143 *
a6c01b45
CW
144 * @return array
145 * array reference of all financial accounts if any
6a488035 146 */
086ca649 147 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
874c9be7 148 $condition = NULL;
6a488035 149 if ($financialAccountTypeId) {
86bfa4f6 150 $condition = " financial_account_type_id = " . $financialAccountTypeId;
6a488035 151 }
086ca649 152 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
6a488035
TO
153 if (!isset(self::$financialAccount[$cacheKey])) {
154 CRM_Core_PseudoConstant::populate(
155 self::$financialAccount[$cacheKey],
156 'CRM_Financial_DAO_FinancialAccount',
157 TRUE,
086ca649 158 $retrieveColumn,
6a488035 159 'is_active',
086ca649
PN
160 $condition,
161 NULL,
162 $key
6a488035
TO
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 *
6a488035 177 *
da6b46f4 178 * @param bool|string $name pseudoconstant to be flushed
6a488035 179 */
3fb36592 180 public static function flush($name = 'cache') {
874c9be7 181 if (isset(self::$$name)) {
fa56270d 182 self::$$name = NULL;
353ffa53 183 }
6a488035
TO
184 }
185
186 /**
69af118f 187 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
3f3a3ba0 188 *
6a488035
TO
189 * Get all the contribution pages
190 *
014c4014
TO
191 * @param int $id
192 * Id of the contribution page.
193 * @param bool $all
194 * Do we want all pages or only active pages.
6a488035 195 *
6a488035 196 *
a6c01b45
CW
197 * @return array
198 * array reference of all contribution pages if any
6a488035
TO
199 */
200 public static function &contributionPage($id = NULL, $all = FALSE) {
201 if ($all) {
202 $cacheVarToUse = &self::$contributionPageAll;
203 }
204 else {
205 $cacheVarToUse = &self::$contributionPageActive;
206 }
207
208 if (!$cacheVarToUse) {
209 CRM_Core_PseudoConstant::populate($cacheVarToUse,
210 'CRM_Contribute_DAO_ContributionPage',
211 $all, 'title'
212 );
213 }
214 if ($id) {
215 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
216 return $pageTitle;
217 }
218 return $cacheVarToUse;
219 }
220
221 /**
69af118f 222 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
3f3a3ba0 223 *
6a488035
TO
224 * Get all the payment instruments
225 *
6a488035 226 *
fd31fa4c
EM
227 * @param string $columnName
228 *
a6c01b45
CW
229 * @return array
230 * array reference of all payment instruments if any
6a488035
TO
231 */
232 public static function &paymentInstrument($columnName = 'label') {
233 if (!isset(self::$paymentInstrument[$columnName])) {
234 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
235 FALSE, FALSE, FALSE, NULL, $columnName
236 );
237 }
238
239 return self::$paymentInstrument[$columnName];
240 }
241
242 /**
fe482240 243 * Get all the valid accepted credit cards.
6a488035 244 *
6a488035 245 *
a6c01b45
CW
246 * @return array
247 * array reference of all payment instruments if any
6a488035
TO
248 */
249 public static function &creditCard() {
3f3a3ba0 250 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
6a488035
TO
251 }
252
253 /**
fe482240 254 * Get all premiums.
6a488035 255 *
6a488035 256 *
100fef9d 257 * @param int $pageID
a6c01b45
CW
258 * @return array
259 * array of all Premiums if any
6a488035
TO
260 */
261 public static function products($pageID = NULL) {
353ffa53
TO
262 $products = array();
263 $dao = new CRM_Contribute_DAO_Product();
6a488035
TO
264 $dao->is_active = 1;
265 $dao->orderBy('id');
266 $dao->find();
267
268 while ($dao->fetch()) {
269 $products[$dao->id] = $dao->name;
270 }
271 if ($pageID) {
353ffa53 272 $dao = new CRM_Contribute_DAO_Premium();
6a488035 273 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 274 $dao->entity_id = $pageID;
6a488035
TO
275 $dao->find(TRUE);
276 $premiumID = $dao->id;
277
278 $productID = array();
279
280 $dao = new CRM_Contribute_DAO_PremiumsProduct();
281 $dao->premiums_id = $premiumID;
282 $dao->find();
283 while ($dao->fetch()) {
284 $productID[$dao->product_id] = $dao->product_id;
285 }
286
287 $tempProduct = array();
288 foreach ($products as $key => $value) {
289 if (!array_key_exists($key, $productID)) {
290 $tempProduct[$key] = $value;
291 }
292 }
293
294 return $tempProduct;
295 }
296
297 return $products;
298 }
299
300 /**
fe482240 301 * Get all the contribution statuses.
6a488035 302 *
6a488035 303 *
100fef9d 304 * @param int $id
da6b46f4 305 * @param string $columnName
a6c01b45
CW
306 * @return array
307 * array reference of all contribution statuses
6a488035
TO
308 */
309 public static function &contributionStatus($id = NULL, $columnName = 'label') {
310 $cacheKey = $columnName;
311 if (!isset(self::$contributionStatus[$cacheKey])) {
312 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
313 FALSE, FALSE, FALSE, NULL, $columnName
314 );
315 }
316 $result = self::$contributionStatus[$cacheKey];
317 if ($id) {
318 $result = CRM_Utils_Array::value($id, $result);
319 }
320
321 return $result;
322 }
323
324 /**
fe482240 325 * Get all the Personal campaign pages.
6a488035 326 *
6a488035 327 *
2a6da8d7 328 * @param null $pageType
100fef9d 329 * @param int $id
2a6da8d7 330 *
a6c01b45
CW
331 * @return array
332 * array reference of all pcp if any
6a488035
TO
333 */
334 public static function &pcPage($pageType = NULL, $id = NULL) {
335 if (!isset(self::$pcPage[$pageType])) {
336 if ($pageType) {
337 $params = "page_type='{$pageType}'";
338 }
339 else {
340 $params = '';
341 }
342 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
343 'CRM_PCP_DAO_PCP',
344 FALSE, 'title', 'is_active', $params
345 );
346 }
347 $result = self::$pcPage[$pageType];
348 if ($id) {
349 return $result = CRM_Utils_Array::value($id, $result);
350 }
351
352 return $result;
353 }
354
355 /**
356 * Get all PCP Statuses.
357 *
358 * The static array pcpStatus is returned
359 *
6a488035 360 *
2a6da8d7 361 * @param string $column
a6c01b45
CW
362 * @return array
363 * array reference of all PCP activity statuses
6a488035
TO
364 */
365 public static function &pcpStatus($column = 'label') {
366 if (NULL === self::$pcpStatus) {
367 self::$pcpStatus = array();
368 }
369 if (!array_key_exists($column, self::$pcpStatus)) {
370 self::$pcpStatus[$column] = array();
371
372 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
373 FALSE, FALSE, NULL, $column
374 );
375 }
376 return self::$pcpStatus[$column];
377 }
378
379 /**
876b8ab0 380 * Get financial account for a Financial type.
6a488035 381 *
6a488035 382 *
876b8ab0
PN
383 * @param int $entityId
384 * @param string $accountRelationType
31484b44 385 * @param string $entityTable
74afdc40 386 * @param string $returnField
876b8ab0 387 * @return int
6a488035 388 */
74afdc40
PN
389 public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') {
390 $params = array(
391 'return' => array($returnField),
31484b44 392 'entity_table' => $entityTable,
876b8ab0 393 'entity_id' => $entityId,
74afdc40
PN
394 );
395 if ($accountRelationType) {
396 $params['account_relationship.name'] = $accountRelationType;
397 }
398 $result = civicrm_api3('EntityFinancialAccount', 'get', $params);
876b8ab0
PN
399 if (!$result['count']) {
400 return NULL;
6a488035 401 }
74afdc40 402 return $result['values'][$result['id']][$returnField];
6a488035 403 }
91aff94c 404
405 /**
fe482240 406 * Get all batches.
91aff94c 407 *
91aff94c 408 *
100fef9d 409 * @param int $id
a6c01b45
CW
410 * @return array
411 * array reference of all batches if any
91aff94c 412 */
413 public static function &batch($id = NULL) {
414 if (!self::$batch) {
415 $orderBy = " id DESC ";
416 CRM_Core_PseudoConstant::populate(
417 self::$batch,
418 'CRM_Batch_DAO_Batch',
419 TRUE,
420 'title',
421 NULL,
422 NULL,
423 $orderBy
424 );
425 }
426
427 if ($id) {
428 $result = CRM_Utils_Array::value($id, self::$batch);
429 return $result;
430 }
431 return self::$batch;
432 }
96025800 433
6a488035 434}