Merge pull request #2107 from mgbdev/CRM-13811
[civicrm-core.git] / CRM / Report / Form / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
35 class CRM_Report_Form_Activity extends CRM_Report_Form {
36 protected $_selectAliasesTotal = array();
37
38 protected $_customGroupExtends = array(
39 'Activity'
40 );
41
42 protected $_nonDisplayFields = array();
43
44 function __construct() {
45 // There could be multiple contacts. We not clear on which contact id to display.
46 // Lets hide it for now.
47 $this->_exposeContactID = FALSE;
48
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(
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',
78 'dbAlias' => "civicrm_contact_assignee.sort_name",
79 'default' => TRUE,
80 ),
81 'contact_target' =>
82 array(
83 'name' => 'sort_name',
84 'title' => ts('Target Contact Name'),
85 'alias' => 'civicrm_contact_target',
86 'dbAlias' => "civicrm_contact_target.sort_name",
87 'default' => TRUE,
88 ),
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 ),
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',
138 'alias' => 'civicrm_contact_target',
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'),
184 'dbAlias' => 'civicrm_email_contact_source_email',
185 ),
186 ),
187 ),
188 'civicrm_activity' =>
189 array(
190 'dao' => 'CRM_Activity_DAO_Activity',
191 'fields' =>
192 array(
193 'id' =>
194 array(
195 'no_display' => TRUE,
196 'title' => ts('Activity ID'),
197 'required' => TRUE,
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'),
206 'required' => TRUE,
207 'type' => CRM_Utils_Type::T_STRING,
208 ),
209 'activity_subject' =>
210 array('title' => ts('Subject'),
211 'default' => TRUE,
212 ),
213 'activity_date_time' =>
214 array('title' => ts('Activity Date'),
215 'required' => TRUE,
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(
249 'activity_date_time' =>
250 array('title' => ts('Activity Date'), 'default_weight' => '1', 'dbAlias' => 'civicrm_activity_activity_date_time'),
251 'activity_type_id' =>
252 array('title' => ts('Activity Type'), 'default_weight' => '2', 'dbAlias' => 'civicrm_activity_activity_type_id'),
253 ),
254 'grouping' => 'activity-fields',
255 'alias' => 'activity',
256 ),
257 'civicrm_activity_contact' =>
258 array(
259 'dao' => 'CRM_Activity_DAO_ActivityContact',
260 'fields' =>
261 array(
262 // so we have $this->_alias populated
263 ),
264 ),
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
304 function select($recordType = NULL) {
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 }
309 parent::select();
310
311 if ($recordType == 'final' && !empty($this->_nonDisplayFields)) {
312 foreach ($this->_nonDisplayFields as $fieldName) {
313 unset($this->_columnHeaders[$fieldName]);
314 }
315 }
316
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) {
324 if (strstr($clause, 'civicrm_contact_assignee.') ||
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) {
335 if (strstr($clause, 'civicrm_contact_target.') ||
336 strstr($clause, 'civicrm_contact_source.') ||
337 strstr($clause, 'civicrm_email_target.') ||
338 strstr($clause, 'civicrm_email_source.')
339 ) {
340 $removeKeys[] = $key;
341 unset($this->_selectClauses[$key]);
342 }
343 }
344 } else if ($recordType == 'source') {
345 foreach ($this->_selectClauses as $key => $clause) {
346 if (strstr($clause, 'civicrm_contact_target.') ||
347 strstr($clause, 'civicrm_contact_assignee.') ||
348 strstr($clause, 'civicrm_email_target.') ||
349 strstr($clause, 'civicrm_email_assignee.')
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) {
358 if (strstr($clause, 'civicrm_contact_contact_target') ||
359 strstr($clause, 'civicrm_contact_contact_assignee') ||
360 strstr($clause, 'civicrm_contact_contact_source') ) {
361 $this->_selectClauses[$key] = "GROUP_CONCAT($clause SEPARATOR ';') as $clause";
362 }
363 }
364 }
365
366 if ($recordType) {
367 foreach ($removeKeys as $key) {
368 unset($this->_selectAliases[$key]);
369 }
370
371 $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " ";
372 }
373 }
374
375 function from($recordType) {
376 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
377 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
378 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
379 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
380
381 if ($recordType == 'target') {
382 $this->_from = "
383 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
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}
387 INNER JOIN civicrm_contact civicrm_contact_target
388 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id
389 {$this->_aclFrom}";
390
391 if ($this->isTableSelected('civicrm_email')) {
392 $this->_from .= "
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";
396 }
397 $this->_aliases['civicrm_contact'] = 'civicrm_contact_target';
398 }
399
400 if ($recordType == 'assignee') {
401 $this->_from = "
402 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
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}
406 INNER JOIN civicrm_contact civicrm_contact_assignee
407 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id
408 {$this->_aclFrom}";
409
410 if ($this->isTableSelected('civicrm_email')) {
411 $this->_from .= "
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";
415 }
416 $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee';
417 }
418
419 if ($recordType == 'source') {
420 $this->_from = "
421 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
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}
425 INNER JOIN civicrm_contact civicrm_contact_source
426 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id
427 {$this->_aclFrom}";
428
429 if ($this->isTableSelected('civicrm_email')) {
430 $this->_from .= "
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";
434 }
435 $this->_aliases['civicrm_contact'] = 'civicrm_contact_source';
436 }
437 $this->addAddressFromClause();
438 }
439
440 function where($recordType = NULL) {
441 $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
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;
451 if ($fieldName != 'contact_' . $recordType &&
452 (strstr($fieldName, '_target') ||
453 strstr($fieldName, '_assignee') ||
454 strstr($fieldName, '_source')
455 )
456 ) {
457 continue;
458 }
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);
468 if ($op) {
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')) {
483 $clause = "civicrm_contact_{$recordType}.id = " . $contactID;
484 }
485 else {
486 $clause = NULL;
487 }
488 }
489 else {
490 $clause = NULL;
491 }
492 }
493 if (!empty($clause)) {
494 $clauses[] = $clause;
495 }
496 }
497 }
498 }
499
500 if (empty($clauses)) {
501 $this->_where .= " ";
502 }
503 else {
504 $this->_where .= " AND " . implode(' AND ', $clauses);
505 }
506
507 if ($this->_aclWhere) {
508 $this->_where .= " AND {$this->_aclWhere} ";
509 }
510 }
511
512 function groupBy() {
513 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id";
514 }
515
516 function buildACLClause($tableAlias = 'contact_a') {
517 //override for ACL( Since Contact may be source
518 //contact/assignee or target also it may be null )
519
520 if (CRM_Core_Permission::check('view all contacts')) {
521 $this->_aclFrom = $this->_aclWhere = NULL;
522 return;
523 }
524
525 $session = CRM_Core_Session::singleton();
526 $contactID = $session->get('userID');
527 if (!$contactID) {
528 $contactID = 0;
529 }
530 $contactID = CRM_Utils_Type::escape($contactID, 'Integer');
531
532 CRM_Contact_BAO_Contact_Permission::cache($contactID);
533 $clauses = array();
534 foreach ($tableAlias as $k => $alias) {
535 $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 ";
536 }
537
538 $this->_aclFrom = implode(" ", $clauses);
539 $this->_aclWhere = NULL;
540 }
541
542 function postProcess() {
543 $this->buildACLClause(array('civicrm_contact_source', 'civicrm_contact_target', 'civicrm_contact_assignee'));
544 $this->beginPostProcess();
545
546 //Assign those recordtype to array which have filter value or operator as 'Is not empty'
547 //which will be further used for where clause buildup of temp tables
548 $sortnameFilters = array();
549 foreach (array('target', 'source', 'assignee') as $type) {
550 if (CRM_Utils_Array::value("contact_{$type}_value", $this->_params) ||
551 CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll'
552 ) {
553 $sortnameFilters[$type] = 1;
554 }
555 }
556
557 // 1. fill temp table with target results
558 $this->select('target');
559 $this->from('target');
560 $this->customDataFrom();
561 if (!empty($sortnameFilters) && !array_key_exists('target', $sortnameFilters)
562 ) {
563 $this->_where = "WHERE FALSE";
564 }
565 else {
566 $this->where('target');
567 }
568 $insertCols = implode(',', $this->_selectAliases);
569 $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
570 {$this->_select} {$this->_from} {$this->_where} ";
571 CRM_Core_DAO::executeQuery($tempQuery);
572
573 // 2. add new columns to hold assignee and source results
574 // fixme: add when required
575 $tempQuery = "
576 ALTER TABLE civireport_activity_temp_target
577 ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128),
578 ADD COLUMN civicrm_contact_contact_source VARCHAR(128),
579 ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128),
580 ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128),
581 ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128),
582 ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)";
583 CRM_Core_DAO::executeQuery($tempQuery);
584
585 // 3. fill temp table with assignee results
586 if (CRM_Utils_Array::value("contact_assignee", $this->_params['fields']) ||
587 CRM_Utils_Array::value("contact_assignee_email", $this->_params['fields']) ||
588 CRM_Utils_Array::value("contact_assignee_value", $this->_params)) {
589 $this->select('assignee');
590 $this->from('assignee');
591 $this->customDataFrom();
592 if (!empty($sortnameFilters) && !array_key_exists('assignee', $sortnameFilters)
593 ) {
594 $this->_where = "WHERE FALSE";
595 }
596 else {
597 $this->where('assignee');
598 }
599 $insertCols = implode(',', $this->_selectAliases);
600 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
601 {$this->_select}
602 {$this->_from} {$this->_where}";
603 CRM_Core_DAO::executeQuery($tempQuery);
604 }
605
606 // 4. fill temp table with source results
607 if (CRM_Utils_Array::value("contact_source", $this->_params['fields']) ||
608 CRM_Utils_Array::value("contact_source_email", $this->_params['fields']) ||
609 CRM_Utils_Array::value("contact_source_value", $this->_params)) {
610 $this->select('source');
611 $this->from('source');
612 $this->customDataFrom();
613 if (!empty($sortnameFilters) && !array_key_exists('source', $sortnameFilters)
614 ) {
615 $this->_where = "WHERE FALSE";
616 }
617 else {
618 $this->where('source');
619 }
620 $insertCols = implode(',', $this->_selectAliases);
621 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
622 {$this->_select}
623 {$this->_from} {$this->_where}";
624 CRM_Core_DAO::executeQuery($tempQuery);
625 }
626
627 // 5. show final result set from temp table
628 $rows = array();
629 $this->select('final');
630 $this->orderBy();
631 $this->limit();
632 $sql = "{$this->_select}
633 FROM civireport_activity_temp_target tar
634 GROUP BY civicrm_activity_id {$this->_orderBy} {$this->_limit}";
635 $this->buildRows($sql, $rows);
636
637 // format result set.
638 $this->formatDisplay($rows);
639
640 // assign variables to templates
641 $this->doTemplateAssignment($rows);
642
643 // do print / pdf / instance stuff if needed
644 $this->endPostProcess($rows);
645 }
646
647 function alterDisplay(&$rows) {
648 // custom code to alter rows
649
650 $entryFound = FALSE;
651 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
652 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
653 $viewLinks = FALSE;
654 $seperator = CRM_CORE_DAO::VALUE_SEPARATOR;
655 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report');
656
657 if (CRM_Core_Permission::check('access CiviCRM')) {
658 $viewLinks = TRUE;
659 $onHover = ts('View Contact Summary for this Contact');
660 $onHoverAct = ts('View Activity Record');
661 }
662
663 foreach ($rows as $rowNum => $row) {
664 if (array_key_exists('civicrm_contact_contact_source', $row)) {
665 if ($value = $row['civicrm_contact_contact_source_id']) {
666 if ($viewLinks) {
667 $url = CRM_Utils_System::url("civicrm/contact/view",
668 'reset=1&cid=' . $value,
669 $this->_absoluteUrl
670 );
671 $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url;
672 $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover;
673 }
674 $entryFound = TRUE;
675 }
676 }
677
678 if (array_key_exists('civicrm_contact_contact_assignee', $row)) {
679 $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']);
680 if ($value = $row['civicrm_contact_contact_assignee_id']) {
681 $assigneeContactIds = explode(';', $value);
682 $link = array();
683 if ($viewLinks) {
684 foreach ($assigneeContactIds as $id => $value) {
685 if (isset($value) && isset($assigneeNames[$id])) {
686 $url = CRM_Utils_System::url("civicrm/contact/view",
687 'reset=1&cid=' . $value,
688 $this->_absoluteUrl
689 );
690 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$assigneeNames[$id]}</a>";
691 }
692 }
693 $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link);
694 }
695 $entryFound = TRUE;
696 }
697 }
698
699 if (array_key_exists('civicrm_contact_contact_target', $row)) {
700 $targetNames = explode(';', $row['civicrm_contact_contact_target']);
701 if ($value = $row['civicrm_contact_contact_target_id']) {
702 $targetContactIds = explode(';', $value);
703 $link = array();
704 if ($viewLinks) {
705 foreach ($targetContactIds as $id => $value) {
706 if (isset($value) && isset($targetNames[$id])) {
707 $url = CRM_Utils_System::url("civicrm/contact/view",
708 'reset=1&cid=' . $value,
709 $this->_absoluteUrl
710 );
711 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$targetNames[$id]}</a>";
712 }
713 }
714 $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link);
715 }
716 $entryFound = TRUE;
717 }
718 }
719
720 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
721 if ($value = $row['civicrm_activity_activity_type_id']) {
722 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
723 if ($viewLinks) {
724 // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
725 // else use source contact id
726 if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
727 $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
728 $cid = $targets[0];
729 }
730 else {
731 $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
732 }
733
734 $actionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
735 CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
736 FALSE,
737 $rows[$rowNum]['civicrm_activity_id']
738 );
739
740 $linkValues = array(
741 'id' => $rows[$rowNum]['civicrm_activity_id'],
742 'cid' => $cid,
743 'cxt' => $context,
744 );
745 $url = CRM_Utils_System::url($actionLinks[CRM_Core_Action::VIEW]['url'],
746 CRM_Core_Action::replace($actionLinks[CRM_Core_Action::VIEW]['qs'], $linkValues), TRUE
747 );
748 $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $url;
749 $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
750 }
751 $entryFound = TRUE;
752 }
753 }
754
755 if (array_key_exists('civicrm_activity_status_id', $row)) {
756 if ($value = $row['civicrm_activity_status_id']) {
757 $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value];
758 $entryFound = TRUE;
759 }
760 }
761
762 if (array_key_exists('civicrm_activity_campaign_id', $row)) {
763 if ($value = $row['civicrm_activity_campaign_id']) {
764 $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value];
765 $entryFound = TRUE;
766 }
767 }
768
769 if (array_key_exists('civicrm_activity_engagement_level', $row)) {
770 if ($value = $row['civicrm_activity_engagement_level']) {
771 $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value];
772 $entryFound = TRUE;
773 }
774 }
775
776 if (array_key_exists('civicrm_activity_activity_date_time', $row) && array_key_exists('civicrm_activity_status_id', $row)) {
777 if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) &&
778 $activityStatus[$row['civicrm_activity_status_id']] != 'Completed'
779 ) {
780 $rows[$rowNum]['class'] = "status-overdue";
781 $entryFound = TRUE;
782 }
783 }
784
785 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ? TRUE : $entryFound;
786
787 if (!$entryFound) {
788 break;
789 }
790 }
791 }
792 }
793