Merge pull request #3317 from eileenmcnaughton/CRM-14197-postprocesfn
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / TagContributions.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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_TagContributions implements CRM_Contact_Form_Search_Interface {
36
4e54c348
PJ
37 protected $_formValues;
38 public $_permissionedComponent;
39
86538308
EM
40 /**
41 * @param $formValues
42 */
4e54c348 43 function __construct(&$formValues) {
6a488035 44 $this->_formValues = $formValues;
4e54c348 45 $this->_permissionedComponent = 'CiviContribute';
6a488035
TO
46
47 /**
48 * Define the columns for search result rows
49 */
50 $this->_columns = array(
51 ts('Contact Id') => 'contact_id',
52 ts('Full Name') => 'sort_name',
53 ts('First Name') => 'first_name',
54 ts('Last Name') => 'last_name',
55 ts('Tag') => 'tag_name',
56 ts('Totals') => 'amount',
57 );
58 }
59
86538308
EM
60 /**
61 * @param $form
62 */
6a488035
TO
63 function buildForm(&$form) {
64
65 /**
66 * You can define a custom title for the search form
67 */
68 $this->setTitle('Find Contribution Amounts by Tag');
69
70 /**
71 * Define the search form fields here
72 */
73
74
75 $form->addDate('start_date', ts('Contribution Date From'), FALSE, array('formatType' => 'custom'));
76 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
cd43c5e3 77 $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
6a488035
TO
78 $form->addElement('select', 'tag', ts('Tagged'), $tag);
79
80 /**
81 * If you are using the sample template, this array tells the template fields to render
82 * for the search form.
83 */
84 $form->assign('elements', array('start_date', 'end_date', 'tag'));
85 }
86
87 /**
88 * Define the smarty template used to layout the search form and results listings.
89 */
90 function templateFile() {
91 return 'CRM/Contact/Form/Search/Custom.tpl';
92 }
93
94 /**
95 * Construct the search query
96 */
97 function all($offset = 0, $rowcount = 0, $sort = NULL,
98 $includeContactIDs = FALSE, $onlyIDs = FALSE
99 ) {
100
101 // SELECT clause must include contact_id as an alias for civicrm_contact.id
102 if ($onlyIDs) {
103 $select = "contact_a.id as contact_id";
104 }
105 else {
106 $select = "
107DISTINCT
108contact_a.id as contact_id,
109contact_a.sort_name as sort_name,
110contact_a.first_name as first_name,
111contact_a.last_name as last_name,
112GROUP_CONCAT(DISTINCT civicrm_tag.name ORDER BY civicrm_tag.name ASC ) as tag_name,
113sum(civicrm_contribution.total_amount) as amount
114";
115 }
116 $from = $this->from();
117
118 $where = $this->where($includeContactIDs);
119
120 $sql = "
121SELECT $select
122FROM $from
123WHERE $where
124";
125 //for only contact ids ignore order and group by.
126 if (!$onlyIDs) {
127 $sql .= " GROUP BY contact_a.id";
128 // Define ORDER BY for query in $sort, with default value
129 if (!empty($sort)) {
130 if (is_string($sort)) {
21d32567 131 $sort = CRM_Utils_Type::escape($sort, 'String');
6a488035
TO
132 $sql .= " ORDER BY $sort ";
133 }
134 else {
135 $sql .= " ORDER BY " . trim($sort->orderBy());
136 }
137 }
138 else {
139 $sql .= "";
140 }
141 }
142 return $sql;
143 }
144
86538308
EM
145 /**
146 * @return string
147 */
6a488035
TO
148 function from() {
149 return "
150 civicrm_contribution,
151 civicrm_contact contact_a
152 LEFT JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND
153 civicrm_entity_tag.entity_id = contact_a.id )
154 LEFT JOIN civicrm_tag ON civicrm_tag.id = civicrm_entity_tag.tag_id
155";
156 }
157
158 /*
159 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
160 *
161 */
86538308
EM
162 /**
163 * @param bool $includeContactIDs
164 *
165 * @return string
166 */
6a488035
TO
167 function where($includeContactIDs = FALSE) {
168 $clauses = array();
169
170 $clauses[] = "contact_a.contact_type = 'Individual'";
171 $clauses[] = "civicrm_contribution.contact_id = contact_a.id";
172
173 $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']);
174 if ($startDate) {
175 $clauses[] = "civicrm_contribution.receive_date >= $startDate";
176 }
177
178 $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']);
179 if ($endDate) {
180 $clauses[] = "civicrm_contribution.receive_date <= $endDate";
181 }
182
183 $tag = CRM_Utils_Array::value('tag', $this->_formValues);
184 if ($tag) {
185 $clauses[] = "civicrm_entity_tag.tag_id = $tag";
186 $clauses[] = "civicrm_tag.id = civicrm_entity_tag.tag_id";
187 }
188 else {
189 $clauses[] = "civicrm_entity_tag.tag_id IS NOT NULL";
190 }
191
192 if ($includeContactIDs) {
193 $contactIDs = array();
194 foreach ($this->_formValues as $id => $value) {
195 if ($value &&
196 substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
197 ) {
198 $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
199 }
200 }
201
202 if (!empty($contactIDs)) {
203 $contactIDs = implode(', ', $contactIDs);
204 $clauses[] = "contact_a.id IN ( $contactIDs )";
205 }
206 }
207 return implode(' AND ', $clauses);
208 }
209
210
211 /*
212 * Functions below generally don't need to be modified
213 */
bb271167
EM
214 /**
215 * @return mixed
216 */
6a488035
TO
217 function count() {
218 $sql = $this->all();
219
220 $dao = CRM_Core_DAO::executeQuery($sql,
221 CRM_Core_DAO::$_nullArray
222 );
223 return $dao->N;
224 }
225
86538308
EM
226 /**
227 * @param int $offset
228 * @param int $rowcount
229 * @param null $sort
230 *
231 * @return string
232 */
6a488035
TO
233 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
234 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
235 }
236
86538308
EM
237 /**
238 * @return array
239 */
6a488035
TO
240 function &columns() {
241 return $this->_columns;
242 }
243
86538308
EM
244 /**
245 * @param $title
246 */
6a488035
TO
247 function setTitle($title) {
248 if ($title) {
249 CRM_Utils_System::setTitle($title);
250 }
251 else {
252 CRM_Utils_System::setTitle(ts('Search'));
253 }
254 }
255
86538308
EM
256 /**
257 * @return null
258 */
6a488035
TO
259 function summary() {
260 return NULL;
261 }
262}
263