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