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