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