3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contact_Form_Search_Custom_ContribSYBNT
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
38 public $_permissionedComponent;
40 function __construct(&$formValues) {
41 $this->_formValues
= $formValues;
42 $this->_permissionedComponent
= 'CiviContribute';
44 $this->_columns
= array(
45 ts('Contact Id') => 'contact_id',
46 ts('Name') => 'display_name',
47 ts('Donation Count') => 'donation_count',
48 ts('Donation Amount') => 'donation_amount',
51 $this->_amounts
= array('min_amount_1' => ts('Min Amount One'),
52 'max_amount_1' => ts('Max Amount One'),
53 'min_amount_2' => ts('Min Amount Two'),
54 'max_amount_2' => ts('Max Amount Two'),
55 'exclude_min_amount' => ts('Exclusion Min Amount'),
56 'exclude_max_amount' => ts('Exclusion Max Amount'),
59 $this->_dates
= array('start_date_1' => ts('Start Date One'),
60 'end_date_1' => ts('End Date One'),
61 'start_date_2' => ts('Start Date Two'),
62 'end_date_2' => ts('End Date Two'),
63 'exclude_start_date' => ts('Exclusion Start Date'),
64 'exclude_end_date' => ts('Exclusion End Date'),
67 $this->_checkboxes
= array('is_first_amount' => ts('First Donation?'));
69 foreach ($this->_amounts
as $name => $title) {
70 $this->{$name} = CRM_Utils_Array
::value($name, $this->_formValues
);
73 foreach ($this->_checkboxes
as $name => $title) {
74 $this->{$name} = CRM_Utils_Array
::value($name, $this->_formValues
, FALSE);
77 foreach ($this->_dates
as $name => $title) {
78 if (!empty($this->_formValues
[$name])) {
79 $this->{$name} = CRM_Utils_Date
::processDate($this->_formValues
[$name]);
84 function buildForm(&$form) {
86 foreach ($this->_amounts
as $name => $title) {
93 foreach ($this->_dates
as $name => $title) {
94 $form->addDate($name, $title, FALSE, array('formatType' => 'custom'));
97 foreach ($this->_checkboxes
as $name => $title) {
98 $form->add('checkbox',
104 $this->setTitle('Contributions made in Year X and not Year Y');
105 // @TODO: Decide on better names for "Exclusion"
106 // @TODO: Add rule to ensure that exclusion dates are not in the inclusion range
112 $dao = CRM_Core_DAO
::executeQuery($sql);
116 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
117 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
124 $includeContactIDs = FALSE,
128 $where = $this->where();
129 if (!empty($where)) {
130 $where = " AND $where";
133 $having = $this->having();
135 $having = " HAVING $having ";
138 $from = $this->from();
141 $select = 'contact_a.id as contact_id';
144 $select = $this->select();
146 DISTINCT contact.id as contact_id,
147 contact.display_name as display_name,
155 FROM civicrm_contact AS contact
156 LEFT JOIN civicrm_contribution contrib_1 ON contrib_1.contact_id = contact.id
158 WHERE contrib_1.contact_id = contact.id
159 AND contrib_1.is_test = 0
163 ORDER BY donation_amount desc
166 // CRM_Core_Error::debug('sql',$sql); exit();
171 if ($this->start_date_2 ||
$this->end_date_2
) {
173 sum(contrib_1.total_amount) + sum(contrib_2.total_amount) AS donation_amount,
174 count(contrib_1.id) + count(contrib_1.id) AS donation_count
179 sum(contrib_1.total_amount) AS donation_amount,
180 count(contrib_1.id) AS donation_count
187 if ($this->start_date_2 ||
$this->end_date_2
) {
188 $from .= " LEFT JOIN civicrm_contribution contrib_2 ON contrib_2.contact_id = contact.id ";
191 if ($this->exclude_start_date ||
192 $this->exclude_end_date ||
193 $this->is_first_amount
195 $from .= " LEFT JOIN XG_CustomSearch_SYBNT xg ON xg.contact_id = contact.id ";
201 function where($includeContactIDs = FALSE) {
204 if ($this->start_date_1
) {
205 $clauses[] = "contrib_1.receive_date >= {$this->start_date_1}";
208 if ($this->end_date_1
) {
209 $clauses[] = "contrib_1.receive_date <= {$this->end_date_1}";
212 if ($this->start_date_2 ||
215 $clauses[] = "contrib_2.is_test = 0";
217 if ($this->start_date_2
) {
218 $clauses[] = "contrib_2.receive_date >= {$this->start_date_2}";
221 if ($this->end_date_2
) {
222 $clauses[] = "contrib_2.receive_date <= {$this->end_date_2}";
226 if ($this->exclude_start_date ||
227 $this->exclude_end_date ||
228 $this->is_first_amount
231 // first create temp table to store contact ids
232 $sql = "DROP TEMPORARY TABLE IF EXISTS XG_CustomSearch_SYBNT";
233 CRM_Core_DAO
::executeQuery($sql);
235 $sql = "CREATE TEMPORARY TABLE XG_CustomSearch_SYBNT ( contact_id int primary key) ENGINE=HEAP";
236 CRM_Core_DAO
::executeQuery($sql);
238 $excludeClauses = array();
239 if ($this->exclude_start_date
) {
240 $excludeClauses[] = "c.receive_date >= {$this->exclude_start_date}";
243 if ($this->exclude_end_date
) {
244 $excludeClauses[] = "c.receive_date <= {$this->exclude_end_date}";
247 $excludeClause = NULL;
248 if ($excludeClauses) {
249 $excludeClause = ' AND ' . implode(' AND ', $excludeClauses);
253 if ($this->exclude_min_amount
) {
254 $having[] = "sum(c.total_amount) >= {$this->exclude_min_amount}";
257 if ($this->exclude_max_amount
) {
258 $having[] = "sum(c.total_amount) <= {$this->exclude_max_amount}";
261 $havingClause = NULL;
262 if (!empty($having)) {
263 $havingClause = "HAVING " . implode(' AND ', $having);
266 if ($excludeClause ||
$havingClause) {
269 REPLACE INTO XG_CustomSearch_SYBNT
270 SELECT DISTINCT contact_id AS contact_id
271 FROM civicrm_contribution c
274 GROUP BY c.contact_id
278 $dao = CRM_Core_DAO
::executeQuery($query);
281 // now ensure we dont consider donors that are not first time
282 if ($this->is_first_amount
) {
284 REPLACE INTO XG_CustomSearch_SYBNT
285 SELECT DISTINCT contact_id AS contact_id
286 FROM civicrm_contribution c
288 AND c.receive_date < {$this->start_date_1}
290 $dao = CRM_Core_DAO
::executeQuery($query);
293 $clauses[] = " xg.contact_id IS NULL ";
296 return implode(' AND ', $clauses);
299 function having($includeContactIDs = FALSE) {
301 $min = CRM_Utils_Array
::value('min_amount', $this->_formValues
);
303 $clauses[] = "sum(contrib_1.total_amount) >= $min";
306 $max = CRM_Utils_Array
::value('max_amount', $this->_formValues
);
308 $clauses[] = "sum(contrib_1.total_amount) <= $max";
311 return implode(' AND ', $clauses);
314 function &columns() {
315 return $this->_columns
;
318 function templateFile() {
319 return 'CRM/Contact/Form/Search/Custom/ContribSYBNT.tpl';
326 function setTitle($title) {
328 CRM_Utils_System
::setTitle($title);
331 CRM_Utils_System
::setTitle(ts('Search'));