3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
33 class CRM_Contact_Form_Search_Custom_EventAggregate
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_formValues;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
38 public $_permissionedComponent;
43 * @param array $formValues
45 public function __construct(&$formValues) {
46 $this->_formValues
= $formValues;
47 $this->_permissionedComponent
= array('CiviContribute', 'CiviEvent');
50 * Define the columns for search result rows
52 $this->_columns
= array(
53 ts('Event') => 'event_name',
54 ts('Type') => 'event_type',
55 ts('Number of<br />Participant') => 'participant_count',
56 ts('Total Payment') => 'payment_amount',
58 ts('Net Payment') => 'net_payment',
59 ts('Participant') => 'participant',
64 * @param CRM_Core_Form $form
66 public function buildForm(&$form) {
69 * You can define a custom title for the search form
71 $this->setTitle('Find Totals for Events');
74 * Define the search form fields here
77 $form->addElement('checkbox', 'paid_online', ts('Only show Credit Card Payments'));
79 $form->addElement('checkbox', 'show_payees', ts('Show payees'));
81 $event_type = CRM_Core_OptionGroup
::values('event_type', FALSE);
82 foreach ($event_type as $eventId => $eventName) {
83 $form->addElement('checkbox', "event_type_id[$eventId]", 'Event Type', $eventName);
85 $events = CRM_Event_BAO_Event
::getEvents(1);
86 $form->add('select', 'event_id', ts('Event Name'), array('' => ts('- select -')) +
$events);
88 $form->addDate('start_date', ts('Payments Date From'), FALSE, array('formatType' => 'custom'));
89 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
92 * If you are using the sample template, this array tells the template fields to render
93 * for the search form.
95 $form->assign('elements', array(
106 * Define the smarty template used to layout the search form and results listings.
108 public function templateFile() {
109 return 'CRM/Contact/Form/Search/Custom/EventDetails.tpl';
113 * Construct the search query.
116 * @param int $rowcount
118 * @param bool $includeContactIDs
119 * @param bool $justIDs
124 $offset = 0, $rowcount = 0, $sort = NULL,
125 $includeContactIDs = FALSE, $justIDs = FALSE
127 // SELECT clause must include contact_id as an alias for civicrm_contact.id if you are going to use "tasks" like export etc.
128 $select = "civicrm_participant.event_id as event_id,
129 COUNT(civicrm_participant.id) as participant_count,
130 GROUP_CONCAT(DISTINCT(civicrm_event.title)) as event_name,
131 civicrm_event.event_type_id as event_type_id,
132 civicrm_option_value.label as event_type,
133 IF(civicrm_contribution.payment_instrument_id <>0 , 'Yes', 'No') as payment_instrument_id,
134 SUM(civicrm_contribution.total_amount) as payment_amount,
135 format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee,
136 format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment";
138 $from = $this->from();
140 $onLine = CRM_Utils_Array
::value('paid_online',
145 inner join civicrm_entity_financial_trxn
146 on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')";
149 $showPayees = CRM_Utils_Array
::value('show_payees',
153 $select .= ", GROUP_CONCAT(DISTINCT(civicrm_contact.display_name)) as participant ";
154 $from .= " inner join civicrm_contact
155 on civicrm_contact.id = civicrm_participant.contact_id";
158 unset($this->_columns
[ts('Participant')]);
161 $where = $this->where();
162 $groupFromSelect = "civicrm_option_value.label, civicrm_contribution.payment_instrument_id";
164 $groupBy = "event_id, event_type_id, {$groupFromSelect}";
165 if (!empty($this->_formValues
['event_type_id'])) {
166 $groupBy = "event_type_id, event_id, {$groupFromSelect}";
175 // Define ORDER BY for query in $sort, with default value
177 if (is_string($sort)) {
178 $sql .= " ORDER BY $sort ";
181 $sql .= " ORDER BY " . trim($sort->orderBy());
185 $sql .= "ORDER BY event_name desc";
188 if ($rowcount > 0 && $offset >= 0) {
189 $offset = CRM_Utils_Type
::escape($offset, 'Int');
190 $rowcount = CRM_Utils_Type
::escape($rowcount, 'Int');
191 $sql .= " LIMIT $offset, $rowcount ";
199 public function from() {
200 $this->buildACLClause('contact_a');
202 civicrm_participant_payment
203 left join civicrm_participant
204 on civicrm_participant_payment.participant_id=civicrm_participant.id
206 left join civicrm_contact contact_a
207 on civicrm_participant.contact_id = contact_a.id
209 left join civicrm_event on
210 civicrm_participant.event_id = civicrm_event.id
212 left join civicrm_contribution
213 on civicrm_contribution.id = civicrm_participant_payment.contribution_id
215 left join civicrm_option_value on
216 ( civicrm_option_value.value = civicrm_event.event_type_id AND civicrm_option_value.option_group_id = 14) {$this->_aclFrom}";
222 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values.
224 * @param bool $includeContactIDs
228 public function where($includeContactIDs = FALSE) {
231 $clauses[] = "civicrm_participant.status_id in ( 1 )";
232 $clauses[] = "civicrm_contribution.is_test = 0";
233 $onLine = CRM_Utils_Array
::value('paid_online',
237 $clauses[] = "civicrm_contribution.payment_instrument_id <> 0";
240 $startDate = CRM_Utils_Date
::processDate($this->_formValues
['start_date']);
242 $clauses[] = "civicrm_contribution.receive_date >= $startDate";
245 $endDate = CRM_Utils_Date
::processDate($this->_formValues
['end_date']);
247 $clauses[] = "civicrm_contribution.receive_date <= {$endDate}235959";
250 if (!empty($this->_formValues
['event_id'])) {
251 $clauses[] = "civicrm_event.id = {$this->_formValues['event_id']}";
254 if ($includeContactIDs) {
255 $contactIDs = array();
256 foreach ($this->_formValues
as $id => $value) {
258 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
260 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
264 if (!empty($contactIDs)) {
265 $contactIDs = implode(', ', $contactIDs);
266 $clauses[] = "contact.id IN ( $contactIDs )";
270 if (!empty($this->_formValues
['event_type_id'])) {
271 $event_type_ids = implode(',', array_keys($this->_formValues
['event_type_id']));
272 $clauses[] = "civicrm_event.event_type_id IN ( $event_type_ids )";
274 if ($this->_aclWhere
) {
275 $clauses[] = "{$this->_aclWhere} ";
277 return implode(' AND ', $clauses);
281 /* This function does a query to get totals for some of the search result columns and returns a totals array. */
285 public function summary() {
287 SUM(civicrm_contribution.total_amount) as payment_amount,COUNT(civicrm_participant.id) as participant_count,
288 format(sum(if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0)),2) as fee,
289 format(sum(civicrm_contribution.total_amount - (if(civicrm_contribution.payment_instrument_id <>0,(civicrm_contribution.total_amount *.034) +.45,0))),2) as net_payment";
291 $from = $this->from();
293 $onLine = CRM_Utils_Array
::value('paid_online',
298 inner join civicrm_entity_financial_trxn
299 on (civicrm_entity_financial_trxn.entity_id = civicrm_participant_payment.contribution_id and civicrm_entity_financial_trxn.entity_table='civicrm_contribution')";
302 $where = $this->where();
310 $dao = CRM_Core_DAO
::executeQuery($sql,
311 CRM_Core_DAO
::$_nullArray
314 while ($dao->fetch()) {
315 $totals['payment_amount'] = $dao->payment_amount
;
316 $totals['fee'] = $dao->fee
;
317 $totals['net_payment'] = $dao->net_payment
;
318 $totals['participant_count'] = $dao->participant_count
;
324 * Functions below generally don't need to be modified
330 public function count() {
333 $dao = CRM_Core_DAO
::executeQuery($sql,
334 CRM_Core_DAO
::$_nullArray
341 * @param int $rowcount
343 * @param bool $returnSQL Not used; included for consistency with parent; SQL is always returned
347 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) {
348 return $this->all($offset, $rowcount, $sort);
354 public function &columns() {
355 return $this->_columns
;
361 public function setTitle($title) {
363 CRM_Utils_System
::setTitle($title);
366 CRM_Utils_System
::setTitle(ts('Search'));
371 * @param string $tableAlias
373 public function buildACLClause($tableAlias = 'contact') {
374 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);