Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
4c6ce474 EM |
3 | /** |
4 | * Class CRM_Case_Audit_Audit | |
5 | */ | |
6a488035 TO |
6 | class 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 | ||
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"); | |
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( | |
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 | ||
4c6ce474 EM |
188 | /** |
189 | * @param $a | |
190 | * @param $b | |
191 | * | |
192 | * @return int | |
193 | */ | |
6a488035 TO |
194 | public function compareActivities($a, $b) { |
195 | // This should work | |
196 | foreach ($this->auditConfig->getSortByLabels() as $label) { | |
197 | $aval .= empty($a['sortValues']) ? "" : (empty($a['sortValues'][$label]) ? "" : $a['sortValues'][$label]); | |
198 | $bval .= empty($b['sortValues']) ? "" : (empty($b['sortValues'][$label]) ? "" : $b['sortValues'][$label]); | |
199 | } | |
200 | ||
201 | if ($aval < $bval) { | |
202 | return - 1; | |
203 | } | |
204 | elseif ($aval > $bval) { | |
205 | return 1; | |
206 | } | |
207 | else { | |
208 | return 0; | |
209 | } | |
210 | } | |
211 | ||
4c6ce474 EM |
212 | /** |
213 | * @param $xmlString | |
100fef9d CW |
214 | * @param int $clientID |
215 | * @param int $caseID | |
4c6ce474 EM |
216 | * @param bool $printReport |
217 | * | |
218 | * @return mixed | |
219 | */ | |
6a488035 | 220 | static |
00be9182 | 221 | public function run($xmlString, $clientID, $caseID, $printReport = FALSE) { |
6a488035 | 222 | /* |
e547f744 TO |
223 | $fh = fopen('C:/temp/audit2.xml', 'w'); |
224 | fwrite($fh, $xmlString); | |
225 | fclose($fh); | |
226 | */ | |
6a488035 TO |
227 | |
228 | $audit = new CRM_Case_Audit_Audit($xmlString, 'audit.conf.xml'); | |
229 | $activities = $audit->getActivities($printReport); | |
230 | ||
231 | $template = CRM_Core_Smarty::singleton(); | |
232 | $template->assign_by_ref('activities', $activities); | |
233 | ||
234 | if ($printReport) { | |
235 | $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i')); | |
236 | $template->assign('reportDate', $reportDate); | |
237 | $contents = $template->fetch('CRM/Case/Audit/Report.tpl'); | |
238 | } | |
239 | else { | |
240 | $contents = $template->fetch('CRM/Case/Audit/Audit.tpl'); | |
241 | } | |
242 | return $contents; | |
243 | } | |
244 | } |