Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Report / Form / Case / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Case_Summary extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38 protected $_relField = FALSE;
39 protected $_exposeContactID = FALSE;
40
41 /**
42 */
43 /**
44 */
45 public function __construct() {
46 $this->case_types = CRM_Case_PseudoConstant::caseType();
47 $this->case_statuses = CRM_Core_OptionGroup::values('case_status');
48 $rels = CRM_Core_PseudoConstant::relationshipType();
49 foreach ($rels as $relid => $v) {
50 $this->rel_types[$relid] = $v['label_b_a'];
51 }
52
53 $this->deleted_labels = array(
54 '' => ts('- select -'),
55 0 => ts('No'),
56 1 => ts('Yes'),
57 );
58
59 $this->_columns = array(
60 'civicrm_c2' => array(
61 'dao' => 'CRM_Contact_DAO_Contact',
62 'fields' => array(
63 'client_name' => array(
64 'name' => 'sort_name',
65 'title' => ts('Client'),
66 'required' => TRUE,
67 ),
68 'id' => array(
69 'no_display' => TRUE,
70 'required' => TRUE,
71 ),
72 ),
73 ),
74 'civicrm_case' => array(
75 'dao' => 'CRM_Case_DAO_Case',
76 'fields' => array(
77 'id' => array(
78 'title' => ts('Case ID'),
79 'required' => TRUE,
80 ),
81 'subject' => array(
82 'title' => ts('Case Subject'),
83 'default' => TRUE,
84 ),
85 'status_id' => array(
86 'title' => ts('Status'),
87 'default' => TRUE,
88 ),
89 'case_type_id' => array(
90 'title' => ts('Case Type'),
91 'default' => TRUE,
92 ),
93 'start_date' => array(
94 'title' => ts('Start Date'),
95 'default' => TRUE,
96 'type' => CRM_Utils_Type::T_DATE,
97 ),
98 'end_date' => array(
99 'title' => ts('End Date'),
100 'default' => TRUE,
101 'type' => CRM_Utils_Type::T_DATE,
102 ),
103 'duration' => array(
104 'title' => ts('Duration (Days)'),
105 'default' => FALSE,
106 ),
107 'is_deleted' => array(
108 'title' => ts('Deleted?'),
109 'default' => FALSE,
110 'type' => CRM_Utils_Type::T_INT,
111 ),
112 ),
113 'filters' => array(
114 'start_date' => array(
115 'title' => ts('Start Date'),
116 'operatorType' => CRM_Report_Form::OP_DATE,
117 'type' => CRM_Utils_Type::T_DATE,
118 ),
119 'end_date' => array(
120 'title' => ts('End Date'),
121 'operatorType' => CRM_Report_Form::OP_DATE,
122 'type' => CRM_Utils_Type::T_DATE,
123 ),
124 'case_type_id' => array(
125 'title' => ts('Case Type'),
126 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
127 'options' => $this->case_types,
128 ),
129 'status_id' => array(
130 'title' => ts('Status'),
131 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
132 'options' => $this->case_statuses,
133 ),
134 'is_deleted' => array(
135 'title' => ts('Deleted?'),
136 'type' => CRM_Report_Form::OP_INT,
137 'operatorType' => CRM_Report_Form::OP_SELECT,
138 'options' => $this->deleted_labels,
139 'default' => 0,
140 ),
141 ),
142 ),
143 'civicrm_contact' => array(
144 'dao' => 'CRM_Contact_DAO_Contact',
145 'fields' => array(
146 'sort_name' => array(
147 'title' => ts('Staff Member'),
148 'default' => TRUE,
149 ),
150 ),
151 'filters' => array(
152 'sort_name' => array(
153 'title' => ts('Staff Member'),
154 ),
155 ),
156 ),
157 'civicrm_relationship' => array(
158 'dao' => 'CRM_Contact_DAO_Relationship',
159 'filters' => array(
160 'relationship_type_id' => array(
161 'title' => ts('Staff Relationship'),
162 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
163 'options' => $this->rel_types,
164 ),
165 ),
166 ),
167 'civicrm_relationship_type' => array(
168 'dao' => 'CRM_Contact_DAO_RelationshipType',
169 'fields' => array(
170 'label_b_a' => array(
171 'title' => ts('Relationship'),
172 'default' => TRUE,
173 ),
174 ),
175 ),
176 'civicrm_case_contact' => array(
177 'dao' => 'CRM_Case_DAO_CaseContact',
178 ),
179 );
180
181 parent::__construct();
182 }
183
184 public function preProcess() {
185 parent::preProcess();
186 }
187
188 public function select() {
189 $select = array();
190 $this->_columnHeaders = array();
191 foreach ($this->_columns as $tableName => $table) {
192 if (array_key_exists('fields', $table)) {
193 foreach ($table['fields'] as $fieldName => $field) {
194 if (!empty($field['required']) ||
195 !empty($this->_params['fields'][$fieldName])
196 ) {
197
198 if ($tableName == 'civicrm_relationship_type') {
199 $this->_relField = TRUE;
200 }
201
202 if ($fieldName == 'duration') {
203 $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}";
204 }
205 else {
206 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
207 }
208 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
209 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
210 }
211 }
212 }
213 }
214
215 $this->_select = "SELECT " . implode(', ', $select) . " ";
216 }
217
218 /**
219 * @param $fields
220 * @param $files
221 * @param $self
222 *
223 * @return array
224 */
225 public static function formRule($fields, $files, $self) {
226 $errors = $grouping = array();
227 if (empty($fields['relationship_type_id_value']) &&
228 (array_key_exists('sort_name', $fields['fields']) ||
229 array_key_exists('label_b_a', $fields['fields']))
230 ) {
231 $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.');
232 }
233 if ((!empty($fields['relationship_type_id_value']) ||
234 !empty($fields['sort_name_value'])) &&
235 (!array_key_exists('sort_name', $fields['fields']) ||
236 !array_key_exists('label_b_a', $fields['fields']))
237 ) {
238 $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.');
239 }
240 return $errors;
241 }
242
243 public function from() {
244
245 $cc = $this->_aliases['civicrm_case'];
246 $c = $this->_aliases['civicrm_contact'];
247 $c2 = $this->_aliases['civicrm_c2'];
248 $cr = $this->_aliases['civicrm_relationship'];
249 $crt = $this->_aliases['civicrm_relationship_type'];
250 $ccc = $this->_aliases['civicrm_case_contact'];
251
252 if ($this->_relField) {
253 $this->_from = "
254 FROM civicrm_contact $c
255 inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b
256 inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id
257 inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id
258 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
259 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
260 ";
261 }
262 else {
263 $this->_from = "
264 FROM civicrm_contact $c, civicrm_case $cc
265 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
266 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
267 ";
268 }
269 }
270
271 public function where() {
272 $clauses = array();
273 $this->_having = '';
274 foreach ($this->_columns as $tableName => $table) {
275 if (array_key_exists('filters', $table)) {
276 foreach ($table['filters'] as $fieldName => $field) {
277 $clause = NULL;
278 if (CRM_Utils_Array::value("operatorType", $field) & CRM_Report_Form::OP_DATE
279 ) {
280 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
281 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
282 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
283
284 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to,
285 CRM_Utils_Array::value('type', $field)
286 );
287 }
288 else {
289
290 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
291 if ($fieldName == 'case_type_id') {
292 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
293 if (!empty($value)) {
294 $clause = "( {$field['dbAlias']} REGEXP '[[:<:]]" .
295 implode('[[:>:]]|[[:<:]]', $value) . "[[:>:]]' )";
296 }
297 $op = NULL;
298 }
299
300 if ($op) {
301 $clause = $this->whereClause($field,
302 $op,
303 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
304 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
305 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
306 );
307 }
308 }
309
310 if (!empty($clause)) {
311 $clauses[] = $clause;
312 }
313 }
314 }
315 }
316
317 if (empty($clauses)) {
318 $this->_where = "WHERE ( 1 ) ";
319 }
320 else {
321 $this->_where = "WHERE " . implode(' AND ', $clauses);
322 }
323 }
324
325 public function groupBy() {
326 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_c2']}.id";
327 }
328
329 public function postProcess() {
330
331 $this->beginPostProcess();
332
333 $sql = $this->buildQuery(TRUE);
334
335 $rows = $graphRows = array();
336 $this->buildRows($sql, $rows);
337
338 $this->formatDisplay($rows);
339 $this->doTemplateAssignment($rows);
340 $this->endPostProcess($rows);
341 }
342
343 /**
344 * Alter display of rows.
345 *
346 * Iterate through the rows retrieved via SQL and make changes for display purposes,
347 * such as rendering contacts as links.
348 *
349 * @param array $rows
350 * Rows generated by SQL, with an array for each row.
351 */
352 public function alterDisplay(&$rows) {
353 $entryFound = FALSE;
354 foreach ($rows as $rowNum => $row) {
355 if (array_key_exists('civicrm_case_status_id', $row)) {
356 if ($value = $row['civicrm_case_status_id']) {
357 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses[$value];
358 $entryFound = TRUE;
359 }
360 }
361
362 if (array_key_exists('civicrm_case_case_type_id', $row) &&
363 !empty($rows[$rowNum]['civicrm_case_case_type_id'])
364 ) {
365 $value = $row['civicrm_case_case_type_id'];
366 $typeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
367 $value = array();
368 foreach ($typeIds as $typeId) {
369 if ($typeId) {
370 $value[$typeId] = $this->case_types[$typeId];
371 }
372 }
373 $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value);
374 $entryFound = TRUE;
375 }
376
377 // convert Case ID and Subject to links to Manage Case
378 if (array_key_exists('civicrm_case_id', $row) &&
379 !empty($rows[$rowNum]['civicrm_c2_id'])
380 ) {
381 $url = CRM_Utils_System::url("civicrm/contact/view/case",
382 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
383 $row['civicrm_case_id'],
384 $this->_absoluteUrl
385 );
386 $rows[$rowNum]['civicrm_case_id_link'] = $url;
387 $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case");
388 $entryFound = TRUE;
389 }
390 if (array_key_exists('civicrm_case_subject', $row) &&
391 !empty($rows[$rowNum]['civicrm_c2_id'])
392 ) {
393 $url = CRM_Utils_System::url("civicrm/contact/view/case",
394 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
395 $row['civicrm_case_id'],
396 $this->_absoluteUrl
397 );
398 $rows[$rowNum]['civicrm_case_subject_link'] = $url;
399 $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case");
400 $entryFound = TRUE;
401 }
402
403 if (array_key_exists('civicrm_case_is_deleted', $row)) {
404 $value = $row['civicrm_case_is_deleted'];
405 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels[$value];
406 $entryFound = TRUE;
407 }
408
409 if (!$entryFound) {
410 break;
411 }
412 }
413 }
414
415 }