Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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(), |
ae5ffbb7 | 107 | ), |
cbfbab2f E |
108 | 'civicrm_line_item' => array( |
109 | 'dao' => 'CRM_Price_DAO_LineItem', | |
110 | ), | |
ae5ffbb7 TO |
111 | ); |
112 | $this->_columns += $this->getAddressColumns(); | |
113 | $this->_columns += array( | |
114 | 'civicrm_contribution' => array( | |
115 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
116 | 'fields' => array( | |
117 | 'total_amount' => array( | |
118 | 'title' => ts('Amount Statistics'), | |
119 | 'required' => TRUE, | |
120 | 'statistics' => array( | |
121 | 'sum' => ts('Aggregate Amount'), | |
122 | 'count' => ts('Donations'), | |
123 | 'avg' => ts('Average'), | |
f3bc9ff5 | 124 | ), |
6a488035 | 125 | ), |
ae5ffbb7 TO |
126 | 'currency' => array( |
127 | 'required' => TRUE, | |
128 | 'no_display' => TRUE, | |
6a488035 TO |
129 | ), |
130 | ), | |
ae5ffbb7 TO |
131 | 'filters' => array( |
132 | 'receive_date' => array( | |
133 | 'default' => 'this.year', | |
134 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
135 | ), | |
136 | 'currency' => array( | |
ccc29f8e | 137 | 'title' => ts('Currency'), |
ae5ffbb7 TO |
138 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
139 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
140 | 'default' => NULL, | |
141 | 'type' => CRM_Utils_Type::T_STRING, | |
142 | ), | |
143 | 'total_range' => array( | |
144 | 'title' => ts('Show no. of Top Donors'), | |
145 | 'type' => CRM_Utils_Type::T_INT, | |
146 | 'default_op' => 'eq', | |
147 | ), | |
148 | 'financial_type_id' => array( | |
149 | 'name' => 'financial_type_id', | |
150 | 'title' => ts('Financial Type'), | |
8ee006e7 | 151 | 'type' => CRM_Utils_Type::T_INT, |
ae5ffbb7 | 152 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
cbfbab2f | 153 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
ae5ffbb7 TO |
154 | ), |
155 | 'contribution_status_id' => array( | |
156 | 'title' => ts('Contribution Status'), | |
157 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
158 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
159 | 'default' => array(1), | |
6a488035 TO |
160 | ), |
161 | ), | |
ae5ffbb7 | 162 | ), |
bdfeefa3 E |
163 | 'civicrm_financial_trxn' => array( |
164 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
165 | 'fields' => array( | |
5e0343e8 | 166 | 'card_type_id' => array( |
d72b084a | 167 | 'title' => ts('Credit Card Type'), |
5e0343e8 | 168 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")', |
bdfeefa3 E |
169 | ), |
170 | ), | |
171 | 'filters' => array( | |
5e0343e8 | 172 | 'card_type_id' => array( |
d72b084a | 173 | 'title' => ts('Credit Card Type'), |
bdfeefa3 | 174 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
5e0343e8 | 175 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
bdfeefa3 E |
176 | 'default' => NULL, |
177 | 'type' => CRM_Utils_Type::T_STRING, | |
178 | ), | |
179 | ), | |
180 | ), | |
ae5ffbb7 TO |
181 | 'civicrm_email' => array( |
182 | 'dao' => 'CRM_Core_DAO_Email', | |
183 | 'fields' => array( | |
184 | 'email' => array( | |
185 | 'title' => ts('Email'), | |
186 | 'default' => TRUE, | |
187 | 'no_repeat' => TRUE, | |
188 | ), | |
189 | ), | |
190 | 'grouping' => 'email-fields', | |
191 | ), | |
192 | 'civicrm_phone' => array( | |
193 | 'dao' => 'CRM_Core_DAO_Phone', | |
194 | 'fields' => array( | |
195 | 'phone' => array( | |
196 | 'title' => ts('Phone'), | |
197 | 'default' => TRUE, | |
198 | 'no_repeat' => TRUE, | |
6a488035 TO |
199 | ), |
200 | ), | |
ae5ffbb7 TO |
201 | 'grouping' => 'phone-fields', |
202 | ), | |
203 | ); | |
6a488035 | 204 | |
16e2e80c | 205 | $this->_groupFilter = TRUE; |
6a488035 | 206 | $this->_tagFilter = TRUE; |
7a961f19 | 207 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
208 | parent::__construct(); |
209 | } | |
210 | ||
00be9182 | 211 | public function preProcess() { |
6a488035 TO |
212 | parent::preProcess(); |
213 | } | |
214 | ||
969a8733 | 215 | /** |
216 | * Select only contact ID when adding to group. | |
217 | * | |
218 | * @todo consider moving that to parent to support AddToGroup in general. | |
219 | */ | |
00be9182 | 220 | public function select() { |
969a8733 | 221 | parent::select(); |
6a488035 TO |
222 | } |
223 | ||
74cf4551 EM |
224 | /** |
225 | * @param $fields | |
226 | * @param $files | |
227 | * @param $self | |
228 | * | |
229 | * @return array | |
230 | */ | |
00be9182 | 231 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
232 | $errors = array(); |
233 | ||
234 | $op = CRM_Utils_Array::value('total_range_op', $fields); | |
235 | $val = CRM_Utils_Array::value('total_range_value', $fields); | |
236 | ||
237 | if (!in_array($op, array( | |
9d72cede | 238 | 'eq', |
21dfd5f5 | 239 | 'lte', |
9d72cede EM |
240 | )) |
241 | ) { | |
6a488035 TO |
242 | $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator"); |
243 | } | |
244 | ||
245 | if ($val && !CRM_Utils_Rule::positiveInteger($val)) { | |
246 | $errors['total_range_value'] = ts("Please enter positive number"); | |
247 | } | |
248 | return $errors; | |
249 | } | |
250 | ||
00be9182 | 251 | public function from() { |
6a488035 TO |
252 | $this->_from = " |
253 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
65f9ed10 E |
254 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
255 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
2f4c2f5d | 256 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
257 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id | |
6a488035 | 258 | AND {$this->_aliases['civicrm_email']}.is_primary = 1 |
2f4c2f5d | 259 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
6a488035 | 260 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
50757f01 | 261 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; |
bdfeefa3 E |
262 | |
263 | // for credit card type | |
264 | $this->addFinancialTrxnFromClause(); | |
265 | ||
21d62de7 | 266 | $this->addAddressFromClause(); |
6a488035 TO |
267 | } |
268 | ||
00be9182 | 269 | public function where() { |
6a488035 | 270 | $clauses = array(); |
93406a82 | 271 | $this->_tempClause = $this->_outerCluase = $this->_groupLimit = ''; |
6a488035 TO |
272 | foreach ($this->_columns as $tableName => $table) { |
273 | if (array_key_exists('filters', $table)) { | |
274 | foreach ($table['filters'] as $fieldName => $field) { | |
275 | $clause = NULL; | |
276 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
277 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
278 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
279 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
280 | |
281 | if ($relative || $from || $to) { | |
282 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
283 | } | |
284 | } | |
285 | else { | |
286 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
287 | if ($op) { | |
288 | $clause = $this->whereClause($field, | |
289 | $op, | |
290 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
291 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
292 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
293 | ); | |
294 | } | |
295 | } | |
296 | ||
297 | if (!empty($clause)) { | |
298 | if ($fieldName == 'total_range') { | |
299 | $value = CRM_Utils_Array::value("total_range_value", $this->_params); | |
300 | $this->_outerCluase = " WHERE (( @rows := @rows + 1) <= {$value}) "; | |
93406a82 | 301 | $this->_groupLimit = " LIMIT {$value}"; |
6a488035 TO |
302 | } |
303 | else { | |
304 | $clauses[] = $clause; | |
305 | } | |
306 | } | |
307 | } | |
308 | } | |
309 | } | |
310 | if (empty($clauses)) { | |
311 | $this->_where = "WHERE ( 1 ) "; | |
312 | } | |
313 | else { | |
314 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
315 | } | |
316 | ||
317 | if ($this->_aclWhere) { | |
318 | $this->_where .= " AND {$this->_aclWhere} "; | |
319 | } | |
320 | } | |
321 | ||
00be9182 | 322 | public function groupBy() { |
b708c08d | 323 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, array("{$this->_aliases['civicrm_contact']}.id", "{$this->_aliases['civicrm_contribution']}.currency")); |
6a488035 TO |
324 | } |
325 | ||
969a8733 | 326 | /** |
327 | * Build output rows. | |
328 | * | |
329 | * @param string $sql | |
330 | * @param array $rows | |
331 | */ | |
332 | public function buildRows($sql, &$rows) { | |
6a488035 TO |
333 | $setVariable = " SET @rows:=0, @rank=0 "; |
334 | CRM_Core_DAO::singleValueQuery($setVariable); | |
969a8733 | 335 | $sql = " |
336 | SELECT * FROM ( {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} | |
337 | ORDER BY civicrm_contribution_total_amount_sum DESC | |
338 | ) as abc {$this->_outerCluase} $this->_limit | |
339 | "; | |
340 | parent::buildRows($sql, $rows); | |
6a488035 TO |
341 | } |
342 | ||
74cf4551 | 343 | /** |
100fef9d | 344 | * @param int $groupID |
74cf4551 | 345 | */ |
00be9182 | 346 | public function add2group($groupID) { |
6a488035 | 347 | if (is_numeric($groupID)) { |
969a8733 | 348 | $this->_limit = $this->_groupLimit; |
349 | $rows = array(); | |
350 | $this->_columnHeaders['civicrm_contact_id'] = 1; | |
351 | $this->buildRows('', $rows); | |
6a488035 TO |
352 | |
353 | $contact_ids = array(); | |
354 | // Add resulting contacts to group | |
969a8733 | 355 | foreach ($rows as $row) { |
356 | $contact_ids[$row['civicrm_contact_id']] = $row['civicrm_contact_id']; | |
6a488035 TO |
357 | } |
358 | ||
359 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $groupID); | |
360 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
361 | } | |
362 | } | |
363 | ||
74cf4551 EM |
364 | /** |
365 | * @param int $rowCount | |
366 | */ | |
00be9182 | 367 | public function limit($rowCount = CRM_Report_Form::ROW_COUNT_LIMIT) { |
6a488035 TO |
368 | // lets do the pager if in html mode |
369 | $this->_limit = NULL; | |
74cf4551 | 370 | |
dbb4a0f9 PN |
371 | // CRM-14115, over-ride row count if rowCount is specified in URL |
372 | if ($this->_dashBoardRowCount) { | |
373 | $rowCount = $this->_dashBoardRowCount; | |
374 | } | |
969a8733 | 375 | if ($this->_outputMode == 'html') { |
ab432335 | 376 | // Replace only first occurrence of SELECT. |
6a488035 | 377 | $this->_select = preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select, 1); |
a3d827a7 | 378 | $pageId = CRM_Utils_Request::retrieve('crmPID', 'Integer'); |
6a488035 TO |
379 | |
380 | if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) { | |
381 | if (!isset($_POST['PagerBottomButton'])) { | |
382 | unset($_POST['crmPID_B']); | |
383 | } | |
384 | else { | |
9d72cede | 385 | $pageId = max((int) @$_POST['crmPID_B'], 1); |
6a488035 TO |
386 | } |
387 | } | |
388 | ||
389 | $pageId = $pageId ? $pageId : 1; | |
390 | $this->set(CRM_Utils_Pager::PAGE_ID, $pageId); | |
391 | $offset = ($pageId - 1) * $rowCount; | |
392 | ||
bf00d1b6 DL |
393 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
394 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
395 | ||
6a488035 TO |
396 | $this->_limit = " LIMIT $offset, " . $rowCount; |
397 | } | |
398 | } | |
399 | ||
74cf4551 | 400 | /** |
ced9bfed EM |
401 | * Alter display of rows. |
402 | * | |
403 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
404 | * such as rendering contacts as links. | |
405 | * | |
406 | * @param array $rows | |
407 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 408 | */ |
00be9182 | 409 | public function alterDisplay(&$rows) { |
6a488035 TO |
410 | $entryFound = FALSE; |
411 | $rank = 1; | |
412 | if (!empty($rows)) { | |
413 | foreach ($rows as $rowNum => $row) { | |
414 | ||
415 | $rows[$rowNum]['civicrm_donor_rank'] = $rank++; | |
416 | // convert display name to links | |
417 | if (array_key_exists('civicrm_contact_display_name', $row) && | |
9d72cede EM |
418 | array_key_exists('civicrm_contact_id', $row) && |
419 | !empty($row['civicrm_contribution_currency']) | |
420 | ) { | |
6a488035 | 421 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
9d72cede EM |
422 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . |
423 | "¤cy_value=" . $row['civicrm_contribution_currency'], | |
6a488035 TO |
424 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
425 | ); | |
426 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; | |
427 | $entryFound = TRUE; | |
428 | } | |
21d62de7 | 429 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
6a488035 | 430 | |
7d793900 | 431 | //handle gender |
432 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
433 | if ($value = $row['civicrm_contact_gender_id']) { | |
434 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
435 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; | |
436 | } | |
437 | $entryFound = TRUE; | |
70bea8e2 | 438 | } |
70bea8e2 | 439 | |
7d793900 | 440 | // display birthday in the configured custom format |
441 | if (array_key_exists('civicrm_contact_birth_date', $row)) { | |
442 | $birthDate = $row['civicrm_contact_birth_date']; | |
443 | if ($birthDate) { | |
444 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); | |
445 | } | |
446 | $entryFound = TRUE; | |
70bea8e2 | 447 | } |
70bea8e2 | 448 | |
5e0343e8 | 449 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
450 | $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 |
451 | $entryFound = TRUE; |
452 | } | |
453 | ||
6a488035 TO |
454 | // skip looking further in rows, if first row itself doesn't |
455 | // have the column we need | |
456 | if (!$entryFound) { | |
457 | break; | |
458 | } | |
459 | } | |
460 | } | |
461 | } | |
96025800 | 462 | |
6a488035 | 463 | } |