3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
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. |
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 |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Report_Form_Event_Summary
extends CRM_Report_Form_Event
{
35 protected $_summary = NULL;
37 protected $_charts = array(
39 'barChart' => 'Bar Chart',
40 'pieChart' => 'Pie Chart',
43 protected $_add2groupSupported = FALSE;
45 protected $_customGroupExtends = array(
48 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
53 public function __construct() {
55 $this->_columns
= array(
56 'civicrm_event' => array(
57 'dao' => 'CRM_Event_DAO_Event',
64 'title' => ts('Event Title'),
67 'event_type_id' => array(
68 'title' => ts('Event Type'),
71 'fee_label' => array('title' => ts('Fee Label')),
72 'event_start_date' => array(
73 'title' => ts('Event Start Date'),
75 'event_end_date' => array('title' => ts('Event End Date')),
76 'max_participants' => array(
77 'title' => ts('Capacity'),
78 'type' => CRM_Utils_Type
::T_INT
,
83 'title' => ts('Event'),
84 'operatorType' => CRM_Report_Form
::OP_ENTITYREF
,
85 'type' => CRM_Utils_Type
::T_INT
,
86 'attributes' => array('select' => array('minimumInputLength' => 0)),
88 'event_type_id' => array(
89 'name' => 'event_type_id',
90 'title' => ts('Event Type'),
91 'type' => CRM_Utils_Type
::T_INT
,
92 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
93 'options' => CRM_Core_OptionGroup
::values('event_type'),
95 'event_start_date' => array(
96 'title' => ts('Event Start Date'),
97 'operatorType' => CRM_Report_Form
::OP_DATE
,
99 'event_end_date' => array(
100 'title' => ts('Event End Date'),
101 'operatorType' => CRM_Report_Form
::OP_DATE
,
106 $this->_currencyColumn
= 'civicrm_participant_fee_currency';
107 parent
::__construct();
110 public function preProcess() {
111 parent
::preProcess();
114 public function select() {
116 foreach ($this->_columns
as $tableName => $table) {
117 if (array_key_exists('fields', $table)) {
118 foreach ($table['fields'] as $fieldName => $field) {
119 if (!empty($field['required']) ||
120 !empty($this->_params
['fields'][$fieldName])
122 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
128 $this->_selectClauses
= $select;
129 $this->_select
= 'SELECT ' . implode(', ', $select);
132 public function from() {
133 $this->_from
= " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
136 public function where() {
138 $this->_participantWhere
= "";
139 foreach ($this->_columns
as $tableName => $table) {
140 if (array_key_exists('filters', $table)) {
141 foreach ($table['filters'] as $fieldName => $field) {
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
);
148 if ($relative ||
$from ||
$to) {
149 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
153 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
155 $clause = $this->whereClause($field,
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
)
163 if (!empty($this->_params
['id_value'])) {
164 $idValue = is_array($this->_params
['id_value']) ?
implode(',', $this->_params
['id_value']) : $this->_params
['id_value'];
165 $this->_participantWhere
= " AND civicrm_participant.event_id IN ( $idValue ) ";
168 if (!empty($clause)) {
169 $clauses[] = $clause;
174 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
175 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
178 public function groupBy() {
179 $this->assign('chartSupported', TRUE);
180 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, "{$this->_aliases['civicrm_event']}.id");
184 * get participants information for events.
187 public function participantInfo() {
189 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1');
190 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0');
193 SELECT civicrm_participant.event_id AS event_id,
194 civicrm_participant.status_id AS statusId,
195 COUNT( civicrm_participant.id ) AS participant,
196 SUM( civicrm_participant.fee_amount ) AS amount,
197 civicrm_participant.fee_currency
199 FROM civicrm_participant
201 WHERE civicrm_participant.is_test = 0
202 $this->_participantWhere
204 GROUP BY civicrm_participant.event_id,
205 civicrm_participant.status_id";
207 $info = CRM_Core_DAO
::executeQuery($sql);
208 $participant_data = $participant_info = $currency = array();
210 while ($info->fetch()) {
211 $participant_data[$info->event_id
][$info->statusId
]['participant'] = $info->participant
;
212 $participant_data[$info->event_id
][$info->statusId
]['amount'] = $info->amount
;
213 $currency[$info->event_id
] = $info->fee_currency
;
216 $amt = $particiType1 = $particiType2 = 0;
218 foreach ($participant_data as $event_id => $event_data) {
219 foreach ($event_data as $status_id => $data) {
221 if (array_key_exists($status_id, $statusType1)) {
222 //total income of event
223 $amt = $amt +
$data['amount'];
225 //number of Registered/Attended participants
226 $particiType1 = $particiType1 +
$data['participant'];
228 elseif (array_key_exists($status_id, $statusType2)) {
230 //number of No-show/Cancelled/Pending participants
231 $particiType2 = $particiType2 +
$data['participant'];
235 $participant_info[$event_id]['totalAmount'] = $amt;
236 $participant_info[$event_id]['statusType1'] = $particiType1;
237 $participant_info[$event_id]['statusType2'] = $particiType2;
238 $participant_info[$event_id]['currency'] = $currency[$event_id];
239 $amt = $particiType1 = $particiType2 = 0;
242 return $participant_info;
246 * Build header for table.
248 public function buildColumnHeaders() {
249 $this->_columnHeaders
= array();
250 foreach ($this->_columns
as $tableName => $table) {
251 if (array_key_exists('fields', $table)) {
252 foreach ($table['fields'] as $fieldName => $field) {
253 if (!empty($field['required']) ||
254 !empty($this->_params
['fields'][$fieldName])
257 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
258 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
264 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1', 'label');
265 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0', 'label');
267 //make column header for participant status Registered/Attended
268 $type1_header = implode('/', $statusType1);
270 //make column header for participant status No-show/Cancelled/Pending
271 $type2_header = implode('/', $statusType2);
273 $this->_columnHeaders
['statusType1'] = array(
274 'title' => $type1_header,
275 'type' => CRM_Utils_Type
::T_INT
,
277 $this->_columnHeaders
['statusType2'] = array(
278 'title' => $type2_header,
279 'type' => CRM_Utils_Type
::T_INT
,
281 $this->_columnHeaders
['totalAmount'] = array(
282 'title' => ts('Total Income'),
283 'type' => CRM_Utils_Type
::T_STRING
,
287 public function postProcess() {
289 $this->beginPostProcess();
291 $this->buildColumnHeaders();
293 $sql = $this->buildQuery(TRUE);
295 $dao = CRM_Core_DAO
::executeQuery($sql);
297 //set pager before exicution of query in function participantInfo()
300 $rows = $graphRows = array();
302 while ($dao->fetch()) {
304 foreach ($this->_columnHeaders
as $key => $value) {
305 if (($key == 'civicrm_event_start_date') ||
306 ($key == 'civicrm_event_end_date')
308 //get event start date and end date in custom datetime format
309 $row[$key] = CRM_Utils_Date
::customFormat($dao->$key);
312 if (isset($dao->$key)) {
313 $row[$key] = $dao->$key;
320 $participant_info = $this->participantInfo();
321 foreach ($rows as $key => $value) {
322 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
323 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
324 $rows[$key][$k] = $v;
329 // do not call pager here
330 $this->formatDisplay($rows, FALSE);
331 unset($this->_columnHeaders
['civicrm_event_id']);
333 $this->doTemplateAssignment($rows);
335 $this->endPostProcess($rows);
341 public function buildChart(&$rows) {
342 $this->_interval
= 'events';
344 if (!empty($this->_params
['charts'])) {
345 foreach ($rows as $key => $value) {
346 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array
::value('totalAmount', $rows[$key]);
347 $graphRows[$this->_interval
][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
348 $rows[$key]['civicrm_event_id'] . ") ";
351 if (CRM_Utils_Array
::value('totalAmount', $rows[$key]) == 0) {
352 $countEvent = count($rows);
355 if ((!empty($rows)) && $countEvent != 1) {
356 $config = CRM_Core_Config
::Singleton();
358 'legend' => 'Event Summary',
360 'yname' => "Total Amount ({$config->defaultCurrency})",
362 if (!empty($graphRows)) {
363 foreach ($graphRows[$this->_interval
] as $key => $val) {
364 $graph[$val] = $graphRows['value'][$key];
366 $chartInfo['values'] = $graph;
367 $chartInfo['xLabelAngle'] = 20;
370 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
371 $this->assign('chartType', $this->_params
['charts']);
378 * Alter display of rows.
380 * Iterate through the rows retrieved via SQL and make changes for display purposes,
381 * such as rendering contacts as links.
384 * Rows generated by SQL, with an array for each row.
386 public function alterDisplay(&$rows) {
388 if (is_array($rows)) {
389 $eventType = CRM_Core_OptionGroup
::values('event_type');
391 foreach ($rows as $rowNum => $row) {
392 if (array_key_exists('totalAmount', $row) &&
393 array_key_exists('currency', $row)
395 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money
::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
397 if (array_key_exists('civicrm_event_title', $row)) {
398 if ($value = $row['civicrm_event_id']) {
399 //CRM_Event_PseudoConstant::event( $value, false );
400 $url = CRM_Report_Utils_Report
::getNextUrl('event/income',
401 'reset=1&force=1&id_op=in&id_value=' . $value,
402 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
404 $rows[$rowNum]['civicrm_event_title_link'] = $url;
405 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
410 if (array_key_exists('civicrm_event_event_type_id', $row)) {
411 if ($value = $row['civicrm_event_event_type_id']) {
412 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];