4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
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',
52 public function __construct() {
53 $this->_columns
= array(
54 'civicrm_contact' => array(
55 'dao' => 'CRM_Contact_DAO_Contact',
61 'display_name' => array(
62 'title' => ts('Contact Name'),
66 'first_name' => array(
67 'title' => ts('First Name'),
70 'title' => ts('Last Name'),
72 'contact_type' => array(
73 'title' => ts('Contact Type'),
75 'contact_sub_type' => array(
76 'title' => ts('Contact Subtype'),
81 +
$this->getAddressColumns()
83 'civicrm_contribution' => array(
84 'dao' => 'CRM_Contribute_DAO_Contribution',
86 'total_amount' => array(
87 'title' => ts('Amount Statistics'),
89 'statistics' => array(
90 'sum' => ts('Aggregate Amount'),
91 'count' => ts('Donations'),
92 'avg' => ts('Average'),
101 'receive_date' => array(
102 'default' => 'this.year',
103 'operatorType' => CRM_Report_Form
::OP_DATE
,
106 'title' => 'Currency',
107 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
108 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
110 'type' => CRM_Utils_Type
::T_STRING
,
112 'total_range' => array(
113 'title' => ts('Show no. of Top Donors'),
114 'type' => CRM_Utils_Type
::T_INT
,
115 'default_op' => 'eq',
117 'financial_type_id' => array(
118 'name' => 'financial_type_id',
119 'title' => ts('Financial Type'),
120 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
121 'options' => CRM_Contribute_PseudoConstant
::financialType(),
123 'contribution_status_id' => array(
124 'title' => ts('Contribution Status'),
125 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
126 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
127 'default' => array(1),
131 'civicrm_email' => array(
132 'dao' => 'CRM_Core_DAO_Email',
135 'title' => ts('Email'),
140 'grouping' => 'email-fields',
142 'civicrm_phone' => array(
143 'dao' => 'CRM_Core_DAO_Phone',
146 'title' => ts('Phone'),
151 'grouping' => 'phone-fields',
155 $this->_groupFilter
= TRUE;
156 $this->_tagFilter
= TRUE;
157 $this->_currencyColumn
= 'civicrm_contribution_currency';
158 parent
::__construct();
161 public function preProcess() {
162 parent
::preProcess();
165 public function select() {
167 $this->_columnHeaders
= array();
168 //Headers for Rank column
169 $this->_columnHeaders
["civicrm_donor_rank"]['title'] = ts('Rank');
170 $this->_columnHeaders
["civicrm_donor_rank"]['type'] = 1;
171 //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank ";
173 foreach ($this->_columns
as $tableName => $table) {
174 if (array_key_exists('fields', $table)) {
175 foreach ($table['fields'] as $fieldName => $field) {
176 if (!empty($field['required']) ||
177 !empty($this->_params
['fields'][$fieldName])
179 // only include statistics columns if set
180 if (!empty($field['statistics'])) {
181 foreach ($field['statistics'] as $stat => $label) {
182 switch (strtolower($stat)) {
184 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
185 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
186 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
187 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
191 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
192 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
193 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
194 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
198 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
199 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
200 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
201 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
207 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
208 // $field['type'] is not always set. Use string type as default if not set.
209 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = isset($field['type']) ?
$field['type'] : 2;
210 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
217 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
227 public static function formRule($fields, $files, $self) {
230 $op = CRM_Utils_Array
::value('total_range_op', $fields);
231 $val = CRM_Utils_Array
::value('total_range_value', $fields);
233 if (!in_array($op, array(
238 $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator");
241 if ($val && !CRM_Utils_Rule
::positiveInteger($val)) {
242 $errors['total_range_value'] = ts("Please enter positive number");
247 public function from() {
249 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
250 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
251 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0
252 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
253 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
254 AND {$this->_aliases['civicrm_email']}.is_primary = 1
255 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
256 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
257 {$this->_aliases['civicrm_phone']}.is_primary = 1
259 $this->addAddressFromClause();
262 public function where() {
264 $this->_tempClause
= $this->_outerCluase
= $this->_groupLimit
= '';
265 foreach ($this->_columns
as $tableName => $table) {
266 if (array_key_exists('filters', $table)) {
267 foreach ($table['filters'] as $fieldName => $field) {
269 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
270 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
271 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
272 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
274 if ($relative ||
$from ||
$to) {
275 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
279 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
281 $clause = $this->whereClause($field,
283 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
284 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
285 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
290 if (!empty($clause)) {
291 if ($fieldName == 'total_range') {
292 $value = CRM_Utils_Array
::value("total_range_value", $this->_params
);
293 $this->_outerCluase
= " WHERE (( @rows := @rows + 1) <= {$value}) ";
294 $this->_groupLimit
= " LIMIT {$value}";
297 $clauses[] = $clause;
303 if (empty($clauses)) {
304 $this->_where
= "WHERE ( 1 ) ";
307 $this->_where
= "WHERE " . implode(' AND ', $clauses);
310 if ($this->_aclWhere
) {
311 $this->_where
.= " AND {$this->_aclWhere} ";
315 public function groupBy() {
316 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
319 public function postProcess() {
321 $this->beginPostProcess();
323 // get the acl clauses built before we assemble the query
324 $this->buildACLClause($this->_aliases
['civicrm_contact']);
336 //set the variable value rank, rows = 0
337 $setVariable = " SET @rows:=0, @rank=0 ";
338 CRM_Core_DAO
::singleValueQuery($setVariable);
340 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
341 ORDER BY civicrm_contribution_total_amount_sum DESC
342 ) as abc {$this->_outerCluase} $this->_limit
345 $dao = CRM_Core_DAO
::executeQuery($sql);
347 while ($dao->fetch()) {
349 foreach ($this->_columnHeaders
as $key => $value) {
350 if (property_exists($dao, $key)) {
351 $row[$key] = $dao->$key;
356 $this->formatDisplay($rows);
358 $this->doTemplateAssignment($rows);
360 $this->endPostProcess($rows);
364 * @param int $groupID
366 public function add2group($groupID) {
367 if (is_numeric($groupID)) {
370 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
371 ORDER BY civicrm_contribution_total_amount_sum DESC
372 ) as abc {$this->_groupLimit}";
373 $dao = CRM_Core_DAO
::executeQuery($sql);
375 $contact_ids = array();
376 // Add resulting contacts to group
377 while ($dao->fetch()) {
378 $contact_ids[$dao->civicrm_contact_id
] = $dao->civicrm_contact_id
;
381 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
382 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
387 * @param int $rowCount
389 public function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
390 // lets do the pager if in html mode
391 $this->_limit
= NULL;
393 // CRM-14115, over-ride row count if rowCount is specified in URL
394 if ($this->_dashBoardRowCount
) {
395 $rowCount = $this->_dashBoardRowCount
;
397 if ($this->_outputMode
== 'html' ||
$this->_outputMode
== 'group') {
398 //replace only first occurence of SELECT
399 $this->_select
= preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
, 1);
400 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
402 if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) {
403 if (!isset($_POST['PagerBottomButton'])) {
404 unset($_POST['crmPID_B']);
407 $pageId = max((int) @$_POST['crmPID_B'], 1);
411 $pageId = $pageId ?
$pageId : 1;
412 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
413 $offset = ($pageId - 1) * $rowCount;
415 $offset = CRM_Utils_Type
::escape($offset, 'Int');
416 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
418 $this->_limit
= " LIMIT $offset, " . $rowCount;
425 public function alterDisplay(&$rows) {
426 // custom code to alter rows
431 foreach ($rows as $rowNum => $row) {
433 $rows[$rowNum]['civicrm_donor_rank'] = $rank++
;
434 // convert display name to links
435 if (array_key_exists('civicrm_contact_display_name', $row) &&
436 array_key_exists('civicrm_contact_id', $row) &&
437 !empty($row['civicrm_contribution_currency'])
439 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
440 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] .
441 "¤cy_value=" . $row['civicrm_contribution_currency'],
442 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
444 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
447 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
449 // skip looking further in rows, if first row itself doesn't
450 // have the column we need