CRM-15603 - Fix missing space in 'World Region'
[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_phone' =>
360 array(
361 'dao' => 'CRM_Core_DAO_Phone',
362 'fields' =>
363 array(
364 'phone' => NULL,
365 'phone_ext' =>
366 array(
367 'title' => ts('Phone Extension')
368 )
369 ),
370 'grouping' => 'contact-fields',
371 ),
372 );
373 $this->_groupFilter = TRUE;
374 $this->_tagFilter = TRUE;
375 parent::__construct();
376 }
377
378 function preProcess() {
379 $this->_csvSupported = FALSE;
380 parent::preProcess();
381 }
382
383 function select() {
384 $select = array();
385 $this->_columnHeaders = array();
386 $this->_component = array('contribution_civireport', 'membership_civireport', 'participant_civireport', 'relationship_civireport', 'activity_civireport');
387 foreach ($this->_columns as $tableName => $table) {
388 if (array_key_exists('fields', $table)) {
389 foreach ($table['fields'] as $fieldName => $field) {
390 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
391 //isolate the select clause compoenent wise
392 if (in_array($table['alias'], $this->_component)) {
393 $select[$table['alias']][] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
394 $this->_columnHeadersComponent[$table['alias']]["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
395 $this->_columnHeadersComponent[$table['alias']]["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
396 }
397 elseif ($table['alias'] == $this->_aliases['civicrm_activity_target'] ||
398 $table['alias'] == $this->_aliases['civicrm_activity_assignment'] ||
399 $table['alias'] == $this->_aliases['civicrm_activity_source']
400 ) {
401 if ($table['alias'] == $this->_aliases['civicrm_activity_target']) {
402 $addContactId = 'civicrm_activity_target.contact_id as target_contact_id';
403 }
404 elseif ($table['alias'] == $this->_aliases['civicrm_activity_source']) {
405 $addContactId = 'civicrm_activity_source.contact_id';
406 }
407 else {
408 $addContactId = 'civicrm_activity_assignment.contact_id as assignee_contact_id';
409 }
410
411 $tableName = $table['alias'];
412 $select['activity_civireport'][] = "$tableName.display_name as {$tableName}_{$fieldName}, $addContactId ";
413 $this->_columnHeadersComponent['activity_civireport']["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
414 $this->_columnHeadersComponent['activity_civireport']["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
415 }
416 else {
417 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
418 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
419 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
420 }
421 }
422 }
423 }
424 }
425
426 foreach ($this->_component as $val) {
427 if (!empty($select[$val])) {
428 $this->_selectComponent[$val] = "SELECT " . implode(', ', $select[$val]) . " ";
429 unset($select[$val]);
430 }
431 }
432
433 $this->_select = "SELECT " . implode(', ', $select) . " ";
434 }
435
436 /**
437 * @param $fields
438 * @param $files
439 * @param $self
440 *
441 * @return array
442 */
443 static function formRule($fields, $files, $self) {
444 $errors = array();
445 return $errors;
446 }
447
448 function from() {
449 $group = " ";
450 $this->_from = "
451 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";
452
453 if ($this->isTableSelected('civicrm_country') || $this->isTableSelected('civicrm_address')) {
454 $this->_from .= "
455 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
456 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
457 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
458 }
459
460 if ($this->isTableSelected('civicrm_email')) {
461 $this->_from .= "
462 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
463 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
464 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
465 }
466
467 if ($this->isTableSelected('civicrm_phone')) {
468 $this->_from .= "
469 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
470 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
471 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
472 }
473
474 if ($this->isTableSelected('civicrm_country')) {
475 $this->_from .= "
476 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
477 ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
478 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
479 }
480
481 $this->_from .= "{$group}";
482
483 foreach ($this->_component as $val) {
484 if (!empty($this->_selectComponent['contribution_civireport'])) {
485 $this->_formComponent['contribution_civireport'] = " FROM
486 civicrm_contact {$this->_aliases['civicrm_contact']}
487 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
488 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
489 {$group}
490 ";
491 }
492 if (!empty($this->_selectComponent['membership_civireport'])) {
493 $this->_formComponent['membership_civireport'] = " FROM
494 civicrm_contact {$this->_aliases['civicrm_contact']}
495 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
496 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
497 {$group} ";
498 }
499 if (!empty($this->_selectComponent['participant_civireport'])) {
500 $this->_formComponent['participant_civireport'] = " FROM
501 civicrm_contact {$this->_aliases['civicrm_contact']}
502 INNER JOIN civicrm_participant {$this->_aliases['civicrm_participant']}
503 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_participant']}.contact_id
504 {$group} ";
505 }
506
507 if (!empty($this->_selectComponent['activity_civireport'])) {
508 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
509 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
510 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
511 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
512
513 $this->_formComponent['activity_civireport'] = "FROM
514 civicrm_activity {$this->_aliases['civicrm_activity']}
515 LEFT JOIN civicrm_activity_contact civicrm_activity_target ON
516 {$this->_aliases['civicrm_activity']}.id = civicrm_activity_target.activity_id AND
517 civicrm_activity_target.record_type_id = {$targetID}
518 LEFT JOIN civicrm_activity_contact civicrm_activity_assignment ON
519 {$this->_aliases['civicrm_activity']}.id = civicrm_activity_assignment.activity_id AND civicrm_activity_assignment.record_type_id = {$assigneeID}
520 LEFT JOIN civicrm_activity_contact civicrm_activity_source
521 ON {$this->_aliases['civicrm_activity']}.id = civicrm_activity_source.activity_id AND
522 civicrm_activity_source.record_type_id = {$sourceID}
523 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_activity_target']} ON
524 civicrm_activity_target.contact_id = {$this->_aliases['civicrm_activity_target']}.id
525
526 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_activity_assignment']} ON
527 civicrm_activity_assignment.contact_id = {$this->_aliases['civicrm_activity_assignment']}.id
528 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_activity_source']} ON
529 civicrm_activity_source.contact_id = {$this->_aliases['civicrm_activity_source']}.id
530 LEFT JOIN civicrm_option_value ON
531 ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
532 LEFT JOIN civicrm_option_group ON
533 civicrm_option_group.id = civicrm_option_value.option_group_id
534 LEFT JOIN civicrm_case_activity ON
535 civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
536 LEFT JOIN civicrm_case ON
537 civicrm_case_activity.case_id = civicrm_case.id
538 LEFT JOIN civicrm_case_contact ON
539 civicrm_case_contact.case_id = civicrm_case.id ";
540 }
541
542 if (!empty($this->_selectComponent['relationship_civireport'])) {
543 $this->_formComponent['relationship_civireport'] = "FROM
544 civicrm_relationship {$this->_aliases['civicrm_relationship']}
545
546 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
547 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.contact_id_b
548 LEFT JOIN civicrm_contact contact_a ON
549 contact_a.id = {$this->_aliases['civicrm_relationship']}.contact_id_a ";
550 }
551 }
552 }
553
554 function where() {
555 $clauses = array();
556
557 foreach ($this->_columns as $tableName => $table) {
558 if (array_key_exists('filters', $table)) {
559 foreach ($table['filters'] as $fieldName => $field) {
560 $clause = NULL;
561 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE) {
562 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
563 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
564 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
565
566 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
567 }
568 else {
569 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
570 $clause = $this->whereClause($field,
571 $op,
572 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
573 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
574 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
575 );
576 }
577 if (!empty($clause)) {
578 $clauses[] = $clause;
579 }
580 }
581 }
582 }
583
584 if (empty($clauses)) {
585 $this->_where = "WHERE ( 1 ) ";
586 }
587 else {
588 $this->_where = "WHERE " . implode(' AND ', $clauses);
589 }
590
591 if ($this->_aclWhere) {
592 $this->_where .= " AND {$this->_aclWhere} ";
593 }
594
595 $this->_where .= " GROUP BY {$this->_aliases['civicrm_contact']}.id ";
596 }
597
598 /**
599 * @return array
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 /**
738 * @param $rows
739 *
740 * @return array
741 */
742 function statistics(&$rows) {
743 $statistics = array();
744
745 $count = count($rows);
746 if ($this->_rollup && ($this->_rollup != '')) {
747 $count++;
748 }
749
750 $this->countStat($statistics, $count);
751 $this->filterStat($statistics);
752
753 return $statistics;
754 }
755
756 //Override to set limit is 10
757 /**
758 * @param int $rowCount
759 */
760 function limit($rowCount = self::ROW_COUNT_LIMIT) {
761 parent::limit($rowCount);
762 }
763
764 //Override to set pager with limit is 10
765 /**
766 * @param int $rowCount
767 */
768 function setPager($rowCount = self::ROW_COUNT_LIMIT) {
769 parent::setPager($rowCount);
770 }
771
772 function postProcess() {
773
774 $this->beginPostProcess();
775
776 // get the acl clauses built before we assemble the query
777 $this->buildACLClause($this->_aliases['civicrm_contact']);
778
779 $sql = $this->buildQuery(TRUE);
780
781 $rows = $graphRows = $this->_contactSelected = array();
782 $this->buildRows($sql, $rows);
783 foreach ($rows as $key => $val) {
784 $rows[$key]['contactID'] = $val['civicrm_contact_id'];
785 $this->_contactSelected[] = $val['civicrm_contact_id'];
786 }
787
788 $this->formatDisplay($rows);
789
790 if (!empty($this->_contactSelected)) {
791 $componentRows = $this->clauseComponent();
792 $this->alterComponentDisplay($componentRows);
793
794 //unset Conmponent id and contact id from display
795 foreach ($this->_columnHeadersComponent as $componentTitle => $headers) {
796 $id_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_' . substr_replace($componentTitle, '', -11, 11) . '_id';
797 $contact_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_contact_id';
798 if ($componentTitle == 'activity_civireport') {
799 $id_header = 'civicrm_' . substr_replace($componentTitle, '', -11, 11) . '_id';
800 }
801
802 unset($this->_columnHeadersComponent[$componentTitle][$id_header]);
803 unset($this->_columnHeadersComponent[$componentTitle][$contact_header]);
804 }
805
806 $this->assign_by_ref('columnHeadersComponent', $this->_columnHeadersComponent);
807 $this->assign_by_ref('componentRows', $componentRows);
808 }
809
810 $this->doTemplateAssignment($rows);
811 $this->endPostProcess();
812 }
813
814 /**
815 * @param $rows
816 */
817 function alterDisplay(&$rows) {
818 // custom code to alter rows
819
820 $entryFound = FALSE;
821
822 foreach ($rows as $rowNum => $row) {
823 // make count columns point to detail report
824
825 // change contact name with link
826 if (array_key_exists('civicrm_contact_sort_name', $row) &&
827 array_key_exists('civicrm_contact_id', $row)
828 ) {
829
830 $url = CRM_Utils_System::url('civicrm/contact/view',
831 'reset=1&cid=' . $row['civicrm_contact_id'],
832 $this->_absoluteUrl
833 );
834 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
835 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact');
836 $entryFound = TRUE;
837 }
838
839 if (array_key_exists('civicrm_address_state_province_id', $row)) {
840 if ($value = $row['civicrm_address_state_province_id']) {
841 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
842 }
843 $entryFound = TRUE;
844 }
845
846
847 // skip looking further in rows, if first row itself doesn't
848 // have the column we need
849 if (!$entryFound) {
850 break;
851 }
852 }
853 }
854
855 /**
856 * @param $componentRows
857 */
858 function alterComponentDisplay(&$componentRows) {
859 // custom code to alter rows
860 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
861 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
862
863 $entryFound = FALSE;
864 foreach ($componentRows as $contactID => $components) {
865 foreach ($components as $component => $rows) {
866 foreach ($rows as $rowNum => $row) {
867 // handle contribution
868 if ($component == 'contribution_civireport') {
869 if ($val = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
870 $componentRows[$contactID][$component][$rowNum]['civicrm_contribution_financial_type_id'] =
871 CRM_Contribute_PseudoConstant::financialType($val, false);
872 }
873
874 if ($val = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
875 $componentRows[$contactID][$component][$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($val);
876 }
877 $entryFound = TRUE;
878 }
879
880 if ($component == 'membership_civireport') {
881 if ($val = CRM_Utils_Array::value('civicrm_membership_membership_type_id', $row)) {
882 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($val, FALSE);
883 }
884
885 if ($val = CRM_Utils_Array::value('civicrm_membership_status_id', $row)) {
886 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_status_id'] = CRM_Member_PseudoConstant::membershipStatus($val, FALSE);
887 }
888 $entryFound = TRUE;
889 }
890
891 if ($component == 'participant_civireport') {
892 if ($val = CRM_Utils_Array::value('civicrm_participant_event_id', $row)) {
893 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant::event($val, FALSE);
894 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
895 'reset=1&force=1&id_op=in&id_value=' . $val,
896 $this->_absoluteUrl, $this->_id
897 );
898 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id_link'] = $url;
899 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_event_id_hover'] = ts('View Event Income details for this Event.');
900 $entryFound = TRUE;
901 }
902
903 if ($val = CRM_Utils_Array::value('civicrm_participant_participant_status_id', $row)) {
904 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_participant_status_id'] = CRM_Event_PseudoConstant::participantStatus($val, FALSE);
905 }
906 if ($val = CRM_Utils_Array::value('civicrm_participant_role_id', $row)) {
907 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $val);
908 $value = array();
909 foreach ($roles as $role) {
910 $value[$role] = CRM_Event_PseudoConstant::participantRole($role, FALSE);
911 }
912 $componentRows[$contactID][$component][$rowNum]['civicrm_participant_role_id'] = implode(', ', $value);
913 }
914
915 $entryFound = TRUE;
916 }
917
918 if ($component == 'activity_civireport') {
919 if ($val = CRM_Utils_Array::value('civicrm_activity_activity_type_id', $row)) {
920 $componentRows[$contactID][$component][$rowNum]['civicrm_activity_activity_type_id'] = $activityTypes[$val];
921 }
922 if ($val = CRM_Utils_Array::value('civicrm_activity_activity_status_id', $row)) {
923 $componentRows[$contactID][$component][$rowNum]['civicrm_activity_activity_status_id'] = $activityStatus[$val];
924 }
925
926 $entryFound = TRUE;
927 }
928 if ($component == 'membership_civireport') {
929 if ($val = CRM_Utils_Array::value('civicrm_membership_membership_status_id', $row)) {
930 $componentRows[$contactID][$component][$rowNum]['civicrm_membership_membership_status_id'] = CRM_Member_PseudoConstant::membershipStatus($val);
931 }
932 $entryFound = TRUE;
933 }
934
935 // skip looking further in rows, if first row itself doesn't
936 // have the column we need
937 if (!$entryFound) {
938 break;
939 }
940 }
941 }
942 }
943 }
944 }
945