3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contact_Form_Search_Custom_ContributionAggregate
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
38 public $_permissionedComponent;
43 function __construct(&$formValues) {
44 $this->_formValues
= $formValues;
46 * Define the columns for search result rows
49 $this->_columns
= array(
50 ts('Contact ID') => 'contact_id',
51 ts('Name') => 'sort_name',
52 ts('Contribution Count') => 'donation_count',
53 ts('Contribution Amount') => 'donation_amount',
56 // define component access permission needed
57 $this->_permissionedComponent
= 'CiviContribute';
61 * @param CRM_Core_Form $form
63 function buildForm(&$form) {
66 * You can define a custom title for the search form
68 $this->setTitle('Find Contributors by Aggregate Totals');
71 * Define the search form fields here
75 ts('Aggregate Total Between $')
77 $form->addRule('min_amount', ts('Please enter a valid amount (numbers and decimal point only).'), 'money');
83 $form->addRule('max_amount', ts('Please enter a valid amount (numbers and decimal point only).'), 'money');
85 $form->addDate('start_date', ts('Contribution Date From'), FALSE, array('formatType' => 'custom'));
86 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
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);
94 * If you are using the sample template, this array tells the template fields to render
95 * for the search form.
97 $form->assign('elements', array('min_amount', 'max_amount', 'start_date', 'end_date', 'financial_type_id'));
101 * Define the smarty template used to layout the search form and results listings.
103 function templateFile() {
104 return 'CRM/Contact/Form/Search/Custom/ContributionAggregate.tpl';
108 * Construct the search query
110 function all($offset = 0, $rowcount = 0, $sort = NULL,
111 $includeContactIDs = FALSE, $justIDs = FALSE
114 // SELECT clause must include contact_id as an alias for civicrm_contact.id
116 $select = "contact_a.id as contact_id";
120 DISTINCT contact_a.id as contact_id,
121 contact_a.sort_name as sort_name,
122 sum(contrib.total_amount) AS donation_amount,
123 count(contrib.id) AS donation_count
126 $from = $this->from();
128 $where = $this->where($includeContactIDs);
130 $having = $this->having();
132 $having = " HAVING $having ";
139 GROUP BY contact_a.id
142 //for only contact ids ignore order.
144 // Define ORDER BY for query in $sort, with default value
146 if (is_string($sort)) {
147 $sort = CRM_Utils_Type
::escape($sort, 'String');
148 $sql .= " ORDER BY $sort ";
151 $sql .= " ORDER BY " . trim($sort->orderBy());
155 $sql .= "ORDER BY donation_amount desc";
159 if ($rowcount > 0 && $offset >= 0) {
160 $offset = CRM_Utils_Type
::escape($offset, 'Int');
161 $rowcount = CRM_Utils_Type
::escape($rowcount, 'Int');
162 $sql .= " LIMIT $offset, $rowcount ";
172 civicrm_contribution AS contrib,
173 civicrm_contact AS contact_a
178 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
182 * @param bool $includeContactIDs
186 function where($includeContactIDs = FALSE) {
189 $clauses[] = "contrib.contact_id = contact_a.id";
190 $clauses[] = "contrib.is_test = 0";
192 $startDate = CRM_Utils_Date
::processDate($this->_formValues
['start_date']);
194 $clauses[] = "contrib.receive_date >= $startDate";
197 $endDate = CRM_Utils_Date
::processDate($this->_formValues
['end_date']);
199 $clauses[] = "contrib.receive_date <= $endDate";
202 if ($includeContactIDs) {
203 $contactIDs = array();
204 foreach ($this->_formValues
as $id => $value) {
206 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
208 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
212 if (!empty($contactIDs)) {
213 $contactIDs = implode(', ', $contactIDs);
214 $clauses[] = "contact_a.id IN ( $contactIDs )";
218 if (!empty($this->_formValues
['financial_type_id'])) {
219 $financial_type_ids = implode(',', array_keys($this->_formValues
['financial_type_id']));
220 $clauses[] = "contrib.financial_type_id IN ($financial_type_ids)";
223 return implode(' AND ', $clauses);
227 * @param bool $includeContactIDs
231 function having($includeContactIDs = FALSE) {
233 $min = CRM_Utils_Array
::value('min_amount', $this->_formValues
);
235 $min = CRM_Utils_Rule
::cleanMoney($min);
236 $clauses[] = "sum(contrib.total_amount) >= $min";
239 $max = CRM_Utils_Array
::value('max_amount', $this->_formValues
);
241 $max = CRM_Utils_Rule
::cleanMoney($max);
242 $clauses[] = "sum(contrib.total_amount) <= $max";
245 return implode(' AND ', $clauses);
249 * Functions below generally don't need to be modified
254 $dao = CRM_Core_DAO
::executeQuery($sql,
255 CRM_Core_DAO
::$_nullArray
262 * @param int $rowcount
267 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
268 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
274 function &columns() {
275 return $this->_columns
;
281 function setTitle($title) {
283 CRM_Utils_System
::setTitle($title);
286 CRM_Utils_System
::setTitle(ts('Search'));