Merge pull request #23753 from eileenmcnaughton/import_validate_int
[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
8655187b 72 * @deprecated
6a488035
TO
73 */
74 private static $pcpStatus;
75
91aff94c 76 /**
100fef9d 77 * Contribution / financial batches
91aff94c 78 * @var array
91aff94c 79 */
80 private static $batch;
81
6a488035 82 /**
69af118f
MW
83 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
84 *
3f3a3ba0 85 *
6a488035
TO
86 * Get all the financial types
87 *
6a488035 88 *
100fef9d 89 * @param int $id
d73f286e 90 * @param bool $includeDisabled
da6b46f4 91 *
a6c01b45
CW
92 * @return array
93 * array reference of all financial types if any
6a488035 94 */
d73f286e 95 public static function &financialType($id = NULL, $includeDisabled = FALSE) {
6a488035 96 if (!self::$financialType) {
d73f286e
SL
97 $condition = "";
98 if (!$includeDisabled) {
99 $condition = " is_active = 1 ";
100 }
6a488035
TO
101 CRM_Core_PseudoConstant::populate(
102 self::$financialType,
103 'CRM_Financial_DAO_FinancialType',
104 TRUE,
105 'name',
106 NULL,
107 $condition
108 );
109 }
110
111 if ($id) {
9c1bc317 112 $result = self::$financialType[$id] ?? NULL;
6a488035
TO
113 return $result;
114 }
115 return self::$financialType;
116 }
117
118 /**
69af118f 119 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
8461467c 120 * TODO: buildOptions() doesn't replace this as it doesn't support filtering, which is used with this function.
3f3a3ba0 121 *
8461467c 122 * Get all/filtered array of the financial Accounts
6a488035 123 *
dd244018 124 *
100fef9d 125 * @param int $id
8461467c 126 * @param int $financialAccountTypeId Optional filer to return only financial accounts of type
dd244018
EM
127 * @param string $retrieveColumn
128 * @param string $key
129 *
a6c01b45
CW
130 * @return array
131 * array reference of all financial accounts if any
6a488035 132 */
086ca649 133 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
874c9be7 134 $condition = NULL;
6a488035 135 if ($financialAccountTypeId) {
86bfa4f6 136 $condition = " financial_account_type_id = " . $financialAccountTypeId;
6a488035 137 }
086ca649 138 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
6a488035
TO
139 if (!isset(self::$financialAccount[$cacheKey])) {
140 CRM_Core_PseudoConstant::populate(
141 self::$financialAccount[$cacheKey],
142 'CRM_Financial_DAO_FinancialAccount',
143 TRUE,
086ca649 144 $retrieveColumn,
6a488035 145 'is_active',
086ca649
PN
146 $condition,
147 NULL,
148 $key
6a488035
TO
149 );
150
151 }
152 if ($id) {
9c1bc317 153 $result = self::$financialAccount[$cacheKey][$id] ?? NULL;
6a488035
TO
154 return $result;
155 }
156 return self::$financialAccount[$cacheKey];
157 }
158
159 /**
160 * Flush given pseudoconstant so it can be reread from db
161 * nex time it's requested.
162 *
6a488035 163 *
da6b46f4 164 * @param bool|string $name pseudoconstant to be flushed
6a488035 165 */
3fb36592 166 public static function flush($name = 'cache') {
874c9be7 167 if (isset(self::$$name)) {
fa56270d 168 self::$$name = NULL;
353ffa53 169 }
6a488035
TO
170 }
171
172 /**
69af118f 173 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
3f3a3ba0 174 *
6a488035
TO
175 * Get all the contribution pages
176 *
014c4014
TO
177 * @param int $id
178 * Id of the contribution page.
179 * @param bool $all
180 * Do we want all pages or only active pages.
6a488035 181 *
6a488035 182 *
a6c01b45
CW
183 * @return array
184 * array reference of all contribution pages if any
6a488035
TO
185 */
186 public static function &contributionPage($id = NULL, $all = FALSE) {
187 if ($all) {
188 $cacheVarToUse = &self::$contributionPageAll;
189 }
190 else {
191 $cacheVarToUse = &self::$contributionPageActive;
192 }
193
194 if (!$cacheVarToUse) {
195 CRM_Core_PseudoConstant::populate($cacheVarToUse,
196 'CRM_Contribute_DAO_ContributionPage',
197 $all, 'title'
198 );
199 }
200 if ($id) {
9c1bc317 201 $pageTitle = $cacheVarToUse[$id] ?? NULL;
6a488035
TO
202 return $pageTitle;
203 }
204 return $cacheVarToUse;
205 }
206
207 /**
69af118f 208 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
3f3a3ba0 209 *
6a488035
TO
210 * Get all the payment instruments
211 *
6a488035 212 *
fd31fa4c
EM
213 * @param string $columnName
214 *
a6c01b45
CW
215 * @return array
216 * array reference of all payment instruments if any
6a488035
TO
217 */
218 public static function &paymentInstrument($columnName = 'label') {
219 if (!isset(self::$paymentInstrument[$columnName])) {
220 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
221 FALSE, FALSE, FALSE, NULL, $columnName
222 );
223 }
224
225 return self::$paymentInstrument[$columnName];
226 }
227
228 /**
fe482240 229 * Get all the valid accepted credit cards.
6a488035 230 *
6a488035 231 *
a6c01b45
CW
232 * @return array
233 * array reference of all payment instruments if any
6a488035
TO
234 */
235 public static function &creditCard() {
3f3a3ba0 236 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
6a488035
TO
237 }
238
239 /**
fe482240 240 * Get all premiums.
6a488035 241 *
6a488035 242 *
100fef9d 243 * @param int $pageID
a6c01b45
CW
244 * @return array
245 * array of all Premiums if any
6a488035
TO
246 */
247 public static function products($pageID = NULL) {
be2fb01f 248 $products = [];
353ffa53 249 $dao = new CRM_Contribute_DAO_Product();
6a488035
TO
250 $dao->is_active = 1;
251 $dao->orderBy('id');
252 $dao->find();
253
254 while ($dao->fetch()) {
255 $products[$dao->id] = $dao->name;
256 }
257 if ($pageID) {
353ffa53 258 $dao = new CRM_Contribute_DAO_Premium();
6a488035 259 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 260 $dao->entity_id = $pageID;
6a488035
TO
261 $dao->find(TRUE);
262 $premiumID = $dao->id;
263
be2fb01f 264 $productID = [];
6a488035
TO
265
266 $dao = new CRM_Contribute_DAO_PremiumsProduct();
267 $dao->premiums_id = $premiumID;
268 $dao->find();
269 while ($dao->fetch()) {
270 $productID[$dao->product_id] = $dao->product_id;
271 }
272
be2fb01f 273 $tempProduct = [];
6a488035
TO
274 foreach ($products as $key => $value) {
275 if (!array_key_exists($key, $productID)) {
276 $tempProduct[$key] = $value;
277 }
278 }
279
280 return $tempProduct;
281 }
282
283 return $products;
284 }
285
286 /**
fe482240 287 * Get all the contribution statuses.
6a488035 288 *
6a488035 289 *
100fef9d 290 * @param int $id
da6b46f4 291 * @param string $columnName
a6c01b45
CW
292 * @return array
293 * array reference of all contribution statuses
6a488035
TO
294 */
295 public static function &contributionStatus($id = NULL, $columnName = 'label') {
296 $cacheKey = $columnName;
297 if (!isset(self::$contributionStatus[$cacheKey])) {
298 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
299 FALSE, FALSE, FALSE, NULL, $columnName
300 );
301 }
302 $result = self::$contributionStatus[$cacheKey];
303 if ($id) {
9c1bc317 304 $result = $result[$id] ?? NULL;
6a488035
TO
305 }
306
307 return $result;
308 }
309
310 /**
fe482240 311 * Get all the Personal campaign pages.
6a488035 312 *
6a488035 313 *
3fd42bb5 314 * @param string|null $pageType
100fef9d 315 * @param int $id
2a6da8d7 316 *
a6c01b45
CW
317 * @return array
318 * array reference of all pcp if any
6a488035
TO
319 */
320 public static function &pcPage($pageType = NULL, $id = NULL) {
321 if (!isset(self::$pcPage[$pageType])) {
322 if ($pageType) {
323 $params = "page_type='{$pageType}'";
324 }
325 else {
326 $params = '';
327 }
328 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
329 'CRM_PCP_DAO_PCP',
330 FALSE, 'title', 'is_active', $params
331 );
332 }
333 $result = self::$pcPage[$pageType];
334 if ($id) {
9c1bc317 335 return $result = $result[$id] ?? NULL;
6a488035
TO
336 }
337
338 return $result;
339 }
340
341 /**
342 * Get all PCP Statuses.
343 *
344 * The static array pcpStatus is returned
345 *
8655187b 346 * @deprecated
2a6da8d7 347 * @param string $column
a6c01b45
CW
348 * @return array
349 * array reference of all PCP activity statuses
6a488035
TO
350 */
351 public static function &pcpStatus($column = 'label') {
8655187b 352 CRM_Core_Error::deprecatedFunctionWarning('Function pcpStatus will be removed');
6a488035 353 if (NULL === self::$pcpStatus) {
be2fb01f 354 self::$pcpStatus = [];
6a488035
TO
355 }
356 if (!array_key_exists($column, self::$pcpStatus)) {
be2fb01f 357 self::$pcpStatus[$column] = [];
6a488035
TO
358
359 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
360 FALSE, FALSE, NULL, $column
361 );
362 }
363 return self::$pcpStatus[$column];
364 }
365
366 /**
876b8ab0 367 * Get financial account for a Financial type.
6a488035 368 *
928a340b 369 * @deprecated use the alternative with caching
370 * CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship
6a488035 371 *
876b8ab0
PN
372 * @param int $entityId
373 * @param string $accountRelationType
31484b44 374 * @param string $entityTable
74afdc40 375 * @param string $returnField
876b8ab0 376 * @return int
6a488035 377 */
74afdc40 378 public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') {
be2fb01f
CW
379 $params = [
380 'return' => [$returnField],
31484b44 381 'entity_table' => $entityTable,
876b8ab0 382 'entity_id' => $entityId,
be2fb01f 383 ];
74afdc40
PN
384 if ($accountRelationType) {
385 $params['account_relationship.name'] = $accountRelationType;
386 }
387 $result = civicrm_api3('EntityFinancialAccount', 'get', $params);
876b8ab0
PN
388 if (!$result['count']) {
389 return NULL;
6a488035 390 }
74afdc40 391 return $result['values'][$result['id']][$returnField];
6a488035 392 }
91aff94c 393
394 /**
fe482240 395 * Get all batches.
91aff94c 396 *
91aff94c 397 *
100fef9d 398 * @param int $id
a6c01b45
CW
399 * @return array
400 * array reference of all batches if any
91aff94c 401 */
402 public static function &batch($id = NULL) {
403 if (!self::$batch) {
404 $orderBy = " id DESC ";
405 CRM_Core_PseudoConstant::populate(
406 self::$batch,
407 'CRM_Batch_DAO_Batch',
408 TRUE,
409 'title',
410 NULL,
411 NULL,
412 $orderBy
413 );
414 }
415
416 if ($id) {
9c1bc317 417 $result = self::$batch[$id] ?? NULL;
91aff94c 418 return $result;
419 }
420 return self::$batch;
421 }
96025800 422
6a488035 423}