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