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