Merge pull request #4666 from davecivicrm/CRM-15180
[civicrm-core.git] / CRM / Report / Form / Activity.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Activity extends CRM_Report_Form {
1f220d30 36 protected $_selectAliasesTotal = array();
6a488035
TO
37
38 protected $_customGroupExtends = array(
430ae6dd
TO
39 'Activity'
40 );
41
09febbae 42 protected $_nonDisplayFields = array();
43
74cf4551
EM
44 /**
45 *
46 */
47 /**
48 *
49 */
430ae6dd 50 function __construct() {
7116d719 51 // There could be multiple contacts. We not clear on which contact id to display.
83e74860
DS
52 // Lets hide it for now.
53 $this->_exposeContactID = FALSE;
54
6a488035
TO
55 $config = CRM_Core_Config::singleton();
56 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
57 if ($campaignEnabled) {
58 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
59 $this->activeCampaigns = $getCampaigns['campaigns'];
60 asort($this->activeCampaigns);
61 $this->engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
62 }
63 $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE);
64 asort($this->activityTypes);
65
66 $this->_columns = array(
67 'civicrm_contact' =>
68 array(
69 'dao' => 'CRM_Contact_DAO_Contact',
70 'fields' =>
71 array(
6a488035
TO
72 'contact_source' =>
73 array(
74 'name' => 'sort_name',
75 'title' => ts('Source Contact Name'),
76 'alias' => 'civicrm_contact_source',
77 'no_repeat' => TRUE,
78 ),
79 'contact_assignee' =>
80 array(
81 'name' => 'sort_name',
82 'title' => ts('Assignee Contact Name'),
83 'alias' => 'civicrm_contact_assignee',
1f220d30 84 'dbAlias' => "civicrm_contact_assignee.sort_name",
6a488035
TO
85 'default' => TRUE,
86 ),
87 'contact_target' =>
88 array(
89 'name' => 'sort_name',
90 'title' => ts('Target Contact Name'),
2f4b4d54 91 'alias' => 'civicrm_contact_target',
1f220d30 92 'dbAlias' => "civicrm_contact_target.sort_name",
6a488035
TO
93 'default' => TRUE,
94 ),
482691e1
RN
95 'contact_source_id' =>
96 array(
97 'name' => 'id',
98 'alias' => 'civicrm_contact_source',
99 'dbAlias' => "civicrm_contact_source.id",
100 'no_display' => TRUE,
101 'default' => TRUE,
102 'required' => TRUE,
103 ),
104 'contact_assignee_id' =>
105 array(
106 'name' => 'id',
107 'alias' => 'civicrm_contact_assignee',
108 'dbAlias' => "civicrm_contact_assignee.id",
109 'no_display' => TRUE,
110 'default' => TRUE,
111 'required' => TRUE,
112 ),
113 'contact_target_id' =>
114 array(
115 'name' => 'id',
116 'alias' => 'civicrm_contact_target',
117 'dbAlias' => "civicrm_contact_target.id",
118 'no_display' => TRUE,
119 'default' => TRUE,
120 'required' => TRUE,
121 ),
6a488035
TO
122 ),
123 'filters' =>
124 array(
125 'contact_source' =>
126 array(
127 'name' => 'sort_name',
128 'alias' => 'civicrm_contact_source',
129 'title' => ts('Source Contact Name'),
130 'operator' => 'like',
131 'type' => CRM_Report_Form::OP_STRING,
132 ),
133 'contact_assignee' =>
134 array(
135 'name' => 'sort_name',
136 'alias' => 'civicrm_contact_assignee',
137 'title' => ts('Assignee Contact Name'),
138 'operator' => 'like',
139 'type' => CRM_Report_Form::OP_STRING,
140 ),
141 'contact_target' =>
142 array(
143 'name' => 'sort_name',
2f4b4d54 144 'alias' => 'civicrm_contact_target',
6a488035
TO
145 'title' => ts('Target Contact Name'),
146 'operator' => 'like',
147 'type' => CRM_Report_Form::OP_STRING,
148 ),
149 'current_user' =>
150 array(
151 'name' => 'current_user',
152 'title' => ts('Limit To Current User'),
153 'type' => CRM_Utils_Type::T_INT,
154 'operatorType' => CRM_Report_Form::OP_SELECT,
155 'options' => array('0' => ts('No'), '1' => ts('Yes')),
156 ),
157 ),
158 'grouping' => 'contact-fields',
159 ),
160 'civicrm_email' =>
161 array(
162 'dao' => 'CRM_Core_DAO_Email',
163 'fields' =>
164 array(
165 'contact_source_email' =>
166 array(
167 'name' => 'email',
168 'title' => ts('Source Contact Email'),
169 'alias' => 'civicrm_email_source',
170 ),
171 'contact_assignee_email' =>
172 array(
173 'name' => 'email',
174 'title' => ts('Assignee Contact Email'),
175 'alias' => 'civicrm_email_assignee',
176 ),
177 'contact_target_email' =>
178 array(
179 'name' => 'email',
180 'title' => ts('Target Contact Email'),
181 'alias' => 'civicrm_email_target',
182 ),
183 ),
184 'order_bys' =>
185 array(
186 'source_contact_email' =>
187 array(
188 'name' => 'email',
189 'title' => ts('Source Contact Email'),
83e74860 190 'dbAlias' => 'civicrm_email_contact_source_email',
6a488035
TO
191 ),
192 ),
193 ),
194 'civicrm_activity' =>
195 array(
196 'dao' => 'CRM_Activity_DAO_Activity',
197 'fields' =>
198 array(
199 'id' =>
200 array(
83e74860 201 'no_display' => TRUE,
2f4b4d54 202 'title' => ts('Activity ID'),
1f220d30 203 'required' => TRUE,
6a488035
TO
204 ),
205 'source_record_id' =>
206 array(
207 'no_display' => TRUE,
208 'required' => TRUE,
209 ),
210 'activity_type_id' =>
211 array('title' => ts('Activity Type'),
7116d719 212 'required' => TRUE,
6a488035
TO
213 'type' => CRM_Utils_Type::T_STRING,
214 ),
215 'activity_subject' =>
216 array('title' => ts('Subject'),
217 'default' => TRUE,
218 ),
6a488035
TO
219 'activity_date_time' =>
220 array('title' => ts('Activity Date'),
7116d719 221 'required' => TRUE,
6a488035
TO
222 ),
223 'status_id' =>
224 array('title' => ts('Activity Status'),
225 'default' => TRUE,
226 'type' => CRM_Utils_Type::T_STRING,
227 ),
228 'duration' =>
229 array('title' => ts('Duration'),
230 'type' => CRM_Utils_Type::T_INT,
231 ),
1efec7ff
DG
232 'details' => array(
233 'title' => ts('Activity Details'),
234 )
6a488035 235 ),
1efec7ff
DG
236 'filters' => array(
237 'activity_date_time' => array(
6a488035
TO
238 'default' => 'this.month',
239 'operatorType' => CRM_Report_Form::OP_DATE,
240 ),
241 'activity_subject' =>
242 array('title' => ts('Activity Subject')),
243 'activity_type_id' =>
244 array('title' => ts('Activity Type'),
245 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
246 'options' => $this->activityTypes,
247 ),
248 'status_id' =>
249 array('title' => ts('Activity Status'),
250 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
251 'options' => CRM_Core_PseudoConstant::activityStatus(),
252 ),
1efec7ff
DG
253 'details' => array(
254 'title' => ts('Activity Details'),
255 'type' => CRM_Utils_Type::T_TEXT,
256 )
6a488035 257 ),
1efec7ff 258 'order_bys' => array(
6a488035 259 'activity_date_time' =>
83e74860 260 array('title' => ts('Activity Date'), 'default_weight' => '1', 'dbAlias' => 'civicrm_activity_activity_date_time'),
6a488035 261 'activity_type_id' =>
83e74860 262 array('title' => ts('Activity Type'), 'default_weight' => '2', 'dbAlias' => 'civicrm_activity_activity_type_id'),
6a488035
TO
263 ),
264 'grouping' => 'activity-fields',
265 'alias' => 'activity',
266 ),
1f220d30 267 'civicrm_activity_contact' =>
6a488035 268 array(
38ba593b 269 'dao' => 'CRM_Activity_DAO_ActivityContact',
6a488035
TO
270 'fields' =>
271 array(
1f220d30 272 // so we have $this->_alias populated
6a488035 273 ),
6a488035 274 ),
6a488035
TO
275 ) + $this->addAddressFields(FALSE, TRUE);
276
277 if ($campaignEnabled) {
278 // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled
279
280 $this->_columns['civicrm_activity']['fields']['result'] = array(
281 'title' => 'Survey Result',
282 'default' => 'false',
283 );
284 $this->_columns['civicrm_activity']['filters']['result'] = array('title' => ts('Survey Result'),
285 'operator' => 'like',
286 'type' => CRM_Utils_Type::T_STRING,
287 );
288 if (!empty($this->activeCampaigns)) {
289 $this->_columns['civicrm_activity']['fields']['campaign_id'] = array(
290 'title' => 'Campaign',
291 'default' => 'false',
292 );
293 $this->_columns['civicrm_activity']['filters']['campaign_id'] = array('title' => ts('Campaign'),
294 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
295 'options' => $this->activeCampaigns,
296 );
297 }
298 if (!empty($this->engagementLevels)) {
299 $this->_columns['civicrm_activity']['fields']['engagement_level'] = array(
300 'title' => 'Engagement Index',
301 'default' => 'false',
302 );
303 $this->_columns['civicrm_activity']['filters']['engagement_level'] = array('title' => ts('Engagement Index'),
304 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
305 'options' => $this->engagementLevels,
306 );
307 }
308 }
309 $this->_groupFilter = TRUE;
310 $this->_tagFilter = TRUE;
311 parent::__construct();
312 }
313
74cf4551
EM
314 /**
315 * @param null $recordType
316 */
1f220d30 317 function select($recordType = NULL) {
09febbae 318 if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && $recordType != 'final') {
319 $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}";
09febbae 320 }
1f220d30 321 parent::select();
1f220d30 322
09febbae 323 if ($recordType == 'final' && !empty($this->_nonDisplayFields)) {
324 foreach ($this->_nonDisplayFields as $fieldName) {
325 unset($this->_columnHeaders[$fieldName]);
326 }
327 }
328
1f220d30
DS
329 if (empty($this->_selectAliasesTotal)) {
330 $this->_selectAliasesTotal = $this->_selectAliases;
331 }
332
333 $removeKeys = array();
334 if ($recordType == 'target') {
335 foreach ($this->_selectClauses as $key => $clause) {
7116d719 336 if (strstr($clause, 'civicrm_contact_assignee.') ||
1f220d30
DS
337 strstr($clause, 'civicrm_contact_source.') ||
338 strstr($clause, 'civicrm_email_assignee.') ||
339 strstr($clause, 'civicrm_email_source.')
340 ) {
341 $removeKeys[] = $key;
342 unset($this->_selectClauses[$key]);
343 }
344 }
345 } else if ($recordType == 'assignee') {
346 foreach ($this->_selectClauses as $key => $clause) {
7116d719 347 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
348 strstr($clause, 'civicrm_contact_source.') ||
349 strstr($clause, 'civicrm_email_target.') ||
482691e1 350 strstr($clause, 'civicrm_email_source.')
1f220d30
DS
351 ) {
352 $removeKeys[] = $key;
353 unset($this->_selectClauses[$key]);
354 }
355 }
356 } else if ($recordType == 'source') {
357 foreach ($this->_selectClauses as $key => $clause) {
7116d719 358 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
359 strstr($clause, 'civicrm_contact_assignee.') ||
360 strstr($clause, 'civicrm_email_target.') ||
482691e1 361 strstr($clause, 'civicrm_email_assignee.')
1f220d30
DS
362 ) {
363 $removeKeys[] = $key;
364 unset($this->_selectClauses[$key]);
365 }
366 }
367 } else if ($recordType == 'final') {
368 $this->_selectClauses = $this->_selectAliasesTotal;
369 foreach ($this->_selectClauses as $key => $clause) {
7116d719 370 if (strstr($clause, 'civicrm_contact_contact_target') ||
1f220d30
DS
371 strstr($clause, 'civicrm_contact_contact_assignee') ||
372 strstr($clause, 'civicrm_contact_contact_source') ) {
482691e1 373 $this->_selectClauses[$key] = "GROUP_CONCAT($clause SEPARATOR ';') as $clause";
6a488035
TO
374 }
375 }
376 }
377
1f220d30
DS
378 if ($recordType) {
379 foreach ($removeKeys as $key) {
380 unset($this->_selectAliases[$key]);
381 }
382
1f220d30 383 $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " ";
1f220d30 384 }
6a488035
TO
385 }
386
74cf4551
EM
387 /**
388 * @param $recordType
389 */
2f4b4d54 390 function from($recordType) {
e7e657f0 391 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
9e74e3ce 392 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
2f4b4d54
DS
393 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
394 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 395
2f4b4d54
DS
396 if ($recordType == 'target') {
397 $this->_from = "
398 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
399 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
400 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
401 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$targetID}
2f4b4d54 402 INNER JOIN civicrm_contact civicrm_contact_target
1f220d30 403 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id
2f4b4d54
DS
404 {$this->_aclFrom}";
405
406 if ($this->isTableSelected('civicrm_email')) {
407 $this->_from .= "
1f220d30
DS
408 LEFT JOIN civicrm_email civicrm_email_target
409 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_target.contact_id AND
410 civicrm_email_target.is_primary = 1";
2f4b4d54 411 }
1f220d30 412 $this->_aliases['civicrm_contact'] = 'civicrm_contact_target';
2f4b4d54
DS
413 }
414
415 if ($recordType == 'assignee') {
416 $this->_from = "
417 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
418 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
419 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
420 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$assigneeID}
2f4b4d54 421 INNER JOIN civicrm_contact civicrm_contact_assignee
1f220d30 422 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id
2f4b4d54
DS
423 {$this->_aclFrom}";
424
425 if ($this->isTableSelected('civicrm_email')) {
426 $this->_from .= "
1f220d30
DS
427 LEFT JOIN civicrm_email civicrm_email_assignee
428 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_assignee.contact_id AND
429 civicrm_email_assignee.is_primary = 1";
2f4b4d54 430 }
482691e1 431 $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee';
2f4b4d54
DS
432 }
433
434 if ($recordType == 'source') {
435 $this->_from = "
436 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
437 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
438 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
439 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID}
2f4b4d54 440 INNER JOIN civicrm_contact civicrm_contact_source
1f220d30 441 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id
2f4b4d54
DS
442 {$this->_aclFrom}";
443
444 if ($this->isTableSelected('civicrm_email')) {
445 $this->_from .= "
1f220d30
DS
446 LEFT JOIN civicrm_email civicrm_email_source
447 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_source.contact_id AND
448 civicrm_email_source.is_primary = 1";
2f4b4d54 449 }
482691e1 450 $this->_aliases['civicrm_contact'] = 'civicrm_contact_source';
6a488035 451 }
482691e1 452 $this->addAddressFromClause();
6a488035
TO
453 }
454
74cf4551
EM
455 /**
456 * @param null $recordType
457 */
b6cab294 458 function where($recordType = NULL) {
2f4b4d54 459 $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
6a488035
TO
460 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
461 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
462
463 $clauses = array();
464 foreach ($this->_columns as $tableName => $table) {
465 if (array_key_exists('filters', $table)) {
466
467 foreach ($table['filters'] as $fieldName => $field) {
468 $clause = NULL;
7116d719 469 if ($fieldName != 'contact_' . $recordType &&
470 (strstr($fieldName, '_target') ||
471 strstr($fieldName, '_assignee') ||
472 strstr($fieldName, '_source')
473 )
474 ) {
7116d719 475 continue;
476 }
6a488035
TO
477 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
478 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
479 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
480 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
481
482 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
483 }
484 else {
485 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
389cfee7 486 if ($op && ($op != 'nnll' || $op != 'nll')) {
6a488035
TO
487 $clause = $this->whereClause($field,
488 $op,
489 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
490 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
491 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
492 );
42b3be07
N
493 if ($fieldName == 'activity_type_id' && empty($this->_params['activity_type_id_value'])) {
494 $actTypes = array_flip(CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE));
495 $clause = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN (".implode(',',$actTypes).") )";
496 }
6a488035
TO
497 }
498 }
499
500 if ($field['name'] == 'current_user') {
501 if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) == 1) {
502 // get current user
503 $session = CRM_Core_Session::singleton();
504 if ($contactID = $session->get('userID')) {
389cfee7 505 $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN
506 (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})";
6a488035
TO
507 }
508 else {
509 $clause = NULL;
510 }
511 }
512 else {
513 $clause = NULL;
514 }
515 }
516 if (!empty($clause)) {
517 $clauses[] = $clause;
518 }
519 }
520 }
521 }
522
523 if (empty($clauses)) {
b6cab294 524 $this->_where .= " ";
6a488035
TO
525 }
526 else {
b6cab294 527 $this->_where .= " AND " . implode(' AND ', $clauses);
6a488035
TO
528 }
529
530 if ($this->_aclWhere) {
531 $this->_where .= " AND {$this->_aclWhere} ";
532 }
533 }
534
535 function groupBy() {
536 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id";
537 }
538
74cf4551
EM
539 /**
540 * @param string $tableAlias
541 */
6a488035 542 function buildACLClause($tableAlias = 'contact_a') {
2a3c101f 543 //override for ACL( Since Contact may be source
6a488035
TO
544 //contact/assignee or target also it may be null )
545
546 if (CRM_Core_Permission::check('view all contacts')) {
547 $this->_aclFrom = $this->_aclWhere = NULL;
548 return;
549 }
550
551 $session = CRM_Core_Session::singleton();
552 $contactID = $session->get('userID');
553 if (!$contactID) {
554 $contactID = 0;
555 }
556 $contactID = CRM_Utils_Type::escape($contactID, 'Integer');
557
558 CRM_Contact_BAO_Contact_Permission::cache($contactID);
559 $clauses = array();
560 foreach ($tableAlias as $k => $alias) {
561 $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 ";
562 }
563
564 $this->_aclFrom = implode(" ", $clauses);
565 $this->_aclWhere = NULL;
566 }
567
74cf4551 568 /**
100fef9d 569 * @param int $groupID
74cf4551
EM
570 *
571 * @throws Exception
572 */
9a2f601e 573 function add2group($groupID) {
574 if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') {
575 CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group'));
576 }
577
578 $query = "{$this->_select}
579FROM civireport_activity_temp_target tar
580GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}";
581 $select = 'AS addtogroup_contact_id';
582 $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query);
583 $dao = CRM_Core_DAO::executeQuery($query);
584
585 $contactIDs = array();
586 // Add resulting contacts to group
587 while ($dao->fetch()) {
588 if ($dao->addtogroup_contact_id) {
589 $contact_id = explode(';', $dao->addtogroup_contact_id);
590 if ($contact_id[0]) {
7bb16323 591 $contactIDs[$contact_id[0]] = $contact_id[0];
9a2f601e 592 }
593 }
594 }
595
596 if ( !empty($contactIDs) ) {
597 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
598 CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
599 }
600 else {
601 CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group."));
602 }
603 }
604
6a488035 605 function postProcess() {
2f4b4d54
DS
606 $this->beginPostProcess();
607
389cfee7 608 //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty'
609 $nullFilters = array();
b6cab294 610 foreach (array('target', 'source', 'assignee') as $type) {
8cc574cf 611 if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll' || !empty($this->_params["contact_{$type}_value"])) {
3ae89fd7 612 $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NOT NULL ";
389cfee7 613 }
614 else if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nll') {
3ae89fd7 615 $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NULL ";
b6cab294 616 }
617 }
618
389cfee7 619 // 1. fill temp table with target results
6e5e50a9 620 $this->buildACLClause(array('civicrm_contact_target'));
1f220d30 621 $this->select('target');
2f4b4d54 622 $this->from('target');
5b1ba2b8 623 $this->customDataFrom();
389cfee7 624 $this->where('target');
1f220d30 625 $insertCols = implode(',', $this->_selectAliases);
7116d719 626 $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
482691e1 627{$this->_select} {$this->_from} {$this->_where} ";
2f4b4d54 628 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 629
1f220d30
DS
630 // 2. add new columns to hold assignee and source results
631 // fixme: add when required
632 $tempQuery = "
633 ALTER TABLE civireport_activity_temp_target
634 ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128),
635 ADD COLUMN civicrm_contact_contact_source VARCHAR(128),
7116d719 636 ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128),
482691e1 637 ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128),
1f220d30
DS
638 ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128),
639 ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)";
640 CRM_Core_DAO::executeQuery($tempQuery);
641
642 // 3. fill temp table with assignee results
6e5e50a9 643 $this->buildACLClause(array('civicrm_contact_assignee'));
389cfee7 644 $this->select('assignee');
645 $this->from('assignee');
646 $this->customDataFrom();
647 $this->where('assignee');
648 $insertCols = implode(',', $this->_selectAliases);
649 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 650{$this->_select}
83e74860 651{$this->_from} {$this->_where}";
389cfee7 652 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 653
1f220d30 654 // 4. fill temp table with source results
6e5e50a9 655 $this->buildACLClause(array('civicrm_contact_source'));
389cfee7 656 $this->select('source');
657 $this->from('source');
658 $this->customDataFrom();
659 $this->where('source');
660 $insertCols = implode(',', $this->_selectAliases);
661 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 662{$this->_select}
83e74860 663{$this->_from} {$this->_where}";
389cfee7 664 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 665
1f220d30 666 // 5. show final result set from temp table
2f4b4d54 667 $rows = array();
1f220d30 668 $this->select('final');
389cfee7 669 $this->_having = "";
670 if (!empty($nullFilters)) {
671 $this->_having = "HAVING " . implode(' AND ', $nullFilters);
672 }
83e74860 673 $this->orderBy();
1f220d30 674 $this->limit();
7116d719 675 $sql = "{$this->_select}
676FROM civireport_activity_temp_target tar
389cfee7 677GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}";
2f4b4d54
DS
678 $this->buildRows($sql, $rows);
679
680 // format result set.
1f220d30 681 $this->formatDisplay($rows);
2f4b4d54
DS
682
683 // assign variables to templates
684 $this->doTemplateAssignment($rows);
685
686 // do print / pdf / instance stuff if needed
687 $this->endPostProcess($rows);
6a488035
TO
688 }
689
74cf4551
EM
690 /**
691 * @param $rows
692 */
6a488035
TO
693 function alterDisplay(&$rows) {
694 // custom code to alter rows
695
696 $entryFound = FALSE;
697 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
698 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
699 $viewLinks = FALSE;
f7ed6ab4 700 $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
6a488035 701 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report');
1efec7ff 702 $actUrl = '';
6a488035
TO
703
704 if (CRM_Core_Permission::check('access CiviCRM')) {
705 $viewLinks = TRUE;
706 $onHover = ts('View Contact Summary for this Contact');
707 $onHoverAct = ts('View Activity Record');
708 }
6a488035 709
38ba593b 710 foreach ($rows as $rowNum => $row) {
1efec7ff
DG
711 // if we have an activity type, format the View Activity link for use in various columns
712 if ($viewLinks && array_key_exists('civicrm_activity_activity_type_id', $row)) {
713 // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
714 // else use source contact id
715 if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
716 $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
717 $cid = $targets[0];
718 }
719 else {
720 $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
721 }
722
723 $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
724 CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
725 FALSE,
726 $rows[$rowNum]['civicrm_activity_id']
727 );
728
729 $actLinkValues = array(
730 'id' => $rows[$rowNum]['civicrm_activity_id'],
731 'cid' => $cid,
732 'cxt' => $context,
733 );
734 $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'],
735 CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE
736 );
737 }
738
482691e1
RN
739 if (array_key_exists('civicrm_contact_contact_source', $row)) {
740 if ($value = $row['civicrm_contact_contact_source_id']) {
741 if ($viewLinks) {
742 $url = CRM_Utils_System::url("civicrm/contact/view",
743 'reset=1&cid=' . $value,
744 $this->_absoluteUrl
745 );
746 $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url;
747 $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover;
748 }
749 $entryFound = TRUE;
750 }
751 }
752
753 if (array_key_exists('civicrm_contact_contact_assignee', $row)) {
754 $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']);
755 if ($value = $row['civicrm_contact_contact_assignee_id']) {
756 $assigneeContactIds = explode(';', $value);
757 $link = array();
758 if ($viewLinks) {
759 foreach ($assigneeContactIds as $id => $value) {
760 if (isset($value) && isset($assigneeNames[$id])) {
761 $url = CRM_Utils_System::url("civicrm/contact/view",
762 'reset=1&cid=' . $value,
763 $this->_absoluteUrl
764 );
765 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$assigneeNames[$id]}</a>";
766 }
767 }
768 $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link);
769 }
770 $entryFound = TRUE;
771 }
772 }
773
774 if (array_key_exists('civicrm_contact_contact_target', $row)) {
775 $targetNames = explode(';', $row['civicrm_contact_contact_target']);
776 if ($value = $row['civicrm_contact_contact_target_id']) {
777 $targetContactIds = explode(';', $value);
778 $link = array();
779 if ($viewLinks) {
780 foreach ($targetContactIds as $id => $value) {
781 if (isset($value) && isset($targetNames[$id])) {
782 $url = CRM_Utils_System::url("civicrm/contact/view",
783 'reset=1&cid=' . $value,
784 $this->_absoluteUrl
785 );
786 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$targetNames[$id]}</a>";
787 }
788 }
789 $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link);
790 }
791 $entryFound = TRUE;
792 }
793 }
794
6a488035
TO
795 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
796 if ($value = $row['civicrm_activity_activity_type_id']) {
797 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
482691e1 798 if ($viewLinks) {
1efec7ff 799 $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $actUrl;
482691e1
RN
800 $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
801 }
6a488035
TO
802 $entryFound = TRUE;
803 }
804 }
805
806 if (array_key_exists('civicrm_activity_status_id', $row)) {
807 if ($value = $row['civicrm_activity_status_id']) {
808 $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value];
809 $entryFound = TRUE;
810 }
811 }
812
1efec7ff
DG
813 if (array_key_exists('civicrm_activity_details', $row)) {
814 if ($value = $row['civicrm_activity_details']) {
815 $fullDetails = $rows[$rowNum]['civicrm_activity_details'];
816 $rows[$rowNum]['civicrm_activity_details'] = substr($fullDetails, 0, strrpos(substr($fullDetails, 0, 80), ' '));
817 if ($actUrl) {
6e5e50a9 818 $rows[$rowNum]['civicrm_activity_details'] .= " <a href='{$actUrl}' title='{$onHoverAct}'>(more)</a>";
1efec7ff
DG
819 }
820 $entryFound = TRUE;
821 }
822 }
823
6a488035
TO
824 if (array_key_exists('civicrm_activity_campaign_id', $row)) {
825 if ($value = $row['civicrm_activity_campaign_id']) {
826 $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value];
827 $entryFound = TRUE;
828 }
829 }
830
831 if (array_key_exists('civicrm_activity_engagement_level', $row)) {
832 if ($value = $row['civicrm_activity_engagement_level']) {
833 $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value];
834 $entryFound = TRUE;
835 }
836 }
837
838 if (array_key_exists('civicrm_activity_activity_date_time', $row) && array_key_exists('civicrm_activity_status_id', $row)) {
839 if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) &&
840 $activityStatus[$row['civicrm_activity_status_id']] != 'Completed'
841 ) {
842 $rows[$rowNum]['class'] = "status-overdue";
843 $entryFound = TRUE;
844 }
845 }
846
482691e1
RN
847 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ? TRUE : $entryFound;
848
6a488035
TO
849 if (!$entryFound) {
850 break;
851 }
852 }
853 }
6a488035
TO
854}
855