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