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