Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { | |
37 | ||
38 | protected $_summary = NULL; | |
39 | ||
40 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); | |
41 | ||
42 | protected $_charts = array( | |
43 | '' => 'Tabular', | |
44 | 'barChart' => 'Bar Chart', | |
45 | 'pieChart' => 'Pie Chart', | |
46 | ); | |
2f4c2f5d | 47 | |
6a488035 TO |
48 | function __construct() { |
49 | $this->_columns = array( | |
50 | 'civicrm_contact' => | |
51 | array( | |
52 | 'dao' => 'CRM_Contact_DAO_Contact', | |
53 | 'fields' => | |
54 | array( | |
55 | 'id' => | |
56 | array( | |
57 | 'no_display' => TRUE, | |
58 | 'required' => TRUE, | |
59 | ), | |
60 | 'display_name' => | |
61 | array('title' => ts('Contact Name'), | |
62 | 'required' => TRUE, | |
63 | 'no_repeat' => TRUE, | |
64 | ), | |
30f85891 RN |
65 | 'first_name' => array( |
66 | 'title' => ts('First Name'), | |
6a488035 | 67 | ), |
30f85891 RN |
68 | 'last_name' => array( |
69 | 'title' => ts('Last Name'), | |
70 | ), | |
71 | 'contact_type' => | |
72 | array( | |
73 | 'title' => ts('Contact Type'), | |
74 | ), | |
75 | 'contact_sub_type' => | |
76 | array( | |
77 | 'title' => ts('Contact SubType'), | |
6a488035 TO |
78 | ), |
79 | ), | |
80 | ), | |
21d62de7 DG |
81 | ) |
82 | + $this->getAddressColumns() | |
83 | + array( | |
6a488035 TO |
84 | 'civicrm_contribution' => |
85 | array( | |
86 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
87 | 'fields' => | |
88 | array( | |
89 | 'total_amount' => | |
90 | array('title' => ts('Amount Statistics'), | |
91 | 'required' => TRUE, | |
92 | 'statistics' => | |
93 | array('sum' => ts('Aggregate Amount'), | |
94 | 'count' => ts('Donations'), | |
95 | 'avg' => ts('Average'), | |
96 | ), | |
97 | ), | |
2f4c2f5d | 98 | 'currency' => |
7a961f19 | 99 | array('required' => TRUE, |
100 | 'no_display' => TRUE, | |
101 | ), | |
6a488035 TO |
102 | ), |
103 | 'filters' => | |
104 | array( | |
105 | 'receive_date' => | |
106 | array( | |
107 | 'default' => 'this.year', | |
108 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
109 | ), | |
7a961f19 | 110 | 'currency' => |
f3bc9ff5 | 111 | array('title' => 'Currency', |
112 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
0637f1c4 | 113 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), |
d1b0931b | 114 | 'default' => NULL, |
7a961f19 | 115 | 'type' => CRM_Utils_Type::T_STRING, |
f3bc9ff5 | 116 | ), |
6a488035 TO |
117 | 'total_range' => |
118 | array('title' => ts('Show no. of Top Donors'), | |
119 | 'type' => CRM_Utils_Type::T_INT, | |
120 | 'default_op' => 'eq', | |
121 | ), | |
f3bc9ff5 | 122 | 'financial_type_id' => |
b914f4e8 PN |
123 | array('name' => 'financial_type_id', |
124 | 'title' => ts('Financial Type'), | |
6a488035 | 125 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
b914f4e8 | 126 | 'options' => CRM_Contribute_PseudoConstant::financialType() , |
6a488035 TO |
127 | ), |
128 | 'contribution_status_id' => | |
129 | array('title' => ts('Donation Status'), | |
130 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
131 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
132 | 'default' => array(1), | |
133 | ), | |
134 | ), | |
135 | ), | |
2f4c2f5d | 136 | 'civicrm_email' => |
6a488035 TO |
137 | array( |
138 | 'dao' => 'CRM_Core_DAO_Email', | |
139 | 'fields' => | |
140 | array( | |
141 | 'email' => | |
142 | array('title' => ts('Email'), | |
143 | 'default' => TRUE, | |
144 | 'no_repeat' => TRUE, | |
145 | ), | |
146 | ), | |
147 | 'grouping' => 'email-fields', | |
148 | ), | |
2f4c2f5d | 149 | |
6a488035 TO |
150 | 'civicrm_phone' => |
151 | array( | |
152 | 'dao' => 'CRM_Core_DAO_Phone', | |
153 | 'fields' => | |
154 | array( | |
155 | 'phone' => | |
156 | array('title' => ts('Phone'), | |
157 | 'default' => TRUE, | |
158 | 'no_repeat' => TRUE, | |
159 | ), | |
160 | ), | |
161 | 'grouping' => 'phone-fields', | |
162 | ), | |
163 | 'civicrm_group' => | |
164 | array( | |
165 | 'dao' => 'CRM_Contact_DAO_GroupContact', | |
166 | 'alias' => 'cgroup', | |
167 | 'filters' => | |
168 | array( | |
169 | 'gid' => | |
170 | array( | |
171 | 'name' => 'group_id', | |
172 | 'title' => ts('Group'), | |
173 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
174 | 'group' => TRUE, | |
175 | 'options' => CRM_Core_PseudoConstant::group(), | |
176 | ), | |
177 | ), | |
178 | ), | |
179 | ); | |
180 | ||
181 | $this->_tagFilter = TRUE; | |
7a961f19 | 182 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
183 | parent::__construct(); |
184 | } | |
185 | ||
186 | function preProcess() { | |
187 | parent::preProcess(); | |
188 | } | |
189 | ||
190 | function select() { | |
191 | $select = array(); | |
192 | $this->_columnHeaders = array(); | |
193 | //Headers for Rank column | |
194 | $this->_columnHeaders["civicrm_donor_rank"]['title'] = ts('Rank'); | |
195 | $this->_columnHeaders["civicrm_donor_rank"]['type'] = 1; | |
196 | //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank "; | |
197 | ||
198 | foreach ($this->_columns as $tableName => $table) { | |
199 | if (array_key_exists('fields', $table)) { | |
200 | foreach ($table['fields'] as $fieldName => $field) { | |
201 | if (CRM_Utils_Array::value('required', $field) || | |
202 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
203 | ) { | |
204 | // only include statistics columns if set | |
205 | if (CRM_Utils_Array::value('statistics', $field)) { | |
206 | foreach ($field['statistics'] as $stat => $label) { | |
207 | switch (strtolower($stat)) { | |
208 | case 'sum': | |
209 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
210 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
211 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
212 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
213 | break; | |
214 | ||
215 | case 'count': | |
216 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
217 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
218 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
219 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
220 | break; | |
221 | ||
222 | case 'avg': | |
223 | $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; | |
224 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
225 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
226 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
227 | break; | |
228 | } | |
229 | } | |
230 | } | |
231 | else { | |
232 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
21d62de7 DG |
233 | // $field['type'] is not always set. Use string type as default if not set. |
234 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = isset($field['type']) ? $field['type'] : 2; | |
6a488035 TO |
235 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; |
236 | } | |
237 | } | |
238 | } | |
239 | } | |
240 | } | |
65f9ed10 | 241 | |
6a488035 TO |
242 | $this->_select = " SELECT * FROM ( SELECT " . implode(', ', $select) . " "; |
243 | } | |
244 | ||
245 | static function formRule($fields, $files, $self) { | |
246 | $errors = array(); | |
247 | ||
248 | $op = CRM_Utils_Array::value('total_range_op', $fields); | |
249 | $val = CRM_Utils_Array::value('total_range_value', $fields); | |
250 | ||
251 | if (!in_array($op, array( | |
252 | 'eq', 'lte'))) { | |
253 | $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator"); | |
254 | } | |
255 | ||
256 | if ($val && !CRM_Utils_Rule::positiveInteger($val)) { | |
257 | $errors['total_range_value'] = ts("Please enter positive number"); | |
258 | } | |
259 | return $errors; | |
260 | } | |
261 | ||
262 | function from() { | |
263 | $this->_from = " | |
264 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
65f9ed10 E |
265 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
266 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
2f4c2f5d | 267 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
268 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id | |
6a488035 | 269 | AND {$this->_aliases['civicrm_email']}.is_primary = 1 |
2f4c2f5d | 270 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
6a488035 | 271 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
2f4c2f5d | 272 | {$this->_aliases['civicrm_phone']}.is_primary = 1 |
65f9ed10 | 273 | "; |
21d62de7 | 274 | $this->addAddressFromClause(); |
6a488035 TO |
275 | } |
276 | ||
277 | function where() { | |
278 | $clauses = array(); | |
279 | $this->_tempClause = $this->_outerCluase = ''; | |
280 | foreach ($this->_columns as $tableName => $table) { | |
281 | if (array_key_exists('filters', $table)) { | |
282 | foreach ($table['filters'] as $fieldName => $field) { | |
283 | $clause = NULL; | |
284 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
285 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
286 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
287 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
288 | ||
289 | if ($relative || $from || $to) { | |
290 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
291 | } | |
292 | } | |
293 | else { | |
294 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
295 | if ($op) { | |
296 | $clause = $this->whereClause($field, | |
297 | $op, | |
298 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
299 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
300 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
301 | ); | |
302 | } | |
303 | } | |
304 | ||
305 | if (!empty($clause)) { | |
306 | if ($fieldName == 'total_range') { | |
307 | $value = CRM_Utils_Array::value("total_range_value", $this->_params); | |
308 | $this->_outerCluase = " WHERE (( @rows := @rows + 1) <= {$value}) "; | |
309 | } | |
310 | else { | |
311 | $clauses[] = $clause; | |
312 | } | |
313 | } | |
314 | } | |
315 | } | |
316 | } | |
317 | if (empty($clauses)) { | |
318 | $this->_where = "WHERE ( 1 ) "; | |
319 | } | |
320 | else { | |
321 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
322 | } | |
323 | ||
324 | if ($this->_aclWhere) { | |
325 | $this->_where .= " AND {$this->_aclWhere} "; | |
326 | } | |
327 | } | |
328 | ||
329 | function groupBy() { | |
7a961f19 | 330 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency"; |
6a488035 TO |
331 | } |
332 | ||
333 | function postProcess() { | |
334 | ||
335 | $this->beginPostProcess(); | |
336 | ||
337 | // get the acl clauses built before we assemble the query | |
338 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
339 | ||
340 | $this->select(); | |
341 | ||
342 | $this->from(); | |
343 | ||
344 | $this->where(); | |
345 | ||
346 | $this->groupBy(); | |
347 | ||
348 | $this->limit(); | |
349 | ||
350 | ||
351 | //set the variable value rank, rows = 0 | |
352 | $setVariable = " SET @rows:=0, @rank=0 "; | |
353 | CRM_Core_DAO::singleValueQuery($setVariable); | |
354 | ||
2f4c2f5d | 355 | $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} |
6a488035 TO |
356 | ORDER BY civicrm_contribution_total_amount_sum DESC |
357 | ) as abc {$this->_outerCluase} $this->_limit | |
358 | "; | |
359 | ||
360 | $dao = CRM_Core_DAO::executeQuery($sql); | |
361 | ||
362 | while ($dao->fetch()) { | |
363 | $row = array(); | |
364 | foreach ($this->_columnHeaders as $key => $value) { | |
365 | if (property_exists($dao, $key)) { | |
366 | $row[$key] = $dao->$key; | |
367 | } | |
368 | } | |
369 | $rows[] = $row; | |
370 | } | |
371 | $this->formatDisplay($rows); | |
372 | ||
373 | $this->doTemplateAssignment($rows); | |
374 | ||
375 | $this->endPostProcess($rows); | |
376 | } | |
377 | ||
378 | function add2group($groupID) { | |
379 | if (is_numeric($groupID)) { | |
380 | ||
2f4c2f5d | 381 | $sql = " |
382 | {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} | |
6a488035 TO |
383 | ORDER BY civicrm_contribution_total_amount_sum DESC |
384 | ) as abc {$this->_outerCluase}"; | |
385 | $dao = CRM_Core_DAO::executeQuery($sql); | |
386 | ||
387 | $contact_ids = array(); | |
388 | // Add resulting contacts to group | |
389 | while ($dao->fetch()) { | |
390 | $contact_ids[$dao->civicrm_contact_id] = $dao->civicrm_contact_id; | |
391 | } | |
392 | ||
393 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $groupID); | |
394 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
395 | } | |
396 | } | |
397 | ||
398 | function limit($rowCount = CRM_Report_Form::ROW_COUNT_LIMIT) { | |
399 | // lets do the pager if in html mode | |
400 | $this->_limit = NULL; | |
401 | if ($this->_outputMode == 'html' || $this->_outputMode == 'group') { | |
402 | //replace only first occurence of SELECT | |
403 | $this->_select = preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select, 1); | |
404 | $pageId = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); | |
405 | ||
406 | if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) { | |
407 | if (!isset($_POST['PagerBottomButton'])) { | |
408 | unset($_POST['crmPID_B']); | |
409 | } | |
410 | else { | |
411 | $pageId = max((int)@$_POST['crmPID_B'], 1); | |
412 | } | |
413 | } | |
414 | ||
415 | $pageId = $pageId ? $pageId : 1; | |
416 | $this->set(CRM_Utils_Pager::PAGE_ID, $pageId); | |
417 | $offset = ($pageId - 1) * $rowCount; | |
418 | ||
bf00d1b6 DL |
419 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
420 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
421 | ||
6a488035 TO |
422 | $this->_limit = " LIMIT $offset, " . $rowCount; |
423 | } | |
424 | } | |
425 | ||
426 | function alterDisplay(&$rows) { | |
427 | // custom code to alter rows | |
428 | ||
429 | $entryFound = FALSE; | |
430 | $rank = 1; | |
431 | if (!empty($rows)) { | |
432 | foreach ($rows as $rowNum => $row) { | |
433 | ||
434 | $rows[$rowNum]['civicrm_donor_rank'] = $rank++; | |
435 | // convert display name to links | |
436 | if (array_key_exists('civicrm_contact_display_name', $row) && | |
7a961f19 | 437 | array_key_exists('civicrm_contact_id', $row) && |
438 | CRM_Utils_Array::value('civicrm_contribution_currency', $row) | |
6a488035 TO |
439 | ) { |
440 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
7a961f19 | 441 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . "¤cy_value=" . $row['civicrm_contribution_currency'], |
6a488035 TO |
442 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
443 | ); | |
444 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; | |
445 | $entryFound = TRUE; | |
446 | } | |
21d62de7 | 447 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
6a488035 TO |
448 | |
449 | // skip looking further in rows, if first row itself doesn't | |
450 | // have the column we need | |
451 | if (!$entryFound) { | |
452 | break; | |
453 | } | |
454 | } | |
455 | } | |
456 | } | |
457 | } | |
458 |