Commit | Line | Data |
---|---|---|
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 | 35 | class CRM_Contact_Form_Search_Custom_TagContributions extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { |
6a488035 | 36 | |
4e54c348 PJ |
37 | protected $_formValues; |
38 | public $_permissionedComponent; | |
39 | ||
86538308 EM |
40 | /** |
41 | * @param $formValues | |
42 | */ | |
00be9182 | 43 | public 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( | |
7b99ead3 | 51 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
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 | 60 | /** |
c490a46a | 61 | * @param CRM_Core_Form $form |
86538308 | 62 | */ |
00be9182 | 63 | public function buildForm(&$form) { |
6a488035 TO |
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 | ||
6a488035 TO |
74 | $form->addDate('start_date', ts('Contribution Date From'), FALSE, array('formatType' => 'custom')); |
75 | $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom')); | |
cd43c5e3 | 76 | $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
6a488035 TO |
77 | $form->addElement('select', 'tag', ts('Tagged'), $tag); |
78 | ||
79 | /** | |
80 | * If you are using the sample template, this array tells the template fields to render | |
81 | * for the search form. | |
82 | */ | |
83 | $form->assign('elements', array('start_date', 'end_date', 'tag')); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Define the smarty template used to layout the search form and results listings. | |
88 | */ | |
00be9182 | 89 | public function templateFile() { |
6a488035 TO |
90 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
91 | } | |
92 | ||
93 | /** | |
fe482240 | 94 | * Construct the search query. |
6a488035 | 95 | */ |
2da40d21 | 96 | public function all( |
51ccfbbe | 97 | $offset = 0, $rowcount = 0, $sort = NULL, |
6a488035 TO |
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 = " | |
107 | DISTINCT | |
108 | contact_a.id as contact_id, | |
109 | contact_a.sort_name as sort_name, | |
110 | contact_a.first_name as first_name, | |
111 | contact_a.last_name as last_name, | |
112 | GROUP_CONCAT(DISTINCT civicrm_tag.name ORDER BY civicrm_tag.name ASC ) as tag_name, | |
113 | sum(civicrm_contribution.total_amount) as amount | |
114 | "; | |
115 | } | |
116 | $from = $this->from(); | |
117 | ||
118 | $where = $this->where($includeContactIDs); | |
119 | ||
120 | $sql = " | |
121 | SELECT $select | |
122 | FROM $from | |
123 | WHERE $where | |
124 | "; | |
7c34ab11 | 125 | |
126 | $sql .= " GROUP BY contact_a.id"; | |
127 | // Define ORDER BY for query in $sort, with default value | |
128 | if (!empty($sort)) { | |
129 | if (is_string($sort)) { | |
130 | $sort = CRM_Utils_Type::escape($sort, 'String'); | |
131 | $sql .= " ORDER BY $sort "; | |
6a488035 TO |
132 | } |
133 | else { | |
7c34ab11 | 134 | $sql .= " ORDER BY " . trim($sort->orderBy()); |
6a488035 TO |
135 | } |
136 | } | |
7c34ab11 | 137 | else { |
138 | $sql .= ""; | |
139 | } | |
6a488035 TO |
140 | return $sql; |
141 | } | |
142 | ||
86538308 EM |
143 | /** |
144 | * @return string | |
145 | */ | |
00be9182 | 146 | public function from() { |
6a488035 TO |
147 | return " |
148 | civicrm_contribution, | |
149 | civicrm_contact contact_a | |
150 | LEFT JOIN civicrm_entity_tag ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND | |
151 | civicrm_entity_tag.entity_id = contact_a.id ) | |
152 | LEFT JOIN civicrm_tag ON civicrm_tag.id = civicrm_entity_tag.tag_id | |
153 | "; | |
154 | } | |
155 | ||
156 | /* | |
e70a7fc0 TO |
157 | * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values |
158 | * | |
159 | */ | |
86538308 EM |
160 | /** |
161 | * @param bool $includeContactIDs | |
162 | * | |
163 | * @return string | |
164 | */ | |
00be9182 | 165 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
166 | $clauses = array(); |
167 | ||
168 | $clauses[] = "contact_a.contact_type = 'Individual'"; | |
169 | $clauses[] = "civicrm_contribution.contact_id = contact_a.id"; | |
170 | ||
171 | $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']); | |
172 | if ($startDate) { | |
173 | $clauses[] = "civicrm_contribution.receive_date >= $startDate"; | |
174 | } | |
175 | ||
176 | $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']); | |
177 | if ($endDate) { | |
178 | $clauses[] = "civicrm_contribution.receive_date <= $endDate"; | |
179 | } | |
180 | ||
181 | $tag = CRM_Utils_Array::value('tag', $this->_formValues); | |
182 | if ($tag) { | |
183 | $clauses[] = "civicrm_entity_tag.tag_id = $tag"; | |
184 | $clauses[] = "civicrm_tag.id = civicrm_entity_tag.tag_id"; | |
185 | } | |
186 | else { | |
187 | $clauses[] = "civicrm_entity_tag.tag_id IS NOT NULL"; | |
188 | } | |
189 | ||
190 | if ($includeContactIDs) { | |
191 | $contactIDs = array(); | |
192 | foreach ($this->_formValues as $id => $value) { | |
193 | if ($value && | |
194 | substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX | |
195 | ) { | |
196 | $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN); | |
197 | } | |
198 | } | |
199 | ||
200 | if (!empty($contactIDs)) { | |
201 | $contactIDs = implode(', ', $contactIDs); | |
202 | $clauses[] = "contact_a.id IN ( $contactIDs )"; | |
203 | } | |
204 | } | |
205 | return implode(' AND ', $clauses); | |
206 | } | |
207 | ||
208 | ||
209 | /* | |
c490a46a CW |
210 | * Functions below generally don't need to be modified |
211 | */ | |
546b78fa | 212 | |
bb271167 | 213 | /** |
546b78fa | 214 | * @inheritDoc |
bb271167 | 215 | */ |
00be9182 | 216 | public function count() { |
6a488035 TO |
217 | $sql = $this->all(); |
218 | ||
219 | $dao = CRM_Core_DAO::executeQuery($sql, | |
220 | CRM_Core_DAO::$_nullArray | |
221 | ); | |
222 | return $dao->N; | |
223 | } | |
224 | ||
86538308 EM |
225 | /** |
226 | * @param int $offset | |
227 | * @param int $rowcount | |
228 | * @param null $sort | |
8bb3a4f3 | 229 | * @param boolean $returnSQL Not used; included for consistency with parent; SQL is always returned |
86538308 EM |
230 | * |
231 | * @return string | |
232 | */ | |
e98a9804 | 233 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) { |
6a488035 TO |
234 | return $this->all($offset, $rowcount, $sort, FALSE, TRUE); |
235 | } | |
236 | ||
86538308 EM |
237 | /** |
238 | * @return array | |
239 | */ | |
00be9182 | 240 | public function &columns() { |
6a488035 TO |
241 | return $this->_columns; |
242 | } | |
243 | ||
86538308 EM |
244 | /** |
245 | * @param $title | |
246 | */ | |
00be9182 | 247 | public function setTitle($title) { |
6a488035 TO |
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 | */ | |
00be9182 | 259 | public function summary() { |
6a488035 TO |
260 | return NULL; |
261 | } | |
96025800 | 262 | |
6a488035 | 263 | } |