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