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