INFRA-132 - phpcbf Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / CRM / Report / Form / Event / ParticipantListCount.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035 7 +--------------------------------------------------------------------+
2f4c2f5d 8 | This file is a part of CiviCRM. |
9 | |
6a488035 10 | CiviCRM is free software; you can copy, modify, and distribute it |
2f4c2f5d 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 |
6a488035 25 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Event_ParticipantListCount extends CRM_Report_Form_Event {
36
37 protected $_summary = NULL;
26dcc739
E
38 protected $_groupFilter = TRUE;
39 protected $_tagFilter = TRUE;
6a488035 40 protected $_customGroupExtends = array(
0f18adb9
E
41 'Participant',
42 'Event',
43 );
6a488035
TO
44
45 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
74cf4551
EM
46
47 /**
74cf4551
EM
48 */
49 /**
74cf4551 50 */
00be9182 51 public function __construct() {
6a488035 52 $this->_columns = array(
9d72cede 53 'civicrm_contact' => array(
6a488035 54 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
55 'fields' => array(
56 'sort_name' => array(
57 'title' => ts('Name'),
6a488035
TO
58 'default' => TRUE,
59 'no_repeat' => TRUE,
60 'required' => TRUE,
61 ),
9d72cede 62 'id' => array(
6a488035
TO
63 'no_display' => TRUE,
64 'required' => TRUE,
65 ),
66 ),
67 'grouping' => 'contact-fields',
68 'filters' => array(
9d72cede
EM
69 'sort_name' => array(
70 'title' => ts('Participant Name'),
6a488035
TO
71 'operator' => 'like',
72 ),
73 ),
74 ),
9d72cede
EM
75 'civicrm_employer' => array(
76 'dao' => 'CRM_Contact_DAO_Contact',
77 'grouping' => 'contact-fields',
78 'fields' => array(
79 'id' => array(
80 'no_display' => TRUE,
81 'required' => TRUE,
9cd28c89
DL
82 ),
83 'organization_name' => array(
9d72cede
EM
84 'title' => 'Employer',
85 'default' => TRUE,
86 'no_repeat' => TRUE,
9cd28c89
DL
87 ),
88 ),
89 ),
9d72cede 90 'civicrm_email' => array(
6a488035
TO
91 'dao' => 'CRM_Core_DAO_Email',
92 'fields' => array(
9d72cede
EM
93 'email' => array(
94 'title' => ts('Email'),
6a488035
TO
95 'no_repeat' => TRUE,
96 ),
97 ),
98 'grouping' => 'contact-fields',
9d72cede
EM
99 'filters' => array(
100 'email' => array(
101 'title' => ts('Participant E-mail'),
6a488035
TO
102 'operator' => 'like',
103 ),
104 ),
105 ),
9d72cede
EM
106 'civicrm_phone' => array(
107 'dao' => 'CRM_Core_DAO_Phone',
108 'grouping' => 'contact-fields',
109 'fields' => array(
110 'phone' => array(
111 'title' => ts('Phone No'),
112 'default' => TRUE,
113 ),
114 ),
115 ),
116 'civicrm_address' => array(
6a488035 117 'dao' => 'CRM_Core_DAO_Address',
9d72cede 118 'fields' => array(
6a488035 119 'street_address' => NULL,
9cd28c89
DL
120 'city' => NULL,
121 'postal_code' => NULL,
9d72cede
EM
122 'state_province_id' => array(
123 'title' => ts('State/Province'),
9cd28c89 124 ),
9d72cede
EM
125 'country_id' => array(
126 'title' => ts('Country'),
9cd28c89 127 ),
6a488035
TO
128 ),
129 'grouping' => 'contact-fields',
130 ),
9d72cede 131 'civicrm_participant' => array(
6a488035 132 'dao' => 'CRM_Event_DAO_Participant',
9d72cede
EM
133 'fields' => array(
134 'participant_id' => array(
135 'title' => ts('Participant ID'),
6a488035
TO
136 'default' => TRUE,
137 ),
9d72cede
EM
138 'event_id' => array(
139 'title' => ts('Event'),
6a488035
TO
140 'type' => CRM_Utils_Type::T_STRING,
141 ),
9d72cede
EM
142 'role_id' => array(
143 'title' => ts('Role'),
6a488035
TO
144 'default' => TRUE,
145 ),
9d72cede
EM
146 'status_id' => array(
147 'title' => ts('Status'),
6a488035
TO
148 'default' => TRUE,
149 ),
9d72cede
EM
150 'participant_register_date' => array(
151 'title' => ts('Registration Date'),
6a488035
TO
152 ),
153 ),
154 'grouping' => 'event-fields',
9d72cede 155 'filters' => array(
2107cde9 156 'event_id' => array(
6a488035
TO
157 'name' => 'event_id',
158 'title' => ts('Event'),
2107cde9
CW
159 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
160 'type' => CRM_Utils_Type::T_INT,
9d72cede
EM
161 'attributes' => array(
162 'entity' => 'event',
21dfd5f5 163 'select' => array('minimumInputLength' => 0),
9d72cede 164 ),
6a488035 165 ),
9d72cede 166 'sid' => array(
6a488035
TO
167 'name' => 'status_id',
168 'title' => ts('Participant Status'),
169 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
170 'options' => CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'),
171 ),
9d72cede 172 'rid' => array(
6a488035
TO
173 'name' => 'role_id',
174 'title' => ts('Participant Role'),
2039ce87 175 'operatorType' => CRM_Report_Form::OP_MULTISELECT_SEPARATOR,
6a488035
TO
176 'options' => CRM_Event_PseudoConstant::participantRole(),
177 ),
9d72cede
EM
178 'participant_register_date' => array(
179 'title' => ts('Registration Date'),
6a488035
TO
180 'operatorType' => CRM_Report_Form::OP_DATE,
181 ),
182 ),
9d72cede
EM
183 'group_bys' => array(
184 'event_id' => array(
185 'title' => ts('Event'),
6a488035
TO
186 ),
187 ),
188 ),
9d72cede 189 'civicrm_event' => array(
6a488035 190 'dao' => 'CRM_Event_DAO_Event',
9d72cede
EM
191 'fields' => array(
192 'event_type_id' => array(
193 'title' => ts('Event Type'),
6a488035 194 ),
9d72cede
EM
195 'start_date' => array(
196 'title' => ts('Event Start Date'),
6a488035 197 ),
9d72cede
EM
198 'end_date' => array(
199 'title' => ts('Event End Date'),
6a488035
TO
200 ),
201 ),
202 'grouping' => 'event-fields',
203 'filters' => array(
9d72cede 204 'eid' => array(
6a488035
TO
205 'name' => 'event_type_id',
206 'title' => ts('Event Type'),
207 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
208 'options' => CRM_Core_OptionGroup::values('event_type'),
209 ),
9d72cede 210 'event_start_date' => array(
6a488035
TO
211 'name' => 'start_date',
212 'title' => ts('Event Start Date'),
213 'operatorType' => CRM_Report_Form::OP_DATE,
214 ),
9d72cede 215 'event_end_date' => array(
6a488035
TO
216 'name' => 'end_date',
217 'title' => ts('Event End Date'),
218 'operatorType' => CRM_Report_Form::OP_DATE,
219 ),
220 ),
9d72cede
EM
221 'group_bys' => array(
222 'event_type_id' => array(
223 'title' => ts('Event Type '),
6a488035
TO
224 ),
225 ),
226 ),
9d72cede 227 'civicrm_line_item' => array(
6a488035 228 'dao' => 'CRM_Price_DAO_LineItem',
9d72cede
EM
229 'fields' => array(
230 'line_total' => array(
231 'title' => ts('Income'),
6a488035 232 'default' => TRUE,
9d72cede
EM
233 'statistics' => array(
234 'sum' => ts('Amount'),
6a488035
TO
235 'avg' => ts('Average'),
236 ),
237 ),
9d72cede
EM
238 'participant_count' => array(
239 'title' => ts('Count'),
6a488035 240 'default' => TRUE,
9d72cede
EM
241 'statistics' => array(
242 'sum' => ts('Count'),
6a488035
TO
243 ),
244 ),
245 ),
246 ),
247 );
248
249 $this->_options = array(
9d72cede
EM
250 'blank_column_begin' => array(
251 'title' => ts('Blank column at the Begining'),
6a488035
TO
252 'type' => 'checkbox',
253 ),
9d72cede
EM
254 'blank_column_end' => array(
255 'title' => ts('Blank column at the End'),
6a488035
TO
256 'type' => 'select',
257 'options' => array(
258 '' => '-select-',
259 1 => ts('One'),
260 2 => ts('Two'),
261 3 => ts('Three'),
262 ),
263 ),
264 );
265 parent::__construct();
266 }
267
00be9182 268 public function preProcess() {
6a488035
TO
269 parent::preProcess();
270 }
271
74cf4551 272 /**
4f1f1f2a 273 * Add The statistics
74cf4551
EM
274 * @param $rows
275 *
276 * @return array
277 */
00be9182 278 public function statistics(&$rows) {
6a488035
TO
279
280 $statistics = parent::statistics($rows);
9d72cede
EM
281 $avg = NULL;
282 $select = " SELECT SUM( {$this->_aliases['civicrm_line_item']}.participant_count ) as count,
2f4c2f5d 283 SUM( {$this->_aliases['civicrm_line_item']}.line_total ) as amount
284 ";
6a488035
TO
285 $sql = "{$select} {$this->_from} {$this->_where}";
286 $dao = CRM_Core_DAO::executeQuery($sql);
287 if ($dao->fetch()) {
288
289 if ($dao->count && $dao->amount) {
290 $avg = $dao->amount / $dao->count;
291 }
292 $statistics['counts']['count'] = array(
293 'value' => $dao->count,
294 'title' => 'Total Participants',
295 'type' => CRM_Utils_Type::T_INT,
296 );
297 $statistics['counts']['amount'] = array(
298 'value' => $dao->amount,
299 'title' => 'Total Income',
300 'type' => CRM_Utils_Type::T_MONEY,
301 );
2f4c2f5d 302 $statistics['counts']['avg '] = array(
6a488035
TO
303 'value' => $avg,
304 'title' => 'Average',
305 'type' => CRM_Utils_Type::T_MONEY,
306 );
307 }
308
309 return $statistics;
310 }
311
00be9182 312 public function select() {
6a488035
TO
313 $select = array();
314 $this->_columnHeaders = array();
315
316 //add blank column at the Start
9d72cede
EM
317 if (array_key_exists('options', $this->_params) &&
318 !empty($this->_params['options']['blank_column_begin'])
319 ) {
6a488035
TO
320 $select[] = " '' as blankColumnBegin";
321 $this->_columnHeaders['blankColumnBegin']['title'] = '_ _ _ _';
322 }
323 foreach ($this->_columns as $tableName => $table) {
324 if (array_key_exists('fields', $table)) {
325 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
326 if (!empty($field['required']) ||
327 !empty($this->_params['fields'][$fieldName])
328 ) {
a7488080 329 if (!empty($field['statistics'])) {
6a488035
TO
330 foreach ($field['statistics'] as $stat => $label) {
331 switch (strtolower($stat)) {
332 case 'sum':
333 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
334 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
335 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
336 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
337 break;
338 }
339 }
340 }
341 else {
342 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
343 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
344 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
345 }
346 }
347 }
348 }
349 }
350 //add blank column at the end
351 if ($blankcols = CRM_Utils_Array::value('blank_column_end', $this->_params)) {
352 for ($i = 1; $i <= $blankcols; $i++) {
353 $select[] = " '' as blankColumnEnd_{$i}";
354 $this->_columnHeaders["blank_{$i}"]['title'] = "_ _ _ _";
355 }
356 }
357 $this->_select = "SELECT " . implode(', ', $select) . " ";
358 }
359
74cf4551
EM
360 /**
361 * @param $fields
362 * @param $files
363 * @param $self
364 *
365 * @return array
366 */
00be9182 367 public static function formRule($fields, $files, $self) {
6a488035
TO
368 $errors = $grouping = array();
369 return $errors;
370 }
371
00be9182 372 public function from() {
6a488035 373 $this->_from = "
f813f78e 374 FROM civicrm_participant {$this->_aliases['civicrm_participant']}
375 LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']}
99f13d8d 376 ON ({$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id ) AND {$this->_aliases['civicrm_event']}.is_template = 0
f813f78e 377 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
378 ON ({$this->_aliases['civicrm_participant']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
379 {$this->_aclFrom}
380 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
381 ON ({$this->_aliases['civicrm_employer']}.id = {$this->_aliases['civicrm_contact']}.employer_id )
382 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
383 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
384 {$this->_aliases['civicrm_address']}.is_primary = 1
385 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
386 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
387 {$this->_aliases['civicrm_email']}.is_primary = 1)
2f4c2f5d 388 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
6a488035
TO
389 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
390 {$this->_aliases['civicrm_phone']}.is_primary = 1
2f4c2f5d 391 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
5fa8e57d 392 ON {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_participant' AND {$this->_aliases['civicrm_participant']}.id ={$this->_aliases['civicrm_line_item']}.entity_id";
6a488035
TO
393 }
394
00be9182 395 public function storeWhereHavingClauseArray() {
e8b2628d
DS
396 parent::storeWhereHavingClauseArray();
397 $this->_whereClauses[] = "{$this->_aliases['civicrm_participant']}.is_test = 0";
6a488035
TO
398 }
399
00be9182 400 public function groupBy() {
6a488035 401 $this->_groupBy = "";
a7488080 402 if (!empty($this->_params['group_bys']) &&
6a488035
TO
403 is_array($this->_params['group_bys']) &&
404 !empty($this->_params['group_bys'])
405 ) {
406 foreach ($this->_columns as $tableName => $table) {
407 if (array_key_exists('group_bys', $table)) {
408 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080 409 if (!empty($this->_params['group_bys'][$fieldName])) {
6a488035
TO
410 $this->_groupBy[] = $field['dbAlias'];
411 }
412 }
413 }
414 }
415 }
416
417 if (!empty($this->_groupBy)) {
9d72cede
EM
418 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) .
419 ", {$this->_aliases['civicrm_contact']}.sort_name";
6a488035
TO
420 }
421 else {
422 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
423 }
9d72cede
EM
424 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_participant']}.id " .
425 $this->_groupBy;
6a488035
TO
426 }
427
00be9182 428 public function postProcess() {
6a488035
TO
429
430 // get ready with post process params
431 $this->beginPostProcess();
432
433 // get the acl clauses built before we assemble the query
434 $this->buildACLClause($this->_aliases['civicrm_contact']);
435 // build query
436 $sql = $this->buildQuery(TRUE);
437
438 // build array of result based on column headers. This method also allows
439 // modifying column headers before using it to build result set i.e $rows.
440 $this->buildRows($sql, $rows);
441
442 // format result set.
443 $this->formatDisplay($rows);
444
445 // assign variables to templates
446 $this->doTemplateAssignment($rows);
447
448 // do print / pdf / instance stuff if needed
449 $this->endPostProcess($rows);
450 }
451
74cf4551
EM
452 /**
453 * @param $rows
454 */
00be9182 455 public function alterDisplay(&$rows) {
6a488035
TO
456
457 $entryFound = FALSE;
458 $eventType = CRM_Core_OptionGroup::values('event_type');
459
460 foreach ($rows as $rowNum => $row) {
461
462 // convert sort name to links
463 if (array_key_exists('civicrm_contact_sort_name', $row) &&
464 array_key_exists('civicrm_contact_id', $row)
465 ) {
466 if ($value = $row['civicrm_contact_sort_name']) {
467 $url = CRM_Utils_System::url("civicrm/contact/view",
468 'reset=1&cid=' . $row['civicrm_contact_id'],
469 $this->_absoluteUrl
470 );
471 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
473 }
474 $entryFound = TRUE;
475 }
476
477 // convert participant ID to links
478 if (array_key_exists('civicrm_participant_participant_id', $row) &&
479 array_key_exists('civicrm_contact_id', $row)
480 ) {
481 if ($value = $row['civicrm_participant_participant_id']) {
482 $url = CRM_Utils_System::url("civicrm/contact/view/participant",
9d72cede
EM
483 'reset=1&id=' . $row['civicrm_participant_participant_id'] .
484 '&cid=' . $row['civicrm_contact_id'] .
485 '&action=view&context=participant',
6a488035
TO
486 $this->_absoluteUrl
487 );
488 $rows[$rowNum]['civicrm_participant_participant_id_link'] = $url;
489 $rows[$rowNum]['civicrm_participant_participant_id_hover'] = ts("View Participant Record for this Contact.");
490 }
491 $entryFound = TRUE;
492 }
493
494 // convert event name to links
495 if (array_key_exists('civicrm_participant_event_id', $row)) {
496 if ($value = $row['civicrm_participant_event_id']) {
497 $rows[$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant::event($value, FALSE);
498 $url = CRM_Report_Utils_Report::getNextUrl('event/Income',
499 'reset=1&force=1&event_id_op=eq&event_id_value=' . $value,
500 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
501 );
502 $rows[$rowNum]['civicrm_participant_event_id_link'] = $url;
503 $rows[$rowNum]['civicrm_participant_event_id_hover'] = ts("View Event Income Details for this Event");
504 }
505 $entryFound = TRUE;
506 }
507
508 // handle event type id
509 if (array_key_exists('civicrm_event_event_type_id', $row)) {
510 if ($value = $row['civicrm_event_event_type_id']) {
511 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
512 }
513 $entryFound = TRUE;
514 }
515
516 // handle participant status id
517 if (array_key_exists('civicrm_participant_status_id', $row)) {
518 if ($value = $row['civicrm_participant_status_id']) {
519 $rows[$rowNum]['civicrm_participant_status_id'] = CRM_Event_PseudoConstant::participantStatus($value, FALSE);
520 }
521 $entryFound = TRUE;
522 }
523
524 // handle participant role id
525 if (array_key_exists('civicrm_participant_role_id', $row)) {
526 if ($value = $row['civicrm_participant_role_id']) {
527 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
528 $value = array();
529 foreach ($roles as $role) {
530 $value[$role] = CRM_Event_PseudoConstant::participantRole($role, FALSE);
531 }
532 $rows[$rowNum]['civicrm_participant_role_id'] = implode(', ', $value);
533 }
534 $entryFound = TRUE;
535 }
536
6a488035
TO
537 // skip looking further in rows, if first row itself doesn't
538 // have the column we need
539 if (!$entryFound) {
540 break;
541 }
542 }
543 }
96025800 544
6a488035 545}