Merge pull request #4897 from totten/master-cleanup2
[civicrm-core.git] / CRM / Case / Audit / Audit.php
CommitLineData
6a488035
TO
1<?php
2
4c6ce474
EM
3/**
4 * Class CRM_Case_Audit_Audit
5 */
6a488035
TO
6class CRM_Case_Audit_Audit {
7 private $auditConfig;
8 private $xmlString;
9
4c6ce474
EM
10 /**
11 * @param $xmlString
100fef9d 12 * @param string $confFilename
4c6ce474 13 */
6a488035
TO
14 public function __construct($xmlString, $confFilename) {
15 $this->xmlString = $xmlString;
16 $this->auditConfig = new CRM_Case_Audit_AuditConfig($confFilename);
17 }
18
4c6ce474
EM
19 /**
20 * @param bool $printReport
21 *
22 * @return array
23 */
6a488035
TO
24 public function getActivities($printReport = FALSE) {
25 $retval = array();
26
27 /*
28 * Loop through the activities in the file and add them to the appropriate region array.
29 */
30
31 $doc = new DOMDocument();
32
33 if ($doc->loadXML($this->xmlString)) {
34 $regionList = $this->auditConfig->getRegions();
35
36 $ifBlanks = $this->auditConfig->getIfBlanks();
37
38 $includeAll = $doc->getElementsByTagName("IncludeActivities")->item(0)->nodeValue;
39 $includeAll = ($includeAll == 'All');
40
41 $activityindex = 0;
42 $activityList = $doc->getElementsByTagName("Activity");
43
44 $caseActivities = array();
45 $activityStatusType = array();
46
47 foreach ($activityList as $activity) {
48 $retval[$activityindex] = array();
49
50 $ifBlankReplacements = array();
51
353ffa53 52 $completed = FALSE;
6a488035 53 $sortValues = array('1970-01-01');
353ffa53 54 $category = '';
6a488035 55 $fieldindex = 1;
353ffa53 56 $fields = $activity->getElementsByTagName("Field");
6a488035
TO
57 foreach ($fields as $field) {
58 $datatype_elements = $field->getElementsByTagName("Type");
59 $datatype = $datatype_elements->item(0)->nodeValue;
60
61 $label_elements = $field->getElementsByTagName("Label");
62 $label = $label_elements->item(0)->nodeValue;
63
64 $value_elements = $field->getElementsByTagName("Value");
65 $value = $value_elements->item(0)->nodeValue;
66
67 $category_elements = $field->getElementsByTagName("Category");
916bdc36 68 if (!empty($category_elements->length)) {
6a488035
TO
69 $category = $category_elements->item(0)->nodeValue;
70 }
71
72 // Based on the config file, does this field's label and value indicate a completed activity?
73 if ($label == $this->auditConfig->getCompletionLabel() && $value == $this->auditConfig->getCompletionValue()) {
74 $completed = TRUE;
75 }
76
77 // Based on the config file, does this field's label match the one to use for sorting activities?
78 if (in_array($label, $this->auditConfig->getSortByLabels())) {
79 $sortValues[$label] = $value;
80 }
81
82 foreach ($regionList as $region) {
83 // Based on the config file, is this field a potential replacement for another?
84 if (!empty($ifBlanks[$region])) {
85 if (in_array($label, $ifBlanks[$region])) {
86 $ifBlankReplacements[$label] = $value;
87 }
88 }
89
90 if ($this->auditConfig->includeInRegion($label, $region)) {
91 $retval[$activityindex][$region][$fieldindex] = array();
92 $retval[$activityindex][$region][$fieldindex]['label'] = $label;
93 $retval[$activityindex][$region][$fieldindex]['datatype'] = $datatype;
94 $retval[$activityindex][$region][$fieldindex]['value'] = $value;
95 if ($datatype == 'Date') {
96 $retval[$activityindex][$region][$fieldindex]['includeTime'] = $this->auditConfig->includeTime($label, $region);
97 }
98
99 //CRM-4570
100 if ($printReport) {
101 if (!in_array($label, array(
353ffa53
TO
102 'Activity Type',
103 'Status'
104 ))
105 ) {
6a488035
TO
106 $caseActivities[$activityindex][$fieldindex] = array();
107 $caseActivities[$activityindex][$fieldindex]['label'] = $label;
108 $caseActivities[$activityindex][$fieldindex]['datatype'] = $datatype;
109 $caseActivities[$activityindex][$fieldindex]['value'] = $value;
110 }
111 else {
112 $activityStatusType[$activityindex][$fieldindex] = array();
113 $activityStatusType[$activityindex][$fieldindex]['label'] = $label;
114 $activityStatusType[$activityindex][$fieldindex]['datatype'] = $datatype;
115 $activityStatusType[$activityindex][$fieldindex]['value'] = $value;
116 }
117 }
118 }
119 }
120
121 $fieldindex++;
122 }
123
124 if ($printReport) {
125 $caseActivities[$activityindex] = CRM_Utils_Array::crmArrayMerge($activityStatusType[$activityindex], $caseActivities[$activityindex]);
126 $caseActivities[$activityindex]['sortValues'] = $sortValues;
127 }
128
129 if ($includeAll || !$completed) {
130 $retval[$activityindex]['completed'] = $completed;
131 $retval[$activityindex]['category'] = $category;
132 $retval[$activityindex]['sortValues'] = $sortValues;
133
134 // Now sort the fields based on the order in the config file.
135 foreach ($regionList as $region) {
136 $this->auditConfig->sort($retval[$activityindex][$region], $region);
137 }
138
139 $retval[$activityindex]['editurl'] = $activity->getElementsByTagName("EditURL")->item(0)->nodeValue;
140
141 // If there are any fields with ifBlank specified, replace their values.
142 // We need to do this as a second pass because if we do it while looping through fields we might not have come across the field we need yet.
143 foreach ($regionList as $region) {
144 foreach ($retval[$activityindex][$region] as & $v) {
145 $vlabel = $v['label'];
146 if (trim($v['value']) == '' && !empty($ifBlanks[$region][$vlabel])) {
147 if (!empty($ifBlankReplacements[$ifBlanks[$region][$vlabel]])) {
148 $v['value'] = $ifBlankReplacements[$ifBlanks[$region][$vlabel]];
149 }
150 }
151 }
152 unset($v);
153 }
154
155 $activityindex++;
156 }
157 else {
158 /* This is a little bit inefficient, but the alternative is to do two passes
159 because we don't know until we've examined all the field values whether the activity
160 is completed, since the field that determines it and its value is configurable,
161 so either way isn't ideal. */
162
163 unset($retval[$activityindex]);
164 unset($caseActivities[$activityindex]);
165 }
166 }
167
168 if ($printReport) {
169 @uasort($caseActivities, array($this, "compareActivities"));
170 }
171 else {
172 @uasort($retval, array($this, "compareActivities"));
173 }
174 }
175
176 if ($printReport) {
177 return $caseActivities;
178 }
179 else {
180 return $retval;
181 }
182 }
183
184 /* compareActivities
185 *
186 * This is intended to be called as a sort callback function, returning whether an activity's date is earlier or later than another's.
187 * The type of date to use is specified in the config.
6a488035
TO
188 */
189
4c6ce474
EM
190 /**
191 * @param $a
192 * @param $b
193 *
194 * @return int
195 */
6a488035
TO
196 public function compareActivities($a, $b) {
197 // This should work
198 foreach ($this->auditConfig->getSortByLabels() as $label) {
199 $aval .= empty($a['sortValues']) ? "" : (empty($a['sortValues'][$label]) ? "" : $a['sortValues'][$label]);
200 $bval .= empty($b['sortValues']) ? "" : (empty($b['sortValues'][$label]) ? "" : $b['sortValues'][$label]);
201 }
202
203 if ($aval < $bval) {
353ffa53 204 return -1;
6a488035
TO
205 }
206 elseif ($aval > $bval) {
207 return 1;
208 }
209 else {
210 return 0;
211 }
212 }
213
4c6ce474
EM
214 /**
215 * @param $xmlString
100fef9d
CW
216 * @param int $clientID
217 * @param int $caseID
4c6ce474
EM
218 * @param bool $printReport
219 *
220 * @return mixed
221 */
6a488035 222 static
00be9182 223 public function run($xmlString, $clientID, $caseID, $printReport = FALSE) {
6a488035 224 /*
e547f744
TO
225 $fh = fopen('C:/temp/audit2.xml', 'w');
226 fwrite($fh, $xmlString);
227 fclose($fh);
228 */
6a488035
TO
229
230 $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml');
231 $activities = $audit->getActivities($printReport);
232
233 $template = CRM_Core_Smarty::singleton();
234 $template->assign_by_ref('activities', $activities);
235
236 if ($printReport) {
237 $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i'));
238 $template->assign('reportDate', $reportDate);
239 $contents = $template->fetch('CRM/Case/Audit/Report.tpl');
240 }
241 else {
242 $contents = $template->fetch('CRM/Case/Audit/Audit.tpl');
243 }
244 return $contents;
245 }
246}