Merge pull request #851 from eileenmcnaughton/CRM-12686
[civicrm-core.git] / CRM / Report / Form / Contact / Detail.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Form_Contact_Detail extends CRM_Report_Form {
38 CONST ROW_COUNT_LIMIT = 10;
39
40 protected $_summary = NULL;
41
42 protected $_customGroupExtends = array(
43 'Contact', 'Individual', 'Household', 'Organization');
44
45 function __construct() {
46 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
47 $this->_columns = array(
48 'civicrm_contact' =>
49 array(
50 'dao' => 'CRM_Contact_DAO_Contact',
51 'fields' =>
52 array(
53 'sort_name' =>
54 array('title' => ts('Contact Name'),
55 'required' => TRUE,
56 'no_repeat' => TRUE,
57 ),
58 'first_name' => array('title' => ts('First Name'),
59 ),
60 'last_name' => array('title' => ts('Last Name'),
61 ),
62 'id' =>
63 array(
64 'no_display' => TRUE,
65 'required' => TRUE,
66 ),
67 ),
68 'filters' =>
69 array(
70 'id' =>
71 array('title' => ts('Contact ID'),
72 'no_display' => TRUE,
73 ),
74 'sort_name' =>
75 array('title' => ts('Contact Name'),
76 ),
77 ),
78 'grouping' => 'contact-fields',
79 'order_bys' =>
80 array(
81 'sort_name' =>
82 array('title' => ts('Last Name, First Name'), 'default' => '1', 'default_weight' => '0', 'default_order' => 'ASC'),
83 ),
84 ),
85 'civicrm_address' =>
86 array(
87 'dao' => 'CRM_Core_DAO_Address',
88 'grouping' => 'contact-fields',
89 'fields' =>
90 array(
91 'street_address' => NULL,
92 'city' => NULL,
93 'postal_code' => NULL,
94 'state_province_id' =>
95 array('title' => ts('State/Province'),
96 ),
97 ),
98 'order_bys' =>
99 array('state_province_id' => array('title' => 'State/Province'),
100 'city' => array('title' => 'City'),
101 'postal_code' => array('title' => 'Postal Code'),
102 ),
103 ),
104 'civicrm_country' =>
105 array(
106 'dao' => 'CRM_Core_DAO_Country',
107 'fields' =>
108 array(
109 'name' =>
110 array('title' => 'Country', 'default' => TRUE),
111 ),
112 'order_bys' =>
113 array(
114 'name' =>
115 array('title' => 'Country'),
116 ),
117 'grouping' => 'contact-fields',
118 ),
119 'civicrm_email' =>
120 array(
121 'dao' => 'CRM_Core_DAO_Email',
122 'fields' =>
123 array(
124 'email' =>
125 array('title' => ts('Email'),
126 'no_repeat' => TRUE,
127 ),
128 ),
129 'grouping' => 'contact-fields',
130 'order_bys' =>
131 array(
132 'email' =>
133 array('title' => ts('Email'),
134 ),
135 ),
136 ),
137 'civicrm_contribution' =>
138 array(
139 'dao' => 'CRM_Contribute_DAO_Contribution',
140 'fields' =>
141 array(
142 'contact_id' =>
143 array(
144 'no_display' => TRUE,
145 'required' => TRUE,
146 ),
147 'contribution_id' =>
148 array('title' => ts('Contribution'),
149 'no_repeat' => TRUE,
150 'default' => TRUE,
151 ),
152 'total_amount' => array('default' => TRUE),
b914f4e8 153 'financial_type_id' => array('title' => ts('Financial Type'),
6a488035
TO
154 'default' => TRUE,
155 ),
156 'trxn_id' => NULL,
157 'receive_date' => array('default' => TRUE),
158 'receipt_date' => NULL,
159 'contribution_status_id' => array('title' => ts('Contribution Status'),
160 'default' => TRUE,
161 ),
162 'contribution_source' => NULL,
163 ),
164 ),
165 'civicrm_membership' =>
166 array(
167 'dao' => 'CRM_Member_DAO_Membership',
168 'fields' =>
169 array(
170 'contact_id' =>
171 array(
172 'no_display' => TRUE,
173 'required' => TRUE,
174 ),
175 'membership_id' =>
176 array('title' => ts('Membership'),
177 'no_repeat' => TRUE,
178 'default' => TRUE,
179 ),
180 'membership_type_id' => array('title' => ts('Membership Type'),
181 'default' => TRUE,
182 ),
183 'join_date' => NULL,
184 'membership_start_date' => array('title' => ts('Start Date'),
185 'default' => TRUE,
186 ),
187 'membership_end_date' => array('title' => ts('End Date'),
188 'default' => TRUE,
189 ),
190 'membership_status_id' => array(
191 'name' => 'status_id',
192 'title' => ts('Membership Status'),
193 'default' => TRUE,
194 ),
195 'source' => array('title' => 'Membership Source'),
196 ),
197 ),
198 'civicrm_participant' =>
199 array(
200 'dao' => 'CRM_Event_DAO_Participant',
201 'fields' =>
202 array(
203 'contact_id' =>
204 array(
205 'no_display' => TRUE,
206 'required' => TRUE,
207 ),
208 'participant_id' =>
209 array('title' => ts('Participant'),
210 'no_repeat' => TRUE,
211 'default' => TRUE,
212 ),
213 'event_id' => array('default' => TRUE),
214 'participant_status_id' => array(
215 'name' => 'status_id',
216 'title' => ts('Participant Status'),
217 'default' => TRUE,
218 ),
219 'role_id' => array('title' => ts('Role'),
220 'default' => TRUE,
221 ),
222 'participant_register_date' => array('title' => ts('Register Date'),
223 'default' => TRUE,
224 ),
225 'fee_level' => array('title' => ts('Fee Level'),
226 'default' => TRUE,
227 ),
228 'fee_amount' => array('title' => ts('Fee Amount'),
229 'default' => TRUE,
230 ),
231 ),
232 ),
233 'civicrm_relationship' =>
234 array(
235 'dao' => 'CRM_Contact_DAO_Relationship',
236 'fields' =>
237 array(
238 'relationship_id' =>
239 array(
240 'name' => 'id',
241 'title' => ts('Relationship'),
242 'no_repeat' => TRUE,
243 'default' => TRUE,
244 ),
245 'relationship_type_id' =>
246 array('title' => ts('Relationship Type'),
247 'default' => TRUE,
248 ),
249 'contact_id_b' =>
250 array('title' => ts('Relationship With'),
251 'default' => TRUE,
252 ),
253 'start_date' =>
254 array(
255 'title' => 'Start Date ',
256 'type' => CRM_Report_Form::OP_DATE,
257 ),
258 'end_date' =>
259 array(
260 'title' => 'End Date ',
261 'type' => CRM_Report_Form::OP_DATE,
262 ),
263 ),
264 ),
265 'civicrm_activity' =>
266 array(
267 'dao' => 'CRM_Activity_DAO_Activity',
268 'fields' =>
269 array(
270 'id' =>
271 array('title' => ts('Activity'),
272 'no_repeat' => TRUE,
273 'default' => TRUE,
274 ),
275 'activity_type_id' =>
276 array('title' => ts('Activity Type'),
277 'default' => TRUE,
278 ),
279 'subject' =>
280 array('title' => ts('Subject'),
281 'default' => TRUE,
282 ),
283 'source_contact_id' =>
284 array('title' => ts('Added By'),
285 'default' => TRUE,
286 ),
287 'activity_date_time' =>
288 array('title' => ts('Activity Date'),
289 'default' => TRUE,
290 ),
291 'activity_status_id' =>
292 array(
293 'name' => 'status_id',
294 'title' => ts('Activity Status'),
295 'default' => TRUE,
296 ),
297 ),
298 'grouping' => 'activity-fields',
299 ),
300 'civicrm_activity_target' =>
301 array(
4e3d3cfc 302 'dao' => 'CRM_Activity_DAO_ActivityContact',
6a488035
TO
303 'fields' =>
304 array(
4e3d3cfc 305 'contact_id' =>
6a488035
TO
306 array('title' => ts('With Contact'),
307 'default' => TRUE,
308 ),
309 ),
310 'grouping' => 'activity-fields',
311 ),
312 'civicrm_activity_assignment' =>
313 array(
4e3d3cfc 314 'dao' => 'CRM_Activity_DAO_ActivityContact',
6a488035
TO
315 'fields' =>
316 array(
4e3d3cfc 317 'contact_id' =>
6a488035
TO
318 array('title' => ts('Assigned To'),
319 'default' => TRUE,
320 ),
321 ),
322 'grouping' => 'activity-fields',
323 ),
324 'civicrm_group' =>
325 array(
326 'dao' => 'CRM_Contact_DAO_Group',
327 'alias' => 'cgroup',
328 'filters' =>
329 array(
330 'gid' =>
331 array(
332 'name' => 'group_id',
333 'title' => ts('Group'),
334 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
335 'group' => TRUE,
336 'options' => CRM_Core_PseudoConstant::group(),
337 ),
338 ),
339 ),
340 'civicrm_phone' =>
341 array(
342 'dao' => 'CRM_Core_DAO_Phone',
343 'fields' =>
344 array('phone' => NULL),
345 'grouping' => 'contact-fields',
346 ),
347 );
348
349 $this->_tagFilter = TRUE;
350 parent::__construct();
351 }
352
353 function preProcess() {
354 $this->_csvSupported = FALSE;
355 parent::preProcess();
356 }
357
358 function select() {
359 $select = array();
360 $this->_columnHeaders = array();
361 $this->_component = array('contribution_civireport', 'membership_civireport', 'participant_civireport', 'relationship_civireport', 'activity_civireport');
362 foreach ($this->_columns as $tableName => $table) {
363 if (array_key_exists('fields', $table)) {
364 foreach ($table['fields'] as $fieldName => $field) {
365 if (CRM_Utils_Array::value('required', $field) ||
366 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
367 ) {
368 //isolate the select clause compoenent wise
369 if (in_array($table['alias'], $this->_component)) {
370 $select[$table['alias']][] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
371 $this->_columnHeadersComponent[$table['alias']]["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
372 $this->_columnHeadersComponent[$table['alias']]["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
373 }
374 elseif ($table['alias'] == $this->_aliases['civicrm_activity_target'] ||
375 $table['alias'] == $this->_aliases['civicrm_activity_assignment']
376 ) {
377 if ($table['alias'] == $this->_aliases['civicrm_activity_target']) {
4e3d3cfc 378 $addContactId = 'civicrm_activity_target.contact_id';
6a488035
TO
379 }
380 else {
4e3d3cfc 381 $addContactId = 'civicrm_activity_assignment.contact_id';
6a488035 382 }
4e3d3cfc 383
6a488035 384 $tableName = $table['alias'];
4e3d3cfc 385 $select['activity_civireport'][] = "$tableName.display_name as {$tableName}_{$fieldName}, $addContactId ";
6a488035
TO
386 $this->_columnHeadersComponent['activity_civireport']["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
387 $this->_columnHeadersComponent['activity_civireport']["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
388 }
389 else {
390 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
391 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
392 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
393 }
394 }
395 }
396 }
397 }
398
399 foreach ($this->_component as $val) {
400 if (CRM_Utils_Array::value($val, $select)) {
401 $this->_selectComponent[$val] = "SELECT " . implode(', ', $select[$val]) . " ";
402 unset($select[$val]);
403 }
404 }
405
406 $this->_select = "SELECT " . implode(', ', $select) . " ";
407 }
408
409 static function formRule($fields, $files, $self) {
410 $errors = array();
411 return $errors;
412 }
413
414 function from() {
415 $group = " ";
416 $this->_from = "
417 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
418
419 if ($this->isTableSelected('civicrm_country') || $this->isTableSelected('civicrm_address')) {
420 $this->_from .= "
421 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
422 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
423 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
424 }
425
426 if ($this->isTableSelected('civicrm_email')) {
427 $this->_from .= "
428 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
429 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
430 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
431 }
432
433 if ($this->isTableSelected('civicrm_phone')) {
434 $this->_from .= "
435 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
436 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
437 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
438 }
439
440 if ($this->isTableSelected('civicrm_country')) {
441 $this->_from .= "
442 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
443 ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
444 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
445 }
446
447 $this->_from .= "{$group}";
448
449 foreach ($this->_component as $val) {
450 if (CRM_Utils_Array::value('contribution_civireport', $this->_selectComponent)) {
451 $this->_formComponent['contribution_civireport'] = " FROM
452 civicrm_contact {$this->_aliases['civicrm_contact']}
453 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
454 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
455 {$group}
456 ";
457 }
458 if (CRM_Utils_Array::value('membership_civireport', $this->_selectComponent)) {
459 $this->_formComponent['membership_civireport'] = " FROM
460 civicrm_contact {$this->_aliases['civicrm_contact']}
461 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
462 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
463 {$group} ";
464 }
465 if (CRM_Utils_Array::value('participant_civireport', $this->_selectComponent)) {
466 $this->_formComponent['participant_civireport'] = " FROM
467 civicrm_contact {$this->_aliases['civicrm_contact']}
468 INNER JOIN civicrm_participant {$this->_aliases['civicrm_participant']}
469 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_participant']}.contact_id
470 {$group} ";
471 }
472
473 if (CRM_Utils_Array::value('activity_civireport', $this->_selectComponent)) {
9e74e3ce 474 $activityContacts = CRM_Core_PseudoConstant::activityContacts('name');
475 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
476 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
477
6a488035
TO
478 $this->_formComponent['activity_civireport'] = "FROM
479 civicrm_activity {$this->_aliases['civicrm_activity']}
4e3d3cfc 480 LEFT JOIN civicrm_activity_contact civicrm_activity_target ON
481 {$this->_aliases['civicrm_activity']}.id = civicrm_activity_target.activity_id AND
9e74e3ce 482 civicrm_activity_target.record_type_id = {$targetID}
4e3d3cfc 483 LEFT JOIN civicrm_activity_contact civicrm_activity_assignment ON
9e74e3ce 484 {$this->_aliases['civicrm_activity']}.id = civicrm_activity_assignment.activity_id AND civicrm_activity_assignment.record_type_id = {$assigneeID}
6a488035
TO
485 LEFT JOIN civicrm_contact sourceContact ON
486 {$this->_aliases['civicrm_activity']}.source_contact_id = sourceContact.id
4e3d3cfc 487 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_activity_target']} ON
488 civicrm_activity_target.contact_id = {$this->_aliases['civicrm_activity_target']}.id
6a488035
TO
489
490 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_activity_assignment']} ON
4e3d3cfc 491 civicrm_activity_assignment.contact_id = {$this->_aliases['civicrm_activity_assignment']}.id
6a488035
TO
492 LEFT JOIN civicrm_option_value ON
493 ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
494 LEFT JOIN civicrm_option_group ON
495 civicrm_option_group.id = civicrm_option_value.option_group_id
496 LEFT JOIN civicrm_case_activity ON
497 civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
498 LEFT JOIN civicrm_case ON
499 civicrm_case_activity.case_id = civicrm_case.id
500 LEFT JOIN civicrm_case_contact ON
501 civicrm_case_contact.case_id = civicrm_case.id ";
502 }
503
504 if (CRM_Utils_Array::value('relationship_civireport', $this->_selectComponent)) {
505 $this->_formComponent['relationship_civireport'] = "FROM
506 civicrm_relationship {$this->_aliases['civicrm_relationship']}
507
508 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
509 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.contact_id_b
510 LEFT JOIN civicrm_contact contact_a ON
511 contact_a.id = {$this->_aliases['civicrm_relationship']}.contact_id_a ";
512 }
513 }
514 }
515
516 function where() {
517 $clauses = array();
518
519 foreach ($this->_columns as $tableName => $table) {
520 if (array_key_exists('filters', $table)) {
521 foreach ($table['filters'] as $fieldName => $field) {
522 $clause = NULL;
523 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
524 if ($op) {
525 $clause = $this->whereClause($field,
526 $op,
527 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
528 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
529 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
530 );
531 }
532 if (!empty($clause)) {
533 $clauses[] = $clause;
534 }
535 }
536 }
537 }
538
539 if (empty($clauses)) {
540 $this->_where = "WHERE ( 1 ) ";
541 }
542 else {
543 $this->_where = "WHERE " . implode(' AND ', $clauses);
544 }
545
546 if ($this->_aclWhere) {
547 $this->_where .= " AND {$this->_aclWhere} ";
548 }
549
550 $this->_where .= " GROUP BY {$this->_aliases['civicrm_contact']}.id ";
551 }
552
553 function clauseComponent() {
554 $selectedContacts = implode(',', $this->_contactSelected);
555 $contribution = $membership = $participant = NULL;
556 $eligibleResult = $rows = $tempArray = array();
557 foreach ($this->_component as $val) {
558 if (CRM_Utils_Array::value($val, $this->_selectComponent) && ($val != 'activity_civireport' && $val != 'relationship_civireport')) {
559 $sql = "{$this->_selectComponent[$val]} {$this->_formComponent[$val]}
560 WHERE {$this->_aliases['civicrm_contact']}.id IN ( $selectedContacts )
561 GROUP BY {$this->_aliases['civicrm_contact']}.id,{$val}.id ";
562
563 $dao = CRM_Core_DAO::executeQuery($sql);
564 while ($dao->fetch()) {
565 $countRecord = 0;
566 $eligibleResult[$val] = $val;
4e3d3cfc 567 $CC = 'civicrm_' . substr_replace($val, '', -11, 11) . '_contact_id';
6a488035
TO
568 $row = array();
569 foreach ($this->_columnHeadersComponent[$val] as $key => $value) {
570 $countRecord++;
571 $row[$key] = $dao->$key;
572 }
573
574 //if record exist for component(except contact_id)
575 //since contact_id is selected for every component
576 if ($countRecord > 1) {
577 $rows[$dao->$CC][$val][] = $row;
578 }
579 $tempArray[$dao->$CC] = $dao->$CC;
580 }
581 }
582 }
583
584 if (CRM_Utils_Array::value('relationship_civireport', $this->_selectComponent)) {
585
586 $relTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
587
588 $val = 'relationship_civireport';
589 $eligibleResult[$val] = $val;
590 $sql = "{$this->_selectComponent[$val]},{$this->_aliases['civicrm_contact']}.display_name as contact_b_name, contact_a.id as contact_a_id , contact_a.display_name as contact_a_name {$this->_formComponent[$val]}
591 WHERE ({$this->_aliases['civicrm_contact']}.id IN ( $selectedContacts )
592 OR
593 contact_a.id IN ( $selectedContacts ) ) AND
594 {$this->_aliases['civicrm_relationship']}.is_active = 1 AND
595 contact_a.is_deleted = 0 AND
596 {$this->_aliases['civicrm_contact']}.is_deleted = 0
597 GROUP BY {$this->_aliases['civicrm_relationship']}.id";
598
599 $dao = CRM_Core_DAO::executeQuery($sql);
600 while ($dao->fetch()) {
601 foreach ($this->_columnHeadersComponent[$val] as $key => $value) {
602 if ($key == 'civicrm_relationship_contact_id_b') {
603 $row[$key] = $dao->contact_b_name;
604 continue;
605 }
606
607 $row[$key] = $dao->$key;
608 }
609
4e3d3cfc 610 $relTitle = "" . $dao->civicrm_relationship_relationship_type_id . '_a_b';
6a488035
TO
611 $row['civicrm_relationship_relationship_type_id'] = $relTypes[$relTitle];
612
613 $rows[$dao->contact_a_id][$val][] = $row;
614
615 $row['civicrm_relationship_contact_id_b'] = $dao->contact_a_name;
4e3d3cfc 616 $relTitle = "" . $dao->civicrm_relationship_relationship_type_id . '_b_a';
6a488035
TO
617 if (isset($relTypes[$relTitle])) {
618 $row['civicrm_relationship_relationship_type_id'] = $relTypes[$relTitle];
619 }
620 $rows[$dao->civicrm_relationship_contact_id_b][$val][] = $row;
621 }
622 }
623
624 if (CRM_Utils_Array::value('activity_civireport', $this->_selectComponent)) {
625
626 $componentClause = "civicrm_option_value.component_id IS NULL";
627 $componentsIn = NULL;
628 $compInfo = CRM_Core_Component::getEnabledComponents();
629 foreach ($compInfo as $compObj) {
630 if ($compObj->info['showActivitiesInCore']) {
631 $componentsIn = $componentsIn ? ($componentsIn . ', ' . $compObj->componentID) : $compObj->componentID;
632 }
633 }
634 if ($componentsIn) {
635 $componentClause = "( $componentClause OR
636 civicrm_option_value.component_id IN ($componentsIn) )";
637 }
638
639 $val = 'activity_civireport';
640 $eligibleResult[$val] = $val;
641 $sql = "{$this->_selectComponent[$val]} ,
642 sourceContact.display_name as added_by {$this->_formComponent[$val]}
643
644 WHERE ( {$this->_aliases['civicrm_activity']}.source_contact_id IN ($selectedContacts) OR
4e3d3cfc 645 civicrm_activity_target.contact_id IN ($selectedContacts) OR
646 civicrm_activity_assignment.contact_id IN ($selectedContacts) OR
6a488035 647 civicrm_case_contact.contact_id IN ($selectedContacts) ) AND
4e3d3cfc 648 civicrm_option_group.name = 'activity_type' AND
649 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
650 ($componentClause)
6a488035
TO
651
652 GROUP BY {$this->_aliases['civicrm_activity']}.id
653
654 ORDER BY {$this->_aliases['civicrm_activity']}.activity_date_time desc ";
655
656 $dao = CRM_Core_DAO::executeQuery($sql);
657 while ($dao->fetch()) {
658 foreach ($this->_columnHeadersComponent[$val] as $key => $value) {
659 if ($key == 'civicrm_activity_source_contact_id') {
660 $row[$key] = $dao->added_by;
661 continue;
662 }
663 $row[$key] = $dao->$key;
664 }
665
666 if (isset($dao->civicrm_activity_source_contact_id)) {
667 $rows[$dao->civicrm_activity_source_contact_id][$val][] = $row;
668 }
669 if (isset($dao->target_contact_id)) {
670 $rows[$dao->target_contact_id][$val][] = $row;
671 }
672 if (isset($dao->assignee_contact_id)) {
673 $rows[$dao->assignee_contact_id][$val][] = $row;
674 }
675 }
676
677 //unset the component header if data is not present
678 foreach ($this->_component as $val) {
679 if (!in_array($val, $eligibleResult)) {
680
681 unset($this->_columnHeadersComponent[$val]);
682 }
683 }
684 }
685
686 return $rows;
687 }
688
689 function statistics(&$rows) {
690 $statistics = array();
691
692 $count = count($rows);
693 if ($this->_rollup && ($this->_rollup != '')) {
694 $count++;
695 }
696
697 $this->countStat($statistics, $count);
698 $this->filterStat($statistics);
699
700 return $statistics;
701 }
702
703 //Override to set limit is 10
704 function limit($rowCount = self::ROW_COUNT_LIMIT) {
705 parent::limit($rowCount);
706 }
707
708 //Override to set pager with limit is 10
709 function setPager($rowCount = self::ROW_COUNT_LIMIT) {
710 parent::setPager($rowCount);
711 }
712
713 function postProcess() {
714
715 $this->beginPostProcess();
716
717 // get the acl clauses built before we assemble the query
718 $this->buildACLClause($this->_aliases['civicrm_contact']);
719
720 $sql = $this->buildQuery(TRUE);
721
722 $rows = $graphRows = $this->_contactSelected = array();
723 $this->buildRows($sql, $rows);
724 foreach ($rows as $key => $val) {
725 $rows[$key]['contactID'] = $val['civicrm_contact_id'];
726 $this->_contactSelected[] = $val['civicrm_contact_id'];
727 }
728
729 $this->formatDisplay($rows);
730
731 if (!empty($this->_contactSelected)) {
732 $componentRows = $this->clauseComponent();
733 $this->alterComponentDisplay($componentRows);
734
735 //unset Conmponent id and contact id from display
736 foreach ($this->_columnHeadersComponent as $componentTitle => $headers) {
4e3d3cfc 737 $id_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_' . substr_replace($componentTitle, '', -11, 11) . '_id';
738 $contact_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_contact_id';
6a488035 739 if ($componentTitle == 'activity_civireport') {
4e3d3cfc 740 $id_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_id';
6a488035
TO
741 }
742
743 unset($this->_columnHeadersComponent[$componentTitle][$id_header]);
744 unset($this->_columnHeadersComponent[$componentTitle][$contact_header]);
745 }
746
747 $this->assign_by_ref('columnHeadersComponent', $this->_columnHeadersComponent);
748 $this->assign_by_ref('componentRows', $componentRows);
749 }
750
751 $this->doTemplateAssignment($rows);
752 $this->endPostProcess();
753 }
754
755 function alterDisplay(&$rows) {
756 // custom code to alter rows
757
758 $entryFound = FALSE;
759
760 foreach ($rows as $rowNum => $row) {
761 // make count columns point to detail report
762
763 // change contact name with link
764 if (array_key_exists('civicrm_contact_sort_name', $row) &&
765 array_key_exists('civicrm_contact_id', $row)
766 ) {
767
4e3d3cfc 768 $url = CRM_Utils_System::url('civicrm/contact/view',
6a488035
TO
769 'reset=1&cid=' . $row['civicrm_contact_id'],
770 $this->_absoluteUrl
771 );
772 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
4e3d3cfc 773 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact');
6a488035
TO
774 $entryFound = TRUE;
775 }
776
777 if (array_key_exists('civicrm_address_state_province_id', $row)) {
778 if ($value = $row['civicrm_address_state_province_id']) {
779 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
780 }
781 $entryFound = TRUE;
782 }
783
784
785 // skip looking further in rows, if first row itself doesn't
786 // have the column we need
787 if (!$entryFound) {
788 break;
789 }
790 }
791 }
792
793 function alterComponentDisplay(&$componentRows) {
794 // custom code to alter rows
795 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
796 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
797
798 $entryFound = FALSE;
799 foreach ($componentRows as $contactID => $components) {
800 foreach ($components as $component => $rows) {
801 foreach ($rows as $rowNum => $row) {
802 // handle contribution
803 if ($component == 'contribution_civireport') {
b914f4e8
PN
804 if ($val = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
805 $componentRows[$contactID][$component][$rowNum]['civicrm_contribution_financial_type_id'] =
806 CRM_Contribute_PseudoConstant::financialType($val, false);
6a488035
TO
807 }
808
809 if ($val = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
810 $componentRows[$contactID][$component][$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($val);
811 }
812 $entryFound = TRUE;
813 }
814
815 if ($component == 'membership_civireport') {
816 if ($val = CRM_Utils_Array::value('civicrm_membership_membership_type_id', $row)) {
817 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($val, FALSE);
818 }
819
820 if ($val = CRM_Utils_Array::value('civicrm_membership_status_id', $row)) {
821 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_status_id'] = CRM_Member_PseudoConstant::membershipStatus($val, FALSE);
822 }
823 $entryFound = TRUE;
824 }
825
826 if ($component == 'participant_civireport') {
827 if ($val = CRM_Utils_Array::value('civicrm_participant_event_id', $row)) {
828 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant::event($val, FALSE);
829 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
830 'reset=1&force=1&id_op=in&id_value=' . $val,
831 $this->_absoluteUrl, $this->_id
832 );
833 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id_link'] = $url;
4e3d3cfc 834 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id_hover'] = ts('View Event Income details for this Event.');
6a488035
TO
835 $entryFound = TRUE;
836 }
837
838 if ($val = CRM_Utils_Array::value('civicrm_participant_participant_status_id', $row)) {
839 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_participant_status_id'] = CRM_Event_PseudoConstant::participantStatus($val, FALSE);
840 }
841 if ($val = CRM_Utils_Array::value('civicrm_participant_role_id', $row)) {
842 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $val);
843 $value = array();
844 foreach ($roles as $role) {
845 $value[$role] = CRM_Event_PseudoConstant::participantRole($role, FALSE);
846 }
847 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_role_id'] = implode(', ', $value);
848 }
849
850 $entryFound = TRUE;
851 }
852
853 if ($component == 'activity_civireport') {
854 if ($val = CRM_Utils_Array::value('civicrm_activity_activity_type_id', $row)) {
855 $componentRows[$contactID][$component][$rowNum]['civicrm_activity_activity_type_id'] = $activityTypes[$val];
856 }
857 if ($val = CRM_Utils_Array::value('civicrm_activity_activity_status_id', $row)) {
858 $componentRows[$contactID][$component][$rowNum]['civicrm_activity_activity_status_id'] = $activityStatus[$val];
859 }
860
861 $entryFound = TRUE;
862 }
863 if ($component == 'membership_civireport') {
864 if ($val = CRM_Utils_Array::value('civicrm_membership_membership_status_id', $row)) {
865 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_membership_status_id'] = CRM_Member_PseudoConstant::membershipStatus($val);
866 }
867 $entryFound = TRUE;
868 }
869
870 // skip looking further in rows, if first row itself doesn't
871 // have the column we need
872 if (!$entryFound) {
873 break;
874 }
875 }
876 }
877 }
878 }
879}
880