4 * Class CRM_Case_Audit_AuditConfig
6 class CRM_Case_Audit_AuditConfig
{
8 private $completionLabel;
9 private $completionValue;
10 private $sortByLabels;
11 private $regionFieldList;
12 private $includeRules;
17 * @param string $filename
19 public function __construct($filename) {
20 $this->filename
= $filename;
23 $this->completionLabel
= "Status";
24 $this->completionValue
= "Completed";
25 $this->sortByLabels
= ["Actual Date", "Due Date"];
34 public function getCompletionValue() {
35 return $this->completionValue
;
41 public function getCompletionLabel() {
42 return $this->completionLabel
;
48 public function getSortByLabels() {
49 return $this->sortByLabels
;
55 public function getIfBlanks() {
56 return $this->ifBlanks
;
59 public function loadConfig() {
60 $this->regionFieldList
= [];
61 $this->includeRules
= [];
63 $doc = new DOMDocument();
64 $xmlString = file_get_contents(dirname(__FILE__
) . '/' . $this->filename
);
65 $load = $doc->loadXML($xmlString);
67 $regions = $doc->getElementsByTagName("region");
68 foreach ($regions as $region) {
69 $regionName = $region->getAttribute("name");
70 $this->regionFieldList
[$regionName] = [];
72 // Inclusion/exclusion settings
73 $includeRule = $region->getAttribute("includeRule");
74 if (empty($includeRule)) {
75 $includeRule = 'include';
77 $this->includeRules
[$regionName] = ['rule' => $includeRule];
78 if ($includeRule == 'exclude') {
79 $altRegion = $region->getAttribute("exclusionCorrespondingRegion");
80 $this->includeRules
[$regionName]['altRegion'] = $altRegion;
83 // Time component display settings
84 $includeTime = $region->getAttribute("includeTime");
85 if (empty($includeTime)) {
86 $includeTime = 'false';
88 $this->includeRules
[$regionName]['includeTime'] = $includeTime;
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). */
98 $this->regionFieldList
[$regionName][$field->nodeValue
] = $fieldCount;
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;
107 $ifBlank = $field->getAttribute("ifBlank");
108 if (!empty($ifBlank)) {
109 $this->ifBlanks
[$regionName][$field->nodeValue
] = $ifBlank;
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
;
121 $value_elements = $completionStatus->item(0)->getElementsByTagName("value");
122 $this->completionValue
= $value_elements->item(0)->nodeValue
;
125 $sortElement = $doc->getElementsByTagName("sortByLabels");
126 if (!empty($sortElement)) {
127 $this->sortByLabels
= [];
128 $label_elements = $sortElement->item(0)->getElementsByTagName("label");
129 foreach ($label_elements as $ele) {
130 $this->sortByLabels
[] = $ele->nodeValue
;
137 * Check if label $n is explicitly listed in region $r in the config.
144 public function inRegion($n, $r) {
145 if (empty($this->regionFieldList
[$r])) {
149 return array_key_exists($n, $this->regionFieldList
[$r]);
154 * Should field $n be included in region $r, taking into account exclusion rules.
161 public function includeInRegion($n, $r) {
163 $rules = $this->includeRules
[$r];
164 if ($rules['rule'] == 'exclude') {
165 if (!$this->inRegion($n, $r) && !$this->inRegion($n, $rules['altRegion'])) {
169 elseif ($this->inRegion($n, $r)) {
176 * Should the time component of field $n in region $r be displayed?
183 public function includeTime($n, $r) {
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'];
192 $retval = $this->regionFieldList
[$r][$n]['includeTime'];
195 // There's a mix of strings and boolean, so convert any strings.
196 if ($retval == 'false') {
199 elseif ($retval == 'true') {
207 * Return a list of all the regions in the config file.
211 public function getRegions() {
212 return array_keys($this->regionFieldList
);
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.
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') {
228 $this->sortRegion
= $r;
229 uasort($f, array(&$this, "compareFields"));
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().
241 public function compareFields($a, $b) {
242 if (empty($this->regionFieldList
[$this->sortRegion
][$a['label']])) {
246 $x = $this->regionFieldList
[$this->sortRegion
][$a['label']];
249 if (empty($this->regionFieldList
[$this->sortRegion
][$b['label']])) {
253 $y = $this->regionFieldList
[$this->sortRegion
][$b['label']];