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