Commit | Line | Data |
---|---|---|
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 | */ |
17 | class CRM_Report_Form_Activity extends CRM_Report_Form { | |
be2fb01f | 18 | protected $_selectAliasesTotal = []; |
6a488035 | 19 | |
be2fb01f | 20 | protected $_customGroupExtends = [ |
21dfd5f5 | 21 | 'Activity', |
be2fb01f | 22 | ]; |
430ae6dd | 23 | |
be2fb01f | 24 | protected $_nonDisplayFields = []; |
09febbae | 25 | |
1728e9a0 | 26 | /** |
27 | * This report has not been optimised for group filtering. | |
28 | * | |
29 | * The functionality for group filtering has been improved but not | |
30 | * all reports have been adjusted to take care of it. This report has not | |
31 | * and will run an inefficient query until fixed. | |
32 | * | |
33 | * CRM-19170 | |
34 | * | |
35 | * @var bool | |
36 | */ | |
37 | protected $groupFilterNotOptimised = TRUE; | |
38 | ||
74cf4551 | 39 | /** |
ab432335 | 40 | * Class constructor. |
74cf4551 | 41 | */ |
00be9182 | 42 | public function __construct() { |
7116d719 | 43 | // There could be multiple contacts. We not clear on which contact id to display. |
83e74860 DS |
44 | // Lets hide it for now. |
45 | $this->_exposeContactID = FALSE; | |
8f621a01 | 46 | // if navigated from count link of activity summary reports. |
a3d827a7 | 47 | $this->_resetDateFilter = CRM_Utils_Request::retrieve('resetDateFilter', 'Boolean'); |
83e74860 | 48 | |
6a488035 TO |
49 | $config = CRM_Core_Config::singleton(); |
50 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
d6c3d40f | 51 | $caseEnabled = in_array("CiviCase", $config->enableComponents); |
6a488035 | 52 | if ($campaignEnabled) { |
6a488035 TO |
53 | $this->engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel(); |
54 | } | |
6a488035 | 55 | |
1023b3bc | 56 | $components = CRM_Core_Component::getEnabledComponents(); |
57 | foreach ($components as $componentName => $componentInfo) { | |
d3e6c71f | 58 | // CRM-19201: Add support for reporting CiviCampaign activities |
44d17ec8 BS |
59 | // For CiviCase, "access all cases and activities" is required here |
60 | // rather than "access my cases and activities" to prevent those with | |
61 | // only the later permission from seeing a list of all cases which might | |
62 | // present a privacy issue. | |
63 | if (CRM_Core_Permission::access($componentName, TRUE, TRUE)) { | |
1023b3bc | 64 | $accessAllowed[] = $componentInfo->componentID; |
65 | } | |
66 | } | |
67 | ||
68 | $include = ''; | |
69 | if (!empty($accessAllowed)) { | |
70 | $include = 'OR v.component_id IN (' . implode(', ', $accessAllowed) . ')'; | |
71 | } | |
72 | $condition = " AND ( v.component_id IS NULL {$include} )"; | |
73 | $this->activityTypes = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition); | |
74 | asort($this->activityTypes); | |
d47468d0 | 75 | |
76 | // @todo split the 3 different contact tables into their own array items. | |
77 | // this will massively simplify the needs of this report. | |
be2fb01f CW |
78 | $this->_columns = [ |
79 | 'civicrm_contact' => [ | |
af9b09df | 80 | 'dao' => 'CRM_Contact_DAO_Contact', |
be2fb01f CW |
81 | 'fields' => [ |
82 | 'contact_source' => [ | |
af9b09df | 83 | 'name' => 'sort_name', |
8c0ff2a6 | 84 | 'title' => ts('Source Name'), |
af9b09df TO |
85 | 'alias' => 'civicrm_contact_source', |
86 | 'no_repeat' => TRUE, | |
be2fb01f CW |
87 | ], |
88 | 'contact_assignee' => [ | |
af9b09df | 89 | 'name' => 'sort_name', |
8c0ff2a6 | 90 | 'title' => ts('Assignee Name'), |
af9b09df TO |
91 | 'alias' => 'civicrm_contact_assignee', |
92 | 'dbAlias' => "civicrm_contact_assignee.sort_name", | |
93 | 'default' => TRUE, | |
be2fb01f CW |
94 | ], |
95 | 'contact_target' => [ | |
af9b09df | 96 | 'name' => 'sort_name', |
8c0ff2a6 | 97 | 'title' => ts('Target Name'), |
af9b09df TO |
98 | 'alias' => 'civicrm_contact_target', |
99 | 'dbAlias' => "civicrm_contact_target.sort_name", | |
100 | 'default' => TRUE, | |
be2fb01f CW |
101 | ], |
102 | 'contact_source_id' => [ | |
af9b09df TO |
103 | 'name' => 'id', |
104 | 'alias' => 'civicrm_contact_source', | |
105 | 'dbAlias' => "civicrm_contact_source.id", | |
106 | 'no_display' => TRUE, | |
107 | 'default' => TRUE, | |
108 | 'required' => TRUE, | |
be2fb01f CW |
109 | ], |
110 | 'contact_assignee_id' => [ | |
af9b09df TO |
111 | 'name' => 'id', |
112 | 'alias' => 'civicrm_contact_assignee', | |
113 | 'dbAlias' => "civicrm_contact_assignee.id", | |
114 | 'no_display' => TRUE, | |
115 | 'default' => TRUE, | |
116 | 'required' => TRUE, | |
be2fb01f CW |
117 | ], |
118 | 'contact_target_id' => [ | |
af9b09df TO |
119 | 'name' => 'id', |
120 | 'alias' => 'civicrm_contact_target', | |
121 | 'dbAlias' => "civicrm_contact_target.id", | |
122 | 'no_display' => TRUE, | |
123 | 'default' => TRUE, | |
124 | 'required' => TRUE, | |
be2fb01f CW |
125 | ], |
126 | ], | |
127 | 'filters' => [ | |
128 | 'contact_source' => [ | |
af9b09df TO |
129 | 'name' => 'sort_name', |
130 | 'alias' => 'civicrm_contact_source', | |
8c0ff2a6 | 131 | 'title' => ts('Source Name'), |
af9b09df TO |
132 | 'operator' => 'like', |
133 | 'type' => CRM_Report_Form::OP_STRING, | |
be2fb01f CW |
134 | ], |
135 | 'contact_assignee' => [ | |
af9b09df TO |
136 | 'name' => 'sort_name', |
137 | 'alias' => 'civicrm_contact_assignee', | |
8c0ff2a6 | 138 | 'title' => ts('Assignee Name'), |
af9b09df TO |
139 | 'operator' => 'like', |
140 | 'type' => CRM_Report_Form::OP_STRING, | |
be2fb01f CW |
141 | ], |
142 | 'contact_target' => [ | |
af9b09df TO |
143 | 'name' => 'sort_name', |
144 | 'alias' => 'civicrm_contact_target', | |
8c0ff2a6 | 145 | 'title' => ts('Target Name'), |
af9b09df TO |
146 | 'operator' => 'like', |
147 | 'type' => CRM_Report_Form::OP_STRING, | |
be2fb01f CW |
148 | ], |
149 | 'current_user' => [ | |
af9b09df TO |
150 | 'name' => 'current_user', |
151 | 'title' => ts('Limit To Current User'), | |
152 | 'type' => CRM_Utils_Type::T_INT, | |
153 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
be2fb01f CW |
154 | 'options' => ['0' => ts('No'), '1' => ts('Yes')], |
155 | ], | |
156 | ], | |
af9b09df | 157 | 'grouping' => 'contact-fields', |
be2fb01f CW |
158 | ], |
159 | 'civicrm_email' => [ | |
af9b09df | 160 | 'dao' => 'CRM_Core_DAO_Email', |
be2fb01f CW |
161 | 'fields' => [ |
162 | 'contact_source_email' => [ | |
af9b09df | 163 | 'name' => 'email', |
8c0ff2a6 | 164 | 'title' => ts('Source Email'), |
af9b09df | 165 | 'alias' => 'civicrm_email_source', |
be2fb01f CW |
166 | ], |
167 | 'contact_assignee_email' => [ | |
af9b09df | 168 | 'name' => 'email', |
8c0ff2a6 | 169 | 'title' => ts('Assignee Email'), |
af9b09df | 170 | 'alias' => 'civicrm_email_assignee', |
be2fb01f CW |
171 | ], |
172 | 'contact_target_email' => [ | |
af9b09df | 173 | 'name' => 'email', |
8c0ff2a6 | 174 | 'title' => ts('Target Email'), |
af9b09df | 175 | 'alias' => 'civicrm_email_target', |
be2fb01f CW |
176 | ], |
177 | ], | |
178 | 'order_bys' => [ | |
179 | 'source_contact_email' => [ | |
af9b09df | 180 | 'name' => 'email', |
8c0ff2a6 | 181 | 'title' => ts('Source Email'), |
af9b09df | 182 | 'dbAlias' => 'civicrm_email_contact_source_email', |
be2fb01f CW |
183 | ], |
184 | ], | |
185 | ], | |
186 | 'civicrm_phone' => [ | |
8c0ff2a6 | 187 | 'dao' => 'CRM_Core_DAO_Phone', |
be2fb01f CW |
188 | 'fields' => [ |
189 | 'contact_source_phone' => [ | |
8c0ff2a6 | 190 | 'name' => 'phone', |
191 | 'title' => ts('Source Phone'), | |
192 | 'alias' => 'civicrm_phone_source', | |
be2fb01f CW |
193 | ], |
194 | 'contact_assignee_phone' => [ | |
8c0ff2a6 | 195 | 'name' => 'phone', |
196 | 'title' => ts('Assignee Phone'), | |
197 | 'alias' => 'civicrm_phone_assignee', | |
be2fb01f CW |
198 | ], |
199 | 'contact_target_phone' => [ | |
8c0ff2a6 | 200 | 'name' => 'phone', |
2afe8e0c | 201 | 'title' => ts('Target Phone'), |
8c0ff2a6 | 202 | 'alias' => 'civicrm_phone_target', |
be2fb01f CW |
203 | ], |
204 | ], | |
205 | ], | |
206 | 'civicrm_activity' => [ | |
af9b09df | 207 | 'dao' => 'CRM_Activity_DAO_Activity', |
be2fb01f CW |
208 | 'fields' => [ |
209 | 'id' => [ | |
af9b09df TO |
210 | 'no_display' => TRUE, |
211 | 'title' => ts('Activity ID'), | |
212 | 'required' => TRUE, | |
be2fb01f CW |
213 | ], |
214 | 'source_record_id' => [ | |
af9b09df TO |
215 | 'no_display' => TRUE, |
216 | 'required' => TRUE, | |
be2fb01f CW |
217 | ], |
218 | 'activity_type_id' => [ | |
af9b09df TO |
219 | 'title' => ts('Activity Type'), |
220 | 'required' => TRUE, | |
221 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
222 | ], |
223 | 'activity_subject' => [ | |
af9b09df TO |
224 | 'title' => ts('Subject'), |
225 | 'default' => TRUE, | |
be2fb01f CW |
226 | ], |
227 | 'activity_date_time' => [ | |
af9b09df TO |
228 | 'title' => ts('Activity Date'), |
229 | 'required' => TRUE, | |
be2fb01f CW |
230 | ], |
231 | 'status_id' => [ | |
af9b09df TO |
232 | 'title' => ts('Activity Status'), |
233 | 'default' => TRUE, | |
234 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
235 | ], |
236 | 'duration' => [ | |
af9b09df TO |
237 | 'title' => ts('Duration'), |
238 | 'type' => CRM_Utils_Type::T_INT, | |
be2fb01f CW |
239 | ], |
240 | 'location' => [ | |
c02f870b JM |
241 | 'title' => ts('Location'), |
242 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
243 | ], |
244 | 'details' => [ | |
1efec7ff | 245 | 'title' => ts('Activity Details'), |
be2fb01f CW |
246 | ], |
247 | 'priority_id' => [ | |
da236f9a | 248 | 'title' => ts('Priority'), |
249 | 'default' => TRUE, | |
250 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
251 | ], |
252 | ], | |
253 | 'filters' => [ | |
254 | 'activity_date_time' => [ | |
1a6c8513 | 255 | 'default' => 'this.month', |
6a488035 | 256 | 'operatorType' => CRM_Report_Form::OP_DATE, |
be2fb01f CW |
257 | ], |
258 | 'activity_subject' => ['title' => ts('Activity Subject')], | |
259 | 'activity_type_id' => [ | |
af9b09df TO |
260 | 'title' => ts('Activity Type'), |
261 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
262 | 'options' => $this->activityTypes, | |
be2fb01f CW |
263 | ], |
264 | 'status_id' => [ | |
af9b09df | 265 | 'title' => ts('Activity Status'), |
8ee006e7 | 266 | 'type' => CRM_Utils_Type::T_STRING, |
af9b09df TO |
267 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
268 | 'options' => CRM_Core_PseudoConstant::activityStatus(), | |
be2fb01f CW |
269 | ], |
270 | 'location' => [ | |
c02f870b JM |
271 | 'title' => ts('Location'), |
272 | 'type' => CRM_Utils_Type::T_TEXT, | |
be2fb01f CW |
273 | ], |
274 | 'details' => [ | |
1efec7ff DG |
275 | 'title' => ts('Activity Details'), |
276 | 'type' => CRM_Utils_Type::T_TEXT, | |
be2fb01f CW |
277 | ], |
278 | 'priority_id' => [ | |
71c30391 | 279 | 'title' => ts('Activity Priority'), |
410e8105 | 280 | 'type' => CRM_Utils_Type::T_STRING, |
71c30391 | 281 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
282 | 'options' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'), | |
be2fb01f CW |
283 | ], |
284 | ], | |
285 | 'order_bys' => [ | |
286 | 'activity_date_time' => [ | |
af9b09df TO |
287 | 'title' => ts('Activity Date'), |
288 | 'default_weight' => '1', | |
289 | 'dbAlias' => 'civicrm_activity_activity_date_time', | |
be2fb01f CW |
290 | ], |
291 | 'activity_type_id' => [ | |
af9b09df TO |
292 | 'title' => ts('Activity Type'), |
293 | 'default_weight' => '2', | |
56bbb442 | 294 | 'dbAlias' => 'field(civicrm_activity_activity_type_id, ' . implode(', ', array_keys($this->activityTypes)) . ')', |
be2fb01f CW |
295 | ], |
296 | ], | |
af9b09df TO |
297 | 'grouping' => 'activity-fields', |
298 | 'alias' => 'activity', | |
be2fb01f | 299 | ], |
56bbb442 | 300 | // Hack to get $this->_alias populated for the table. |
be2fb01f | 301 | 'civicrm_activity_contact' => [ |
af9b09df | 302 | 'dao' => 'CRM_Activity_DAO_ActivityContact', |
be2fb01f CW |
303 | 'fields' => [], |
304 | ], | |
305 | ] + $this->addressFields(TRUE); | |
6a488035 | 306 | |
d6c3d40f | 307 | if ($caseEnabled && CRM_Core_Permission::check('access all cases and activities')) { |
be2fb01f | 308 | $this->_columns['civicrm_activity']['filters']['include_case_activities'] = [ |
d6c3d40f | 309 | 'name' => 'include_case_activities', |
310 | 'title' => ts('Include Case Activities'), | |
311 | 'type' => CRM_Utils_Type::T_INT, | |
312 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
be2fb01f CW |
313 | 'options' => ['0' => ts('No'), '1' => ts('Yes')], |
314 | ]; | |
d6c3d40f | 315 | } |
316 | ||
6a488035 TO |
317 | if ($campaignEnabled) { |
318 | // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled | |
319 | ||
be2fb01f | 320 | $this->_columns['civicrm_activity']['fields']['result'] = [ |
fd6a6828 | 321 | 'title' => ts('Survey Result'), |
6a488035 | 322 | 'default' => 'false', |
be2fb01f CW |
323 | ]; |
324 | $this->_columns['civicrm_activity']['filters']['result'] = [ | |
9d72cede | 325 | 'title' => ts('Survey Result'), |
6a488035 TO |
326 | 'operator' => 'like', |
327 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f | 328 | ]; |
254ca428 SL |
329 | // If we have campaigns enabled, add those elements to both the fields, filters. |
330 | $this->addCampaignFields('civicrm_activity'); | |
331 | ||
6a488035 | 332 | if (!empty($this->engagementLevels)) { |
be2fb01f | 333 | $this->_columns['civicrm_activity']['fields']['engagement_level'] = [ |
fd6a6828 | 334 | 'title' => ts('Engagement Index'), |
6a488035 | 335 | 'default' => 'false', |
be2fb01f CW |
336 | ]; |
337 | $this->_columns['civicrm_activity']['filters']['engagement_level'] = [ | |
9d72cede | 338 | 'title' => ts('Engagement Index'), |
09b5cdcf | 339 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
340 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
341 | 'options' => $this->engagementLevels, | |
be2fb01f | 342 | ]; |
6a488035 TO |
343 | } |
344 | } | |
345 | $this->_groupFilter = TRUE; | |
346 | $this->_tagFilter = TRUE; | |
ed795723 | 347 | $this->_tagFilterTable = 'civicrm_activity'; |
b811ac0b | 348 | |
6a488035 TO |
349 | parent::__construct(); |
350 | } | |
351 | ||
b811ac0b MWMC |
352 | public function preProcess() { |
353 | // Is "Include Case Activities" selected? If yes, include the case_id as a hidden column | |
7bc28fa2 | 354 | $formToUse = $this->noController ? NULL : $this; |
355 | $includeCaseActivities = CRM_Utils_Request::retrieve('include_case_activities_value', 'Boolean', $formToUse); | |
b811ac0b MWMC |
356 | if (!empty($includeCaseActivities)) { |
357 | $this->_columns['civicrm_case_activity'] = [ | |
358 | 'dao' => 'CRM_Case_DAO_CaseActivity', | |
359 | 'fields' => [ | |
360 | 'case_id' => [ | |
361 | 'no_display' => TRUE, | |
362 | 'required' => TRUE, | |
363 | ], | |
364 | ], | |
365 | ]; | |
366 | } | |
367 | parent::preProcess(); | |
368 | } | |
369 | ||
be2e0c6a TO |
370 | /** |
371 | * Adding address fields with dbAlias for order clause. | |
ab432335 EM |
372 | * |
373 | * @param bool $orderBy | |
374 | * | |
a6c01b45 | 375 | * @return array |
ab432335 | 376 | * Address fields |
667abe3a | 377 | */ |
00be9182 | 378 | public function addressFields($orderBy = FALSE) { |
667abe3a | 379 | $address = parent::addAddressFields(FALSE, TRUE); |
380 | if ($orderBy) { | |
381 | foreach ($address['civicrm_address']['order_bys'] as $fieldName => $field) { | |
382 | $address['civicrm_address']['order_bys'][$fieldName]['dbAlias'] = "civicrm_address_{$fieldName}"; | |
383 | } | |
384 | } | |
385 | return $address; | |
386 | } | |
387 | ||
74cf4551 | 388 | /** |
a0375fa3 | 389 | * Build select clause. |
390 | * | |
d47468d0 | 391 | * @todo get rid of $recordType param. It's only because 3 separate contact tables |
392 | * are mis-declared as one that we need it. | |
393 | * | |
394 | * @param null $recordType deprecated | |
395 | * Parameter to hack around the bad decision made in construct to misrepresent | |
396 | * different tables as the same table. | |
74cf4551 | 397 | */ |
3ede1c56 | 398 | public function select($recordType = 'target') { |
9d72cede EM |
399 | if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && |
400 | $recordType != 'final' | |
401 | ) { | |
09febbae | 402 | $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}"; |
09febbae | 403 | } |
1f220d30 | 404 | parent::select(); |
1f220d30 | 405 | |
09febbae | 406 | if ($recordType == 'final' && !empty($this->_nonDisplayFields)) { |
407 | foreach ($this->_nonDisplayFields as $fieldName) { | |
408 | unset($this->_columnHeaders[$fieldName]); | |
409 | } | |
410 | } | |
411 | ||
1f220d30 DS |
412 | if (empty($this->_selectAliasesTotal)) { |
413 | $this->_selectAliasesTotal = $this->_selectAliases; | |
414 | } | |
415 | ||
be2fb01f | 416 | $removeKeys = []; |
1f220d30 | 417 | if ($recordType == 'target') { |
d47468d0 | 418 | // @todo - fix up the way the tables are declared in construct & remove this. |
1f220d30 | 419 | foreach ($this->_selectClauses as $key => $clause) { |
7116d719 | 420 | if (strstr($clause, 'civicrm_contact_assignee.') || |
1f220d30 DS |
421 | strstr($clause, 'civicrm_contact_source.') || |
422 | strstr($clause, 'civicrm_email_assignee.') || | |
8c0ff2a6 | 423 | strstr($clause, 'civicrm_email_source.') || |
424 | strstr($clause, 'civicrm_phone_assignee.') || | |
425 | strstr($clause, 'civicrm_phone_source.') | |
1f220d30 DS |
426 | ) { |
427 | $removeKeys[] = $key; | |
428 | unset($this->_selectClauses[$key]); | |
429 | } | |
430 | } | |
9d72cede | 431 | } |
4c9b6178 | 432 | elseif ($recordType == 'assignee') { |
d47468d0 | 433 | // @todo - fix up the way the tables are declared in construct & remove this. |
1f220d30 | 434 | foreach ($this->_selectClauses as $key => $clause) { |
7116d719 | 435 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
436 | strstr($clause, 'civicrm_contact_source.') || |
437 | strstr($clause, 'civicrm_email_target.') || | |
8c0ff2a6 | 438 | strstr($clause, 'civicrm_email_source.') || |
439 | strstr($clause, 'civicrm_phone_target.') || | |
c00963a1 | 440 | strstr($clause, 'civicrm_phone_source.') || |
441 | strstr($clause, 'civicrm_address_') | |
1f220d30 DS |
442 | ) { |
443 | $removeKeys[] = $key; | |
444 | unset($this->_selectClauses[$key]); | |
445 | } | |
446 | } | |
9d72cede | 447 | } |
4c9b6178 | 448 | elseif ($recordType == 'source') { |
d47468d0 | 449 | // @todo - fix up the way the tables are declared in construct & remove this. |
1f220d30 | 450 | foreach ($this->_selectClauses as $key => $clause) { |
7116d719 | 451 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
452 | strstr($clause, 'civicrm_contact_assignee.') || |
453 | strstr($clause, 'civicrm_email_target.') || | |
8c0ff2a6 | 454 | strstr($clause, 'civicrm_email_assignee.') || |
455 | strstr($clause, 'civicrm_phone_target.') || | |
c00963a1 | 456 | strstr($clause, 'civicrm_phone_assignee.') || |
457 | strstr($clause, 'civicrm_address_') | |
1f220d30 DS |
458 | ) { |
459 | $removeKeys[] = $key; | |
460 | unset($this->_selectClauses[$key]); | |
461 | } | |
462 | } | |
9d72cede | 463 | } |
4c9b6178 | 464 | elseif ($recordType == 'final') { |
1f220d30 DS |
465 | $this->_selectClauses = $this->_selectAliasesTotal; |
466 | foreach ($this->_selectClauses as $key => $clause) { | |
d47468d0 | 467 | // @todo - fix up the way the tables are declared in construct & remove this. |
7116d719 | 468 | if (strstr($clause, 'civicrm_contact_contact_target') || |
1f220d30 | 469 | strstr($clause, 'civicrm_contact_contact_assignee') || |
8c0ff2a6 | 470 | strstr($clause, 'civicrm_contact_contact_source') || |
471 | strstr($clause, 'civicrm_phone_contact_source_phone') || | |
472 | strstr($clause, 'civicrm_phone_contact_assignee_phone') || | |
473 | strstr($clause, 'civicrm_email_contact_source_email') || | |
474 | strstr($clause, 'civicrm_email_contact_assignee_email') || | |
475 | strstr($clause, 'civicrm_email_contact_target_email') || | |
66ad82d6 | 476 | strstr($clause, 'civicrm_phone_contact_target_phone') || |
477 | strstr($clause, 'civicrm_address_') | |
9d72cede | 478 | ) { |
66ad82d6 | 479 | $this->_selectClauses[$key] = "GROUP_CONCAT(DISTINCT $clause SEPARATOR ';') as $clause"; |
6a488035 TO |
480 | } |
481 | } | |
482 | } | |
483 | ||
1f220d30 DS |
484 | if ($recordType) { |
485 | foreach ($removeKeys as $key) { | |
486 | unset($this->_selectAliases[$key]); | |
487 | } | |
488 | ||
c00963a1 | 489 | if ($recordType == 'target') { |
667abe3a | 490 | foreach ($this->_columns['civicrm_address']['order_bys'] as $fieldName => $field) { |
491 | $orderByFld = $this->_columns['civicrm_address']['order_bys'][$fieldName]; | |
492 | $fldInfo = $this->_columns['civicrm_address']['fields'][$fieldName]; | |
493 | $this->_selectAliases[] = $orderByFld['dbAlias']; | |
494 | $this->_selectClauses[] = "{$fldInfo['dbAlias']} as {$orderByFld['dbAlias']}"; | |
495 | } | |
667abe3a | 496 | $this->_selectAliases = array_unique($this->_selectAliases); |
497 | $this->_selectClauses = array_unique($this->_selectClauses); | |
498 | } | |
1f220d30 | 499 | $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " "; |
1f220d30 | 500 | } |
6a488035 TO |
501 | } |
502 | ||
74cf4551 | 503 | /** |
a0375fa3 | 504 | * Build from clause. |
d47468d0 | 505 | * @todo remove this function & declare the 3 contact tables separately |
74cf4551 | 506 | */ |
d47468d0 | 507 | public function from() { |
b811ac0b | 508 | $this->buildFrom('target'); |
6a488035 TO |
509 | } |
510 | ||
74cf4551 | 511 | /** |
a0375fa3 | 512 | * Build where clause. |
513 | * | |
d47468d0 | 514 | * @todo get rid of $recordType param. It's only because 3 separate contact tables |
515 | * are mis-declared as one that we need it. | |
516 | * | |
a0375fa3 | 517 | * @param string $recordType |
74cf4551 | 518 | */ |
00be9182 | 519 | public function where($recordType = NULL) { |
2f4b4d54 | 520 | $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND |
6a488035 TO |
521 | {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND |
522 | {$this->_aliases['civicrm_activity']}.is_current_revision = 1"; | |
523 | ||
be2fb01f | 524 | $clauses = []; |
6a488035 TO |
525 | foreach ($this->_columns as $tableName => $table) { |
526 | if (array_key_exists('filters', $table)) { | |
527 | ||
528 | foreach ($table['filters'] as $fieldName => $field) { | |
529 | $clause = NULL; | |
7116d719 | 530 | if ($fieldName != 'contact_' . $recordType && |
531 | (strstr($fieldName, '_target') || | |
532 | strstr($fieldName, '_assignee') || | |
533 | strstr($fieldName, '_source') | |
534 | ) | |
535 | ) { | |
7116d719 | 536 | continue; |
537 | } | |
6a488035 TO |
538 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { |
539 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
540 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
541 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
542 | |
543 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
544 | } | |
545 | else { | |
546 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
024311d5 | 547 | if ($op && ($op != 'nnll' && $op != 'nll')) { |
6a488035 TO |
548 | $clause = $this->whereClause($field, |
549 | $op, | |
550 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
551 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
552 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
553 | ); | |
d6c3d40f | 554 | if ($field['name'] == 'include_case_activities') { |
555 | $clause = NULL; | |
556 | } | |
9d72cede EM |
557 | if ($fieldName == 'activity_type_id' && |
558 | empty($this->_params['activity_type_id_value']) | |
559 | ) { | |
355b8a20 | 560 | if (empty($this->_params['include_case_activities_value'])) { |
561 | $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE); | |
562 | } | |
1023b3bc | 563 | $actTypes = array_flip($this->activityTypes); |
6c552737 | 564 | $clause = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN (" . |
9d72cede | 565 | implode(',', $actTypes) . ") )"; |
42b3be07 | 566 | } |
6a488035 TO |
567 | } |
568 | } | |
569 | ||
570 | if ($field['name'] == 'current_user') { | |
9d72cede EM |
571 | if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) == |
572 | 1 | |
573 | ) { | |
6a488035 TO |
574 | // get current user |
575 | $session = CRM_Core_Session::singleton(); | |
576 | if ($contactID = $session->get('userID')) { | |
389cfee7 | 577 | $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN |
578 | (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})"; | |
6a488035 TO |
579 | } |
580 | else { | |
581 | $clause = NULL; | |
582 | } | |
583 | } | |
584 | else { | |
585 | $clause = NULL; | |
586 | } | |
587 | } | |
588 | if (!empty($clause)) { | |
589 | $clauses[] = $clause; | |
590 | } | |
591 | } | |
592 | } | |
593 | } | |
594 | ||
595 | if (empty($clauses)) { | |
b6cab294 | 596 | $this->_where .= " "; |
6a488035 TO |
597 | } |
598 | else { | |
b6cab294 | 599 | $this->_where .= " AND " . implode(' AND ', $clauses); |
6a488035 TO |
600 | } |
601 | ||
602 | if ($this->_aclWhere) { | |
603 | $this->_where .= " AND {$this->_aclWhere} "; | |
604 | } | |
605 | } | |
606 | ||
a0375fa3 | 607 | /** |
608 | * Override group by function. | |
609 | */ | |
00be9182 | 610 | public function groupBy() { |
b708c08d | 611 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_activity']}.id"); |
6a488035 TO |
612 | } |
613 | ||
74cf4551 | 614 | /** |
a0375fa3 | 615 | * Build ACL clause. |
616 | * | |
74cf4551 EM |
617 | * @param string $tableAlias |
618 | */ | |
00be9182 | 619 | public function buildACLClause($tableAlias = 'contact_a') { |
2a3c101f | 620 | //override for ACL( Since Contact may be source |
6a488035 TO |
621 | //contact/assignee or target also it may be null ) |
622 | ||
623 | if (CRM_Core_Permission::check('view all contacts')) { | |
624 | $this->_aclFrom = $this->_aclWhere = NULL; | |
625 | return; | |
626 | } | |
627 | ||
628 | $session = CRM_Core_Session::singleton(); | |
629 | $contactID = $session->get('userID'); | |
630 | if (!$contactID) { | |
631 | $contactID = 0; | |
632 | } | |
633 | $contactID = CRM_Utils_Type::escape($contactID, 'Integer'); | |
634 | ||
635 | CRM_Contact_BAO_Contact_Permission::cache($contactID); | |
be2fb01f | 636 | $clauses = []; |
6a488035 TO |
637 | foreach ($tableAlias as $k => $alias) { |
638 | $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON ( {$alias}.id = aclContactCache_{$k}.contact_id OR {$alias}.id IS NULL ) AND aclContactCache_{$k}.user_id = $contactID "; | |
639 | } | |
640 | ||
641 | $this->_aclFrom = implode(" ", $clauses); | |
642 | $this->_aclWhere = NULL; | |
643 | } | |
644 | ||
74cf4551 | 645 | /** |
100fef9d | 646 | * @param int $groupID |
74cf4551 EM |
647 | * |
648 | * @throws Exception | |
649 | */ | |
00be9182 | 650 | public function add2group($groupID) { |
9a2f601e | 651 | if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') { |
652 | CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group')); | |
653 | } | |
654 | ||
7f6464a0 JM |
655 | $new_select = 'AS addtogroup_contact_id'; |
656 | $select = str_ireplace('AS civicrm_contact_contact_target_id', $new_select, $this->_select); | |
657 | $new_having = ' addtogroup_contact_id'; | |
658 | $having = str_ireplace(' civicrm_contact_contact_target_id', $new_having, $this->_having); | |
659 | $query = "$select | |
a8b58e72 | 660 | FROM {$this->temporaryTables['activity_temp_table']['name']} tar |
7f6464a0 | 661 | GROUP BY civicrm_activity_id $having {$this->_orderBy}"; |
9a2f601e | 662 | $select = 'AS addtogroup_contact_id'; |
663 | $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query); | |
dfb264b2 | 664 | CRM_Core_DAO::disableFullGroupByMode(); |
f38e3c19 | 665 | $dao = $this->executeReportQuery($query); |
dfb264b2 | 666 | CRM_Core_DAO::reenableFullGroupByMode(); |
9a2f601e | 667 | |
be2fb01f | 668 | $contactIDs = []; |
9a2f601e | 669 | // Add resulting contacts to group |
670 | while ($dao->fetch()) { | |
671 | if ($dao->addtogroup_contact_id) { | |
672 | $contact_id = explode(';', $dao->addtogroup_contact_id); | |
673 | if ($contact_id[0]) { | |
7bb16323 | 674 | $contactIDs[$contact_id[0]] = $contact_id[0]; |
9a2f601e | 675 | } |
676 | } | |
677 | } | |
678 | ||
9d72cede | 679 | if (!empty($contactIDs)) { |
9a2f601e | 680 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); |
681 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
682 | } | |
683 | else { | |
684 | CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group.")); | |
685 | } | |
686 | } | |
687 | ||
d6c3d40f | 688 | /** |
689 | * @param $fields | |
690 | * @param $files | |
691 | * @param $self | |
692 | * | |
693 | * @return array | |
694 | */ | |
695 | public static function formRule($fields, $files, $self) { | |
be2fb01f | 696 | $errors = []; |
355b8a20 | 697 | $config = CRM_Core_Config::singleton(); |
698 | if (in_array("CiviCase", $config->enableComponents)) { | |
699 | $componentId = CRM_Core_Component::getComponentID('CiviCase'); | |
700 | $caseActivityTypes = CRM_Core_OptionGroup::values('activity_type', TRUE, FALSE, FALSE, " AND v.component_id={$componentId}"); | |
701 | if (!empty($fields['activity_type_id_value']) && is_array($fields['activity_type_id_value']) && empty($fields['include_case_activities_value'])) { | |
702 | foreach ($fields['activity_type_id_value'] as $activityTypeId) { | |
703 | if (in_array($activityTypeId, $caseActivityTypes)) { | |
704 | $errors['fields'] = ts("Please enable 'Include Case Activities' to filter with Case Activity types."); | |
705 | } | |
d6c3d40f | 706 | } |
707 | } | |
708 | } | |
709 | return $errors; | |
710 | } | |
711 | ||
a1bf1058 ML |
712 | /** |
713 | * @param $applyLimit | |
714 | * | |
715 | * @return string | |
716 | */ | |
717 | public function buildQuery($applyLimit = TRUE) { | |
44f817d4 | 718 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
52272965 | 719 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2f4b4d54 | 720 | |
389cfee7 | 721 | //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty' |
be2fb01f CW |
722 | $nullFilters = []; |
723 | foreach (['target', 'source', 'assignee'] as $type) { | |
9d72cede EM |
724 | if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == |
725 | 'nnll' || !empty($this->_params["contact_{$type}_value"]) | |
726 | ) { | |
3ae89fd7 | 727 | $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NOT NULL "; |
389cfee7 | 728 | } |
4c9b6178 | 729 | elseif (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == |
9d72cede EM |
730 | 'nll' |
731 | ) { | |
3ae89fd7 | 732 | $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NULL "; |
b6cab294 | 733 | } |
734 | } | |
735 | ||
d47468d0 | 736 | // @todo - all this temp table stuff is here because pre 4.4 the activity contact |
737 | // form did not exist. | |
738 | // Fixing the way the construct method declares them will make all this redundant. | |
9d72cede | 739 | // 1. fill temp table with target results |
be2fb01f | 740 | $this->buildACLClause(['civicrm_contact_target']); |
1f220d30 | 741 | $this->select('target'); |
d47468d0 | 742 | $this->from(); |
5b1ba2b8 | 743 | $this->customDataFrom(); |
389cfee7 | 744 | $this->where('target'); |
5a14305b | 745 | $tempTableName = $this->createTemporaryTable('activity_temp_table', "{$this->_select} {$this->_from} {$this->_where}"); |
2f4b4d54 | 746 | |
1f220d30 DS |
747 | // 2. add new columns to hold assignee and source results |
748 | // fixme: add when required | |
749 | $tempQuery = " | |
5a14305b | 750 | ALTER TABLE $tempTableName |
e7b3275b | 751 | MODIFY COLUMN civicrm_contact_contact_target_id VARCHAR(128), |
1f220d30 DS |
752 | ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128), |
753 | ADD COLUMN civicrm_contact_contact_source VARCHAR(128), | |
7116d719 | 754 | ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128), |
482691e1 | 755 | ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128), |
8c0ff2a6 | 756 | ADD COLUMN civicrm_phone_contact_assignee_phone VARCHAR(128), |
757 | ADD COLUMN civicrm_phone_contact_source_phone VARCHAR(128), | |
1f220d30 DS |
758 | ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128), |
759 | ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)"; | |
f38e3c19 | 760 | $this->executeReportQuery($tempQuery); |
1f220d30 DS |
761 | |
762 | // 3. fill temp table with assignee results | |
be2fb01f | 763 | $this->buildACLClause(['civicrm_contact_assignee']); |
389cfee7 | 764 | $this->select('assignee'); |
d47468d0 | 765 | $this->buildAssigneeFrom(); |
766 | ||
389cfee7 | 767 | $this->customDataFrom(); |
768 | $this->where('assignee'); | |
769 | $insertCols = implode(',', $this->_selectAliases); | |
5a14305b | 770 | $tempQuery = "INSERT INTO $tempTableName ({$insertCols}) |
1f220d30 | 771 | {$this->_select} |
83e74860 | 772 | {$this->_from} {$this->_where}"; |
f38e3c19 | 773 | $this->executeReportQuery($tempQuery); |
2f4b4d54 | 774 | |
1f220d30 | 775 | // 4. fill temp table with source results |
be2fb01f | 776 | $this->buildACLClause(['civicrm_contact_source']); |
389cfee7 | 777 | $this->select('source'); |
d47468d0 | 778 | $this->buildSourceFrom(); |
389cfee7 | 779 | $this->customDataFrom(); |
780 | $this->where('source'); | |
781 | $insertCols = implode(',', $this->_selectAliases); | |
5a14305b | 782 | $tempQuery = "INSERT INTO $tempTableName ({$insertCols}) |
1f220d30 | 783 | {$this->_select} |
83e74860 | 784 | {$this->_from} {$this->_where}"; |
f38e3c19 | 785 | $this->executeReportQuery($tempQuery); |
2f4b4d54 | 786 | |
1f220d30 | 787 | // 5. show final result set from temp table |
be2fb01f | 788 | $rows = []; |
1f220d30 | 789 | $this->select('final'); |
389cfee7 | 790 | $this->_having = ""; |
791 | if (!empty($nullFilters)) { | |
792 | $this->_having = "HAVING " . implode(' AND ', $nullFilters); | |
793 | } | |
83e74860 | 794 | $this->orderBy(); |
ecf1e543 | 795 | foreach ($this->_sections as $alias => $section) { |
796 | if (!empty($section) && $section['name'] == 'activity_date_time') { | |
5a14305b | 797 | $this->alterSectionHeaderForDateTime($tempTableName, $section['tplField']); |
ecf1e543 | 798 | } |
e06df9af | 799 | } |
a1bf1058 ML |
800 | |
801 | if ($applyLimit) { | |
802 | $this->limit(); | |
803 | } | |
804 | ||
d1641c51 | 805 | $groupByFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, 'civicrm_activity_id'); |
52272965 | 806 | |
cbbc16ec | 807 | $this->_where = " WHERE (1)"; |
52272965 | 808 | $this->buildPermissionClause(); |
cbbc16ec TS |
809 | if ($this->_aclWhere) { |
810 | $this->_where .= " AND {$this->_aclWhere} "; | |
811 | } | |
52272965 | 812 | |
b811ac0b MWMC |
813 | $caseJoin = ''; |
814 | if (!empty($this->_params['include_case_activities_value'])) { | |
815 | $caseJoin = "LEFT JOIN civicrm_case_activity {$this->_aliases['civicrm_case_activity']} ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_case_activity']}.activity_id"; | |
816 | } | |
817 | ||
7116d719 | 818 | $sql = "{$this->_select} |
5a14305b | 819 | FROM $tempTableName tar |
52272965 E |
820 | INNER JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = tar.civicrm_activity_id |
821 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} ON {$this->_aliases['civicrm_activity_contact']}.activity_id = {$this->_aliases['civicrm_activity']}.id | |
822 | AND {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID} | |
cbbc16ec | 823 | LEFT JOIN civicrm_contact contact_civireport ON contact_civireport.id = {$this->_aliases['civicrm_activity_contact']}.contact_id |
b811ac0b | 824 | {$caseJoin} |
cbbc16ec | 825 | {$this->_where} {$groupByFromSelect} {$this->_having} {$this->_orderBy} {$this->_limit}"; |
52272965 | 826 | |
a1bf1058 | 827 | CRM_Utils_Hook::alterReportVar('sql', $this, $this); |
cbbc16ec | 828 | $this->addToDeveloperTab($sql); |
a1bf1058 ML |
829 | |
830 | return $sql; | |
831 | } | |
832 | ||
833 | public function postProcess() { | |
834 | //reset value of activity_date | |
835 | if (!empty($this->_resetDateFilter)) { | |
836 | $this->_formValues["activity_date_time_relative"] = NULL; | |
837 | } | |
838 | ||
839 | $this->beginPostProcess(); | |
840 | $sql = $this->buildQuery(TRUE); | |
2f4b4d54 DS |
841 | $this->buildRows($sql, $rows); |
842 | ||
843 | // format result set. | |
1f220d30 | 844 | $this->formatDisplay($rows); |
2f4b4d54 DS |
845 | |
846 | // assign variables to templates | |
847 | $this->doTemplateAssignment($rows); | |
848 | ||
849 | // do print / pdf / instance stuff if needed | |
850 | $this->endPostProcess($rows); | |
6a488035 TO |
851 | } |
852 | ||
74cf4551 | 853 | /** |
4b62bc4f EM |
854 | * Alter display of rows. |
855 | * | |
856 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
857 | * such as rendering contacts as links. | |
858 | * | |
859 | * @param array $rows | |
860 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 861 | */ |
00be9182 | 862 | public function alterDisplay(&$rows) { |
9d72cede EM |
863 | $entryFound = FALSE; |
864 | $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); | |
6a488035 | 865 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
410e8105 | 866 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
9d72cede | 867 | $viewLinks = FALSE; |
f2e37338 | 868 | |
d47468d0 | 869 | // Would we ever want to retrieve from the form controller?? |
870 | $form = $this->noController ? NULL : $this; | |
871 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form, FALSE, 'report'); | |
9d72cede | 872 | $actUrl = ''; |
6a488035 TO |
873 | |
874 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
9d72cede EM |
875 | $viewLinks = TRUE; |
876 | $onHover = ts('View Contact Summary for this Contact'); | |
6a488035 TO |
877 | $onHoverAct = ts('View Activity Record'); |
878 | } | |
38ba593b | 879 | foreach ($rows as $rowNum => $row) { |
1efec7ff | 880 | // if we have an activity type, format the View Activity link for use in various columns |
9d72cede EM |
881 | if ($viewLinks && |
882 | array_key_exists('civicrm_activity_activity_type_id', $row) | |
883 | ) { | |
1efec7ff DG |
884 | // Check for target contact id(s) and use the first contact id in that list for view activity link if found, |
885 | // else use source contact id | |
886 | if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) { | |
887 | $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']); | |
888 | $cid = $targets[0]; | |
889 | } | |
890 | else { | |
891 | $cid = $rows[$rowNum]['civicrm_contact_contact_source_id']; | |
892 | } | |
893 | ||
b811ac0b MWMC |
894 | if (empty($this->_params['include_case_activities_value']) || empty($rows[$rowNum]['civicrm_case_activity_case_id'])) { |
895 | // Generate a "view activity" link | |
896 | $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'], | |
897 | CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]), | |
898 | FALSE, | |
899 | $rows[$rowNum]['civicrm_activity_id'] | |
900 | ); | |
901 | ||
902 | $actLinkValues = [ | |
903 | 'id' => $rows[$rowNum]['civicrm_activity_id'], | |
904 | 'cid' => $cid, | |
905 | 'cxt' => $context, | |
906 | ]; | |
907 | $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'], | |
908 | CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE | |
909 | ); | |
910 | } | |
911 | else { | |
912 | // Generate a "view case activity" link | |
913 | $caseActionLinks = CRM_Case_Selector_Search::actionLinks(); | |
914 | $caseLinkValues = [ | |
915 | 'aid' => $rows[$rowNum]['civicrm_activity_id'], | |
916 | 'caseid' => $rows[$rowNum]['civicrm_case_activity_case_id'], | |
917 | 'cid' => $cid, | |
918 | 'cxt' => $context, | |
919 | ]; | |
920 | $actUrl = CRM_Utils_System::url($caseActionLinks[CRM_Core_Action::VIEW]['url'], | |
921 | CRM_Core_Action::replace($caseActionLinks[CRM_Core_Action::VIEW]['qs'], $caseLinkValues), TRUE | |
922 | ); | |
923 | } | |
1efec7ff DG |
924 | } |
925 | ||
482691e1 RN |
926 | if (array_key_exists('civicrm_contact_contact_source', $row)) { |
927 | if ($value = $row['civicrm_contact_contact_source_id']) { | |
928 | if ($viewLinks) { | |
929 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
930 | 'reset=1&cid=' . $value, | |
931 | $this->_absoluteUrl | |
932 | ); | |
933 | $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url; | |
934 | $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover; | |
935 | } | |
936 | $entryFound = TRUE; | |
937 | } | |
938 | } | |
939 | ||
940 | if (array_key_exists('civicrm_contact_contact_assignee', $row)) { | |
941 | $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']); | |
942 | if ($value = $row['civicrm_contact_contact_assignee_id']) { | |
943 | $assigneeContactIds = explode(';', $value); | |
be2fb01f | 944 | $link = []; |
482691e1 RN |
945 | if ($viewLinks) { |
946 | foreach ($assigneeContactIds as $id => $value) { | |
947 | if (isset($value) && isset($assigneeNames[$id])) { | |
948 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
949 | 'reset=1&cid=' . $value, | |
950 | $this->_absoluteUrl | |
951 | ); | |
9d72cede EM |
952 | $link[] = "<a title='" . $onHover . "' href='" . $url . |
953 | "'>{$assigneeNames[$id]}</a>"; | |
482691e1 RN |
954 | } |
955 | } | |
956 | $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link); | |
957 | } | |
958 | $entryFound = TRUE; | |
959 | } | |
960 | } | |
961 | ||
962 | if (array_key_exists('civicrm_contact_contact_target', $row)) { | |
963 | $targetNames = explode(';', $row['civicrm_contact_contact_target']); | |
964 | if ($value = $row['civicrm_contact_contact_target_id']) { | |
965 | $targetContactIds = explode(';', $value); | |
be2fb01f | 966 | $link = []; |
482691e1 RN |
967 | if ($viewLinks) { |
968 | foreach ($targetContactIds as $id => $value) { | |
969 | if (isset($value) && isset($targetNames[$id])) { | |
970 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
971 | 'reset=1&cid=' . $value, | |
972 | $this->_absoluteUrl | |
973 | ); | |
9d72cede EM |
974 | $link[] = "<a title='" . $onHover . "' href='" . $url . |
975 | "'>{$targetNames[$id]}</a>"; | |
482691e1 RN |
976 | } |
977 | } | |
978 | $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link); | |
979 | } | |
980 | $entryFound = TRUE; | |
981 | } | |
982 | } | |
983 | ||
6a488035 TO |
984 | if (array_key_exists('civicrm_activity_activity_type_id', $row)) { |
985 | if ($value = $row['civicrm_activity_activity_type_id']) { | |
986 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value]; | |
482691e1 | 987 | if ($viewLinks) { |
1efec7ff | 988 | $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $actUrl; |
482691e1 RN |
989 | $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct; |
990 | } | |
6a488035 TO |
991 | $entryFound = TRUE; |
992 | } | |
993 | } | |
994 | ||
995 | if (array_key_exists('civicrm_activity_status_id', $row)) { | |
996 | if ($value = $row['civicrm_activity_status_id']) { | |
997 | $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value]; | |
998 | $entryFound = TRUE; | |
999 | } | |
1000 | } | |
1001 | ||
410e8105 | 1002 | if (array_key_exists('civicrm_activity_priority_id', $row)) { |
1003 | if ($value = $row['civicrm_activity_priority_id']) { | |
1004 | $rows[$rowNum]['civicrm_activity_priority_id'] = $priority[$value]; | |
1005 | $entryFound = TRUE; | |
1006 | } | |
1007 | } | |
1008 | ||
34e0bdb6 | 1009 | if (array_key_exists('civicrm_activity_details', $row) && $this->_outputMode == 'html') { |
1efec7ff DG |
1010 | if ($value = $row['civicrm_activity_details']) { |
1011 | $fullDetails = $rows[$rowNum]['civicrm_activity_details']; | |
1012 | $rows[$rowNum]['civicrm_activity_details'] = substr($fullDetails, 0, strrpos(substr($fullDetails, 0, 80), ' ')); | |
1013 | if ($actUrl) { | |
6e5e50a9 | 1014 | $rows[$rowNum]['civicrm_activity_details'] .= " <a href='{$actUrl}' title='{$onHoverAct}'>(more)</a>"; |
1efec7ff DG |
1015 | } |
1016 | $entryFound = TRUE; | |
1017 | } | |
1018 | } | |
1019 | ||
6a488035 TO |
1020 | if (array_key_exists('civicrm_activity_campaign_id', $row)) { |
1021 | if ($value = $row['civicrm_activity_campaign_id']) { | |
254ca428 | 1022 | $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->campaigns[$value]; |
6a488035 TO |
1023 | $entryFound = TRUE; |
1024 | } | |
1025 | } | |
1026 | ||
1027 | if (array_key_exists('civicrm_activity_engagement_level', $row)) { | |
1028 | if ($value = $row['civicrm_activity_engagement_level']) { | |
1029 | $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value]; | |
1030 | $entryFound = TRUE; | |
1031 | } | |
1032 | } | |
1033 | ||
9d72cede EM |
1034 | if (array_key_exists('civicrm_activity_activity_date_time', $row) && |
1035 | array_key_exists('civicrm_activity_status_id', $row) | |
1036 | ) { | |
6a488035 TO |
1037 | if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) && |
1038 | $activityStatus[$row['civicrm_activity_status_id']] != 'Completed' | |
1039 | ) { | |
1040 | $rows[$rowNum]['class'] = "status-overdue"; | |
1041 | $entryFound = TRUE; | |
1042 | } | |
1043 | } | |
1044 | ||
66ad82d6 | 1045 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this', ';') ? TRUE : $entryFound; |
482691e1 | 1046 | |
6a488035 TO |
1047 | if (!$entryFound) { |
1048 | break; | |
1049 | } | |
1050 | } | |
1051 | } | |
667abe3a | 1052 | |
00be9182 | 1053 | public function sectionTotals() { |
667abe3a | 1054 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. |
1055 | if (empty($this->_selectAliases)) { | |
1056 | return; | |
1057 | } | |
1058 | ||
1059 | if (!empty($this->_sections)) { | |
1060 | // pull section aliases out of $this->_sections | |
1061 | $sectionAliases = array_keys($this->_sections); | |
1062 | ||
be2fb01f | 1063 | $ifnulls = []; |
667abe3a | 1064 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { |
1065 | $ifnulls[] = "ifnull($alias, '') as $alias"; | |
1066 | } | |
b708c08d | 1067 | $this->_select = "SELECT " . implode(", ", $ifnulls); |
36d2f4d5 | 1068 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($ifnulls, $sectionAliases); |
667abe3a | 1069 | |
b708c08d | 1070 | $query = $this->_select . |
ab663ec4 | 1071 | ", count(DISTINCT civicrm_activity_id) as ct from {$this->temporaryTables['activity_temp_table']['name']} group by " . |
667abe3a | 1072 | implode(", ", $sectionAliases); |
1073 | ||
1074 | // initialize array of total counts | |
be2fb01f | 1075 | $totals = []; |
f38e3c19 | 1076 | $dao = $this->executeReportQuery($query); |
667abe3a | 1077 | while ($dao->fetch()) { |
1078 | // let $this->_alterDisplay translate any integer ids to human-readable values. | |
1079 | $rows[0] = $dao->toArray(); | |
1080 | $this->alterDisplay($rows); | |
1081 | $row = $rows[0]; | |
1082 | ||
1083 | // add totals for all permutations of section values | |
be2fb01f | 1084 | $values = []; |
667abe3a | 1085 | $i = 1; |
1086 | $aliasCount = count($sectionAliases); | |
1087 | foreach ($sectionAliases as $alias) { | |
1088 | $values[] = $row[$alias]; | |
1089 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); | |
1090 | if ($i == $aliasCount) { | |
1091 | // the last alias is the lowest-level section header; use count as-is | |
1092 | $totals[$key] = $dao->ct; | |
1093 | } | |
1094 | else { | |
1095 | // other aliases are higher level; roll count into their total | |
1096 | $totals[$key] += $dao->ct; | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | $this->assign('sectionTotals', $totals); | |
1101 | } | |
1102 | } | |
96025800 | 1103 | |
d47468d0 | 1104 | /** |
1105 | * @todo remove this function & declare the 3 contact tables separately | |
1106 | * | |
1107 | * (Currently the construct method incorrectly melds them - this is an interim | |
1108 | * refactor in order to get this under ReportTemplateTests) | |
1109 | */ | |
1110 | protected function buildAssigneeFrom() { | |
b811ac0b | 1111 | $this->buildFrom('assignee'); |
d47468d0 | 1112 | } |
1113 | ||
1114 | /** | |
1115 | * @todo remove this function & declare the 3 contact tables separately | |
1116 | * | |
1117 | * (Currently the construct method incorrectly melds them - this is an interim | |
1118 | * refactor in order to get this under ReportTemplateTests) | |
1119 | */ | |
1120 | protected function buildSourceFrom() { | |
b811ac0b MWMC |
1121 | $this->buildFrom('source'); |
1122 | } | |
1123 | ||
1124 | /** | |
1125 | * Shared function to build the from clause | |
1126 | * | |
1127 | * @param string $recordType (one of 'source', 'activity', 'target') | |
1128 | */ | |
1129 | protected function buildFrom($recordType) { | |
d47468d0 | 1130 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
b811ac0b MWMC |
1131 | switch ($recordType) { |
1132 | case 'target': | |
1133 | $recordTypeID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
1134 | break; | |
1135 | ||
1136 | case 'source': | |
1137 | $recordTypeID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
1138 | break; | |
1139 | ||
1140 | case 'assignee': | |
1141 | $recordTypeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
1142 | break; | |
1143 | ||
1144 | } | |
1145 | ||
d47468d0 | 1146 | $this->_from = " |
b811ac0b MWMC |
1147 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} |
1148 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} | |
1149 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
1150 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$recordTypeID} | |
1151 | INNER JOIN civicrm_contact civicrm_contact_{$recordType} | |
1152 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_{$recordType}.id | |
1153 | {$this->_aclFrom}"; | |
1154 | if (!empty($this->_params['include_case_activities_value'])) { | |
1155 | $this->_from .= " | |
1156 | LEFT JOIN civicrm_case_activity {$this->_aliases['civicrm_case_activity']} | |
1157 | ON {$this->_aliases['civicrm_case_activity']}.activity_id = {$this->_aliases['civicrm_activity']}.id"; | |
1158 | } | |
d47468d0 | 1159 | |
1160 | if ($this->isTableSelected('civicrm_email')) { | |
1161 | $this->_from .= " | |
b811ac0b MWMC |
1162 | LEFT JOIN civicrm_email civicrm_email_{$recordType} |
1163 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_{$recordType}.contact_id AND | |
1164 | civicrm_email_{$recordType}.is_primary = 1"; | |
d47468d0 | 1165 | } |
b811ac0b | 1166 | |
d47468d0 | 1167 | if ($this->isTableSelected('civicrm_phone')) { |
1168 | $this->_from .= " | |
b811ac0b MWMC |
1169 | LEFT JOIN civicrm_phone civicrm_phone_{$recordType} |
1170 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_{$recordType}.contact_id AND | |
1171 | civicrm_phone_{$recordType}.is_primary = 1 "; | |
d47468d0 | 1172 | } |
b811ac0b MWMC |
1173 | $this->_aliases['civicrm_contact'] = "civicrm_contact_{$recordType}"; |
1174 | ||
d47468d0 | 1175 | $this->joinAddressFromContact(); |
1176 | } | |
1177 | ||
6a488035 | 1178 | } |