| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 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 | * |
| 101 | * Get all the financial types |
| 102 | * |
| 103 | * |
| 104 | * @param int $id |
| 105 | * @param bool $includeDisabled |
| 106 | * |
| 107 | * @return array |
| 108 | * array reference of all financial types if any |
| 109 | */ |
| 110 | public static function &financialType($id = NULL, $includeDisabled = FALSE) { |
| 111 | if (!self::$financialType) { |
| 112 | $condition = ""; |
| 113 | if (!$includeDisabled) { |
| 114 | $condition = " is_active = 1 "; |
| 115 | } |
| 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 | /** |
| 134 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 135 | * TODO: buildOptions() doesn't replace this as it doesn't support filtering, which is used with this function. |
| 136 | * |
| 137 | * Get all/filtered array of the financial Accounts |
| 138 | * |
| 139 | * |
| 140 | * @param int $id |
| 141 | * @param int $financialAccountTypeId Optional filer to return only financial accounts of type |
| 142 | * @param string $retrieveColumn |
| 143 | * @param string $key |
| 144 | * |
| 145 | * @return array |
| 146 | * array reference of all financial accounts if any |
| 147 | */ |
| 148 | public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') { |
| 149 | $condition = NULL; |
| 150 | if ($financialAccountTypeId) { |
| 151 | $condition = " financial_account_type_id = " . $financialAccountTypeId; |
| 152 | } |
| 153 | $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}"; |
| 154 | if (!isset(self::$financialAccount[$cacheKey])) { |
| 155 | CRM_Core_PseudoConstant::populate( |
| 156 | self::$financialAccount[$cacheKey], |
| 157 | 'CRM_Financial_DAO_FinancialAccount', |
| 158 | TRUE, |
| 159 | $retrieveColumn, |
| 160 | 'is_active', |
| 161 | $condition, |
| 162 | NULL, |
| 163 | $key |
| 164 | ); |
| 165 | |
| 166 | } |
| 167 | if ($id) { |
| 168 | $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]); |
| 169 | return $result; |
| 170 | } |
| 171 | return self::$financialAccount[$cacheKey]; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Flush given pseudoconstant so it can be reread from db |
| 176 | * nex time it's requested. |
| 177 | * |
| 178 | * |
| 179 | * @param bool|string $name pseudoconstant to be flushed |
| 180 | */ |
| 181 | public static function flush($name = 'cache') { |
| 182 | if (isset(self::$$name)) { |
| 183 | self::$$name = NULL; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 189 | * |
| 190 | * Get all the contribution pages |
| 191 | * |
| 192 | * @param int $id |
| 193 | * Id of the contribution page. |
| 194 | * @param bool $all |
| 195 | * Do we want all pages or only active pages. |
| 196 | * |
| 197 | * |
| 198 | * @return array |
| 199 | * array reference of all contribution pages if any |
| 200 | */ |
| 201 | public static function &contributionPage($id = NULL, $all = FALSE) { |
| 202 | if ($all) { |
| 203 | $cacheVarToUse = &self::$contributionPageAll; |
| 204 | } |
| 205 | else { |
| 206 | $cacheVarToUse = &self::$contributionPageActive; |
| 207 | } |
| 208 | |
| 209 | if (!$cacheVarToUse) { |
| 210 | CRM_Core_PseudoConstant::populate($cacheVarToUse, |
| 211 | 'CRM_Contribute_DAO_ContributionPage', |
| 212 | $all, 'title' |
| 213 | ); |
| 214 | } |
| 215 | if ($id) { |
| 216 | $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse); |
| 217 | return $pageTitle; |
| 218 | } |
| 219 | return $cacheVarToUse; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @deprecated. Please use the buildOptions() method in the appropriate BAO object. |
| 224 | * |
| 225 | * Get all the payment instruments |
| 226 | * |
| 227 | * |
| 228 | * @param string $columnName |
| 229 | * |
| 230 | * @return array |
| 231 | * array reference of all payment instruments if any |
| 232 | */ |
| 233 | public static function &paymentInstrument($columnName = 'label') { |
| 234 | if (!isset(self::$paymentInstrument[$columnName])) { |
| 235 | self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument', |
| 236 | FALSE, FALSE, FALSE, NULL, $columnName |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | return self::$paymentInstrument[$columnName]; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get all the valid accepted credit cards. |
| 245 | * |
| 246 | * |
| 247 | * @return array |
| 248 | * array reference of all payment instruments if any |
| 249 | */ |
| 250 | public static function &creditCard() { |
| 251 | return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get all premiums. |
| 256 | * |
| 257 | * |
| 258 | * @param int $pageID |
| 259 | * @return array |
| 260 | * array of all Premiums if any |
| 261 | */ |
| 262 | public static function products($pageID = NULL) { |
| 263 | $products = []; |
| 264 | $dao = new CRM_Contribute_DAO_Product(); |
| 265 | $dao->is_active = 1; |
| 266 | $dao->orderBy('id'); |
| 267 | $dao->find(); |
| 268 | |
| 269 | while ($dao->fetch()) { |
| 270 | $products[$dao->id] = $dao->name; |
| 271 | } |
| 272 | if ($pageID) { |
| 273 | $dao = new CRM_Contribute_DAO_Premium(); |
| 274 | $dao->entity_table = 'civicrm_contribution_page'; |
| 275 | $dao->entity_id = $pageID; |
| 276 | $dao->find(TRUE); |
| 277 | $premiumID = $dao->id; |
| 278 | |
| 279 | $productID = []; |
| 280 | |
| 281 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); |
| 282 | $dao->premiums_id = $premiumID; |
| 283 | $dao->find(); |
| 284 | while ($dao->fetch()) { |
| 285 | $productID[$dao->product_id] = $dao->product_id; |
| 286 | } |
| 287 | |
| 288 | $tempProduct = []; |
| 289 | foreach ($products as $key => $value) { |
| 290 | if (!array_key_exists($key, $productID)) { |
| 291 | $tempProduct[$key] = $value; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return $tempProduct; |
| 296 | } |
| 297 | |
| 298 | return $products; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Get all the contribution statuses. |
| 303 | * |
| 304 | * |
| 305 | * @param int $id |
| 306 | * @param string $columnName |
| 307 | * @return array |
| 308 | * array reference of all contribution statuses |
| 309 | */ |
| 310 | public static function &contributionStatus($id = NULL, $columnName = 'label') { |
| 311 | $cacheKey = $columnName; |
| 312 | if (!isset(self::$contributionStatus[$cacheKey])) { |
| 313 | self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status', |
| 314 | FALSE, FALSE, FALSE, NULL, $columnName |
| 315 | ); |
| 316 | } |
| 317 | $result = self::$contributionStatus[$cacheKey]; |
| 318 | if ($id) { |
| 319 | $result = CRM_Utils_Array::value($id, $result); |
| 320 | } |
| 321 | |
| 322 | return $result; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Get all the Personal campaign pages. |
| 327 | * |
| 328 | * |
| 329 | * @param null $pageType |
| 330 | * @param int $id |
| 331 | * |
| 332 | * @return array |
| 333 | * array reference of all pcp if any |
| 334 | */ |
| 335 | public static function &pcPage($pageType = NULL, $id = NULL) { |
| 336 | if (!isset(self::$pcPage[$pageType])) { |
| 337 | if ($pageType) { |
| 338 | $params = "page_type='{$pageType}'"; |
| 339 | } |
| 340 | else { |
| 341 | $params = ''; |
| 342 | } |
| 343 | CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType], |
| 344 | 'CRM_PCP_DAO_PCP', |
| 345 | FALSE, 'title', 'is_active', $params |
| 346 | ); |
| 347 | } |
| 348 | $result = self::$pcPage[$pageType]; |
| 349 | if ($id) { |
| 350 | return $result = CRM_Utils_Array::value($id, $result); |
| 351 | } |
| 352 | |
| 353 | return $result; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Get all PCP Statuses. |
| 358 | * |
| 359 | * The static array pcpStatus is returned |
| 360 | * |
| 361 | * |
| 362 | * @param string $column |
| 363 | * @return array |
| 364 | * array reference of all PCP activity statuses |
| 365 | */ |
| 366 | public static function &pcpStatus($column = 'label') { |
| 367 | if (NULL === self::$pcpStatus) { |
| 368 | self::$pcpStatus = []; |
| 369 | } |
| 370 | if (!array_key_exists($column, self::$pcpStatus)) { |
| 371 | self::$pcpStatus[$column] = []; |
| 372 | |
| 373 | self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE, |
| 374 | FALSE, FALSE, NULL, $column |
| 375 | ); |
| 376 | } |
| 377 | return self::$pcpStatus[$column]; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get financial account for a Financial type. |
| 382 | * |
| 383 | * @deprecated use the alternative with caching |
| 384 | * CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship |
| 385 | * |
| 386 | * @param int $entityId |
| 387 | * @param string $accountRelationType |
| 388 | * @param string $entityTable |
| 389 | * @param string $returnField |
| 390 | * @return int |
| 391 | */ |
| 392 | public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') { |
| 393 | $params = [ |
| 394 | 'return' => [$returnField], |
| 395 | 'entity_table' => $entityTable, |
| 396 | 'entity_id' => $entityId, |
| 397 | ]; |
| 398 | if ($accountRelationType) { |
| 399 | $params['account_relationship.name'] = $accountRelationType; |
| 400 | } |
| 401 | $result = civicrm_api3('EntityFinancialAccount', 'get', $params); |
| 402 | if (!$result['count']) { |
| 403 | return NULL; |
| 404 | } |
| 405 | return $result['values'][$result['id']][$returnField]; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Get all batches. |
| 410 | * |
| 411 | * |
| 412 | * @param int $id |
| 413 | * @return array |
| 414 | * array reference of all batches if any |
| 415 | */ |
| 416 | public static function &batch($id = NULL) { |
| 417 | if (!self::$batch) { |
| 418 | $orderBy = " id DESC "; |
| 419 | CRM_Core_PseudoConstant::populate( |
| 420 | self::$batch, |
| 421 | 'CRM_Batch_DAO_Batch', |
| 422 | TRUE, |
| 423 | 'title', |
| 424 | NULL, |
| 425 | NULL, |
| 426 | $orderBy |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | if ($id) { |
| 431 | $result = CRM_Utils_Array::value($id, self::$batch); |
| 432 | return $result; |
| 433 | } |
| 434 | return self::$batch; |
| 435 | } |
| 436 | |
| 437 | } |