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(
58 'dao' => 'CRM_Contact_DAO_Contact',
67 array('title' => ts('Contact Name'),
71 'first_name' => array(
72 'title' => ts('First Name'),
75 'title' => ts('Last Name'),
79 'title' => ts('Contact Type'),
83 'title' => ts('Contact SubType'),
88 +
$this->getAddressColumns()
90 'civicrm_contribution' =>
92 'dao' => 'CRM_Contribute_DAO_Contribution',
96 array('title' => ts('Amount Statistics'),
99 array('sum' => ts('Aggregate Amount'),
100 'count' => ts('Donations'),
101 'avg' => ts('Average'),
105 array('required' => TRUE,
106 'no_display' => TRUE,
113 'default' => 'this.year',
114 'operatorType' => CRM_Report_Form
::OP_DATE
,
117 array('title' => 'Currency',
118 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
119 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
121 'type' => CRM_Utils_Type
::T_STRING
,
124 array('title' => ts('Show no. of Top Donors'),
125 'type' => CRM_Utils_Type
::T_INT
,
126 'default_op' => 'eq',
128 'financial_type_id' =>
129 array('name' => 'financial_type_id',
130 'title' => ts('Financial Type'),
131 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
132 'options' => CRM_Contribute_PseudoConstant
::financialType() ,
134 'contribution_status_id' =>
135 array('title' => ts('Donation Status'),
136 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
137 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
138 'default' => array(1),
144 'dao' => 'CRM_Core_DAO_Email',
148 array('title' => ts('Email'),
153 'grouping' => 'email-fields',
158 'dao' => 'CRM_Core_DAO_Phone',
162 array('title' => ts('Phone'),
167 'grouping' => 'phone-fields',
171 'dao' => 'CRM_Contact_DAO_GroupContact',
177 'name' => 'group_id',
178 'title' => ts('Group'),
179 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
181 'options' => CRM_Core_PseudoConstant
::group(),
187 $this->_tagFilter
= TRUE;
188 $this->_currencyColumn
= 'civicrm_contribution_currency';
189 parent
::__construct();
192 function preProcess() {
193 parent
::preProcess();
198 $this->_columnHeaders
= array();
199 //Headers for Rank column
200 $this->_columnHeaders
["civicrm_donor_rank"]['title'] = ts('Rank');
201 $this->_columnHeaders
["civicrm_donor_rank"]['type'] = 1;
202 //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank ";
204 foreach ($this->_columns
as $tableName => $table) {
205 if (array_key_exists('fields', $table)) {
206 foreach ($table['fields'] as $fieldName => $field) {
207 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
208 // only include statistics columns if set
209 if (!empty($field['statistics'])) {
210 foreach ($field['statistics'] as $stat => $label) {
211 switch (strtolower($stat)) {
213 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
214 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
215 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
216 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
220 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
221 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
222 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
223 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
227 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
228 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
229 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
230 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
236 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
237 // $field['type'] is not always set. Use string type as default if not set.
238 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = isset($field['type']) ?
$field['type'] : 2;
239 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
246 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
256 static function formRule($fields, $files, $self) {
259 $op = CRM_Utils_Array
::value('total_range_op', $fields);
260 $val = CRM_Utils_Array
::value('total_range_value', $fields);
262 if (!in_array($op, array(
264 $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator");
267 if ($val && !CRM_Utils_Rule
::positiveInteger($val)) {
268 $errors['total_range_value'] = ts("Please enter positive number");
275 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
276 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
277 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0
278 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
279 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
280 AND {$this->_aliases['civicrm_email']}.is_primary = 1
281 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
282 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
283 {$this->_aliases['civicrm_phone']}.is_primary = 1
285 $this->addAddressFromClause();
290 $this->_tempClause
= $this->_outerCluase
= $this->_groupLimit
= '';
291 foreach ($this->_columns
as $tableName => $table) {
292 if (array_key_exists('filters', $table)) {
293 foreach ($table['filters'] as $fieldName => $field) {
295 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
296 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
297 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
298 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
300 if ($relative ||
$from ||
$to) {
301 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
305 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
307 $clause = $this->whereClause($field,
309 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
310 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
311 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
316 if (!empty($clause)) {
317 if ($fieldName == 'total_range') {
318 $value = CRM_Utils_Array
::value("total_range_value", $this->_params
);
319 $this->_outerCluase
= " WHERE (( @rows := @rows + 1) <= {$value}) ";
320 $this->_groupLimit
= " LIMIT {$value}";
323 $clauses[] = $clause;
329 if (empty($clauses)) {
330 $this->_where
= "WHERE ( 1 ) ";
333 $this->_where
= "WHERE " . implode(' AND ', $clauses);
336 if ($this->_aclWhere
) {
337 $this->_where
.= " AND {$this->_aclWhere} ";
342 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
345 function postProcess() {
347 $this->beginPostProcess();
349 // get the acl clauses built before we assemble the query
350 $this->buildACLClause($this->_aliases
['civicrm_contact']);
363 //set the variable value rank, rows = 0
364 $setVariable = " SET @rows:=0, @rank=0 ";
365 CRM_Core_DAO
::singleValueQuery($setVariable);
367 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
368 ORDER BY civicrm_contribution_total_amount_sum DESC
369 ) as abc {$this->_outerCluase} $this->_limit
372 $dao = CRM_Core_DAO
::executeQuery($sql);
374 while ($dao->fetch()) {
376 foreach ($this->_columnHeaders
as $key => $value) {
377 if (property_exists($dao, $key)) {
378 $row[$key] = $dao->$key;
383 $this->formatDisplay($rows);
385 $this->doTemplateAssignment($rows);
387 $this->endPostProcess($rows);
393 function add2group($groupID) {
394 if (is_numeric($groupID)) {
397 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
398 ORDER BY civicrm_contribution_total_amount_sum DESC
399 ) as abc {$this->_groupLimit}";
400 $dao = CRM_Core_DAO
::executeQuery($sql);
402 $contact_ids = array();
403 // Add resulting contacts to group
404 while ($dao->fetch()) {
405 $contact_ids[$dao->civicrm_contact_id
] = $dao->civicrm_contact_id
;
408 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
409 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
414 * @param int $rowCount
416 function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
417 // lets do the pager if in html mode
418 $this->_limit
= NULL;
420 // CRM-14115, over-ride row count if rowCount is specified in URL
421 if ($this->_dashBoardRowCount
) {
422 $rowCount = $this->_dashBoardRowCount
;
424 if ($this->_outputMode
== 'html' ||
$this->_outputMode
== 'group') {
425 //replace only first occurence of SELECT
426 $this->_select
= preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
, 1);
427 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
429 if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) {
430 if (!isset($_POST['PagerBottomButton'])) {
431 unset($_POST['crmPID_B']);
434 $pageId = max((int)@$_POST['crmPID_B'], 1);
438 $pageId = $pageId ?
$pageId : 1;
439 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
440 $offset = ($pageId - 1) * $rowCount;
442 $offset = CRM_Utils_Type
::escape($offset, 'Int');
443 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
445 $this->_limit
= " LIMIT $offset, " . $rowCount;
452 function alterDisplay(&$rows) {
453 // custom code to alter rows
458 foreach ($rows as $rowNum => $row) {
460 $rows[$rowNum]['civicrm_donor_rank'] = $rank++
;
461 // convert display name to links
462 if (array_key_exists('civicrm_contact_display_name', $row) &&
463 array_key_exists('civicrm_contact_id', $row) && !empty($row['civicrm_contribution_currency'])) {
464 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
465 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . "¤cy_value=" . $row['civicrm_contribution_currency'],
466 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
468 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
471 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
473 // skip looking further in rows, if first row itself doesn't
474 // have the column we need