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