4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contribute_TopDonor
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
42 protected $_charts = array(
44 'barChart' => 'Bar Chart',
45 'pieChart' => 'Pie Chart',
54 function __construct() {
55 $this->_columns
= array(
56 'civicrm_contact' => array(
57 'dao' => 'CRM_Contact_DAO_Contact',
63 'display_name' => array(
64 'title' => ts('Contact Name'),
68 'first_name' => array(
69 'title' => ts('First Name'),
72 'title' => ts('Last Name'),
74 'contact_type' => array(
75 'title' => ts('Contact Type'),
77 'contact_sub_type' => array(
78 'title' => ts('Contact Subtype'),
83 +
$this->getAddressColumns()
85 'civicrm_contribution' => array(
86 'dao' => 'CRM_Contribute_DAO_Contribution',
88 'total_amount' => array(
89 'title' => ts('Amount Statistics'),
91 'statistics' => array(
92 'sum' => ts('Aggregate Amount'),
93 'count' => ts('Donations'),
94 'avg' => ts('Average'),
103 'receive_date' => array(
104 'default' => 'this.year',
105 'operatorType' => CRM_Report_Form
::OP_DATE
,
108 'title' => 'Currency',
109 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
110 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
112 'type' => CRM_Utils_Type
::T_STRING
,
114 'total_range' => array(
115 'title' => ts('Show no. of Top Donors'),
116 'type' => CRM_Utils_Type
::T_INT
,
117 'default_op' => 'eq',
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(),
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),
133 'civicrm_email' => array(
134 'dao' => 'CRM_Core_DAO_Email',
137 'title' => ts('Email'),
142 'grouping' => 'email-fields',
144 'civicrm_phone' => array(
145 'dao' => 'CRM_Core_DAO_Phone',
148 'title' => ts('Phone'),
153 'grouping' => 'phone-fields',
157 $this->_groupFilter
= TRUE;
158 $this->_tagFilter
= TRUE;
159 $this->_currencyColumn
= 'civicrm_contribution_currency';
160 parent
::__construct();
163 function preProcess() {
164 parent
::preProcess();
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 ";
175 foreach ($this->_columns
as $tableName => $table) {
176 if (array_key_exists('fields', $table)) {
177 foreach ($table['fields'] as $fieldName => $field) {
178 if (!empty($field['required']) ||
179 !empty($this->_params
['fields'][$fieldName])
181 // only include statistics columns if set
182 if (!empty($field['statistics'])) {
183 foreach ($field['statistics'] as $stat => $label) {
184 switch (strtolower($stat)) {
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}";
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}";
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}";
209 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
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;
212 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
219 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
229 static function formRule($fields, $files, $self) {
232 $op = CRM_Utils_Array
::value('total_range_op', $fields);
233 $val = CRM_Utils_Array
::value('total_range_value', $fields);
235 if (!in_array($op, array(
240 $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator");
243 if ($val && !CRM_Utils_Rule
::positiveInteger($val)) {
244 $errors['total_range_value'] = ts("Please enter positive number");
251 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
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
254 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
255 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
256 AND {$this->_aliases['civicrm_email']}.is_primary = 1
257 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
258 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
259 {$this->_aliases['civicrm_phone']}.is_primary = 1
261 $this->addAddressFromClause();
266 $this->_tempClause
= $this->_outerCluase
= $this->_groupLimit
= '';
267 foreach ($this->_columns
as $tableName => $table) {
268 if (array_key_exists('filters', $table)) {
269 foreach ($table['filters'] as $fieldName => $field) {
271 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
272 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
273 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
274 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
276 if ($relative ||
$from ||
$to) {
277 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
281 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
283 $clause = $this->whereClause($field,
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
)
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}) ";
296 $this->_groupLimit
= " LIMIT {$value}";
299 $clauses[] = $clause;
305 if (empty($clauses)) {
306 $this->_where
= "WHERE ( 1 ) ";
309 $this->_where
= "WHERE " . implode(' AND ', $clauses);
312 if ($this->_aclWhere
) {
313 $this->_where
.= " AND {$this->_aclWhere} ";
318 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
321 function postProcess() {
323 $this->beginPostProcess();
325 // get the acl clauses built before we assemble the query
326 $this->buildACLClause($this->_aliases
['civicrm_contact']);
339 //set the variable value rank, rows = 0
340 $setVariable = " SET @rows:=0, @rank=0 ";
341 CRM_Core_DAO
::singleValueQuery($setVariable);
343 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
344 ORDER BY civicrm_contribution_total_amount_sum DESC
345 ) as abc {$this->_outerCluase} $this->_limit
348 $dao = CRM_Core_DAO
::executeQuery($sql);
350 while ($dao->fetch()) {
352 foreach ($this->_columnHeaders
as $key => $value) {
353 if (property_exists($dao, $key)) {
354 $row[$key] = $dao->$key;
359 $this->formatDisplay($rows);
361 $this->doTemplateAssignment($rows);
363 $this->endPostProcess($rows);
367 * @param int $groupID
369 function add2group($groupID) {
370 if (is_numeric($groupID)) {
373 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
374 ORDER BY civicrm_contribution_total_amount_sum DESC
375 ) as abc {$this->_groupLimit}";
376 $dao = CRM_Core_DAO
::executeQuery($sql);
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
;
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');
390 * @param int $rowCount
392 function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
393 // lets do the pager if in html mode
394 $this->_limit
= NULL;
396 // CRM-14115, over-ride row count if rowCount is specified in URL
397 if ($this->_dashBoardRowCount
) {
398 $rowCount = $this->_dashBoardRowCount
;
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);
405 if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) {
406 if (!isset($_POST['PagerBottomButton'])) {
407 unset($_POST['crmPID_B']);
410 $pageId = max((int) @$_POST['crmPID_B'], 1);
414 $pageId = $pageId ?
$pageId : 1;
415 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
416 $offset = ($pageId - 1) * $rowCount;
418 $offset = CRM_Utils_Type
::escape($offset, 'Int');
419 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
421 $this->_limit
= " LIMIT $offset, " . $rowCount;
428 function alterDisplay(&$rows) {
429 // custom code to alter rows
434 foreach ($rows as $rowNum => $row) {
436 $rows[$rowNum]['civicrm_donor_rank'] = $rank++
;
437 // convert display name to links
438 if (array_key_exists('civicrm_contact_display_name', $row) &&
439 array_key_exists('civicrm_contact_id', $row) &&
440 !empty($row['civicrm_contribution_currency'])
442 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
443 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] .
444 "¤cy_value=" . $row['civicrm_contribution_currency'],
445 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
447 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
450 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
452 // skip looking further in rows, if first row itself doesn't
453 // have the column we need