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