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