3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_ContribSYBNT
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
38 public $_permissionedComponent;
43 public function __construct(&$formValues) {
44 $this->_formValues
= $formValues;
45 $this->_permissionedComponent
= 'CiviContribute';
47 $this->_columns
= array(
48 ts('Contact ID') => 'contact_id',
49 ts('Name') => 'display_name',
50 ts('Contribution Count') => 'donation_count',
51 ts('Contribution Amount') => 'donation_amount',
54 $this->_amounts
= array(
55 'min_amount_1' => ts('Min Amount One'),
56 'max_amount_1' => ts('Max Amount One'),
57 'min_amount_2' => ts('Min Amount Two'),
58 'max_amount_2' => ts('Max Amount Two'),
59 'exclude_min_amount' => ts('Exclusion Min Amount'),
60 'exclude_max_amount' => ts('Exclusion Max Amount'),
63 $this->_dates
= array(
64 'start_date_1' => ts('Start Date One'),
65 'end_date_1' => ts('End Date One'),
66 'start_date_2' => ts('Start Date Two'),
67 'end_date_2' => ts('End Date Two'),
68 'exclude_start_date' => ts('Exclusion Start Date'),
69 'exclude_end_date' => ts('Exclusion End Date'),
72 $this->_checkboxes
= array('is_first_amount' => ts('First Donation?'));
74 foreach ($this->_amounts
as $name => $title) {
75 $this->{$name} = CRM_Utils_Array
::value($name, $this->_formValues
);
78 foreach ($this->_checkboxes
as $name => $title) {
79 $this->{$name} = CRM_Utils_Array
::value($name, $this->_formValues
, FALSE);
82 foreach ($this->_dates
as $name => $title) {
83 if (!empty($this->_formValues
[$name])) {
84 $this->{$name} = CRM_Utils_Date
::processDate($this->_formValues
[$name]);
90 * @param CRM_Core_Form $form
92 public function buildForm(&$form) {
94 foreach ($this->_amounts
as $name => $title) {
101 foreach ($this->_dates
as $name => $title) {
102 $form->addDate($name, $title, FALSE, array('formatType' => 'custom'));
105 foreach ($this->_checkboxes
as $name => $title) {
106 $form->add('checkbox',
112 $this->setTitle('Contributions made in Year X and not Year Y');
113 // @TODO: Decide on better names for "Exclusion"
114 // @TODO: Add rule to ensure that exclusion dates are not in the inclusion range
120 public function count() {
123 $dao = CRM_Core_DAO
::executeQuery($sql);
129 * @param int $rowcount
131 * @param boolean $returnSQL Not used; included for consistency with parent; SQL is always returned
135 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) {
136 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
141 * @param int $rowcount
143 * @param bool $includeContactIDs
144 * @param bool $justIDs
152 $includeContactIDs = FALSE,
156 $where = $this->where();
157 if (!empty($where)) {
158 $where = " AND $where";
161 $having = $this->having();
163 $having = " HAVING $having ";
166 $from = $this->from();
169 $select = $this->select();
170 $select .= ', contact_a.id, display_name';
173 $select = $this->select();
175 DISTINCT contact_a.id as contact_id,
176 contact_a.display_name as display_name,
184 FROM civicrm_contact AS contact_a
185 LEFT JOIN civicrm_contribution contrib_1 ON contrib_1.contact_id = contact_a.id
187 WHERE contrib_1.contact_id = contact_a.id
188 AND contrib_1.is_test = 0
190 GROUP BY contact_a.id
192 ORDER BY donation_amount desc
196 CRM_Core_DAO
::executeQuery("DROP TEMPORARY TABLE IF EXISTS CustomSearch_SYBNT_temp");
197 $query = "CREATE TEMPORARY TABLE CustomSearch_SYBNT_temp AS ({$sql})";
198 $dao = CRM_Core_DAO
::executeQuery($query);
199 $sql = "SELECT contact_a.id as contact_id FROM CustomSearch_SYBNT_temp as contact_a";
208 public function select() {
209 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
211 sum(contrib_1.total_amount) + sum(contrib_2.total_amount) AS donation_amount,
212 count(contrib_1.id) + count(contrib_1.id) AS donation_count
217 sum(contrib_1.total_amount) AS donation_amount,
218 count(contrib_1.id) AS donation_count
224 * @return null|string
226 public function from() {
228 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
229 $from .= " LEFT JOIN civicrm_contribution contrib_2 ON contrib_2.contact_id = contact_a.id ";
232 if (!empty($this->exclude_start_date
) ||
233 !empty($this->exclude_end_date
) ||
234 !empty($this->is_first_amount
)
236 $from .= " LEFT JOIN XG_CustomSearch_SYBNT xg ON xg.contact_id = contact_a.id ";
243 * @param bool $includeContactIDs
247 public function where($includeContactIDs = FALSE) {
250 if (!empty($this->start_date_1
)) {
251 $clauses[] = "contrib_1.receive_date >= {$this->start_date_1}";
254 if (!empty($this->end_date_1
)) {
255 $clauses[] = "contrib_1.receive_date <= {$this->end_date_1}";
258 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
259 $clauses[] = "contrib_2.is_test = 0";
261 if (!empty($this->start_date_2
)) {
262 $clauses[] = "contrib_2.receive_date >= {$this->start_date_2}";
265 if (!empty($this->end_date_2
)) {
266 $clauses[] = "contrib_2.receive_date <= {$this->end_date_2}";
270 if (!empty($this->exclude_start_date
) ||
271 !empty($this->exclude_end_date
) ||
272 !empty($this->is_first_amount
)
275 // first create temp table to store contact ids
276 $sql = "DROP TEMPORARY TABLE IF EXISTS XG_CustomSearch_SYBNT";
277 CRM_Core_DAO
::executeQuery($sql);
279 $sql = "CREATE TEMPORARY TABLE XG_CustomSearch_SYBNT ( contact_id int primary key) ENGINE=HEAP";
280 CRM_Core_DAO
::executeQuery($sql);
282 $excludeClauses = array();
283 if ($this->exclude_start_date
) {
284 $excludeClauses[] = "c.receive_date >= {$this->exclude_start_date}";
287 if ($this->exclude_end_date
) {
288 $excludeClauses[] = "c.receive_date <= {$this->exclude_end_date}";
291 $excludeClause = NULL;
292 if ($excludeClauses) {
293 $excludeClause = ' AND ' . implode(' AND ', $excludeClauses);
297 if ($this->exclude_min_amount
) {
298 $having[] = "sum(c.total_amount) >= {$this->exclude_min_amount}";
301 if ($this->exclude_max_amount
) {
302 $having[] = "sum(c.total_amount) <= {$this->exclude_max_amount}";
305 $havingClause = NULL;
306 if (!empty($having)) {
307 $havingClause = "HAVING " . implode(' AND ', $having);
310 if ($excludeClause ||
$havingClause) {
313 REPLACE INTO XG_CustomSearch_SYBNT
314 SELECT DISTINCT contact_id AS contact_id
315 FROM civicrm_contribution c
318 GROUP BY c.contact_id
322 $dao = CRM_Core_DAO
::executeQuery($query);
325 // now ensure we dont consider donors that are not first time
326 if ($this->is_first_amount
) {
328 REPLACE INTO XG_CustomSearch_SYBNT
329 SELECT DISTINCT contact_id AS contact_id
330 FROM civicrm_contribution c
332 AND c.receive_date < {$this->start_date_1}
334 $dao = CRM_Core_DAO
::executeQuery($query);
337 $clauses[] = " xg.contact_id IS NULL ";
340 return implode(' AND ', $clauses);
344 * @param bool $includeContactIDs
348 public function having($includeContactIDs = FALSE) {
350 $min = CRM_Utils_Array
::value('min_amount', $this->_formValues
);
352 $clauses[] = "sum(contrib_1.total_amount) >= $min";
355 $max = CRM_Utils_Array
::value('max_amount', $this->_formValues
);
357 $clauses[] = "sum(contrib_1.total_amount) <= $max";
360 return implode(' AND ', $clauses);
366 public function &columns() {
367 return $this->_columns
;
373 public function templateFile() {
374 return 'CRM/Contact/Form/Search/Custom/ContribSYBNT.tpl';
380 public function summary() {
387 public function setTitle($title) {
389 CRM_Utils_System
::setTitle($title);
392 CRM_Utils_System
::setTitle(ts('Search'));