Merge pull request #16086 from civicrm/5.21
[civicrm-core.git] / CRM / Member / 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
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 */
24class CRM_Member_PseudoConstant extends CRM_Core_PseudoConstant {
25
26 /**
fe482240 27 * Membership types.
6a488035 28 * @var array
6a488035
TO
29 */
30 private static $membershipType;
31
32 /**
fe482240 33 * Membership types.
6a488035 34 * @var array
6a488035
TO
35 */
36 private static $membershipStatus;
37
38 /**
fe482240 39 * Get all the membership types.
6a488035 40 *
6a488035 41 *
100fef9d 42 * @param int $id
6c8f6e67
EM
43 * @param bool $force
44 *
a6c01b45
CW
45 * @return array
46 * array reference of all membership types if any
6a488035 47 */
08fd4b45 48 public static function membershipType($id = NULL, $force = TRUE) {
6a488035
TO
49 if (!self::$membershipType || $force) {
50 CRM_Core_PseudoConstant::populate(self::$membershipType,
51 'CRM_Member_DAO_MembershipType',
7a897d9a 52 FALSE, 'name', 'is_active', NULL, 'weight', 'id'
6a488035
TO
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 /**
fe482240 68 * Get all the membership statuss.
6a488035 69 *
6a488035 70 *
100fef9d 71 * @param int $id
6c8f6e67
EM
72 * @param null $cond
73 * @param string $column
74 * @param bool $force
75 *
ad37ac8e 76 * @param bool $allStatus
77 *
a6c01b45 78 * @return array
971e129b 79 * array reference of all membership statuses if any
6a488035 80 */
7ff60806 81 public static function &membershipStatus($id = NULL, $cond = NULL, $column = 'name', $force = FALSE, $allStatus = FALSE) {
6a488035 82 if (self::$membershipStatus === NULL) {
be2fb01f 83 self::$membershipStatus = [];
6a488035
TO
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',
7ff60806 93 $allStatus, $column, 'is_active', $cond, 'weight'
6a488035
TO
94 );
95 }
96
97 $value = NULL;
98 if ($id) {
99 $value = CRM_Utils_Array::value($id, self::$membershipStatus[$cacheKey]);
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 *
6a488035 112 *
da6b46f4 113 * @param bool|string $name pseudoconstant to be flushed
6a488035 114 */
3fb36592 115 public static function flush($name = 'cache') {
b09fe5ed 116 if (isset(self::$$name)) {
fa56270d 117 self::$$name = NULL;
353ffa53 118 }
14b9e069 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();
6a488035 122 }
96025800 123
6a488035 124}