3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Event_Summary
extends CRM_Report_Form
{
19 protected $_summary = NULL;
21 protected $_add2groupSupported = FALSE;
23 protected $_customGroupExtends = [
26 public $_drilldownReport = ['event/income' => 'Link to Detail Report'];
31 public function __construct() {
35 'dao' => 'CRM_Event_DAO_Event',
42 'title' => ts('Event Title'),
46 'title' => ts('Event Type'),
49 'fee_label' => ['title' => ts('Fee Label')],
50 'event_start_date' => [
51 'title' => ts('Event Start Date'),
53 'event_end_date' => ['title' => ts('Event End Date')],
54 'max_participants' => [
55 'title' => ts('Capacity'),
56 'type' => CRM_Utils_Type
::T_INT
,
61 'title' => ts('Event'),
62 'operatorType' => CRM_Report_Form
::OP_ENTITYREF
,
63 'type' => CRM_Utils_Type
::T_INT
,
64 'attributes' => ['select' => ['minimumInputLength' => 0]],
67 'name' => 'event_type_id',
68 'title' => ts('Event Type'),
69 'type' => CRM_Utils_Type
::T_INT
,
70 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
71 'options' => CRM_Core_OptionGroup
::values('event_type'),
73 'event_start_date' => [
74 'title' => ts('Event Start Date'),
75 'operatorType' => CRM_Report_Form
::OP_DATE
,
78 'title' => ts('Event End Date'),
79 'operatorType' => CRM_Report_Form
::OP_DATE
,
83 'event_start_date' => [
84 'title' => ts('Event Start Date'),
86 'default_weight' => '0',
87 'default_order' => 'DESC',
90 'title' => ts('Event End Date'),
92 'max_participants' => [
93 'title' => ts('Capacity'),
96 'title' => ts('Event Title'),
101 $this->_currencyColumn
= 'civicrm_participant_fee_currency';
103 // Add charts support
106 'barChart' => ts('Bar Chart'),
107 'pieChart' => ts('Pie Chart'),
110 parent
::__construct();
113 public function preProcess() {
114 parent
::preProcess();
117 public function select() {
119 foreach ($this->_columns
as $tableName => $table) {
120 if (array_key_exists('fields', $table)) {
121 foreach ($table['fields'] as $fieldName => $field) {
122 if (!empty($field['required']) ||
123 !empty($this->_params
['fields'][$fieldName])
125 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
131 $this->_selectClauses
= $select;
132 $this->_select
= 'SELECT ' . implode(', ', $select);
135 public function from() {
136 $this->_from
= " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
139 public function where() {
141 $this->_participantWhere
= "";
142 foreach ($this->_columns
as $tableName => $table) {
143 if (array_key_exists('filters', $table)) {
144 foreach ($table['filters'] as $fieldName => $field) {
146 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
147 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
148 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
149 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
151 if ($relative ||
$from ||
$to) {
152 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
156 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
158 $clause = $this->whereClause($field,
160 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
161 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
162 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
166 if (!empty($this->_params
['id_value'])) {
167 $idValue = is_array($this->_params
['id_value']) ?
implode(',', $this->_params
['id_value']) : $this->_params
['id_value'];
168 $this->_participantWhere
= " AND civicrm_participant.event_id IN ( $idValue ) ";
171 if (!empty($clause)) {
172 $clauses[] = $clause;
177 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
178 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
181 public function groupBy() {
182 $this->assign('chartSupported', TRUE);
183 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, "{$this->_aliases['civicrm_event']}.id");
187 * get participants information for events.
190 public function participantInfo() {
192 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1');
193 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0');
196 SELECT civicrm_participant.event_id AS event_id,
197 civicrm_participant.status_id AS statusId,
198 COUNT( civicrm_participant.id ) AS participant,
199 SUM( civicrm_participant.fee_amount ) AS amount,
200 civicrm_participant.fee_currency
202 FROM civicrm_participant
204 WHERE civicrm_participant.is_test = 0
205 $this->_participantWhere
207 GROUP BY civicrm_participant.event_id,
208 civicrm_participant.status_id,
209 civicrm_participant.fee_currency";
211 $info = CRM_Core_DAO
::executeQuery($sql);
212 $participant_data = $participant_info = $currency = [];
214 while ($info->fetch()) {
215 $participant_data[$info->event_id
][$info->statusId
]['participant'] = $info->participant
;
216 $participant_data[$info->event_id
][$info->statusId
]['amount'] = $info->amount
;
217 $currency[$info->event_id
] = $info->fee_currency
;
220 $amt = $particiType1 = $particiType2 = 0;
222 foreach ($participant_data as $event_id => $event_data) {
223 foreach ($event_data as $status_id => $data) {
225 if (array_key_exists($status_id, $statusType1)) {
226 //total income of event
227 $amt = $amt +
$data['amount'];
229 //number of Registered/Attended participants
230 $particiType1 = $particiType1 +
$data['participant'];
232 elseif (array_key_exists($status_id, $statusType2)) {
234 //number of No-show/Cancelled/Pending participants
235 $particiType2 = $particiType2 +
$data['participant'];
239 $participant_info[$event_id]['totalAmount'] = $amt;
240 $participant_info[$event_id]['statusType1'] = $particiType1;
241 $participant_info[$event_id]['statusType2'] = $particiType2;
242 $participant_info[$event_id]['currency'] = $currency[$event_id];
243 $amt = $particiType1 = $particiType2 = 0;
246 return $participant_info;
250 * Build header for table.
252 public function buildColumnHeaders() {
253 $this->_columnHeaders
= [];
254 foreach ($this->_columns
as $tableName => $table) {
255 if (array_key_exists('fields', $table)) {
256 foreach ($table['fields'] as $fieldName => $field) {
257 if (!empty($field['required']) ||
258 !empty($this->_params
['fields'][$fieldName])
261 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
262 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
268 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1', 'label');
269 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0', 'label');
271 //make column header for participant status Registered/Attended
272 $type1_header = implode('/', $statusType1);
274 //make column header for participant status No-show/Cancelled/Pending
275 $type2_header = implode('/', $statusType2);
277 $this->_columnHeaders
['statusType1'] = [
278 'title' => $type1_header,
279 'type' => CRM_Utils_Type
::T_INT
,
281 $this->_columnHeaders
['statusType2'] = [
282 'title' => $type2_header,
283 'type' => CRM_Utils_Type
::T_INT
,
285 $this->_columnHeaders
['totalAmount'] = [
286 'title' => ts('Total Income'),
287 'type' => CRM_Utils_Type
::T_STRING
,
291 public function postProcess() {
293 $this->beginPostProcess();
295 $this->buildColumnHeaders();
297 $sql = $this->buildQuery(TRUE);
299 $dao = CRM_Core_DAO
::executeQuery($sql);
301 //set pager before exicution of query in function participantInfo()
304 $rows = $graphRows = [];
306 while ($dao->fetch()) {
308 foreach ($this->_columnHeaders
as $key => $value) {
309 if (($key == 'civicrm_event_start_date') ||
310 ($key == 'civicrm_event_end_date')
312 //get event start date and end date in custom datetime format
313 $row[$key] = CRM_Utils_Date
::customFormat($dao->$key);
316 if (isset($dao->$key)) {
317 $row[$key] = $dao->$key;
324 $participant_info = $this->participantInfo();
325 foreach ($rows as $key => $value) {
326 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
327 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
328 $rows[$key][$k] = $v;
333 // do not call pager here
334 $this->formatDisplay($rows, FALSE);
335 unset($this->_columnHeaders
['civicrm_event_id']);
337 $this->doTemplateAssignment($rows);
339 $this->endPostProcess($rows);
345 public function buildChart(&$rows) {
346 $this->_interval
= 'events';
348 if (!empty($this->_params
['charts'])) {
349 foreach ($rows as $key => $value) {
350 $graphRows['totalAmount'][] = $graphRows['value'][] = $rows[$key]['totalAmount'] ??
NULL;
351 $graphRows[$this->_interval
][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
352 $rows[$key]['civicrm_event_id'] . ") ";
355 if (empty($rows[$key]['totalAmount'])) {
356 $countEvent = count($rows);
359 if ((!empty($rows)) && $countEvent != 1) {
360 $config = CRM_Core_Config
::Singleton();
362 'legend' => ts('Event Summary'),
363 'xname' => ts('Event'),
364 'yname' => ts('Total Amount (%1)', [1 => $config->defaultCurrency
]),
366 if (!empty($graphRows)) {
367 foreach ($graphRows[$this->_interval
] as $key => $val) {
368 $graph[$val] = $graphRows['value'][$key];
370 $chartInfo['values'] = $graph;
371 $chartInfo['xLabelAngle'] = 20;
374 CRM_Utils_Chart
::buildChart($chartInfo, $this->_params
['charts']);
375 $this->assign('chartType', $this->_params
['charts']);
382 * Alter display of rows.
384 * Iterate through the rows retrieved via SQL and make changes for display purposes,
385 * such as rendering contacts as links.
388 * Rows generated by SQL, with an array for each row.
390 public function alterDisplay(&$rows) {
392 if (is_array($rows)) {
393 $eventType = CRM_Core_OptionGroup
::values('event_type');
395 foreach ($rows as $rowNum => $row) {
396 if (array_key_exists('totalAmount', $row) &&
397 array_key_exists('currency', $row)
399 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money
::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
401 if (array_key_exists('civicrm_event_title', $row)) {
402 if ($value = $row['civicrm_event_id']) {
403 //CRM_Event_PseudoConstant::event( $value, false );
404 $url = CRM_Report_Utils_Report
::getNextUrl('event/income',
405 'reset=1&force=1&id_op=in&id_value=' . $value,
406 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
408 $rows[$rowNum]['civicrm_event_title_link'] = $url;
409 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
414 if (array_key_exists('civicrm_event_event_type_id', $row)) {
415 if ($value = $row['civicrm_event_event_type_id']) {
416 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];