Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-30-12-58-23
[civicrm-core.git] / CRM / Case / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class holds all the Pseudo constants that are specific for CiviCase.
38 *
39 */
40 class CRM_Case_PseudoConstant extends CRM_Core_PseudoConstant {
41
42 /**
43 * case statues
44 * @var array
45 * @static
46 */
47 static $caseStatus = array();
48
49 /**
50 * redaction rules
51 * @var array
52 * @static
53 */
54 static $redactionRule;
55
56 /**
57 * case type
58 * @var array
59 * @static
60 */
61 static $caseType = array();
62
63 /**
64 * Encounter Medium
65 * @var array
66 * @static
67 */
68 static $encounterMedium = array();
69
70 /**
71 * activity type
72 * @var array
73 * @static
74 */
75 static $activityTypeList = array();
76
77 /**
78 * case type
79 * @var array
80 * @static
81 */
82 static $caseTypePair = array();
83
84 /**
85 * Get all the case statues
86 *
87 * @access public
88 *
89 * @return array - array reference of all case statues
90 * @static
91 */
92 public static function caseStatus($column = 'label', $onlyActive = TRUE, $condition = NULL, $fresh = FALSE) {
93 $cacheKey = "{$column}_" . (int)$onlyActive;
94 if (!$condition) {
95 $condition = 'AND filter = 0';
96 }
97
98 if (!isset(self::$caseStatus[$cacheKey]) || $fresh) {
99 self::$caseStatus[$cacheKey] = CRM_Core_OptionGroup::values('case_status',
100 FALSE, FALSE, FALSE, $condition,
101 $column, $onlyActive, $fresh
102 );
103 }
104
105 return self::$caseStatus[$cacheKey];
106 }
107
108 /**
109 * Get all the redaction rules
110 *
111 * @access public
112 *
113 * @return array - array reference of all redaction rules
114 * @static
115 */
116
117 public static function redactionRule($filter = NULL) {
118 // if ( ! self::$redactionRule ) {
119 self::$redactionRule = array();
120
121 if ($filter === 0) {
122 $condition = " AND (v.filter = 0 OR v.filter IS NULL)";
123 }
124 elseif ($filter === 1) {
125 $condition = " AND v.filter = 1";
126 }
127 elseif ($filter === NULL) {
128 $condition = NULL;
129 }
130
131 self::$redactionRule = CRM_Core_OptionGroup::values('redaction_rule', TRUE, FALSE, FALSE, $condition);
132 // }
133 return self::$redactionRule;
134 }
135
136 /**
137 * Get all the case type
138 *
139 * @access public
140 *
141 * @return array - array reference of all case type
142 * @static
143 */
144 public static function caseType($column = 'label', $onlyActive = TRUE, $condition = NULL) {
145 $cacheKey = "{$column}_" . (int)$onlyActive;
146 if (!$condition) {
147 $condition = 'AND filter = 0';
148 }
149 if (!isset(self::$caseType[$cacheKey])) {
150 self::$caseType[$cacheKey] = CRM_Core_OptionGroup::values('case_type',
151 FALSE, FALSE, FALSE, $condition,
152 $column, $onlyActive
153 );
154 }
155
156 return self::$caseType[$cacheKey];
157 }
158
159 /**
160 * Get all the Encounter Medium
161 *
162 * @access public
163 *
164 * @return array - array reference of all Encounter Medium.
165 * @static
166 */
167 public static function encounterMedium($column = 'label', $onlyActive = TRUE) {
168 $cacheKey = "{$column}_" . (int)$onlyActive;
169 if (!isset(self::$encounterMedium[$cacheKey])) {
170 self::$encounterMedium[$cacheKey] = CRM_Core_OptionGroup::values('encounter_medium',
171 FALSE, FALSE, FALSE, NULL,
172 $column, $onlyActive
173 );
174 }
175
176 return self::$encounterMedium[$cacheKey];
177 }
178
179 /**
180 * Get all Activty types for the CiviCase component
181 *
182 * The static array activityType is returned
183 *
184 * @param boolean $indexName - true return activity name in array
185 * key else activity id as array key.
186 *
187 * @access public
188 * @static
189 *
190 * @return array - array reference of all activty types.
191 */
192 public static function &caseActivityType($indexName = TRUE, $all = FALSE) {
193 $cache = (int) $indexName . '_' . (int) $all;
194
195 if (!array_key_exists($cache, self::$activityTypeList)) {
196 self::$activityTypeList[$cache] = array();
197
198 $query = "
199 SELECT v.label as label ,v.value as value, v.name as name, v.description as description
200 FROM civicrm_option_value v,
201 civicrm_option_group g
202 WHERE v.option_group_id = g.id
203 AND g.name = 'activity_type'
204 AND v.is_active = 1
205 AND g.is_active = 1";
206
207 if (!$all) {
208 $componentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component',
209 'CiviCase',
210 'id', 'name'
211 );
212 $query .= " AND v.component_id = {$componentId} ";
213 }
214
215 $query .= " ORDER BY v.weight";
216
217 $dao = CRM_Core_DAO::executeQuery($query);
218
219 $activityTypes = array();
220 while ($dao->fetch()) {
221 if ($indexName) {
222 $index = $dao->name;
223 }
224 else {
225 $index = $dao->value;
226 }
227 $activityTypes[$index] = array();
228 $activityTypes[$index]['id'] = $dao->value;
229 $activityTypes[$index]['label'] = $dao->label;
230 $activityTypes[$index]['name'] = $dao->name;
231 $activityTypes[$index]['description'] = $dao->description;
232 }
233 self::$activityTypeList[$cache] = $activityTypes;
234 }
235 return self::$activityTypeList[$cache];
236 }
237
238 /**
239 * Get the associated case type name/id, given a case Id
240 *
241 * @access public
242 *
243 * @return array - array reference of all case type name/id
244 * @static
245 */
246 public static function caseTypeName($caseId, $column = 'name') {
247 if (!$caseId) {
248 return FALSE;
249 }
250
251 if (!array_key_exists($caseId, self::$caseTypePair) || empty(self::$caseTypePair[$caseId][$column])) {
252 $caseTypes = self::caseType($column);
253 $caseTypeIds = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case',
254 $caseId,
255 'case_type_id'
256 );
257 $caseTypeId = explode(CRM_Core_DAO::VALUE_SEPARATOR,
258 trim($caseTypeIds,
259 CRM_Core_DAO::VALUE_SEPARATOR
260 )
261 );
262 $caseTypeId = $caseTypeId[0];
263
264 self::$caseTypePair[$caseId][$column] = array(
265 'id' => $caseTypeId,
266 'name' => $caseTypes[$caseTypeId],
267 );
268 }
269
270 return self::$caseTypePair[$caseId][$column];
271 }
272
273 /**
274 * Flush given pseudoconstant so it can be reread from db
275 * next time it's requested.
276 *
277 * @access public
278 * @static
279 *
280 * @param boolean $name pseudoconstant to be flushed
281 *
282 */
283 public static function flush($name = 'cache') {
284 if (isset(self::$$name)) {
285 self::$$name = NULL;
286 }
287 }
288 }
289