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
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
134 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
135 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
140 * @param int $rowcount
142 * @param bool $includeContactIDs
143 * @param bool $justIDs
151 $includeContactIDs = FALSE,
155 $where = $this->where();
156 if (!empty($where)) {
157 $where = " AND $where";
160 $having = $this->having();
162 $having = " HAVING $having ";
165 $from = $this->from();
168 $select = $this->select();
169 $select .= ', contact_a.id, display_name';
172 $select = $this->select();
174 DISTINCT contact_a.id as contact_id,
175 contact_a.display_name as display_name,
183 FROM civicrm_contact AS contact_a
184 LEFT JOIN civicrm_contribution contrib_1 ON contrib_1.contact_id = contact_a.id
186 WHERE contrib_1.contact_id = contact_a.id
187 AND contrib_1.is_test = 0
189 GROUP BY contact_a.id
191 ORDER BY donation_amount desc
195 CRM_Core_DAO
::executeQuery("DROP TEMPORARY TABLE IF EXISTS CustomSearch_SYBNT_temp");
196 $query = "CREATE TEMPORARY TABLE CustomSearch_SYBNT_temp AS ({$sql})";
197 $dao = CRM_Core_DAO
::executeQuery($query);
198 $sql = "SELECT contact_a.id as contact_id FROM CustomSearch_SYBNT_temp as contact_a";
207 public function select() {
208 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
210 sum(contrib_1.total_amount) + sum(contrib_2.total_amount) AS donation_amount,
211 count(contrib_1.id) + count(contrib_1.id) AS donation_count
216 sum(contrib_1.total_amount) AS donation_amount,
217 count(contrib_1.id) AS donation_count
223 * @return null|string
225 public function from() {
227 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
228 $from .= " LEFT JOIN civicrm_contribution contrib_2 ON contrib_2.contact_id = contact_a.id ";
231 if (!empty($this->exclude_start_date
) ||
232 !empty($this->exclude_end_date
) ||
233 !empty($this->is_first_amount
)
235 $from .= " LEFT JOIN XG_CustomSearch_SYBNT xg ON xg.contact_id = contact_a.id ";
242 * @param bool $includeContactIDs
246 public function where($includeContactIDs = FALSE) {
249 if (!empty($this->start_date_1
)) {
250 $clauses[] = "contrib_1.receive_date >= {$this->start_date_1}";
253 if (!empty($this->end_date_1
)) {
254 $clauses[] = "contrib_1.receive_date <= {$this->end_date_1}";
257 if (!empty($this->start_date_2
) ||
!empty($this->end_date_2
)) {
258 $clauses[] = "contrib_2.is_test = 0";
260 if (!empty($this->start_date_2
)) {
261 $clauses[] = "contrib_2.receive_date >= {$this->start_date_2}";
264 if (!empty($this->end_date_2
)) {
265 $clauses[] = "contrib_2.receive_date <= {$this->end_date_2}";
269 if (!empty($this->exclude_start_date
) ||
270 !empty($this->exclude_end_date
) ||
271 !empty($this->is_first_amount
)
274 // first create temp table to store contact ids
275 $sql = "DROP TEMPORARY TABLE IF EXISTS XG_CustomSearch_SYBNT";
276 CRM_Core_DAO
::executeQuery($sql);
278 $sql = "CREATE TEMPORARY TABLE XG_CustomSearch_SYBNT ( contact_id int primary key) ENGINE=HEAP";
279 CRM_Core_DAO
::executeQuery($sql);
281 $excludeClauses = array();
282 if ($this->exclude_start_date
) {
283 $excludeClauses[] = "c.receive_date >= {$this->exclude_start_date}";
286 if ($this->exclude_end_date
) {
287 $excludeClauses[] = "c.receive_date <= {$this->exclude_end_date}";
290 $excludeClause = NULL;
291 if ($excludeClauses) {
292 $excludeClause = ' AND ' . implode(' AND ', $excludeClauses);
296 if ($this->exclude_min_amount
) {
297 $having[] = "sum(c.total_amount) >= {$this->exclude_min_amount}";
300 if ($this->exclude_max_amount
) {
301 $having[] = "sum(c.total_amount) <= {$this->exclude_max_amount}";
304 $havingClause = NULL;
305 if (!empty($having)) {
306 $havingClause = "HAVING " . implode(' AND ', $having);
309 if ($excludeClause ||
$havingClause) {
312 REPLACE INTO XG_CustomSearch_SYBNT
313 SELECT DISTINCT contact_id AS contact_id
314 FROM civicrm_contribution c
317 GROUP BY c.contact_id
321 $dao = CRM_Core_DAO
::executeQuery($query);
324 // now ensure we dont consider donors that are not first time
325 if ($this->is_first_amount
) {
327 REPLACE INTO XG_CustomSearch_SYBNT
328 SELECT DISTINCT contact_id AS contact_id
329 FROM civicrm_contribution c
331 AND c.receive_date < {$this->start_date_1}
333 $dao = CRM_Core_DAO
::executeQuery($query);
336 $clauses[] = " xg.contact_id IS NULL ";
339 return implode(' AND ', $clauses);
343 * @param bool $includeContactIDs
347 public function having($includeContactIDs = FALSE) {
349 $min = CRM_Utils_Array
::value('min_amount', $this->_formValues
);
351 $clauses[] = "sum(contrib_1.total_amount) >= $min";
354 $max = CRM_Utils_Array
::value('max_amount', $this->_formValues
);
356 $clauses[] = "sum(contrib_1.total_amount) <= $max";
359 return implode(' AND ', $clauses);
365 public function &columns() {
366 return $this->_columns
;
372 public function templateFile() {
373 return 'CRM/Contact/Form/Search/Custom/ContribSYBNT.tpl';
379 public function summary() {
386 public function setTitle($title) {
388 CRM_Utils_System
::setTitle($title);
391 CRM_Utils_System
::setTitle(ts('Search'));