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