Merge branch 4.5 into master
[civicrm-core.git] / CRM / Case / Audit / Audit.php
1 <?php
2
3 /**
4 * Class CRM_Case_Audit_Audit
5 */
6 class CRM_Case_Audit_Audit {
7 private $auditConfig;
8 private $xmlString;
9
10 /**
11 * @param $xmlString
12 * @param string $confFilename
13 */
14 public function __construct($xmlString, $confFilename) {
15 $this->xmlString = $xmlString;
16 $this->auditConfig = new CRM_Case_Audit_AuditConfig($confFilename);
17 }
18
19 /**
20 * @param bool $printReport
21 *
22 * @return array
23 */
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
52 $completed = FALSE;
53 $sortValues = array('1970-01-01');
54 $category = '';
55 $fieldindex = 1;
56 $fields = $activity->getElementsByTagName("Field");
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");
68 if (!empty($category_elements->length)) {
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(
102 'Activity Type', 'Status'))) {
103 $caseActivities[$activityindex][$fieldindex] = array();
104 $caseActivities[$activityindex][$fieldindex]['label'] = $label;
105 $caseActivities[$activityindex][$fieldindex]['datatype'] = $datatype;
106 $caseActivities[$activityindex][$fieldindex]['value'] = $value;
107 }
108 else {
109 $activityStatusType[$activityindex][$fieldindex] = array();
110 $activityStatusType[$activityindex][$fieldindex]['label'] = $label;
111 $activityStatusType[$activityindex][$fieldindex]['datatype'] = $datatype;
112 $activityStatusType[$activityindex][$fieldindex]['value'] = $value;
113 }
114 }
115 }
116 }
117
118 $fieldindex++;
119 }
120
121 if ($printReport) {
122 $caseActivities[$activityindex] = CRM_Utils_Array::crmArrayMerge($activityStatusType[$activityindex], $caseActivities[$activityindex]);
123 $caseActivities[$activityindex]['sortValues'] = $sortValues;
124 }
125
126 if ($includeAll || !$completed) {
127 $retval[$activityindex]['completed'] = $completed;
128 $retval[$activityindex]['category'] = $category;
129 $retval[$activityindex]['sortValues'] = $sortValues;
130
131 // Now sort the fields based on the order in the config file.
132 foreach ($regionList as $region) {
133 $this->auditConfig->sort($retval[$activityindex][$region], $region);
134 }
135
136 $retval[$activityindex]['editurl'] = $activity->getElementsByTagName("EditURL")->item(0)->nodeValue;
137
138 // If there are any fields with ifBlank specified, replace their values.
139 // 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.
140 foreach ($regionList as $region) {
141 foreach ($retval[$activityindex][$region] as & $v) {
142 $vlabel = $v['label'];
143 if (trim($v['value']) == '' && !empty($ifBlanks[$region][$vlabel])) {
144 if (!empty($ifBlankReplacements[$ifBlanks[$region][$vlabel]])) {
145 $v['value'] = $ifBlankReplacements[$ifBlanks[$region][$vlabel]];
146 }
147 }
148 }
149 unset($v);
150 }
151
152 $activityindex++;
153 }
154 else {
155 /* This is a little bit inefficient, but the alternative is to do two passes
156 because we don't know until we've examined all the field values whether the activity
157 is completed, since the field that determines it and its value is configurable,
158 so either way isn't ideal. */
159
160 unset($retval[$activityindex]);
161 unset($caseActivities[$activityindex]);
162 }
163 }
164
165 if ($printReport) {
166 @uasort($caseActivities, array($this, "compareActivities"));
167 }
168 else {
169 @uasort($retval, array($this, "compareActivities"));
170 }
171 }
172
173 if ($printReport) {
174 return $caseActivities;
175 }
176 else {
177 return $retval;
178 }
179 }
180
181 /* compareActivities
182 *
183 * This is intended to be called as a sort callback function, returning whether an activity's date is earlier or later than another's.
184 * The type of date to use is specified in the config.
185 */
186
187 /**
188 * @param $a
189 * @param $b
190 *
191 * @return int
192 */
193 public function compareActivities($a, $b) {
194 // This should work
195 foreach ($this->auditConfig->getSortByLabels() as $label) {
196 $aval .= empty($a['sortValues']) ? "" : (empty($a['sortValues'][$label]) ? "" : $a['sortValues'][$label]);
197 $bval .= empty($b['sortValues']) ? "" : (empty($b['sortValues'][$label]) ? "" : $b['sortValues'][$label]);
198 }
199
200 if ($aval < $bval) {
201 return - 1;
202 }
203 elseif ($aval > $bval) {
204 return 1;
205 }
206 else {
207 return 0;
208 }
209 }
210
211 /**
212 * @param $xmlString
213 * @param int $clientID
214 * @param int $caseID
215 * @param bool $printReport
216 *
217 * @return mixed
218 */
219 static
220 public function run($xmlString, $clientID, $caseID, $printReport = FALSE) {
221 /*
222 $fh = fopen('C:/temp/audit2.xml', 'w');
223 fwrite($fh, $xmlString);
224 fclose($fh);
225 */
226
227 $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml');
228 $activities = $audit->getActivities($printReport);
229
230 $template = CRM_Core_Smarty::singleton();
231 $template->assign_by_ref('activities', $activities);
232
233 if ($printReport) {
234 $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i'));
235 $template->assign('reportDate', $reportDate);
236 $contents = $template->fetch('CRM/Case/Audit/Report.tpl');
237 }
238 else {
239 $contents = $template->fetch('CRM/Case/Audit/Audit.tpl');
240 }
241 return $contents;
242 }
243 }