4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 class CRM_Report_Form_Case_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
39 protected $_relField = FALSE;
40 function __construct() {
41 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
42 $this->case_statuses
= CRM_Case_PseudoConstant
::caseStatus();
43 $rels = CRM_Core_PseudoConstant
::relationshipType();
44 foreach ($rels as $relid => $v) {
45 $this->rel_types
[$relid] = $v['label_b_a'];
48 $this->deleted_labels
= array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes'));
50 $this->_columns
= array(
53 'dao' => 'CRM_Contact_DAO_Contact',
58 'name' => 'sort_name',
59 'title' => ts('Client'),
71 'dao' => 'CRM_Case_DAO_Case',
75 array('title' => ts('Case ID'),
79 'title' => ts('Case Subject'), 'default' => TRUE,
82 'title' => ts('Status'), 'default' => TRUE,
84 'case_type_id' => array(
85 'title' => ts('Case Type'), 'default' => TRUE,
87 'start_date' => array(
88 'title' => ts('Start Date'), 'default' => TRUE,
89 'type' => CRM_Utils_Type
::T_DATE
,
92 'title' => ts('End Date'), 'default' => TRUE,
93 'type' => CRM_Utils_Type
::T_DATE
,
96 'title' => ts('Duration (Days)'), 'default' => FALSE,
98 'is_deleted' => array(
99 'title' => ts('Deleted?'), 'default' => FALSE, 'type' => CRM_Utils_Type
::T_INT
,
103 array('start_date' => array('title' => ts('Start Date'),
104 'operatorType' => CRM_Report_Form
::OP_DATE
,
105 'type' => CRM_Utils_Type
::T_DATE
,
107 'end_date' => array('title' => ts('End Date'),
108 'operatorType' => CRM_Report_Form
::OP_DATE
,
109 'type' => CRM_Utils_Type
::T_DATE
,
111 'case_type_id' => array('title' => ts('Case Type'),
112 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
113 'options' => $this->case_types
,
115 'status_id' => array('title' => ts('Status'),
116 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
117 'options' => $this->case_statuses
,
119 'is_deleted' => array('title' => ts('Deleted?'),
120 'type' => CRM_Report_Form
::OP_INT
,
121 'operatorType' => CRM_Report_Form
::OP_SELECT
,
122 'options' => $this->deleted_labels
,
129 'dao' => 'CRM_Contact_DAO_Contact',
133 array('title' => ts('Staff Member'),
138 array('sort_name' => array('title' => ts('Staff Member'),
142 'civicrm_relationship' =>
144 'dao' => 'CRM_Contact_DAO_Relationship',
146 array('relationship_type_id' => array('title' => ts('Staff Relationship'),
147 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
148 'options' => $this->rel_types
,
152 'civicrm_relationship_type' =>
154 'dao' => 'CRM_Contact_DAO_RelationshipType',
159 'title' => ts('Relationship'), 'default' => TRUE,
163 'civicrm_case_contact' =>
165 'dao' => 'CRM_Case_DAO_CaseContact',
169 parent
::__construct();
172 function preProcess() {
173 parent
::preProcess();
178 $this->_columnHeaders
= array();
179 foreach ($this->_columns
as $tableName => $table) {
180 if (array_key_exists('fields', $table)) {
181 foreach ($table['fields'] as $fieldName => $field) {
182 if (!empty($field['required']) ||
183 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
186 if ($tableName == 'civicrm_relationship_type') {
187 $this->_relField
= TRUE;
190 if ($fieldName == 'duration') {
191 $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}";
194 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
196 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
197 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
203 $this->_select
= "SELECT " . implode(', ', $select) . " ";
206 static function formRule($fields, $files, $self) {
207 $errors = $grouping = array();
208 if (empty($fields['relationship_type_id_value']) && (array_key_exists('sort_name', $fields['fields']) ||
array_key_exists('label_b_a', $fields['fields']))) {
209 $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.');
211 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']))) {
212 $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.');
219 $cc = $this->_aliases
['civicrm_case'];
220 $c = $this->_aliases
['civicrm_contact'];
221 $c2 = $this->_aliases
['civicrm_c2'];
222 $cr = $this->_aliases
['civicrm_relationship'];
223 $crt = $this->_aliases
['civicrm_relationship_type'];
224 $ccc = $this->_aliases
['civicrm_case_contact'];
226 if ($this->_relField
) {
228 FROM civicrm_contact $c
229 inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b
230 inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id
231 inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id
232 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
233 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
238 FROM civicrm_case $cc
239 inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
240 inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
248 foreach ($this->_columns
as $tableName => $table) {
249 if (array_key_exists('filters', $table)) {
250 foreach ($table['filters'] as $fieldName => $field) {
252 if (CRM_Utils_Array
::value("operatorType", $field) & CRM_Report_Form
::OP_DATE
) {
253 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
254 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
255 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
257 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to,
258 CRM_Utils_Array
::value('type', $field)
263 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
264 if ($fieldName == 'case_type_id') {
265 $value = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
266 if (!empty($value)) {
267 $clause = "( {$field['dbAlias']} REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', $value) . "[[:>:]]' )";
273 $clause = $this->whereClause($field,
275 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
276 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
277 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
282 if (!empty($clause)) {
283 $clauses[] = $clause;
289 if (empty($clauses)) {
290 $this->_where
= "WHERE ( 1 ) ";
293 $this->_where
= "WHERE " . implode(' AND ', $clauses);
298 $this->_groupBy
= "";
301 function postProcess() {
303 $this->beginPostProcess();
305 $sql = $this->buildQuery(TRUE);
307 $rows = $graphRows = array();
308 $this->buildRows($sql, $rows);
310 $this->formatDisplay($rows);
311 $this->doTemplateAssignment($rows);
312 $this->endPostProcess($rows);
315 function alterDisplay(&$rows) {
317 foreach ($rows as $rowNum => $row) {
318 if (array_key_exists('civicrm_case_status_id', $row)) {
319 if ($value = $row['civicrm_case_status_id']) {
320 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
325 if (array_key_exists('civicrm_case_case_type_id', $row) &&
326 CRM_Utils_Array
::value('civicrm_case_case_type_id', $rows[$rowNum])
328 $value = $row['civicrm_case_case_type_id'];
329 $typeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
331 foreach ($typeIds as $typeId) {
333 $value[$typeId] = $this->case_types
[$typeId];
336 $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value);
340 // convert Case ID and Subject to links to Manage Case
341 if (array_key_exists('civicrm_case_id', $row) &&
342 CRM_Utils_Array
::value('civicrm_c2_id', $rows[$rowNum])
344 $url = CRM_Utils_System
::url("civicrm/contact/view/case",
345 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . $row['civicrm_case_id'],
348 $rows[$rowNum]['civicrm_case_id_link'] = $url;
349 $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case");
352 if (array_key_exists('civicrm_case_subject', $row) &&
353 CRM_Utils_Array
::value('civicrm_c2_id', $rows[$rowNum])
355 $url = CRM_Utils_System
::url("civicrm/contact/view/case",
356 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . $row['civicrm_case_id'],
359 $rows[$rowNum]['civicrm_case_subject_link'] = $url;
360 $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case");
364 if (array_key_exists('civicrm_case_is_deleted', $row)) {
365 $value = $row['civicrm_case_is_deleted'];
366 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];