Merge pull request #22316 from braders/core-3003-preserve-tab-between-pageloads
[civicrm-core.git] / CRM / Report / Form / Contact / Log.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Report_Form_Contact_Log extends CRM_Report_Form {
18
19 protected $_summary = NULL;
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
25
26 $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
27 asort($this->activityTypes);
28
29 $this->_columns = [
30 'civicrm_contact' => [
31 'dao' => 'CRM_Contact_DAO_Contact',
32 'fields' => [
33 'sort_name' => [
34 'title' => ts('Modified By'),
35 'required' => TRUE,
36 ],
37 'id' => [
38 'no_display' => TRUE,
39 'required' => TRUE,
40 ],
41 ],
42 'filters' => [
43 'sort_name' => [
44 'title' => ts('Modified By'),
45 'type' => CRM_Utils_Type::T_STRING,
46 ],
47 ],
48 'grouping' => 'contact-fields',
49 ],
50 'civicrm_contact_touched' => [
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'fields' => [
53 'sort_name_touched' => [
54 'title' => ts('Touched Contact'),
55 'name' => 'sort_name',
56 'required' => TRUE,
57 ],
58 'id' => [
59 'no_display' => TRUE,
60 'required' => TRUE,
61 ],
62 ],
63 'filters' => [
64 'sort_name_touched' => [
65 'title' => ts('Touched Contact'),
66 'name' => 'sort_name',
67 'type' => CRM_Utils_Type::T_STRING,
68 ],
69 ],
70 'grouping' => 'contact-fields',
71 ],
72 'civicrm_activity' => [
73 'dao' => 'CRM_Activity_DAO_Activity',
74 'fields' => [
75 'id' => [
76 'title' => ts('Activity ID'),
77 'no_display' => TRUE,
78 'required' => TRUE,
79 ],
80 'subject' => [
81 'title' => ts('Touched Activity'),
82 'required' => TRUE,
83 ],
84 'activity_type_id' => [
85 'title' => ts('Activity Type'),
86 'required' => TRUE,
87 ],
88 ],
89 ],
90 'civicrm_activity_source' => [
91 'dao' => 'CRM_Activity_DAO_ActivityContact',
92 'fields' => [
93 'contact_id' => [
94 'no_display' => TRUE,
95 'required' => TRUE,
96 ],
97 ],
98 ],
99 'civicrm_log' => [
100 'dao' => 'CRM_Core_DAO_Log',
101 'fields' => [
102 'modified_date' => [
103 'title' => ts('Modified Date'),
104 'required' => TRUE,
105 ],
106 'data' => [
107 'title' => ts('Description'),
108 ],
109 ],
110 'filters' => [
111 'modified_date' => [
112 'title' => ts('Modified Date'),
113 'operatorType' => CRM_Report_Form::OP_DATE,
114 'type' => CRM_Utils_Type::T_DATE,
115 'default' => 'this.week',
116 ],
117 ],
118 ],
119 ];
120
121 parent::__construct();
122 }
123
124 public function preProcess() {
125 parent::preProcess();
126 }
127
128 public function select() {
129 $select = [];
130 $this->_columnHeaders = [];
131 foreach ($this->_columns as $tableName => $table) {
132 if (array_key_exists('fields', $table)) {
133 foreach ($table['fields'] as $fieldName => $field) {
134 if (!empty($field['required']) ||
135 !empty($this->_params['fields'][$fieldName])
136 ) {
137
138 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
139 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
140 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
141 }
142 }
143 }
144 }
145
146 $this->_select = "SELECT " . implode(', ', $select) . " ";
147 }
148
149 /**
150 * @param $fields
151 * @param $files
152 * @param self $self
153 *
154 * @return array
155 */
156 public static function formRule($fields, $files, $self) {
157 $errors = $grouping = [];
158 return $errors;
159 }
160
161 public function from() {
162 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
163 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
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)
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}
172 ";
173 }
174
175 public function where() {
176 $clauses = [];
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;
182 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE
183 ) {
184 $relative = $this->_params["{$fieldName}_relative"] ?? NULL;
185 $from = $this->_params["{$fieldName}_from"] ?? NULL;
186 $to = $this->_params["{$fieldName}_to"] ?? NULL;
187
188 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
189 }
190 else {
191 $op = $this->_params["{$fieldName}_op"] ?? NULL;
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
213 public function orderBy() {
214 $this->_orderBy = "
215 ORDER BY {$this->_aliases['civicrm_log']}.modified_date DESC, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_touched']}.sort_name
216 ";
217 }
218
219 /**
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.
227 */
228 public function alterDisplay(&$rows) {
229
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',
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'],
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 }
287
288 }