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