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