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