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