| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 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. |
| 22 | */ |
| 23 | class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant { |
| 24 | |
| 25 | /** |
| 26 | * Financial types |
| 27 | * @var array |
| 28 | */ |
| 29 | private static $financialType; |
| 30 | |
| 31 | /** |
| 32 | * Financial types |
| 33 | * @var array |
| 34 | */ |
| 35 | private static $financialAccount; |
| 36 | |
| 37 | /** |
| 38 | * Contribution pages |
| 39 | * @var array |
| 40 | */ |
| 41 | private static $contributionPageActive = NULL; |
| 42 | |
| 43 | /** |
| 44 | * Contribution pages |
| 45 | * @var array |
| 46 | */ |
| 47 | private static $contributionPageAll = NULL; |
| 48 | |
| 49 | /** |
| 50 | * Payment instruments |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | private static $paymentInstrument; |
| 55 | |
| 56 | /** |
| 57 | * Contribution status |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
| 61 | private static $contributionStatus; |
| 62 | |
| 63 | /** |
| 64 | * Personal campaign pages |
| 65 | * @var array |
| 66 | */ |
| 67 | private static $pcPage; |
| 68 | |
| 69 | /** |
| 70 | * Status of personal campaign page |
| 71 | * @var array |
| 72 | * @deprecated |
| 73 | */ |
| 74 | private static $pcpStatus; |
| 75 | |
| 76 | /** |
| 77 | * Contribution / financial batches |
| 78 | * @var array |
| 79 | */ |
| 80 | private static $batch; |
| 81 | |
| 82 | /** |
| 83 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 84 | * |
| 85 | * |
| 86 | * Get all the financial types |
| 87 | * |
| 88 | * |
| 89 | * @param int $id |
| 90 | * @param bool $includeDisabled |
| 91 | * |
| 92 | * @return array |
| 93 | * array reference of all financial types if any |
| 94 | */ |
| 95 | public static function &financialType($id = NULL, $includeDisabled = FALSE) { |
| 96 | if (!self::$financialType) { |
| 97 | $condition = ""; |
| 98 | if (!$includeDisabled) { |
| 99 | $condition = " is_active = 1 "; |
| 100 | } |
| 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) { |
| 112 | $result = self::$financialType[$id] ?? NULL; |
| 113 | return $result; |
| 114 | } |
| 115 | return self::$financialType; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 120 | * TODO: buildOptions() doesn't replace this as it doesn't support filtering, which is used with this function. |
| 121 | * |
| 122 | * Get all/filtered array of the financial Accounts |
| 123 | * |
| 124 | * |
| 125 | * @param int $id |
| 126 | * @param int $financialAccountTypeId Optional filer to return only financial accounts of type |
| 127 | * @param string $retrieveColumn |
| 128 | * @param string $key |
| 129 | * |
| 130 | * @return array |
| 131 | * array reference of all financial accounts if any |
| 132 | */ |
| 133 | public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') { |
| 134 | $condition = NULL; |
| 135 | if ($financialAccountTypeId) { |
| 136 | $condition = " financial_account_type_id = " . $financialAccountTypeId; |
| 137 | } |
| 138 | $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}"; |
| 139 | if (!isset(self::$financialAccount[$cacheKey])) { |
| 140 | CRM_Core_PseudoConstant::populate( |
| 141 | self::$financialAccount[$cacheKey], |
| 142 | 'CRM_Financial_DAO_FinancialAccount', |
| 143 | TRUE, |
| 144 | $retrieveColumn, |
| 145 | 'is_active', |
| 146 | $condition, |
| 147 | NULL, |
| 148 | $key |
| 149 | ); |
| 150 | |
| 151 | } |
| 152 | if ($id) { |
| 153 | $result = self::$financialAccount[$cacheKey][$id] ?? NULL; |
| 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 | * |
| 163 | * |
| 164 | * @param bool|string $name pseudoconstant to be flushed |
| 165 | */ |
| 166 | public static function flush($name = 'cache') { |
| 167 | if (isset(self::$$name)) { |
| 168 | self::$$name = NULL; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 174 | * |
| 175 | * Get all the contribution pages |
| 176 | * |
| 177 | * @param int $id |
| 178 | * Id of the contribution page. |
| 179 | * @param bool $all |
| 180 | * Do we want all pages or only active pages. |
| 181 | * |
| 182 | * |
| 183 | * @return array |
| 184 | * array reference of all contribution pages if any |
| 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) { |
| 201 | $pageTitle = $cacheVarToUse[$id] ?? NULL; |
| 202 | return $pageTitle; |
| 203 | } |
| 204 | return $cacheVarToUse; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 209 | * |
| 210 | * Get all the payment instruments |
| 211 | * |
| 212 | * |
| 213 | * @param string $columnName |
| 214 | * |
| 215 | * @return array |
| 216 | * array reference of all payment instruments if any |
| 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 | /** |
| 229 | * Get all the valid accepted credit cards. |
| 230 | * |
| 231 | * |
| 232 | * @return array |
| 233 | * array reference of all payment instruments if any |
| 234 | */ |
| 235 | public static function &creditCard() { |
| 236 | return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get all premiums. |
| 241 | * |
| 242 | * |
| 243 | * @param int $pageID |
| 244 | * @return array |
| 245 | * array of all Premiums if any |
| 246 | */ |
| 247 | public static function products($pageID = NULL) { |
| 248 | $products = []; |
| 249 | $dao = new CRM_Contribute_DAO_Product(); |
| 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) { |
| 258 | $dao = new CRM_Contribute_DAO_Premium(); |
| 259 | $dao->entity_table = 'civicrm_contribution_page'; |
| 260 | $dao->entity_id = $pageID; |
| 261 | $dao->find(TRUE); |
| 262 | $premiumID = $dao->id; |
| 263 | |
| 264 | $productID = []; |
| 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 | |
| 273 | $tempProduct = []; |
| 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 | /** |
| 287 | * Get all the contribution statuses. |
| 288 | * |
| 289 | * |
| 290 | * @param int $id |
| 291 | * @param string $columnName |
| 292 | * @return array |
| 293 | * array reference of all contribution statuses |
| 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) { |
| 304 | $result = $result[$id] ?? NULL; |
| 305 | } |
| 306 | |
| 307 | return $result; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get all the Personal campaign pages. |
| 312 | * |
| 313 | * |
| 314 | * @param string|null $pageType |
| 315 | * @param int $id |
| 316 | * |
| 317 | * @return array |
| 318 | * array reference of all pcp if any |
| 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) { |
| 335 | return $result = $result[$id] ?? NULL; |
| 336 | } |
| 337 | |
| 338 | return $result; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get all PCP Statuses. |
| 343 | * |
| 344 | * The static array pcpStatus is returned |
| 345 | * |
| 346 | * @deprecated |
| 347 | * @param string $column |
| 348 | * @return array |
| 349 | * array reference of all PCP activity statuses |
| 350 | */ |
| 351 | public static function &pcpStatus($column = 'label') { |
| 352 | CRM_Core_Error::deprecatedFunctionWarning('Function pcpStatus will be removed'); |
| 353 | if (NULL === self::$pcpStatus) { |
| 354 | self::$pcpStatus = []; |
| 355 | } |
| 356 | if (!array_key_exists($column, self::$pcpStatus)) { |
| 357 | self::$pcpStatus[$column] = []; |
| 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 | /** |
| 367 | * Get financial account for a Financial type. |
| 368 | * |
| 369 | * @deprecated use the alternative with caching |
| 370 | * CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship |
| 371 | * |
| 372 | * @param int $entityId |
| 373 | * @param string $accountRelationType |
| 374 | * @param string $entityTable |
| 375 | * @param string $returnField |
| 376 | * @return int |
| 377 | */ |
| 378 | public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') { |
| 379 | $params = [ |
| 380 | 'return' => [$returnField], |
| 381 | 'entity_table' => $entityTable, |
| 382 | 'entity_id' => $entityId, |
| 383 | ]; |
| 384 | if ($accountRelationType) { |
| 385 | $params['account_relationship.name'] = $accountRelationType; |
| 386 | } |
| 387 | $result = civicrm_api3('EntityFinancialAccount', 'get', $params); |
| 388 | if (!$result['count']) { |
| 389 | return NULL; |
| 390 | } |
| 391 | return $result['values'][$result['id']][$returnField]; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Get all batches. |
| 396 | * |
| 397 | * |
| 398 | * @param int $id |
| 399 | * @return array |
| 400 | * array reference of all batches if any |
| 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) { |
| 417 | $result = self::$batch[$id] ?? NULL; |
| 418 | return $result; |
| 419 | } |
| 420 | return self::$batch; |
| 421 | } |
| 422 | |
| 423 | } |