CRM-18010: Ensure type of each report filters
[civicrm-core.git] / CRM / Report / Form / Contact / Log.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_Contact_Log extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38
39 /**
40 */
41 /**
42 */
43 public function __construct() {
44
45 $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
46 asort($this->activityTypes);
47
48 $this->_columns = array(
49 'civicrm_contact' => array(
50 'dao' => 'CRM_Contact_DAO_Contact',
51 'fields' => array(
52 'sort_name' => array(
53 'title' => ts('Modified By'),
54 'required' => TRUE,
55 ),
56 'id' => array(
57 'no_display' => TRUE,
58 'required' => TRUE,
59 ),
60 ),
61 'filters' => array(
62 'sort_name' => array(
63 'title' => ts('Modified By'),
64 'type' => CRM_Utils_Type::T_STRING,
65 ),
66 ),
67 'grouping' => 'contact-fields',
68 ),
69 'civicrm_contact_touched' => array(
70 'dao' => 'CRM_Contact_DAO_Contact',
71 'fields' => array(
72 'sort_name_touched' => array(
73 'title' => ts('Touched Contact'),
74 'name' => 'sort_name',
75 'required' => TRUE,
76 ),
77 'id' => array(
78 'no_display' => TRUE,
79 'required' => TRUE,
80 ),
81 ),
82 'filters' => array(
83 'sort_name_touched' => array(
84 'title' => ts('Touched Contact'),
85 'name' => 'sort_name',
86 'type' => CRM_Utils_Type::T_STRING,
87 ),
88 ),
89 'grouping' => 'contact-fields',
90 ),
91 'civicrm_activity' => array(
92 'dao' => 'CRM_Activity_DAO_Activity',
93 'fields' => array(
94 'id' => array(
95 'title' => ts('Activity ID'),
96 'no_display' => TRUE,
97 'required' => TRUE,
98 ),
99 'subject' => array(
100 'title' => ts('Touched Activity'),
101 'required' => TRUE,
102 ),
103 'activity_type_id' => array(
104 'title' => ts('Activity Type'),
105 'required' => TRUE,
106 ),
107 ),
108 ),
109 'civicrm_activity_source' => array(
110 'dao' => 'CRM_Activity_DAO_ActivityContact',
111 'fields' => array(
112 'contact_id' => array(
113 'no_display' => TRUE,
114 'required' => TRUE,
115 ),
116 ),
117 ),
118 'civicrm_log' => array(
119 'dao' => 'CRM_Core_DAO_Log',
120 'fields' => array(
121 'modified_date' => array(
122 'title' => ts('Modified Date'),
123 'required' => TRUE,
124 ),
125 'data' => array(
126 'title' => ts('Description'),
127 ),
128 ),
129 'filters' => array(
130 'modified_date' => array(
131 'title' => ts('Modified Date'),
132 'operatorType' => CRM_Report_Form::OP_DATE,
133 'type' => CRM_Utils_Type::T_DATE,
134 'default' => 'this.week',
135 ),
136 ),
137 ),
138 );
139
140 parent::__construct();
141 }
142
143 public function preProcess() {
144 parent::preProcess();
145 }
146
147 public function select() {
148 $select = array();
149 $this->_columnHeaders = array();
150 foreach ($this->_columns as $tableName => $table) {
151 if (array_key_exists('fields', $table)) {
152 foreach ($table['fields'] as $fieldName => $field) {
153 if (!empty($field['required']) ||
154 !empty($this->_params['fields'][$fieldName])
155 ) {
156
157 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
158 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
159 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
160 }
161 }
162 }
163 }
164
165 $this->_select = "SELECT " . implode(', ', $select) . " ";
166 }
167
168 /**
169 * @param $fields
170 * @param $files
171 * @param $self
172 *
173 * @return array
174 */
175 public static function formRule($fields, $files, $self) {
176 $errors = $grouping = array();
177 return $errors;
178 }
179
180 public function from() {
181 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
182 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
183 $this->_from = "
184 FROM civicrm_log {$this->_aliases['civicrm_log']}
185 inner join civicrm_contact {$this->_aliases['civicrm_contact']} on {$this->_aliases['civicrm_log']}.modified_id = {$this->_aliases['civicrm_contact']}.id
186 left join civicrm_contact {$this->_aliases['civicrm_contact_touched']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_contact' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_contact_touched']}.id)
187 left join civicrm_activity {$this->_aliases['civicrm_activity']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_activity' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_activity']}.id)
188 LEFT JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_source']} ON
189 {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_source']}.activity_id AND
190 {$this->_aliases['civicrm_activity_source']}.record_type_id = {$sourceID}
191 ";
192 }
193
194 public function where() {
195 $clauses = array();
196 $this->_having = '';
197 foreach ($this->_columns as $tableName => $table) {
198 if (array_key_exists('filters', $table)) {
199 foreach ($table['filters'] as $fieldName => $field) {
200 $clause = NULL;
201 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE
202 ) {
203 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
204 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
205 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
206
207 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
208 }
209 else {
210 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
211 if ($op) {
212 $clause = $this->whereClause($field,
213 $op,
214 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
215 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
216 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
217 );
218 }
219 }
220
221 if (!empty($clause)) {
222 $clauses[] = $clause;
223 }
224 }
225 }
226 }
227
228 $clauses[] = "({$this->_aliases['civicrm_log']}.entity_table <> 'civicrm_domain')";
229 $this->_where = "WHERE " . implode(' AND ', $clauses);
230 }
231
232 public function orderBy() {
233 $this->_orderBy = "
234 ORDER BY {$this->_aliases['civicrm_log']}.modified_date DESC, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_touched']}.sort_name
235 ";
236 }
237
238 /**
239 * Alter display of rows.
240 *
241 * Iterate through the rows retrieved via SQL and make changes for display purposes,
242 * such as rendering contacts as links.
243 *
244 * @param array $rows
245 * Rows generated by SQL, with an array for each row.
246 */
247 public function alterDisplay(&$rows) {
248
249 $entryFound = FALSE;
250 foreach ($rows as $rowNum => $row) {
251 // convert display name to links
252 if (array_key_exists('civicrm_contact_sort_name', $row) &&
253 array_key_exists('civicrm_contact_id', $row)
254 ) {
255 $url = CRM_Utils_System::url('civicrm/contact/view',
256 'reset=1&cid=' . $row['civicrm_contact_id'],
257 $this->_absoluteUrl
258 );
259 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
260 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
261 $entryFound = TRUE;
262 }
263
264 if (array_key_exists('civicrm_contact_touched_sort_name_touched', $row) &&
265 array_key_exists('civicrm_contact_touched_id', $row) &&
266 $row['civicrm_contact_touched_sort_name_touched'] !== ''
267 ) {
268 $url = CRM_Utils_System::url('civicrm/contact/view',
269 'reset=1&cid=' . $row['civicrm_contact_touched_id'],
270 $this->_absoluteUrl
271 );
272 $rows[$rowNum]['civicrm_contact_touched_sort_name_touched_link'] = $url;
273 $rows[$rowNum]['civicrm_contact_touched_sort_name_touched_hover'] = ts("View Contact details for this contact.");
274 $entryFound = TRUE;
275 }
276
277 if (array_key_exists('civicrm_activity_subject', $row) &&
278 array_key_exists('civicrm_activity_id', $row) &&
279 $row['civicrm_activity_subject'] !== ''
280 ) {
281 $url = CRM_Utils_System::url('civicrm/contact/view/activity',
282 'reset=1&action=view&id=' . $row['civicrm_activity_id'] . '&cid=' .
283 $row['civicrm_activity_source_contact_id'] . '&atype=' .
284 $row['civicrm_activity_activity_type_id'],
285 $this->_absoluteUrl
286 );
287 $rows[$rowNum]['civicrm_activity_subject_link'] = $url;
288 $rows[$rowNum]['civicrm_activity_subject_hover'] = ts("View Contact details for this contact.");
289 $entryFound = TRUE;
290 }
291
292 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
293 if ($value = $row['civicrm_activity_activity_type_id']) {
294 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $this->activityTypes[$value];
295 }
296 $entryFound = TRUE;
297 }
298
299 // skip looking further in rows, if first row itself doesn't
300 // have the column we need
301 if (!$entryFound) {
302 break;
303 }
304 }
305 }
306
307 }