Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form { | |
34 | ||
35 | protected $_summary = NULL; | |
70bea8e2 | 36 | protected $_customGroupExtends = array( |
37 | 'Contact', | |
38 | 'Individual', | |
7d793900 | 39 | 'Contribution', |
70bea8e2 | 40 | ); |
6a488035 | 41 | |
1728e9a0 | 42 | /** |
43 | * This report has not been optimised for group filtering. | |
44 | * | |
45 | * The functionality for group filtering has been improved but not | |
46 | * all reports have been adjusted to take care of it. This report has not | |
47 | * and will run an inefficient query until fixed. | |
48 | * | |
49 | * CRM-19170 | |
50 | * | |
51 | * @var bool | |
52 | */ | |
53 | protected $groupFilterNotOptimised = TRUE; | |
54 | ||
6a488035 TO |
55 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); |
56 | ||
57 | protected $_charts = array( | |
58 | '' => 'Tabular', | |
59 | 'barChart' => 'Bar Chart', | |
60 | 'pieChart' => 'Pie Chart', | |
61 | ); | |
2f4c2f5d | 62 | |
74cf4551 | 63 | /** |
74cf4551 | 64 | */ |
00be9182 | 65 | public function __construct() { |
70bea8e2 | 66 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
6a488035 | 67 | $this->_columns = array( |
ae5ffbb7 TO |
68 | 'civicrm_contact' => array( |
69 | 'dao' => 'CRM_Contact_DAO_Contact', | |
70 | 'fields' => array( | |
ae5ffbb7 TO |
71 | 'display_name' => array( |
72 | 'title' => ts('Contact Name'), | |
73 | 'required' => TRUE, | |
74 | 'no_repeat' => TRUE, | |
75 | ), | |
76 | 'first_name' => array( | |
77 | 'title' => ts('First Name'), | |
78 | ), | |
70bea8e2 | 79 | 'middle_name' => array( |
80 | 'title' => ts('Middle Name'), | |
81 | ), | |
ae5ffbb7 TO |
82 | 'last_name' => array( |
83 | 'title' => ts('Last Name'), | |
84 | ), | |
70bea8e2 | 85 | 'id' => array( |
86 | 'no_display' => TRUE, | |
87 | 'required' => TRUE, | |
88 | ), | |
89 | 'gender_id' => array( | |
90 | 'title' => ts('Gender'), | |
91 | ), | |
92 | 'birth_date' => array( | |
93 | 'title' => ts('Birth Date'), | |
94 | ), | |
95 | 'age' => array( | |
96 | 'title' => ts('Age'), | |
97 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
98 | ), | |
ae5ffbb7 TO |
99 | 'contact_type' => array( |
100 | 'title' => ts('Contact Type'), | |
101 | ), | |
102 | 'contact_sub_type' => array( | |
103 | 'title' => ts('Contact Subtype'), | |
7a961f19 | 104 | ), |
6a488035 | 105 | ), |
bef8d64c | 106 | 'filters' => $this->getBasicContactFilters(), |
c3fdd2b7 | 107 | 'group_bys' => ['contact_contact_id' => ['name' => 'id', 'required' => 1, 'no_display' => 1]], |
ae5ffbb7 | 108 | ), |
cbfbab2f E |
109 | 'civicrm_line_item' => array( |
110 | 'dao' => 'CRM_Price_DAO_LineItem', | |
111 | ), | |
ae5ffbb7 | 112 | ); |
c3fdd2b7 | 113 | $this->_columns += $this->getAddressColumns(['group_by' => FALSE]); |
ae5ffbb7 TO |
114 | $this->_columns += array( |
115 | 'civicrm_contribution' => array( | |
116 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
117 | 'fields' => array( | |
118 | 'total_amount' => array( | |
119 | 'title' => ts('Amount Statistics'), | |
120 | 'required' => TRUE, | |
121 | 'statistics' => array( | |
122 | 'sum' => ts('Aggregate Amount'), | |
123 | 'count' => ts('Donations'), | |
124 | 'avg' => ts('Average'), | |
f3bc9ff5 | 125 | ), |
6a488035 | 126 | ), |
ae5ffbb7 TO |
127 | 'currency' => array( |
128 | 'required' => TRUE, | |
129 | 'no_display' => TRUE, | |
6a488035 TO |
130 | ), |
131 | ), | |
ae5ffbb7 TO |
132 | 'filters' => array( |
133 | 'receive_date' => array( | |
134 | 'default' => 'this.year', | |
135 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
136 | ), | |
137 | 'currency' => array( | |
ccc29f8e | 138 | 'title' => ts('Currency'), |
ae5ffbb7 TO |
139 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
140 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
141 | 'default' => NULL, | |
142 | 'type' => CRM_Utils_Type::T_STRING, | |
143 | ), | |
144 | 'total_range' => array( | |
145 | 'title' => ts('Show no. of Top Donors'), | |
146 | 'type' => CRM_Utils_Type::T_INT, | |
147 | 'default_op' => 'eq', | |
148 | ), | |
149 | 'financial_type_id' => array( | |
150 | 'name' => 'financial_type_id', | |
151 | 'title' => ts('Financial Type'), | |
8ee006e7 | 152 | 'type' => CRM_Utils_Type::T_INT, |
ae5ffbb7 | 153 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
cbfbab2f | 154 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
ae5ffbb7 TO |
155 | ), |
156 | 'contribution_status_id' => array( | |
157 | 'title' => ts('Contribution Status'), | |
158 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
159 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
160 | 'default' => array(1), | |
6a488035 TO |
161 | ), |
162 | ), | |
c3fdd2b7 | 163 | 'group_bys' => ['contribution_currency' => ['name' => 'currency', 'required' => 1, 'no_display' => 1]], |
ae5ffbb7 | 164 | ), |
bdfeefa3 E |
165 | 'civicrm_financial_trxn' => array( |
166 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
167 | 'fields' => array( | |
5e0343e8 | 168 | 'card_type_id' => array( |
d72b084a | 169 | 'title' => ts('Credit Card Type'), |
5e0343e8 | 170 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")', |
bdfeefa3 E |
171 | ), |
172 | ), | |
173 | 'filters' => array( | |
5e0343e8 | 174 | 'card_type_id' => array( |
d72b084a | 175 | 'title' => ts('Credit Card Type'), |
bdfeefa3 | 176 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
5e0343e8 | 177 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
bdfeefa3 E |
178 | 'default' => NULL, |
179 | 'type' => CRM_Utils_Type::T_STRING, | |
180 | ), | |
181 | ), | |
182 | ), | |
ae5ffbb7 TO |
183 | 'civicrm_email' => array( |
184 | 'dao' => 'CRM_Core_DAO_Email', | |
185 | 'fields' => array( | |
186 | 'email' => array( | |
187 | 'title' => ts('Email'), | |
188 | 'default' => TRUE, | |
189 | 'no_repeat' => TRUE, | |
190 | ), | |
191 | ), | |
192 | 'grouping' => 'email-fields', | |
193 | ), | |
194 | 'civicrm_phone' => array( | |
195 | 'dao' => 'CRM_Core_DAO_Phone', | |
196 | 'fields' => array( | |
197 | 'phone' => array( | |
198 | 'title' => ts('Phone'), | |
199 | 'default' => TRUE, | |
200 | 'no_repeat' => TRUE, | |
6a488035 TO |
201 | ), |
202 | ), | |
ae5ffbb7 TO |
203 | 'grouping' => 'phone-fields', |
204 | ), | |
205 | ); | |
6a488035 | 206 | |
16e2e80c | 207 | $this->_groupFilter = TRUE; |
6a488035 | 208 | $this->_tagFilter = TRUE; |
7a961f19 | 209 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
210 | parent::__construct(); |
211 | } | |
212 | ||
74cf4551 EM |
213 | /** |
214 | * @param $fields | |
215 | * @param $files | |
216 | * @param $self | |
217 | * | |
218 | * @return array | |
219 | */ | |
00be9182 | 220 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
221 | $errors = array(); |
222 | ||
223 | $op = CRM_Utils_Array::value('total_range_op', $fields); | |
224 | $val = CRM_Utils_Array::value('total_range_value', $fields); | |
225 | ||
226 | if (!in_array($op, array( | |
9d72cede | 227 | 'eq', |
21dfd5f5 | 228 | 'lte', |
9d72cede EM |
229 | )) |
230 | ) { | |
6a488035 TO |
231 | $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator"); |
232 | } | |
233 | ||
234 | if ($val && !CRM_Utils_Rule::positiveInteger($val)) { | |
235 | $errors['total_range_value'] = ts("Please enter positive number"); | |
236 | } | |
237 | return $errors; | |
238 | } | |
239 | ||
00be9182 | 240 | public function from() { |
6a488035 TO |
241 | $this->_from = " |
242 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
65f9ed10 E |
243 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
244 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
18f511e2 | 245 | "; |
bdfeefa3 E |
246 | |
247 | // for credit card type | |
248 | $this->addFinancialTrxnFromClause(); | |
249 | ||
18f511e2 | 250 | $this->joinAddressFromContact(); |
251 | $this->joinPhoneFromContact(); | |
252 | $this->joinEmailFromContact(); | |
6a488035 TO |
253 | } |
254 | ||
00be9182 | 255 | public function where() { |
6a488035 | 256 | $clauses = array(); |
93406a82 | 257 | $this->_tempClause = $this->_outerCluase = $this->_groupLimit = ''; |
6a488035 TO |
258 | foreach ($this->_columns as $tableName => $table) { |
259 | if (array_key_exists('filters', $table)) { | |
260 | foreach ($table['filters'] as $fieldName => $field) { | |
261 | $clause = NULL; | |
262 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
263 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
264 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
265 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
266 | |
267 | if ($relative || $from || $to) { | |
268 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
269 | } | |
270 | } | |
271 | else { | |
272 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
273 | if ($op) { | |
274 | $clause = $this->whereClause($field, | |
275 | $op, | |
276 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
277 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
278 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
279 | ); | |
280 | } | |
281 | } | |
282 | ||
283 | if (!empty($clause)) { | |
284 | if ($fieldName == 'total_range') { | |
285 | $value = CRM_Utils_Array::value("total_range_value", $this->_params); | |
286 | $this->_outerCluase = " WHERE (( @rows := @rows + 1) <= {$value}) "; | |
93406a82 | 287 | $this->_groupLimit = " LIMIT {$value}"; |
6a488035 TO |
288 | } |
289 | else { | |
290 | $clauses[] = $clause; | |
291 | } | |
292 | } | |
293 | } | |
294 | } | |
295 | } | |
296 | if (empty($clauses)) { | |
297 | $this->_where = "WHERE ( 1 ) "; | |
298 | } | |
299 | else { | |
300 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
301 | } | |
302 | ||
303 | if ($this->_aclWhere) { | |
304 | $this->_where .= " AND {$this->_aclWhere} "; | |
305 | } | |
306 | } | |
307 | ||
969a8733 | 308 | /** |
309 | * Build output rows. | |
310 | * | |
311 | * @param string $sql | |
312 | * @param array $rows | |
313 | */ | |
314 | public function buildRows($sql, &$rows) { | |
6a488035 TO |
315 | $setVariable = " SET @rows:=0, @rank=0 "; |
316 | CRM_Core_DAO::singleValueQuery($setVariable); | |
969a8733 | 317 | $sql = " |
318 | SELECT * FROM ( {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} | |
319 | ORDER BY civicrm_contribution_total_amount_sum DESC | |
320 | ) as abc {$this->_outerCluase} $this->_limit | |
321 | "; | |
322 | parent::buildRows($sql, $rows); | |
6a488035 TO |
323 | } |
324 | ||
74cf4551 | 325 | /** |
100fef9d | 326 | * @param int $groupID |
74cf4551 | 327 | */ |
00be9182 | 328 | public function add2group($groupID) { |
6a488035 | 329 | if (is_numeric($groupID)) { |
969a8733 | 330 | $this->_limit = $this->_groupLimit; |
331 | $rows = array(); | |
332 | $this->_columnHeaders['civicrm_contact_id'] = 1; | |
333 | $this->buildRows('', $rows); | |
6a488035 TO |
334 | |
335 | $contact_ids = array(); | |
336 | // Add resulting contacts to group | |
969a8733 | 337 | foreach ($rows as $row) { |
338 | $contact_ids[$row['civicrm_contact_id']] = $row['civicrm_contact_id']; | |
6a488035 TO |
339 | } |
340 | ||
341 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $groupID); | |
342 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
343 | } | |
344 | } | |
345 | ||
74cf4551 EM |
346 | /** |
347 | * @param int $rowCount | |
348 | */ | |
00be9182 | 349 | public function limit($rowCount = CRM_Report_Form::ROW_COUNT_LIMIT) { |
6a488035 TO |
350 | // lets do the pager if in html mode |
351 | $this->_limit = NULL; | |
74cf4551 | 352 | |
dbb4a0f9 PN |
353 | // CRM-14115, over-ride row count if rowCount is specified in URL |
354 | if ($this->_dashBoardRowCount) { | |
355 | $rowCount = $this->_dashBoardRowCount; | |
356 | } | |
969a8733 | 357 | if ($this->_outputMode == 'html') { |
ab432335 | 358 | // Replace only first occurrence of SELECT. |
6a488035 | 359 | $this->_select = preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select, 1); |
a3d827a7 | 360 | $pageId = CRM_Utils_Request::retrieve('crmPID', 'Integer'); |
6a488035 TO |
361 | |
362 | if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) { | |
363 | if (!isset($_POST['PagerBottomButton'])) { | |
364 | unset($_POST['crmPID_B']); | |
365 | } | |
366 | else { | |
9d72cede | 367 | $pageId = max((int) @$_POST['crmPID_B'], 1); |
6a488035 TO |
368 | } |
369 | } | |
370 | ||
371 | $pageId = $pageId ? $pageId : 1; | |
372 | $this->set(CRM_Utils_Pager::PAGE_ID, $pageId); | |
373 | $offset = ($pageId - 1) * $rowCount; | |
374 | ||
bf00d1b6 DL |
375 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
376 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
377 | ||
6a488035 TO |
378 | $this->_limit = " LIMIT $offset, " . $rowCount; |
379 | } | |
380 | } | |
381 | ||
74cf4551 | 382 | /** |
ced9bfed EM |
383 | * Alter display of rows. |
384 | * | |
385 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
386 | * such as rendering contacts as links. | |
387 | * | |
388 | * @param array $rows | |
389 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 390 | */ |
00be9182 | 391 | public function alterDisplay(&$rows) { |
6a488035 TO |
392 | $entryFound = FALSE; |
393 | $rank = 1; | |
394 | if (!empty($rows)) { | |
395 | foreach ($rows as $rowNum => $row) { | |
396 | ||
397 | $rows[$rowNum]['civicrm_donor_rank'] = $rank++; | |
398 | // convert display name to links | |
399 | if (array_key_exists('civicrm_contact_display_name', $row) && | |
9d72cede EM |
400 | array_key_exists('civicrm_contact_id', $row) && |
401 | !empty($row['civicrm_contribution_currency']) | |
402 | ) { | |
6a488035 | 403 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
9d72cede EM |
404 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . |
405 | "¤cy_value=" . $row['civicrm_contribution_currency'], | |
6a488035 TO |
406 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
407 | ); | |
408 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; | |
409 | $entryFound = TRUE; | |
410 | } | |
21d62de7 | 411 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
6a488035 | 412 | |
7d793900 | 413 | //handle gender |
414 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
415 | if ($value = $row['civicrm_contact_gender_id']) { | |
416 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
417 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
418 | } | |
419 | $entryFound = TRUE; | |
70bea8e2 | 420 | } |
70bea8e2 | 421 | |
7d793900 | 422 | // display birthday in the configured custom format |
423 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
424 | $birthDate = $row['civicrm_contact_birth_date']; | |
425 | if ($birthDate) { | |
426 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
427 | } | |
428 | $entryFound = TRUE; | |
70bea8e2 | 429 | } |
70bea8e2 | 430 | |
5e0343e8 | 431 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
432 | $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id'); | |
bdfeefa3 E |
433 | $entryFound = TRUE; |
434 | } | |
435 | ||
6a488035 TO |
436 | // skip looking further in rows, if first row itself doesn't |
437 | // have the column we need | |
438 | if (!$entryFound) { | |
439 | break; | |
440 | } | |
441 | } | |
442 | } | |
443 | } | |
96025800 | 444 | |
6a488035 | 445 | } |