INFRA-132 - CRM/Contact - Misc
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / ContributionAggregate.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Search_Custom_ContributionAggregate implements CRM_Contact_Form_Search_Interface {
36
4e54c348
PJ
37 protected $_formValues;
38 public $_permissionedComponent;
6a488035 39
86538308
EM
40 /**
41 * @param $formValues
42 */
00be9182 43 public function __construct(&$formValues) {
4e54c348 44 $this->_formValues = $formValues;
6a488035
TO
45 /**
46 * Define the columns for search result rows
47 */
4e54c348 48
6a488035 49 $this->_columns = array(
7b99ead3 50 ts('Contact ID') => 'contact_id',
6a488035 51 ts('Name') => 'sort_name',
be205937
CW
52 ts('Contribution Count') => 'donation_count',
53 ts('Contribution Amount') => 'donation_amount',
6a488035 54 );
4e54c348
PJ
55
56 // define component access permission needed
57 $this->_permissionedComponent = 'CiviContribute';
6a488035
TO
58 }
59
86538308 60 /**
c490a46a 61 * @param CRM_Core_Form $form
86538308 62 */
00be9182 63 public function buildForm(&$form) {
6a488035
TO
64
65 /**
66 * You can define a custom title for the search form
67 */
68 $this->setTitle('Find Contributors by Aggregate Totals');
69
70 /**
71 * Define the search form fields here
72 */
73 $form->add('text',
74 'min_amount',
75 ts('Aggregate Total Between $')
76 );
77 $form->addRule('min_amount', ts('Please enter a valid amount (numbers and decimal point only).'), 'money');
78
79 $form->add('text',
80 'max_amount',
81 ts('...and $')
82 );
83 $form->addRule('max_amount', ts('Please enter a valid amount (numbers and decimal point only).'), 'money');
84
85 $form->addDate('start_date', ts('Contribution Date From'), FALSE, array('formatType' => 'custom'));
86 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
87
88 $financial_types = CRM_Contribute_PseudoConstant::financialType();
89 foreach($financial_types as $financial_type_id => $financial_type) {
90 $form->addElement('checkbox', "financial_type_id[{$financial_type_id}]", 'Financial Type', $financial_type);
91 }
92
93 /**
94 * If you are using the sample template, this array tells the template fields to render
95 * for the search form.
96 */
97 $form->assign('elements', array('min_amount', 'max_amount', 'start_date', 'end_date', 'financial_type_id'));
98 }
99
100 /**
101 * Define the smarty template used to layout the search form and results listings.
102 */
00be9182 103 public function templateFile() {
6a488035
TO
104 return 'CRM/Contact/Form/Search/Custom/ContributionAggregate.tpl';
105 }
106
107 /**
108 * Construct the search query
109 */
51ccfbbe
TO
110 function all(
111 $offset = 0, $rowcount = 0, $sort = NULL,
6a488035
TO
112 $includeContactIDs = FALSE, $justIDs = FALSE
113 ) {
114
115 // SELECT clause must include contact_id as an alias for civicrm_contact.id
116 if ($justIDs) {
117 $select = "contact_a.id as contact_id";
118 }
119 else {
120 $select = "
121DISTINCT contact_a.id as contact_id,
122contact_a.sort_name as sort_name,
123sum(contrib.total_amount) AS donation_amount,
124count(contrib.id) AS donation_count
125";
126 }
127 $from = $this->from();
128
129 $where = $this->where($includeContactIDs);
130
131 $having = $this->having();
132 if ($having) {
133 $having = " HAVING $having ";
134 }
135
136 $sql = "
137SELECT $select
138FROM $from
139WHERE $where
140GROUP BY contact_a.id
141$having
142";
143 //for only contact ids ignore order.
144 if (!$justIDs) {
145 // Define ORDER BY for query in $sort, with default value
146 if (!empty($sort)) {
147 if (is_string($sort)) {
bf00d1b6 148 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
149 $sql .= " ORDER BY $sort ";
150 }
151 else {
152 $sql .= " ORDER BY " . trim($sort->orderBy());
153 }
154 }
155 else {
156 $sql .= "ORDER BY donation_amount desc";
157 }
158 }
159
160 if ($rowcount > 0 && $offset >= 0) {
bf00d1b6 161 $offset = CRM_Utils_Type::escape($offset, 'Int');
dd3a4117 162 $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
6a488035
TO
163 $sql .= " LIMIT $offset, $rowcount ";
164 }
165 return $sql;
166 }
167
86538308
EM
168 /**
169 * @return string
170 */
00be9182 171 public function from() {
6a488035
TO
172 return "
173civicrm_contribution AS contrib,
174civicrm_contact AS contact_a
175";
176 }
177
178 /*
179 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
180 *
181 */
86538308
EM
182 /**
183 * @param bool $includeContactIDs
184 *
185 * @return string
186 */
00be9182 187 public function where($includeContactIDs = FALSE) {
6a488035
TO
188 $clauses = array();
189
190 $clauses[] = "contrib.contact_id = contact_a.id";
191 $clauses[] = "contrib.is_test = 0";
192
193 $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']);
194 if ($startDate) {
195 $clauses[] = "contrib.receive_date >= $startDate";
196 }
197
198 $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']);
199 if ($endDate) {
200 $clauses[] = "contrib.receive_date <= $endDate";
201 }
202
203 if ($includeContactIDs) {
204 $contactIDs = array();
205 foreach ($this->_formValues as $id => $value) {
206 if ($value &&
207 substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
208 ) {
209 $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
210 }
211 }
212
213 if (!empty($contactIDs)) {
214 $contactIDs = implode(', ', $contactIDs);
215 $clauses[] = "contact_a.id IN ( $contactIDs )";
216 }
217 }
218
219 if (!empty($this->_formValues['financial_type_id'])) {
220 $financial_type_ids = implode(',', array_keys($this->_formValues['financial_type_id']));
221 $clauses[] = "contrib.financial_type_id IN ($financial_type_ids)";
222 }
223
224 return implode(' AND ', $clauses);
225 }
226
86538308
EM
227 /**
228 * @param bool $includeContactIDs
229 *
230 * @return string
231 */
00be9182 232 public function having($includeContactIDs = FALSE) {
6a488035
TO
233 $clauses = array();
234 $min = CRM_Utils_Array::value('min_amount', $this->_formValues);
235 if ($min) {
236 $min = CRM_Utils_Rule::cleanMoney($min);
237 $clauses[] = "sum(contrib.total_amount) >= $min";
238 }
239
240 $max = CRM_Utils_Array::value('max_amount', $this->_formValues);
241 if ($max) {
242 $max = CRM_Utils_Rule::cleanMoney($max);
243 $clauses[] = "sum(contrib.total_amount) <= $max";
244 }
245
246 return implode(' AND ', $clauses);
247 }
248
249 /*
c490a46a
CW
250 * Functions below generally don't need to be modified
251 */
00be9182 252 public function count() {
6a488035
TO
253 $sql = $this->all();
254
255 $dao = CRM_Core_DAO::executeQuery($sql,
256 CRM_Core_DAO::$_nullArray
257 );
258 return $dao->N;
259 }
260
86538308
EM
261 /**
262 * @param int $offset
263 * @param int $rowcount
264 * @param null $sort
265 *
266 * @return string
267 */
00be9182 268 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
6a488035
TO
269 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
270 }
271
86538308
EM
272 /**
273 * @return array
274 */
00be9182 275 public function &columns() {
6a488035
TO
276 return $this->_columns;
277 }
278
86538308
EM
279 /**
280 * @param $title
281 */
00be9182 282 public function setTitle($title) {
6a488035
TO
283 if ($title) {
284 CRM_Utils_System::setTitle($title);
285 }
286 else {
287 CRM_Utils_System::setTitle(ts('Search'));
288 }
289 }
290
86538308
EM
291 /**
292 * @return null
293 */
00be9182 294 public function summary() {
6a488035
TO
295 return NULL;
296 }
297}