4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Case_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
39 protected $_relField = FALSE;
40 protected $_exposeContactID = FALSE;
48 function __construct() {
49 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
50 $this->case_statuses
= CRM_Core_OptionGroup
::values('case_status');
51 $rels = CRM_Core_PseudoConstant
::relationshipType();
52 foreach ($rels as $relid => $v) {
53 $this->rel_types
[$relid] = $v['label_b_a'];
56 $this->deleted_labels
= array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes'));
58 $this->_columns
= array(
61 'dao' => 'CRM_Contact_DAO_Contact',
66 'name' => 'sort_name',
67 'title' => ts('Client'),
79 'dao' => 'CRM_Case_DAO_Case',
83 array('title' => ts('Case ID'),
87 'title' => ts('Case Subject'), 'default' => TRUE,
90 'title' => ts('Status'), 'default' => TRUE,
92 'case_type_id' => array(
93 'title' => ts('Case Type'), 'default' => TRUE,
95 'start_date' => array(
96 'title' => ts('Start Date'), 'default' => TRUE,
97 'type' => CRM_Utils_Type
::T_DATE
,
100 'title' => ts('End Date'), 'default' => TRUE,
101 'type' => CRM_Utils_Type
::T_DATE
,
104 'title' => ts('Duration (Days)'), 'default' => FALSE,
106 'is_deleted' => array(
107 'title' => ts('Deleted?'), 'default' => FALSE, 'type' => CRM_Utils_Type
::T_INT
,
111 array('start_date' => array('title' => ts('Start Date'),
112 'operatorType' => CRM_Report_Form
::OP_DATE
,
113 'type' => CRM_Utils_Type
::T_DATE
,
115 'end_date' => array('title' => ts('End Date'),
116 'operatorType' => CRM_Report_Form
::OP_DATE
,
117 'type' => CRM_Utils_Type
::T_DATE
,
119 'case_type_id' => array('title' => ts('Case Type'),
120 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
121 'options' => $this->case_types
,
123 'status_id' => array('title' => ts('Status'),
124 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
125 'options' => $this->case_statuses
,
127 'is_deleted' => array('title' => ts('Deleted?'),
128 'type' => CRM_Report_Form
::OP_INT
,
129 'operatorType' => CRM_Report_Form
::OP_SELECT
,
130 'options' => $this->deleted_labels
,
137 'dao' => 'CRM_Contact_DAO_Contact',
141 array('title' => ts('Staff Member'),
146 array('sort_name' => array('title' => ts('Staff Member'),
150 'civicrm_relationship' =>
152 'dao' => 'CRM_Contact_DAO_Relationship',
154 array('relationship_type_id' => array('title' => ts('Staff Relationship'),
155 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
156 'options' => $this->rel_types
,
160 'civicrm_relationship_type' =>
162 'dao' => 'CRM_Contact_DAO_RelationshipType',
167 'title' => ts('Relationship'), 'default' => TRUE,
171 'civicrm_case_contact' =>
173 'dao' => 'CRM_Case_DAO_CaseContact',
177 parent
::__construct();
180 function preProcess() {
181 parent
::preProcess();
186 $this->_columnHeaders
= array();
187 foreach ($this->_columns
as $tableName => $table) {
188 if (array_key_exists('fields', $table)) {
189 foreach ($table['fields'] as $fieldName => $field) {
190 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
192 if ($tableName == 'civicrm_relationship_type') {
193 $this->_relField
= TRUE;
196 if ($fieldName == 'duration') {
197 $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}";
200 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
202 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
203 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
209 $this->_select
= "SELECT " . implode(', ', $select) . " ";
219 static function formRule($fields, $files, $self) {
220 $errors = $grouping = array();
221 if (empty($fields['relationship_type_id_value']) && (array_key_exists('sort_name', $fields['fields']) ||
array_key_exists('label_b_a', $fields['fields']))) {
222 $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.');
224 if ((!empty($fields['relationship_type_id_value']) ||
!empty($fields['sort_name_value'])) && (!array_key_exists('sort_name', $fields['fields']) ||
!array_key_exists('label_b_a', $fields['fields']))) {
225 $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.');
232 $cc = $this->_aliases
['civicrm_case'];
233 $c = $this->_aliases
['civicrm_contact'];
234 $c2 = $this->_aliases
['civicrm_c2'];
235 $cr = $this->_aliases
['civicrm_relationship'];
236 $crt = $this->_aliases
['civicrm_relationship_type'];
237 $ccc = $this->_aliases
['civicrm_case_contact'];
239 if ($this->_relField
) {
241 FROM civicrm_contact $c
242 inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b
243 inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id
244 inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id
245 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
246 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
251 FROM civicrm_case $cc
252 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
253 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
261 foreach ($this->_columns
as $tableName => $table) {
262 if (array_key_exists('filters', $table)) {
263 foreach ($table['filters'] as $fieldName => $field) {
265 if (CRM_Utils_Array
::value("operatorType", $field) & CRM_Report_Form
::OP_DATE
) {
266 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
267 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
268 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
270 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to,
271 CRM_Utils_Array
::value('type', $field)
276 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
277 if ($fieldName == 'case_type_id') {
278 $value = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
279 if (!empty($value)) {
280 $clause = "( {$field['dbAlias']} REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', $value) . "[[:>:]]' )";
286 $clause = $this->whereClause($field,
288 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
289 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
290 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
295 if (!empty($clause)) {
296 $clauses[] = $clause;
302 if (empty($clauses)) {
303 $this->_where
= "WHERE ( 1 ) ";
306 $this->_where
= "WHERE " . implode(' AND ', $clauses);
311 $this->_groupBy
= "";
314 function postProcess() {
316 $this->beginPostProcess();
318 $sql = $this->buildQuery(TRUE);
320 $rows = $graphRows = array();
321 $this->buildRows($sql, $rows);
323 $this->formatDisplay($rows);
324 $this->doTemplateAssignment($rows);
325 $this->endPostProcess($rows);
331 function alterDisplay(&$rows) {
333 foreach ($rows as $rowNum => $row) {
334 if (array_key_exists('civicrm_case_status_id', $row)) {
335 if ($value = $row['civicrm_case_status_id']) {
336 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
341 if (array_key_exists('civicrm_case_case_type_id', $row) && !empty($rows[$rowNum]['civicrm_case_case_type_id'])) {
342 $value = $row['civicrm_case_case_type_id'];
343 $typeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
345 foreach ($typeIds as $typeId) {
347 $value[$typeId] = $this->case_types
[$typeId];
350 $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value);
354 // convert Case ID and Subject to links to Manage Case
355 if (array_key_exists('civicrm_case_id', $row) && !empty($rows[$rowNum]['civicrm_c2_id'])) {
356 $url = CRM_Utils_System
::url("civicrm/contact/view/case",
357 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . $row['civicrm_case_id'],
360 $rows[$rowNum]['civicrm_case_id_link'] = $url;
361 $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case");
364 if (array_key_exists('civicrm_case_subject', $row) && !empty($rows[$rowNum]['civicrm_c2_id'])) {
365 $url = CRM_Utils_System
::url("civicrm/contact/view/case",
366 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . $row['civicrm_case_id'],
369 $rows[$rowNum]['civicrm_case_subject_link'] = $url;
370 $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case");
374 if (array_key_exists('civicrm_case_is_deleted', $row)) {
375 $value = $row['civicrm_case_is_deleted'];
376 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];