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