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