Merge pull request #19464 from eileenmcnaughton/menu
[civicrm-core.git] / CRM / Case / Audit / AuditConfig.php
CommitLineData
6a488035
TO
1<?php
2
4c6ce474
EM
3/**
4 * Class CRM_Case_Audit_AuditConfig
5 */
6a488035
TO
6class CRM_Case_Audit_AuditConfig {
7 private $filename;
8 private $completionLabel;
9 private $completionValue;
10 private $sortByLabels;
11 private $regionFieldList;
12 private $includeRules;
13 private $sortRegion;
14 private $ifBlanks;
15
4c6ce474 16 /**
100fef9d 17 * @param string $filename
4c6ce474 18 */
6a488035
TO
19 public function __construct($filename) {
20 $this->filename = $filename;
21
22 // set some defaults
23 $this->completionLabel = "Status";
24 $this->completionValue = "Completed";
be2fb01f
CW
25 $this->sortByLabels = ["Actual Date", "Due Date"];
26 $this->ifBlanks = [];
6a488035
TO
27
28 $this->loadConfig();
29 }
30
4c6ce474
EM
31 /**
32 * @return string
33 */
6a488035
TO
34 public function getCompletionValue() {
35 return $this->completionValue;
36 }
37
4c6ce474
EM
38 /**
39 * @return string
40 */
6a488035
TO
41 public function getCompletionLabel() {
42 return $this->completionLabel;
43 }
44
4c6ce474
EM
45 /**
46 * @return array
47 */
6a488035
TO
48 public function getSortByLabels() {
49 return $this->sortByLabels;
50 }
51
4c6ce474
EM
52 /**
53 * @return array
54 */
6a488035
TO
55 public function getIfBlanks() {
56 return $this->ifBlanks;
57 }
58
59 public function loadConfig() {
be2fb01f
CW
60 $this->regionFieldList = [];
61 $this->includeRules = [];
6a488035
TO
62
63 $doc = new DOMDocument();
f9857c59
JP
64 $xmlString = file_get_contents(dirname(__FILE__) . '/' . $this->filename);
65 $load = $doc->loadXML($xmlString);
6498e0de 66 if ($load) {
6a488035
TO
67 $regions = $doc->getElementsByTagName("region");
68 foreach ($regions as $region) {
69 $regionName = $region->getAttribute("name");
be2fb01f 70 $this->regionFieldList[$regionName] = [];
6a488035
TO
71
72 // Inclusion/exclusion settings
73 $includeRule = $region->getAttribute("includeRule");
74 if (empty($includeRule)) {
75 $includeRule = 'include';
76 }
be2fb01f 77 $this->includeRules[$regionName] = ['rule' => $includeRule];
6a488035
TO
78 if ($includeRule == 'exclude') {
79 $altRegion = $region->getAttribute("exclusionCorrespondingRegion");
80 $this->includeRules[$regionName]['altRegion'] = $altRegion;
81 }
82
83 // Time component display settings
84 $includeTime = $region->getAttribute("includeTime");
85 if (empty($includeTime)) {
86 $includeTime = 'false';
87 }
88 $this->includeRules[$regionName]['includeTime'] = $includeTime;
89
90 $fieldCount = 0;
91 $fields = $region->getElementsByTagName("field");
92 foreach ($fields as $field) {
93 /* Storing them this way, which is backwards to how you might normally
94 have arrays with a numeric key and a text value, ends up making things better
95 in the other functions, in particular the sorting and also inRegion should end
96 up being more efficient (searching for a key instead of a value). */
97
98 $this->regionFieldList[$regionName][$field->nodeValue] = $fieldCount;
99
100 // Field-level overrides of time component display settings
101 $includeTime = $field->getAttribute("includeTime");
102 if (!empty($includeTime)) {
103 $this->regionFieldList[$regionName][$field->nodeValue]['includeTime'] = $includeTime;
104 }
105
106 // ifBlank attribute
107 $ifBlank = $field->getAttribute("ifBlank");
108 if (!empty($ifBlank)) {
109 $this->ifBlanks[$regionName][$field->nodeValue] = $ifBlank;
110 }
111
112 $fieldCount++;
113 }
114 }
115
116 $completionStatus = $doc->getElementsByTagName("completionStatus");
117 if (!empty($completionStatus)) {
118 $label_elements = $completionStatus->item(0)->getElementsByTagName("label");
119 $this->completionLabel = $label_elements->item(0)->nodeValue;
120
121 $value_elements = $completionStatus->item(0)->getElementsByTagName("value");
122 $this->completionValue = $value_elements->item(0)->nodeValue;
123 }
124
125 $sortElement = $doc->getElementsByTagName("sortByLabels");
126 if (!empty($sortElement)) {
be2fb01f 127 $this->sortByLabels = [];
6a488035
TO
128 $label_elements = $sortElement->item(0)->getElementsByTagName("label");
129 foreach ($label_elements as $ele) {
130 $this->sortByLabels[] = $ele->nodeValue;
131 }
132 }
133 }
134 }
135
4c6ce474 136 /**
76e7a76c
CW
137 * Check if label $n is explicitly listed in region $r in the config.
138 *
4c6ce474
EM
139 * @param $n
140 * @param $r
141 *
142 * @return bool
143 */
6a488035
TO
144 public function inRegion($n, $r) {
145 if (empty($this->regionFieldList[$r])) {
146 return FALSE;
147 }
148 else {
149 return array_key_exists($n, $this->regionFieldList[$r]);
150 }
151 }
152
4c6ce474 153 /**
76e7a76c
CW
154 * Should field $n be included in region $r, taking into account exclusion rules.
155 *
4c6ce474
EM
156 * @param $n
157 * @param $r
158 *
159 * @return bool
160 */
6a488035
TO
161 public function includeInRegion($n, $r) {
162 $add_it = FALSE;
163 $rules = $this->includeRules[$r];
164 if ($rules['rule'] == 'exclude') {
165 if (!$this->inRegion($n, $r) && !$this->inRegion($n, $rules['altRegion'])) {
166 $add_it = TRUE;
167 }
168 }
169 elseif ($this->inRegion($n, $r)) {
170 $add_it = TRUE;
171 }
172 return $add_it;
173 }
174
4c6ce474 175 /**
76e7a76c
CW
176 * Should the time component of field $n in region $r be displayed?
177 *
4c6ce474
EM
178 * @param $n
179 * @param $r
180 *
181 * @return bool
182 */
6a488035
TO
183 public function includeTime($n, $r) {
184 $retval = FALSE;
185 if (empty($this->regionFieldList[$r][$n]['includeTime'])) {
186 // No field-level override, so look at the region's settings
187 if (!empty($this->includeRules[$r]['includeTime'])) {
188 $retval = $this->includeRules[$r]['includeTime'];
189 }
190 }
191 else {
192 $retval = $this->regionFieldList[$r][$n]['includeTime'];
193 }
194
195 // There's a mix of strings and boolean, so convert any strings.
196 if ($retval == 'false') {
197 $retval = FALSE;
198 }
199 elseif ($retval == 'true') {
200 $retval = TRUE;
201 }
202
203 return $retval;
204 }
205
4c6ce474 206 /**
76e7a76c
CW
207 * Return a list of all the regions in the config file.
208 *
4c6ce474
EM
209 * @return array
210 */
6a488035
TO
211 public function getRegions() {
212 return array_keys($this->regionFieldList);
213 }
214
76e7a76c 215 /**
6a488035
TO
216 * Sort a group of fields for a given region according to the order in the config.
217 * The array to be sorted should have elements that have a member with a key of 'label', and the value should be the field label.
76e7a76c 218 *
4c6ce474
EM
219 * @param $f
220 * @param $r
221 */
6a488035
TO
222 public function sort(&$f, $r) {
223 // For exclusion-type regions, there's nothing to do, because we won't have been given any ordering.
224 if ($this->includeRules[$r]['rule'] == 'exclude') {
225 return;
226 }
227
228 $this->sortRegion = $r;
229 uasort($f, array(&$this, "compareFields"));
230 }
231
76e7a76c 232 /**
6a488035
TO
233 * This is intended to be called as a sort callback function, returning whether a field in a region comes before or after another one.
234 * See also PHP's usort().
76e7a76c 235 *
4c6ce474
EM
236 * @param $a
237 * @param $b
238 *
239 * @return int
240 */
6a488035
TO
241 public function compareFields($a, $b) {
242 if (empty($this->regionFieldList[$this->sortRegion][$a['label']])) {
243 $x = 0;
244 }
245 else {
246 $x = $this->regionFieldList[$this->sortRegion][$a['label']];
247 }
248
249 if (empty($this->regionFieldList[$this->sortRegion][$b['label']])) {
250 $y = 0;
251 }
252 else {
253 $y = $this->regionFieldList[$this->sortRegion][$b['label']];
254 }
255
256 return $x - $y;
257 }
96025800 258
6a488035 259}