Merge pull request #21514 from mattwire/1624_2319_casedashboard
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Report_Form_Event_Summary extends CRM_Report_Form {
18
19 protected $_summary = NULL;
20
21 protected $_add2groupSupported = FALSE;
22
23 protected $_customGroupExtends = [
24 'Event',
25 ];
26 public $_drilldownReport = ['event/income' => 'Link to Detail Report'];
27
28 /**
29 * Class constructor.
30 */
31 public function __construct() {
32
33 $this->_columns = [
34 'civicrm_event' => [
35 'dao' => 'CRM_Event_DAO_Event',
36 'fields' => [
37 'id' => [
38 'no_display' => TRUE,
39 'required' => TRUE,
40 ],
41 'title' => [
42 'title' => ts('Event Title'),
43 'required' => TRUE,
44 ],
45 'event_type_id' => [
46 'title' => ts('Event Type'),
47 'required' => TRUE,
48 ],
49 'fee_label' => ['title' => ts('Fee Label')],
50 'event_start_date' => [
51 'title' => ts('Event Start Date'),
52 ],
53 'event_end_date' => ['title' => ts('Event End Date')],
54 'max_participants' => [
55 'title' => ts('Capacity'),
56 'type' => CRM_Utils_Type::T_INT,
57 ],
58 ],
59 'filters' => [
60 'id' => [
61 'title' => ts('Event'),
62 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
63 'type' => CRM_Utils_Type::T_INT,
64 'attributes' => ['select' => ['minimumInputLength' => 0]],
65 ],
66 'event_type_id' => [
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'),
72 ],
73 'event_start_date' => [
74 'title' => ts('Event Start Date'),
75 'operatorType' => CRM_Report_Form::OP_DATE,
76 ],
77 'event_end_date' => [
78 'title' => ts('Event End Date'),
79 'operatorType' => CRM_Report_Form::OP_DATE,
80 ],
81 ],
82 'order_bys' => [
83 'event_start_date' => [
84 'title' => ts('Event Start Date'),
85 'default' => '1',
86 'default_weight' => '0',
87 'default_order' => 'DESC',
88 ],
89 'event_end_date' => [
90 'title' => ts('Event End Date'),
91 ],
92 'max_participants' => [
93 'title' => ts('Capacity'),
94 ],
95 'title' => [
96 'title' => ts('Event Title'),
97 ],
98 ],
99 ],
100 ];
101 $this->_currencyColumn = 'civicrm_participant_fee_currency';
102
103 // Add charts support
104 $this->_charts = [
105 '' => ts('Tabular'),
106 'barChart' => ts('Bar Chart'),
107 'pieChart' => ts('Pie Chart'),
108 ];
109
110 parent::__construct();
111 }
112
113 public function preProcess() {
114 parent::preProcess();
115 }
116
117 public function select() {
118 $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])
124 ) {
125 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
126 }
127 }
128 }
129 }
130
131 $this->_selectClauses = $select;
132 $this->_select = 'SELECT ' . implode(', ', $select);
133 }
134
135 public function from() {
136 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
137 }
138
139 public function where() {
140 $clauses = [];
141 $this->_participantWhere = "";
142 foreach ($this->_columns as $tableName => $table) {
143 if (array_key_exists('filters', $table)) {
144 foreach ($table['filters'] as $fieldName => $field) {
145 $clause = NULL;
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;
150
151 if ($relative || $from || $to) {
152 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
153 }
154 }
155 else {
156 $op = $this->_params["{$fieldName}_op"] ?? NULL;
157 if ($op) {
158 $clause = $this->whereClause($field,
159 $op,
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)
163 );
164 }
165 }
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 ) ";
169 }
170
171 if (!empty($clause)) {
172 $clauses[] = $clause;
173 }
174 }
175 }
176 }
177 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
178 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
179 }
180
181 public function groupBy() {
182 $this->assign('chartSupported', TRUE);
183 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
184 }
185
186 /**
187 * get participants information for events.
188 * @return array
189 */
190 public function participantInfo() {
191
192 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
193 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
194
195 $sql = "
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
201
202 FROM civicrm_participant
203
204 WHERE civicrm_participant.is_test = 0
205 $this->_participantWhere
206
207 GROUP BY civicrm_participant.event_id,
208 civicrm_participant.status_id,
209 civicrm_participant.fee_currency";
210
211 $info = CRM_Core_DAO::executeQuery($sql);
212 $participant_data = $participant_info = $currency = [];
213
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;
218 }
219
220 $amt = $particiType1 = $particiType2 = 0;
221
222 foreach ($participant_data as $event_id => $event_data) {
223 foreach ($event_data as $status_id => $data) {
224
225 if (array_key_exists($status_id, $statusType1)) {
226 //total income of event
227 $amt = $amt + $data['amount'];
228
229 //number of Registered/Attended participants
230 $particiType1 = $particiType1 + $data['participant'];
231 }
232 elseif (array_key_exists($status_id, $statusType2)) {
233
234 //number of No-show/Cancelled/Pending participants
235 $particiType2 = $particiType2 + $data['participant'];
236 }
237 }
238
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;
244 }
245
246 return $participant_info;
247 }
248
249 /**
250 * Build header for table.
251 */
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])
259 ) {
260
261 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
262 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'] ?? NULL;
263 }
264 }
265 }
266 }
267
268 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
269 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
270
271 //make column header for participant status Registered/Attended
272 $type1_header = implode('/', $statusType1);
273
274 //make column header for participant status No-show/Cancelled/Pending
275 $type2_header = implode('/', $statusType2);
276
277 $this->_columnHeaders['statusType1'] = [
278 'title' => $type1_header,
279 'type' => CRM_Utils_Type::T_INT,
280 ];
281 $this->_columnHeaders['statusType2'] = [
282 'title' => $type2_header,
283 'type' => CRM_Utils_Type::T_INT,
284 ];
285 $this->_columnHeaders['totalAmount'] = [
286 'title' => ts('Total Income'),
287 'type' => CRM_Utils_Type::T_STRING,
288 ];
289 }
290
291 public function postProcess() {
292
293 $this->beginPostProcess();
294
295 $this->buildColumnHeaders();
296
297 $sql = $this->buildQuery(TRUE);
298
299 $dao = CRM_Core_DAO::executeQuery($sql);
300
301 //set pager before exicution of query in function participantInfo()
302 $this->setPager();
303
304 $rows = $graphRows = [];
305 $count = 0;
306 while ($dao->fetch()) {
307 $row = [];
308 foreach ($this->_columnHeaders as $key => $value) {
309 if (($key == 'civicrm_event_start_date') ||
310 ($key == 'civicrm_event_end_date')
311 ) {
312 //get event start date and end date in custom datetime format
313 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
314 }
315 else {
316 if (isset($dao->$key)) {
317 $row[$key] = $dao->$key;
318 }
319 }
320 }
321 $rows[] = $row;
322 }
323 if (!empty($rows)) {
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;
329 }
330 }
331 }
332 }
333 // do not call pager here
334 $this->formatDisplay($rows, FALSE);
335 unset($this->_columnHeaders['civicrm_event_id']);
336
337 $this->doTemplateAssignment($rows);
338
339 $this->endPostProcess($rows);
340 }
341
342 /**
343 * @param $rows
344 */
345 public function buildChart(&$rows) {
346 $this->_interval = 'events';
347 $countEvent = NULL;
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'] . ") ";
353 }
354
355 if (empty($rows[$key]['totalAmount'])) {
356 $countEvent = count($rows);
357 }
358
359 if ((!empty($rows)) && $countEvent != 1) {
360 $config = CRM_Core_Config::Singleton();
361 $chartInfo = [
362 'legend' => ts('Event Summary'),
363 'xname' => ts('Event'),
364 'yname' => ts('Total Amount (%1)', [1 => $config->defaultCurrency]),
365 ];
366 if (!empty($graphRows)) {
367 foreach ($graphRows[$this->_interval] as $key => $val) {
368 $graph[$val] = $graphRows['value'][$key];
369 }
370 $chartInfo['values'] = $graph;
371 $chartInfo['xLabelAngle'] = 20;
372
373 // build the chart.
374 CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']);
375 $this->assign('chartType', $this->_params['charts']);
376 }
377 }
378 }
379 }
380
381 /**
382 * Alter display of rows.
383 *
384 * Iterate through the rows retrieved via SQL and make changes for display purposes,
385 * such as rendering contacts as links.
386 *
387 * @param array $rows
388 * Rows generated by SQL, with an array for each row.
389 */
390 public function alterDisplay(&$rows) {
391
392 if (is_array($rows)) {
393 $eventType = CRM_Core_OptionGroup::values('event_type');
394
395 foreach ($rows as $rowNum => $row) {
396 if (array_key_exists('totalAmount', $row) &&
397 array_key_exists('currency', $row)
398 ) {
399 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
400 }
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
407 );
408 $rows[$rowNum]['civicrm_event_title_link'] = $url;
409 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
410 }
411 }
412
413 //handle event type
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];
417 }
418 }
419 }
420 }
421 }
422
423 }