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