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