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