| 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 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * This class holds all the Pseudo constants that are specific to the civimember component. This avoids |
| 22 | * polluting the core class and isolates the mass mailer class |
| 23 | */ |
| 24 | class CRM_Member_PseudoConstant extends CRM_Core_PseudoConstant { |
| 25 | |
| 26 | /** |
| 27 | * Membership types. |
| 28 | * @var array |
| 29 | */ |
| 30 | private static $membershipType; |
| 31 | |
| 32 | /** |
| 33 | * Membership types. |
| 34 | * @var array |
| 35 | */ |
| 36 | private static $membershipStatus; |
| 37 | |
| 38 | /** |
| 39 | * Get all the membership types. |
| 40 | * |
| 41 | * |
| 42 | * @param int $id |
| 43 | * @param bool $force |
| 44 | * |
| 45 | * @return array |
| 46 | * array reference of all membership types if any |
| 47 | */ |
| 48 | public static function membershipType($id = NULL, $force = TRUE) { |
| 49 | if (!self::$membershipType || $force) { |
| 50 | CRM_Core_PseudoConstant::populate(self::$membershipType, |
| 51 | 'CRM_Member_DAO_MembershipType', |
| 52 | FALSE, 'name', 'is_active', NULL, 'weight', 'id' |
| 53 | ); |
| 54 | } |
| 55 | if ($id) { |
| 56 | if (array_key_exists($id, self::$membershipType)) { |
| 57 | return self::$membershipType[$id]; |
| 58 | } |
| 59 | else { |
| 60 | $result = NULL; |
| 61 | return $result; |
| 62 | } |
| 63 | } |
| 64 | return self::$membershipType; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get all the membership statuss. |
| 69 | * |
| 70 | * |
| 71 | * @param int $id |
| 72 | * @param null $cond |
| 73 | * @param string $column |
| 74 | * @param bool $force |
| 75 | * |
| 76 | * @param bool $allStatus |
| 77 | * |
| 78 | * @return array |
| 79 | * array reference of all membership statuses if any |
| 80 | */ |
| 81 | public static function &membershipStatus($id = NULL, $cond = NULL, $column = 'name', $force = FALSE, $allStatus = FALSE) { |
| 82 | if (self::$membershipStatus === NULL) { |
| 83 | self::$membershipStatus = []; |
| 84 | } |
| 85 | |
| 86 | $cacheKey = $column; |
| 87 | if ($cond) { |
| 88 | $cacheKey .= "_{$cond}"; |
| 89 | } |
| 90 | if (!isset(self::$membershipStatus[$cacheKey]) || $force) { |
| 91 | CRM_Core_PseudoConstant::populate(self::$membershipStatus[$cacheKey], |
| 92 | 'CRM_Member_DAO_MembershipStatus', |
| 93 | $allStatus, $column, 'is_active', $cond, 'weight' |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | $value = NULL; |
| 98 | if ($id) { |
| 99 | $value = self::$membershipStatus[$cacheKey][$id] ?? NULL; |
| 100 | } |
| 101 | else { |
| 102 | $value = self::$membershipStatus[$cacheKey]; |
| 103 | } |
| 104 | |
| 105 | return $value; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Flush given pseudoconstant so it can be reread from db |
| 110 | * next time it's requested. |
| 111 | * |
| 112 | * |
| 113 | * @param bool|string $name pseudoconstant to be flushed |
| 114 | */ |
| 115 | public static function flush($name = 'cache') { |
| 116 | if (isset(self::$$name)) { |
| 117 | self::$$name = NULL; |
| 118 | } |
| 119 | // The preferred source of membership pseudoconstants is in fact the Core class. |
| 120 | // which buildOptions accesses - better flush that too. |
| 121 | CRM_Core_PseudoConstant::flush(); |
| 122 | } |
| 123 | |
| 124 | } |