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