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
= array("Actual Date", "Due Date");
26 $this->ifBlanks
= array();
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
= array();
61 $this->includeRules
= array();
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();
70 // Inclusion/exclusion settings
71 $includeRule = $region->getAttribute("includeRule");
72 if (empty($includeRule)) {
73 $includeRule = 'include';
75 $this->includeRules
[$regionName] = array('rule' => $includeRule);
76 if ($includeRule == 'exclude') {
77 $altRegion = $region->getAttribute("exclusionCorrespondingRegion");
78 $this->includeRules
[$regionName]['altRegion'] = $altRegion;
81 // Time component display settings
82 $includeTime = $region->getAttribute("includeTime");
83 if (empty($includeTime)) {
84 $includeTime = 'false';
86 $this->includeRules
[$regionName]['includeTime'] = $includeTime;
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). */
96 $this->regionFieldList
[$regionName][$field->nodeValue
] = $fieldCount;
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;
105 $ifBlank = $field->getAttribute("ifBlank");
106 if (!empty($ifBlank)) {
107 $this->ifBlanks
[$regionName][$field->nodeValue
] = $ifBlank;
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
;
119 $value_elements = $completionStatus->item(0)->getElementsByTagName("value");
120 $this->completionValue
= $value_elements->item(0)->nodeValue
;
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
;
135 * Check if label $n is explicitly listed in region $r in the config.
142 public function inRegion($n, $r) {
143 if (empty($this->regionFieldList
[$r])) {
147 return array_key_exists($n, $this->regionFieldList
[$r]);
152 * Should field $n be included in region $r, taking into account exclusion rules.
159 public function includeInRegion($n, $r) {
161 $rules = $this->includeRules
[$r];
162 if ($rules['rule'] == 'exclude') {
163 if (!$this->inRegion($n, $r) && !$this->inRegion($n, $rules['altRegion'])) {
167 elseif ($this->inRegion($n, $r)) {
174 * Should the time component of field $n in region $r be displayed?
181 public function includeTime($n, $r) {
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'];
190 $retval = $this->regionFieldList
[$r][$n]['includeTime'];
193 // There's a mix of strings and boolean, so convert any strings.
194 if ($retval == 'false') {
197 elseif ($retval == 'true') {
205 * Return a list of all the regions in the config file.
209 public function getRegions() {
210 return array_keys($this->regionFieldList
);
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.
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') {
226 $this->sortRegion
= $r;
227 uasort($f, array(&$this, "compareFields"));
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().
239 public function compareFields($a, $b) {
240 if (empty($this->regionFieldList
[$this->sortRegion
][$a['label']])) {
244 $x = $this->regionFieldList
[$this->sortRegion
][$a['label']];
247 if (empty($this->regionFieldList
[$this->sortRegion
][$b['label']])) {
251 $y = $this->regionFieldList
[$this->sortRegion
][$b['label']];