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