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