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