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