CRM-9394
[civicrm-core.git] / CRM / Report / Form / Event / ParticipantListing.php
CommitLineData
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 */
37class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
38
39 protected $_summary = NULL;
40
41 protected $_customGroupExtends = array(
42 'Participant');
43 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
44
45 function __construct() {
46 $this->_columns = array(
47 'civicrm_contact' =>
48 array(
49 'dao' => 'CRM_Contact_DAO_Contact',
50 'fields' =>
51 array(
52 'sort_name_linked' =>
53 array('title' => ts('Participant Name'),
54 'required' => TRUE,
55 'no_repeat' => TRUE,
56 'dbAlias' => 'contact_civireport.sort_name',
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 'employer_id' =>
68 array('title' => ts('Organization'),
69 ),
70 ),
71 'grouping' => 'contact-fields',
72 'filters' =>
73 array(
74 'sort_name' =>
75 array('title' => ts('Participant Name'),
76 'operator' => 'like',
77 ),
78 ),
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_email' =>
86 array(
87 'dao' => 'CRM_Core_DAO_Email',
88 'fields' =>
89 array(
90 'email' =>
91 array('title' => ts('Email'),
92 'no_repeat' => TRUE,
93 ),
94 ),
95 'grouping' => 'contact-fields',
96 'filters' =>
97 array(
98 'email' =>
99 array('title' => ts('Participant E-mail'),
100 'operator' => 'like',
101 ),
102 ),
103 ),
104 'civicrm_address' =>
105 array(
106 'dao' => 'CRM_Core_DAO_Address',
107 'fields' =>
108 array(
109 'street_address' => NULL,
110 'city' => NULL,
111 'postal_code' => NULL,
112 'state_province_id' =>
113 array('title' => ts('State/Province'),
114 ),
115 'country_id' =>
116 array('title' => ts('Country'),
117 ),
118 ),
119 'grouping' => 'contact-fields',
120 ),
121 'civicrm_participant' =>
122 array(
123 'dao' => 'CRM_Event_DAO_Participant',
124 'fields' =>
125 array('participant_id' => array('title' => 'Participant ID'),
126 'participant_record' => array(
127 'name' => 'id',
128 'no_display' => TRUE,
129 'required' => TRUE,
130 ),
131 'event_id' => array(
132 'default' => TRUE,
133 'type' => CRM_Utils_Type::T_STRING,
134 ),
135 'status_id' => array('title' => ts('Status'),
136 'default' => TRUE,
137 ),
138 'role_id' => array('title' => ts('Role'),
139 'default' => TRUE,
140 ),
141 'participant_fee_level' => NULL,
142 'participant_fee_amount' => NULL,
143 'participant_register_date' => array('title' => ts('Registration Date')),
144 ),
145 'grouping' => 'event-fields',
146 'filters' =>
147 array(
148 'event_id' => array('name' => 'event_id',
149 'title' => ts('Event'),
150 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
151 'options' => $this->getEventFilterOptions(),
152 ),
153 'sid' => array(
154 'name' => 'status_id',
155 'title' => ts('Participant Status'),
156 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
157 'options' => CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'),
158 ),
159 'rid' => array(
160 'name' => 'role_id',
161 'title' => ts('Participant Role'),
162 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
163 'options' => CRM_Event_PseudoConstant::participantRole(),
164 ),
165 'participant_register_date' => array(
166 'title' => ' Registration Date',
167 'operatorType' => CRM_Report_Form::OP_DATE,
168 ),
169 ),
170 'order_bys' =>
171 array(
172 'event_id' =>
173 array('title' => ts('Event'), 'default_weight' => '1', 'default_order' => 'ASC'),
174 ),
175 ),
176 'civicrm_phone' =>
177 array(
178 'dao' => 'CRM_Core_DAO_Phone',
179 'fields' =>
180 array(
181 'phone' =>
182 array('title' => ts('Phone'),
183 'default' => TRUE,
184 'no_repeat' => TRUE,
185 ),
186 ),
187 'grouping' => 'contact-fields',
188 ),
189 'civicrm_event' =>
190 array(
191 'dao' => 'CRM_Event_DAO_Event',
192 'fields' =>
193 array(
194 'event_type_id' => array('title' => ts('Event Type')),
195 ),
196 'grouping' => 'event-fields',
197 'filters' =>
198 array(
199 'eid' => array(
200 'name' => 'event_type_id',
201 'title' => ts('Event Type'),
202 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
203 'options' => CRM_Core_OptionGroup::values('event_type'),
204 ),
205 ),
206 'order_bys' =>
207 array(
208 'event_type_id' =>
209 array('title' => ts('Event Type'), 'default_weight' => '2', 'default_order' => 'ASC'),
210 ),
211 ),
212 );
213 $this->_options = array('blank_column_begin' => array('title' => ts('Blank column at the Begining'),
214 'type' => 'checkbox',
215 ),
216 'blank_column_end' => array('title' => ts('Blank column at the End'),
217 'type' => 'select',
218 'options' => array(
219 '' => '-select-',
220 1 => ts('One'),
221 2 => ts('Two'),
222 3 => ts('Three'),
223 ),
224 ),
225 );
226 parent::__construct();
227 }
228
229 function preProcess() {
230 parent::preProcess();
231 }
232
233 function select() {
234 $select = array();
235 $this->_columnHeaders = array();
236
237 //add blank column at the Start
238 if (array_key_exists('options', $this->_params) &&
239 CRM_Utils_Array::value('blank_column_begin', $this->_params['options'])) {
240 $select[] = " '' as blankColumnBegin";
241 $this->_columnHeaders['blankColumnBegin']['title'] = '_ _ _ _';
242 }
243 foreach ($this->_columns as $tableName => $table) {
244 if (array_key_exists('fields', $table)) {
245 foreach ($table['fields'] as $fieldName => $field) {
246 if (CRM_Utils_Array::value('required', $field) ||
247 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
248 ) {
249
250 $alias = "{$tableName}_{$fieldName}";
251 $select[] = "{$field['dbAlias']} as $alias";
252 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
253 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
254 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
255 $this->_selectAliases[] = $alias;
256 }
257 }
258 }
259 }
260 //add blank column at the end
261 if ($blankcols = CRM_Utils_Array::value('blank_column_end', $this->_params)) {
262 for ($i = 1; $i <= $blankcols; $i++) {
263 $select[] = " '' as blankColumnEnd_{$i}";
264 $this->_columnHeaders["blank_{$i}"]['title'] = "_ _ _ _";
265 }
266 }
267
268 $this->_select = "SELECT " . implode(', ', $select) . " ";
269 }
270
271 static function formRule($fields, $files, $self) {
272 $errors = $grouping = array();
273 return $errors;
274 }
275
276 function from() {
277 $this->_from = "
278 FROM civicrm_participant {$this->_aliases['civicrm_participant']}
279 LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']}
280 ON ({$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id ) AND
281 ({$this->_aliases['civicrm_event']}.is_template IS NULL OR
282 {$this->_aliases['civicrm_event']}.is_template = 0)
283 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
284 ON ({$this->_aliases['civicrm_participant']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
285 {$this->_aclFrom}
286 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
287 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
288 {$this->_aliases['civicrm_address']}.is_primary = 1
289 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
290 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
291 {$this->_aliases['civicrm_email']}.is_primary = 1)
292 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
293 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
294 {$this->_aliases['civicrm_phone']}.is_primary = 1
295 ";
296 }
297
298 function where() {
299 $clauses = array();
300 foreach ($this->_columns as $tableName => $table) {
301 if (array_key_exists('filters', $table)) {
302 foreach ($table['filters'] as $fieldName => $field) {
303 $clause = NULL;
304
305 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
306 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
307 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
308 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
309
310 if ($relative || $from || $to) {
311 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
312 }
313 }
314 else {
315 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
316
317 if ($fieldName == 'rid') {
318 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
319 if (!empty($value)) {
320 $clause = "( {$field['dbAlias']} REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', $value) . "[[:>:]]' )";
321 }
322 $op = NULL;
323 }
324
325 if ($op) {
326 $clause = $this->whereClause($field,
327 $op,
328 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
329 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
330 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
331 );
332 }
333 }
334
335 if (!empty($clause)) {
336 $clauses[] = $clause;
337 }
338 }
339 }
340 }
341 if (empty($clauses)) {
342 $this->_where = "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 ";
343 }
344 else {
345 $this->_where = "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 AND " . implode(' AND ', $clauses);
346 }
347 if ($this->_aclWhere) {
348 $this->_where .= " AND {$this->_aclWhere} ";
349 }
350 }
351
352 function postProcess() {
353
354 // get ready with post process params
355 $this->beginPostProcess();
356
357 // get the acl clauses built before we assemble the query
358 $this->buildACLClause($this->_aliases['civicrm_contact']);
359 // build query
360 $sql = $this->buildQuery(TRUE);
361
362 // build array of result based on column headers. This method also allows
363 // modifying column headers before using it to build result set i.e $rows.
364 $this->buildRows($sql, $rows);
365
366 // format result set.
367 $this->formatDisplay($rows);
368
369 // assign variables to templates
370 $this->doTemplateAssignment($rows);
371
372 // do print / pdf / instance stuff if needed
373 $this->endPostProcess($rows);
374 }
375
376 function alterDisplay(&$rows) {
377 // custom code to alter rows
378
379 $entryFound = FALSE;
380 $eventType = CRM_Core_OptionGroup::values('event_type');
381
382 foreach ($rows as $rowNum => $row) {
383 // make count columns point to detail report
384 // convert display name to links
385 if (array_key_exists('civicrm_participant_event_id', $row)) {
386 if ($value = $row['civicrm_participant_event_id']) {
387 $rows[$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant::event($value, FALSE);
388 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
389 'reset=1&force=1&id_op=in&id_value=' . $value,
390 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
391 );
392 $rows[$rowNum]['civicrm_participant_event_id_link'] = $url;
393 $rows[$rowNum]['civicrm_participant_event_id_hover'] = ts("View Event Income Details for this Event");
394 }
395 $entryFound = TRUE;
396 }
397
398 // handle event type id
399 if (array_key_exists('civicrm_event_event_type_id', $row)) {
400 if ($value = $row['civicrm_event_event_type_id']) {
401 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
402 }
403 $entryFound = TRUE;
404 }
405
406 // handle participant status id
407 if (array_key_exists('civicrm_participant_status_id', $row)) {
408 if ($value = $row['civicrm_participant_status_id']) {
409 $rows[$rowNum]['civicrm_participant_status_id'] = CRM_Event_PseudoConstant::participantStatus($value, FALSE, 'label');
410 }
411 $entryFound = TRUE;
412 }
413
414 // handle participant role id
415 if (array_key_exists('civicrm_participant_role_id', $row)) {
416 if ($value = $row['civicrm_participant_role_id']) {
417 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
418 $value = array();
419 foreach ($roles as $role) {
420 $value[$role] = CRM_Event_PseudoConstant::participantRole($role, FALSE);
421 }
422 $rows[$rowNum]['civicrm_participant_role_id'] = implode(', ', $value);
423 }
424 $entryFound = TRUE;
425 }
426
427 // Handel value seperator in Fee Level
428 if (array_key_exists('civicrm_participant_participant_fee_level', $row)) {
429 if ($value = $row['civicrm_participant_participant_fee_level']) {
430 CRM_Event_BAO_Participant::fixEventLevel($value);
431 $rows[$rowNum]['civicrm_participant_participant_fee_level'] = $value;
432 }
433 $entryFound = TRUE;
434 }
435
436 // Convert display name to link
437 if (($displayName = CRM_Utils_Array::value('civicrm_contact_sort_name_linked', $row)) &&
438 ($cid = CRM_Utils_Array::value('civicrm_contact_id', $row)) &&
439 ($id = CRM_Utils_Array::value('civicrm_participant_participant_record', $row))
440 ) {
441 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
442 "reset=1&force=1&id_op=eq&id_value=$cid",
443 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
444 );
445
446 $viewUrl = CRM_Utils_System::url("civicrm/contact/view/participant",
447 "reset=1&id=$id&cid=$cid&action=view&context=participant"
448 );
449
450 $contactTitle = ts('View Contact Details');
451 $participantTitle = ts('View Participant Record');
452
453 $rows[$rowNum]['civicrm_contact_sort_name_linked'] = "<a title='$contactTitle' href=$url>$displayName</a>";
454 if ($this->_outputMode !== 'csv') {
455 $rows[$rowNum]['civicrm_contact_sort_name_linked'] .= "<span style='float: right;'><a title='$participantTitle' href=$viewUrl>" . ts('View') . "</a></span>";
456 }
457 $entryFound = TRUE;
458 }
459
460 // Handle country id
461 if (array_key_exists('civicrm_address_country_id', $row)) {
462 if ($value = $row['civicrm_address_country_id']) {
463 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, TRUE);
464 }
465 $entryFound = TRUE;
466 }
467
468 // Handle state/province id
469 if (array_key_exists('civicrm_address_state_province_id', $row)) {
470 if ($value = $row['civicrm_address_state_province_id']) {
471 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, TRUE);
472 }
473 $entryFound = TRUE;
474 }
475
476 // Handle employer id
477 if (array_key_exists('civicrm_contact_employer_id', $row)) {
478 if ($value = $row['civicrm_contact_employer_id']) {
479 $rows[$rowNum]['civicrm_contact_employer_id'] = CRM_Contact_BAO_Contact::displayName($value);
480 $url = CRM_Utils_System::url('civicrm/contact/view',
481 'reset=1&cid=' . $value, $this->_absoluteUrl
482 );
483 $rows[$rowNum]['civicrm_contact_employer_id_link'] = $url;
484 $rows[$rowNum]['civicrm_contact_employer_id_hover'] = ts('View Contact Summary for this Contact.');
485 }
486 }
487
488 // skip looking further in rows, if first row itself doesn't
489 // have the column we need
490 if (!$entryFound) {
491 break;
492 }
493 }
494 }
495}
496