Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event { | |
37 | ||
38 | protected $_summary = NULL; | |
39 | ||
40 | protected $_charts = array( | |
41 | '' => 'Tabular', | |
42 | 'barChart' => 'Bar Chart', | |
43 | 'pieChart' => 'Pie Chart', | |
44 | ); | |
45 | ||
46 | protected $_add2groupSupported = FALSE; | |
47 | ||
48 | protected $_customGroupExtends = array( | |
2f4c2f5d | 49 | 'Event'); |
6a488035 | 50 | public $_drilldownReport = array('event/income' => 'Link to Detail Report'); |
2f4c2f5d | 51 | |
74cf4551 EM |
52 | /** |
53 | * | |
54 | */ | |
55 | /** | |
56 | * | |
57 | */ | |
6a488035 TO |
58 | function __construct() { |
59 | ||
60 | $this->_columns = array( | |
61 | 'civicrm_event' => | |
62 | array( | |
63 | 'dao' => 'CRM_Event_DAO_Event', | |
64 | 'fields' => | |
65 | array( | |
66 | 'id' => array( | |
67 | 'no_display' => TRUE, | |
68 | 'required' => TRUE, | |
69 | ), | |
70 | 'title' => array('title' => ts('Event Title'), | |
71 | 'required' => TRUE, | |
72 | ), | |
73 | 'event_type_id' => array('title' => ts('Event Type'), | |
74 | 'required' => TRUE, | |
75 | ), | |
76 | 'fee_label' => array('title' => ts('Fee Label')), | |
77 | 'event_start_date' => array('title' => ts('Event Start Date'), | |
78 | ), | |
79 | 'event_end_date' => array('title' => ts('Event End Date')), | |
80 | 'max_participants' => array('title' => ts('Capacity'), | |
81 | 'type' => CRM_Utils_Type::T_INT, | |
82 | ), | |
83 | ), | |
84 | 'filters' => | |
85 | array( | |
2107cde9 CW |
86 | 'id' => array( |
87 | 'title' => ts('Event'), | |
88 | 'operatorType' => CRM_Report_Form::OP_ENTITYREF, | |
89 | 'type' => CRM_Utils_Type::T_INT, | |
90 | 'attributes' => array('select' => array('minimumInputLength' => 0)), | |
6a488035 TO |
91 | ), |
92 | 'event_type_id' => array( | |
93 | 'name' => 'event_type_id', | |
94 | 'title' => ts('Event Type'), | |
95 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
96 | 'options' => CRM_Core_OptionGroup::values('event_type'), | |
97 | ), | |
98 | 'event_start_date' => array( | |
99 | 'title' => 'Event Start Date', | |
100 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
101 | ), | |
102 | 'event_end_date' => array( | |
103 | 'title' => 'Event End Date', | |
104 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
105 | ), | |
106 | ), | |
107 | ), | |
108 | ); | |
9bf1940a | 109 | $this->_currencyColumn = 'civicrm_participant_fee_currency'; |
6a488035 TO |
110 | parent::__construct(); |
111 | } | |
112 | ||
113 | function preProcess() { | |
114 | parent::preProcess(); | |
115 | } | |
116 | ||
117 | function select() { | |
118 | $select = array(); | |
119 | foreach ($this->_columns as $tableName => $table) { | |
120 | if (array_key_exists('fields', $table)) { | |
121 | foreach ($table['fields'] as $fieldName => $field) { | |
8cc574cf | 122 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
123 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; |
124 | } | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
1b06955f | 129 | $this->_select = 'SELECT ' . implode(', ', $select); |
6a488035 TO |
130 | } |
131 | ||
132 | function from() { | |
133 | $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} "; | |
134 | } | |
135 | ||
136 | function where() { | |
137 | $clauses = array(); | |
138 | $this->_participantWhere = ""; | |
139 | foreach ($this->_columns as $tableName => $table) { | |
140 | if (array_key_exists('filters', $table)) { | |
141 | foreach ($table['filters'] as $fieldName => $field) { | |
142 | $clause = NULL; | |
143 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
144 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
145 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
146 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
147 | ||
148 | if ($relative || $from || $to) { | |
149 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
150 | } | |
151 | } | |
152 | else { | |
153 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
154 | if ($op) { | |
155 | $clause = $this->whereClause($field, | |
156 | $op, | |
157 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
158 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
159 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
160 | ); | |
161 | } | |
162 | } | |
163 | if (!empty($this->_params['id_value'])) { | |
2107cde9 | 164 | $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) "; |
6a488035 TO |
165 | } |
166 | ||
167 | if (!empty($clause)) { | |
168 | $clauses[] = $clause; | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
99f13d8d | 173 | $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0"; |
1b06955f | 174 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); |
6a488035 TO |
175 | } |
176 | ||
177 | function groupBy() { | |
178 | $this->assign('chartSupported', TRUE); | |
179 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id"; | |
180 | } | |
181 | ||
182 | //get participants information for events | |
74cf4551 EM |
183 | /** |
184 | * @return array | |
185 | */ | |
6a488035 TO |
186 | function participantInfo() { |
187 | ||
1b06955f | 188 | $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); |
189 | $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0'); | |
6a488035 TO |
190 | |
191 | $sql = " | |
2f4c2f5d | 192 | SELECT civicrm_participant.event_id AS event_id, |
193 | civicrm_participant.status_id AS statusId, | |
194 | COUNT( civicrm_participant.id ) AS participant, | |
1b06955f | 195 | SUM( civicrm_participant.fee_amount ) AS amount, |
196 | civicrm_participant.fee_currency | |
6a488035 TO |
197 | |
198 | FROM civicrm_participant | |
199 | ||
2f4c2f5d | 200 | WHERE civicrm_participant.is_test = 0 |
6a488035 TO |
201 | $this->_participantWhere |
202 | ||
2f4c2f5d | 203 | GROUP BY civicrm_participant.event_id, |
6a488035 TO |
204 | civicrm_participant.status_id"; |
205 | ||
206 | $info = CRM_Core_DAO::executeQuery($sql); | |
1b06955f | 207 | $participant_data = $participant_info = $currency = array(); |
6a488035 TO |
208 | |
209 | while ($info->fetch()) { | |
210 | $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant; | |
211 | $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount; | |
1b06955f | 212 | $currency[$info->event_id] = $info->fee_currency; |
6a488035 TO |
213 | } |
214 | ||
215 | $amt = $particiType1 = $particiType2 = 0; | |
216 | ||
217 | foreach ($participant_data as $event_id => $event_data) { | |
218 | foreach ($event_data as $status_id => $data) { | |
219 | ||
220 | if (array_key_exists($status_id, $statusType1)) { | |
221 | //total income of event | |
222 | $amt = $amt + $data['amount']; | |
223 | ||
224 | //number of Registered/Attended participants | |
225 | $particiType1 = $particiType1 + $data['participant']; | |
226 | } | |
227 | elseif (array_key_exists($status_id, $statusType2)) { | |
228 | ||
229 | //number of No-show/Cancelled/Pending participants | |
230 | $particiType2 = $particiType2 + $data['participant']; | |
231 | } | |
232 | } | |
233 | ||
2437f726 | 234 | $participant_info[$event_id]['totalAmount'] = $amt; |
6a488035 TO |
235 | $participant_info[$event_id]['statusType1'] = $particiType1; |
236 | $participant_info[$event_id]['statusType2'] = $particiType2; | |
2437f726 | 237 | $participant_info[$event_id]['currency'] = $currency[$event_id]; |
6a488035 TO |
238 | $amt = $particiType1 = $particiType2 = 0; |
239 | } | |
240 | ||
241 | return $participant_info; | |
242 | } | |
243 | ||
244 | //build header for table | |
245 | function buildColumnHeaders() { | |
246 | ||
247 | $this->_columnHeaders = array(); | |
248 | foreach ($this->_columns as $tableName => $table) { | |
249 | if (array_key_exists('fields', $table)) { | |
250 | foreach ($table['fields'] as $fieldName => $field) { | |
8cc574cf | 251 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
252 | |
253 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
254 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
255 | } | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
1b06955f | 260 | $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); |
261 | $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0'); | |
6a488035 TO |
262 | |
263 | //make column header for participant status Registered/Attended | |
264 | $type1_header = implode('/', $statusType1); | |
265 | ||
266 | //make column header for participant status No-show/Cancelled/Pending | |
267 | $type2_header = implode('/', $statusType2); | |
268 | ||
269 | $this->_columnHeaders['statusType1'] = array( | |
270 | 'title' => $type1_header, | |
271 | 'type' => CRM_Utils_Type::T_INT, | |
272 | ); | |
273 | $this->_columnHeaders['statusType2'] = array( | |
274 | 'title' => $type2_header, | |
275 | 'type' => CRM_Utils_Type::T_INT, | |
276 | ); | |
277 | $this->_columnHeaders['totalAmount'] = array( | |
278 | 'title' => 'Total Income', | |
1b06955f | 279 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 TO |
280 | ); |
281 | } | |
282 | ||
283 | function postProcess() { | |
284 | ||
285 | $this->beginPostProcess(); | |
286 | ||
287 | $this->buildColumnHeaders(); | |
288 | ||
289 | $sql = $this->buildQuery(TRUE); | |
290 | ||
291 | $dao = CRM_Core_DAO::executeQuery($sql); | |
292 | ||
293 | //set pager before exicution of query in function participantInfo() | |
294 | $this->setPager(); | |
295 | ||
296 | $rows = $graphRows = array(); | |
297 | $count = 0; | |
298 | while ($dao->fetch()) { | |
299 | $row = array(); | |
300 | foreach ($this->_columnHeaders as $key => $value) { | |
301 | if (($key == 'civicrm_event_start_date') || ($key == 'civicrm_event_end_date')) { | |
302 | //get event start date and end date in custom datetime format | |
303 | $row[$key] = CRM_Utils_Date::customFormat($dao->$key); | |
304 | } | |
305 | else { | |
306 | if (isset($dao->$key)) { | |
307 | $row[$key] = $dao->$key; | |
308 | } | |
309 | } | |
310 | } | |
311 | $rows[] = $row; | |
312 | } | |
313 | if (!empty($rows)) { | |
314 | $participant_info = $this->participantInfo(); | |
315 | foreach ($rows as $key => $value) { | |
316 | if (array_key_exists($value['civicrm_event_id'], $participant_info)) { | |
317 | foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) { | |
318 | $rows[$key][$k] = $v; | |
319 | } | |
320 | } | |
321 | } | |
322 | } | |
323 | // do not call pager here | |
324 | $this->formatDisplay($rows, FALSE); | |
325 | unset($this->_columnHeaders['civicrm_event_id']); | |
326 | ||
327 | $this->doTemplateAssignment($rows); | |
328 | ||
329 | $this->endPostProcess($rows); | |
330 | } | |
331 | ||
74cf4551 EM |
332 | /** |
333 | * @param $rows | |
334 | */ | |
6a488035 TO |
335 | function buildChart(&$rows) { |
336 | $this->_interval = 'events'; | |
337 | $countEvent = NULL; | |
a7488080 | 338 | if (!empty($this->_params['charts'])) { |
6a488035 TO |
339 | foreach ($rows as $key => $value) { |
340 | $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]); | |
341 | $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" . $rows[$key]['civicrm_event_id'] . ") "; | |
342 | } | |
343 | ||
344 | if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) { | |
345 | $countEvent = count($rows); | |
346 | } | |
347 | ||
348 | if ((!empty($rows)) && $countEvent != 1) { | |
349 | $config = CRM_Core_Config::Singleton(); | |
350 | $chartInfo = array( | |
351 | 'legend' => 'Event Summary', | |
352 | 'xname' => 'Event', | |
353 | 'yname' => "Total Amount ({$config->defaultCurrency})", | |
354 | ); | |
355 | if (!empty($graphRows)) { | |
356 | foreach ($graphRows[$this->_interval] as $key => $val) { | |
357 | $graph[$val] = $graphRows['value'][$key]; | |
358 | } | |
359 | $chartInfo['values'] = $graph; | |
360 | $chartInfo['xLabelAngle'] = 20; | |
361 | ||
362 | // build the chart. | |
363 | CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); | |
364 | $this->assign('chartType', $this->_params['charts']); | |
365 | } | |
366 | } | |
367 | } | |
368 | } | |
369 | ||
74cf4551 EM |
370 | /** |
371 | * @param $rows | |
372 | */ | |
6a488035 TO |
373 | function alterDisplay(&$rows) { |
374 | ||
375 | if (is_array($rows)) { | |
376 | $eventType = CRM_Core_OptionGroup::values('event_type'); | |
6a488035 | 377 | |
2437f726 PJ |
378 | foreach ($rows as $rowNum => $row) { |
379 | if (array_key_exists('totalAmount', $row) && array_key_exists('currency', $row)) { | |
380 | $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']); | |
381 | } | |
6a488035 TO |
382 | if (array_key_exists('civicrm_event_title', $row)) { |
383 | if ($value = $row['civicrm_event_id']) { | |
384 | //CRM_Event_PseudoConstant::event( $value, false ); | |
385 | $url = CRM_Report_Utils_Report::getNextUrl('event/income', | |
386 | 'reset=1&force=1&id_op=in&id_value=' . $value, | |
387 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
388 | ); | |
389 | $rows[$rowNum]['civicrm_event_title_link'] = $url; | |
1b06955f | 390 | $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event'); |
6a488035 TO |
391 | } |
392 | } | |
393 | ||
394 | //handle event type | |
395 | if (array_key_exists('civicrm_event_event_type_id', $row)) { | |
396 | if ($value = $row['civicrm_event_event_type_id']) { | |
397 | $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value]; | |
398 | } | |
399 | } | |
400 | } | |
401 | } | |
402 | } | |
403 | } | |
404 |