Merge pull request #2402 from dlobo/CRM-14118
[civicrm-core.git] / CRM / Report / Form / Activity.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 ),
226 ),
227 'filters' =>
228 array(
229 'activity_date_time' =>
230 array(
231 'default' => 'this.month',
232 'operatorType' => CRM_Report_Form::OP_DATE,
233 ),
234 'activity_subject' =>
235 array('title' => ts('Activity Subject')),
236 'activity_type_id' =>
237 array('title' => ts('Activity Type'),
238 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
239 'options' => $this->activityTypes,
240 ),
241 'status_id' =>
242 array('title' => ts('Activity Status'),
243 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
244 'options' => CRM_Core_PseudoConstant::activityStatus(),
245 ),
246 ),
247 'order_bys' =>
248 array(
6a488035 249 'activity_date_time' =>
83e74860 250 array('title' => ts('Activity Date'), 'default_weight' => '1', 'dbAlias' => 'civicrm_activity_activity_date_time'),
6a488035 251 'activity_type_id' =>
83e74860 252 array('title' => ts('Activity Type'), 'default_weight' => '2', 'dbAlias' => 'civicrm_activity_activity_type_id'),
6a488035
TO
253 ),
254 'grouping' => 'activity-fields',
255 'alias' => 'activity',
256 ),
1f220d30 257 'civicrm_activity_contact' =>
6a488035 258 array(
38ba593b 259 'dao' => 'CRM_Activity_DAO_ActivityContact',
6a488035
TO
260 'fields' =>
261 array(
1f220d30 262 // so we have $this->_alias populated
6a488035 263 ),
6a488035 264 ),
6a488035
TO
265 ) + $this->addAddressFields(FALSE, TRUE);
266
267 if ($campaignEnabled) {
268 // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled
269
270 $this->_columns['civicrm_activity']['fields']['result'] = array(
271 'title' => 'Survey Result',
272 'default' => 'false',
273 );
274 $this->_columns['civicrm_activity']['filters']['result'] = array('title' => ts('Survey Result'),
275 'operator' => 'like',
276 'type' => CRM_Utils_Type::T_STRING,
277 );
278 if (!empty($this->activeCampaigns)) {
279 $this->_columns['civicrm_activity']['fields']['campaign_id'] = array(
280 'title' => 'Campaign',
281 'default' => 'false',
282 );
283 $this->_columns['civicrm_activity']['filters']['campaign_id'] = array('title' => ts('Campaign'),
284 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
285 'options' => $this->activeCampaigns,
286 );
287 }
288 if (!empty($this->engagementLevels)) {
289 $this->_columns['civicrm_activity']['fields']['engagement_level'] = array(
290 'title' => 'Engagement Index',
291 'default' => 'false',
292 );
293 $this->_columns['civicrm_activity']['filters']['engagement_level'] = array('title' => ts('Engagement Index'),
294 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
295 'options' => $this->engagementLevels,
296 );
297 }
298 }
299 $this->_groupFilter = TRUE;
300 $this->_tagFilter = TRUE;
301 parent::__construct();
302 }
303
1f220d30 304 function select($recordType = NULL) {
09febbae 305 if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && $recordType != 'final') {
306 $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}";
307 $this->_params['fields']["contact_{$recordType}"] = 1;
308 }
1f220d30 309 parent::select();
1f220d30 310
09febbae 311 if ($recordType == 'final' && !empty($this->_nonDisplayFields)) {
312 foreach ($this->_nonDisplayFields as $fieldName) {
313 unset($this->_columnHeaders[$fieldName]);
314 }
315 }
316
1f220d30
DS
317 if (empty($this->_selectAliasesTotal)) {
318 $this->_selectAliasesTotal = $this->_selectAliases;
319 }
320
321 $removeKeys = array();
322 if ($recordType == 'target') {
323 foreach ($this->_selectClauses as $key => $clause) {
7116d719 324 if (strstr($clause, 'civicrm_contact_assignee.') ||
1f220d30
DS
325 strstr($clause, 'civicrm_contact_source.') ||
326 strstr($clause, 'civicrm_email_assignee.') ||
327 strstr($clause, 'civicrm_email_source.')
328 ) {
329 $removeKeys[] = $key;
330 unset($this->_selectClauses[$key]);
331 }
332 }
333 } else if ($recordType == 'assignee') {
334 foreach ($this->_selectClauses as $key => $clause) {
7116d719 335 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
336 strstr($clause, 'civicrm_contact_source.') ||
337 strstr($clause, 'civicrm_email_target.') ||
482691e1 338 strstr($clause, 'civicrm_email_source.')
1f220d30
DS
339 ) {
340 $removeKeys[] = $key;
341 unset($this->_selectClauses[$key]);
342 }
343 }
344 } else if ($recordType == 'source') {
345 foreach ($this->_selectClauses as $key => $clause) {
7116d719 346 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
347 strstr($clause, 'civicrm_contact_assignee.') ||
348 strstr($clause, 'civicrm_email_target.') ||
482691e1 349 strstr($clause, 'civicrm_email_assignee.')
1f220d30
DS
350 ) {
351 $removeKeys[] = $key;
352 unset($this->_selectClauses[$key]);
353 }
354 }
355 } else if ($recordType == 'final') {
356 $this->_selectClauses = $this->_selectAliasesTotal;
357 foreach ($this->_selectClauses as $key => $clause) {
7116d719 358 if (strstr($clause, 'civicrm_contact_contact_target') ||
1f220d30
DS
359 strstr($clause, 'civicrm_contact_contact_assignee') ||
360 strstr($clause, 'civicrm_contact_contact_source') ) {
482691e1 361 $this->_selectClauses[$key] = "GROUP_CONCAT($clause SEPARATOR ';') as $clause";
6a488035
TO
362 }
363 }
364 }
365
1f220d30
DS
366 if ($recordType) {
367 foreach ($removeKeys as $key) {
368 unset($this->_selectAliases[$key]);
369 }
370
1f220d30 371 $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " ";
1f220d30 372 }
6a488035
TO
373 }
374
2f4b4d54 375 function from($recordType) {
e7e657f0 376 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
9e74e3ce 377 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
2f4b4d54
DS
378 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
379 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 380
2f4b4d54
DS
381 if ($recordType == 'target') {
382 $this->_from = "
383 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
384 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
385 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
386 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$targetID}
2f4b4d54 387 INNER JOIN civicrm_contact civicrm_contact_target
1f220d30 388 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id
2f4b4d54
DS
389 {$this->_aclFrom}";
390
391 if ($this->isTableSelected('civicrm_email')) {
392 $this->_from .= "
1f220d30
DS
393 LEFT JOIN civicrm_email civicrm_email_target
394 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_target.contact_id AND
395 civicrm_email_target.is_primary = 1";
2f4b4d54 396 }
1f220d30 397 $this->_aliases['civicrm_contact'] = 'civicrm_contact_target';
2f4b4d54
DS
398 }
399
400 if ($recordType == 'assignee') {
401 $this->_from = "
402 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
403 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
404 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
405 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$assigneeID}
2f4b4d54 406 INNER JOIN civicrm_contact civicrm_contact_assignee
1f220d30 407 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id
2f4b4d54
DS
408 {$this->_aclFrom}";
409
410 if ($this->isTableSelected('civicrm_email')) {
411 $this->_from .= "
1f220d30
DS
412 LEFT JOIN civicrm_email civicrm_email_assignee
413 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_assignee.contact_id AND
414 civicrm_email_assignee.is_primary = 1";
2f4b4d54 415 }
482691e1 416 $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee';
2f4b4d54
DS
417 }
418
419 if ($recordType == 'source') {
420 $this->_from = "
421 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
422 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
423 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
424 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID}
2f4b4d54 425 INNER JOIN civicrm_contact civicrm_contact_source
1f220d30 426 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id
2f4b4d54
DS
427 {$this->_aclFrom}";
428
429 if ($this->isTableSelected('civicrm_email')) {
430 $this->_from .= "
1f220d30
DS
431 LEFT JOIN civicrm_email civicrm_email_source
432 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_source.contact_id AND
433 civicrm_email_source.is_primary = 1";
2f4b4d54 434 }
482691e1 435 $this->_aliases['civicrm_contact'] = 'civicrm_contact_source';
6a488035 436 }
482691e1 437 $this->addAddressFromClause();
6a488035
TO
438 }
439
b6cab294 440 function where($recordType = NULL) {
2f4b4d54 441 $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
6a488035
TO
442 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
443 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
444
445 $clauses = array();
446 foreach ($this->_columns as $tableName => $table) {
447 if (array_key_exists('filters', $table)) {
448
449 foreach ($table['filters'] as $fieldName => $field) {
450 $clause = NULL;
7116d719 451 if ($fieldName != 'contact_' . $recordType &&
452 (strstr($fieldName, '_target') ||
453 strstr($fieldName, '_assignee') ||
454 strstr($fieldName, '_source')
455 )
456 ) {
7116d719 457 continue;
458 }
6a488035
TO
459 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
460 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
461 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
462 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
463
464 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
465 }
466 else {
467 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
389cfee7 468 if ($op && ($op != 'nnll' || $op != 'nll')) {
6a488035
TO
469 $clause = $this->whereClause($field,
470 $op,
471 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
472 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
473 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
474 );
475 }
476 }
477
478 if ($field['name'] == 'current_user') {
479 if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) == 1) {
480 // get current user
481 $session = CRM_Core_Session::singleton();
482 if ($contactID = $session->get('userID')) {
389cfee7 483 $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN
484 (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})";
6a488035
TO
485 }
486 else {
487 $clause = NULL;
488 }
489 }
490 else {
491 $clause = NULL;
492 }
493 }
494 if (!empty($clause)) {
495 $clauses[] = $clause;
496 }
497 }
498 }
499 }
500
501 if (empty($clauses)) {
b6cab294 502 $this->_where .= " ";
6a488035
TO
503 }
504 else {
b6cab294 505 $this->_where .= " AND " . implode(' AND ', $clauses);
6a488035
TO
506 }
507
508 if ($this->_aclWhere) {
509 $this->_where .= " AND {$this->_aclWhere} ";
510 }
511 }
512
513 function groupBy() {
514 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id";
515 }
516
517 function buildACLClause($tableAlias = 'contact_a') {
2a3c101f 518 //override for ACL( Since Contact may be source
6a488035
TO
519 //contact/assignee or target also it may be null )
520
521 if (CRM_Core_Permission::check('view all contacts')) {
522 $this->_aclFrom = $this->_aclWhere = NULL;
523 return;
524 }
525
526 $session = CRM_Core_Session::singleton();
527 $contactID = $session->get('userID');
528 if (!$contactID) {
529 $contactID = 0;
530 }
531 $contactID = CRM_Utils_Type::escape($contactID, 'Integer');
532
533 CRM_Contact_BAO_Contact_Permission::cache($contactID);
534 $clauses = array();
535 foreach ($tableAlias as $k => $alias) {
536 $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 ";
537 }
538
539 $this->_aclFrom = implode(" ", $clauses);
540 $this->_aclWhere = NULL;
541 }
542
9a2f601e 543 function add2group($groupID) {
544 if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') {
545 CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group'));
546 }
547
548 $query = "{$this->_select}
549FROM civireport_activity_temp_target tar
550GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}";
551 $select = 'AS addtogroup_contact_id';
552 $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query);
553 $dao = CRM_Core_DAO::executeQuery($query);
554
555 $contactIDs = array();
556 // Add resulting contacts to group
557 while ($dao->fetch()) {
558 if ($dao->addtogroup_contact_id) {
559 $contact_id = explode(';', $dao->addtogroup_contact_id);
560 if ($contact_id[0]) {
7bb16323 561 $contactIDs[$contact_id[0]] = $contact_id[0];
9a2f601e 562 }
563 }
564 }
565
566 if ( !empty($contactIDs) ) {
567 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
568 CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
569 }
570 else {
571 CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group."));
572 }
573 }
574
6a488035 575 function postProcess() {
83e74860 576 $this->buildACLClause(array('civicrm_contact_source', 'civicrm_contact_target', 'civicrm_contact_assignee'));
2f4b4d54
DS
577 $this->beginPostProcess();
578
389cfee7 579 //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty'
580 $nullFilters = array();
b6cab294 581 foreach (array('target', 'source', 'assignee') as $type) {
389cfee7 582 if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll' ||
583 CRM_Utils_Array::value("contact_{$type}_value", $this->_params)) {
584 $nullFilters[] = " civicrm_contact_contact_{$type} IS NOT NULL ";
585 }
586 else if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nll') {
587 $nullFilters[] = " civicrm_contact_contact_{$type} IS NULL ";
b6cab294 588 }
589 }
590
389cfee7 591 // 1. fill temp table with target results
1f220d30 592 $this->select('target');
2f4b4d54 593 $this->from('target');
5b1ba2b8 594 $this->customDataFrom();
389cfee7 595 $this->where('target');
1f220d30 596 $insertCols = implode(',', $this->_selectAliases);
7116d719 597 $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
482691e1 598{$this->_select} {$this->_from} {$this->_where} ";
2f4b4d54 599 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 600
1f220d30
DS
601 // 2. add new columns to hold assignee and source results
602 // fixme: add when required
603 $tempQuery = "
604 ALTER TABLE civireport_activity_temp_target
605 ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128),
606 ADD COLUMN civicrm_contact_contact_source VARCHAR(128),
7116d719 607 ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128),
482691e1 608 ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128),
1f220d30
DS
609 ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128),
610 ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)";
611 CRM_Core_DAO::executeQuery($tempQuery);
612
613 // 3. fill temp table with assignee results
389cfee7 614 $this->select('assignee');
615 $this->from('assignee');
616 $this->customDataFrom();
617 $this->where('assignee');
618 $insertCols = implode(',', $this->_selectAliases);
619 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 620{$this->_select}
83e74860 621{$this->_from} {$this->_where}";
389cfee7 622 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 623
1f220d30 624 // 4. fill temp table with source results
389cfee7 625 $this->select('source');
626 $this->from('source');
627 $this->customDataFrom();
628 $this->where('source');
629 $insertCols = implode(',', $this->_selectAliases);
630 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 631{$this->_select}
83e74860 632{$this->_from} {$this->_where}";
389cfee7 633 CRM_Core_DAO::executeQuery($tempQuery);
2f4b4d54 634
1f220d30 635 // 5. show final result set from temp table
2f4b4d54 636 $rows = array();
1f220d30 637 $this->select('final');
389cfee7 638 $this->_having = "";
639 if (!empty($nullFilters)) {
640 $this->_having = "HAVING " . implode(' AND ', $nullFilters);
641 }
83e74860 642 $this->orderBy();
1f220d30 643 $this->limit();
7116d719 644 $sql = "{$this->_select}
645FROM civireport_activity_temp_target tar
389cfee7 646GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}";
2f4b4d54
DS
647 $this->buildRows($sql, $rows);
648
649 // format result set.
1f220d30 650 $this->formatDisplay($rows);
2f4b4d54
DS
651
652 // assign variables to templates
653 $this->doTemplateAssignment($rows);
654
655 // do print / pdf / instance stuff if needed
656 $this->endPostProcess($rows);
6a488035
TO
657 }
658
659 function alterDisplay(&$rows) {
660 // custom code to alter rows
661
662 $entryFound = FALSE;
663 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
664 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
665 $viewLinks = FALSE;
f7ed6ab4 666 $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
667 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report');
668
669 if (CRM_Core_Permission::check('access CiviCRM')) {
670 $viewLinks = TRUE;
671 $onHover = ts('View Contact Summary for this Contact');
672 $onHoverAct = ts('View Activity Record');
673 }
6a488035 674
38ba593b 675 foreach ($rows as $rowNum => $row) {
482691e1
RN
676 if (array_key_exists('civicrm_contact_contact_source', $row)) {
677 if ($value = $row['civicrm_contact_contact_source_id']) {
678 if ($viewLinks) {
679 $url = CRM_Utils_System::url("civicrm/contact/view",
680 'reset=1&cid=' . $value,
681 $this->_absoluteUrl
682 );
683 $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url;
684 $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover;
685 }
686 $entryFound = TRUE;
687 }
688 }
689
690 if (array_key_exists('civicrm_contact_contact_assignee', $row)) {
691 $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']);
692 if ($value = $row['civicrm_contact_contact_assignee_id']) {
693 $assigneeContactIds = explode(';', $value);
694 $link = array();
695 if ($viewLinks) {
696 foreach ($assigneeContactIds as $id => $value) {
697 if (isset($value) && isset($assigneeNames[$id])) {
698 $url = CRM_Utils_System::url("civicrm/contact/view",
699 'reset=1&cid=' . $value,
700 $this->_absoluteUrl
701 );
702 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$assigneeNames[$id]}</a>";
703 }
704 }
705 $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link);
706 }
707 $entryFound = TRUE;
708 }
709 }
710
711 if (array_key_exists('civicrm_contact_contact_target', $row)) {
712 $targetNames = explode(';', $row['civicrm_contact_contact_target']);
713 if ($value = $row['civicrm_contact_contact_target_id']) {
714 $targetContactIds = explode(';', $value);
715 $link = array();
716 if ($viewLinks) {
717 foreach ($targetContactIds as $id => $value) {
718 if (isset($value) && isset($targetNames[$id])) {
719 $url = CRM_Utils_System::url("civicrm/contact/view",
720 'reset=1&cid=' . $value,
721 $this->_absoluteUrl
722 );
723 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$targetNames[$id]}</a>";
724 }
725 }
726 $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link);
727 }
728 $entryFound = TRUE;
729 }
730 }
731
6a488035
TO
732 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
733 if ($value = $row['civicrm_activity_activity_type_id']) {
734 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
482691e1
RN
735 if ($viewLinks) {
736 // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
737 // else use source contact id
738 if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
739 $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
740 $cid = $targets[0];
741 }
742 else {
743 $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
744 }
745
746 $actionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
747 CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
748 FALSE,
749 $rows[$rowNum]['civicrm_activity_id']
750 );
751
752 $linkValues = array(
753 'id' => $rows[$rowNum]['civicrm_activity_id'],
754 'cid' => $cid,
755 'cxt' => $context,
756 );
757 $url = CRM_Utils_System::url($actionLinks[CRM_Core_Action::VIEW]['url'],
758 CRM_Core_Action::replace($actionLinks[CRM_Core_Action::VIEW]['qs'], $linkValues), TRUE
759 );
760 $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $url;
761 $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
762 }
6a488035
TO
763 $entryFound = TRUE;
764 }
765 }
766
767 if (array_key_exists('civicrm_activity_status_id', $row)) {
768 if ($value = $row['civicrm_activity_status_id']) {
769 $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value];
770 $entryFound = TRUE;
771 }
772 }
773
774 if (array_key_exists('civicrm_activity_campaign_id', $row)) {
775 if ($value = $row['civicrm_activity_campaign_id']) {
776 $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value];
777 $entryFound = TRUE;
778 }
779 }
780
781 if (array_key_exists('civicrm_activity_engagement_level', $row)) {
782 if ($value = $row['civicrm_activity_engagement_level']) {
783 $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value];
784 $entryFound = TRUE;
785 }
786 }
787
788 if (array_key_exists('civicrm_activity_activity_date_time', $row) && array_key_exists('civicrm_activity_status_id', $row)) {
789 if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) &&
790 $activityStatus[$row['civicrm_activity_status_id']] != 'Completed'
791 ) {
792 $rows[$rowNum]['class'] = "status-overdue";
793 $entryFound = TRUE;
794 }
795 }
796
482691e1
RN
797 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ? TRUE : $entryFound;
798
6a488035
TO
799 if (!$entryFound) {
800 break;
801 }
802 }
803 }
6a488035
TO
804}
805