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 $this->_groupFilter
= TRUE;
172 $this->_tagFilter
= TRUE;
173 $this->_currencyColumn
= 'civicrm_contribution_currency';
174 parent
::__construct();
177 function preProcess() {
178 parent
::preProcess();
183 $this->_columnHeaders
= array();
184 //Headers for Rank column
185 $this->_columnHeaders
["civicrm_donor_rank"]['title'] = ts('Rank');
186 $this->_columnHeaders
["civicrm_donor_rank"]['type'] = 1;
187 //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank ";
189 foreach ($this->_columns
as $tableName => $table) {
190 if (array_key_exists('fields', $table)) {
191 foreach ($table['fields'] as $fieldName => $field) {
192 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
193 // only include statistics columns if set
194 if (!empty($field['statistics'])) {
195 foreach ($field['statistics'] as $stat => $label) {
196 switch (strtolower($stat)) {
198 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
199 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
200 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
201 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
205 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
206 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
207 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
208 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
212 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
213 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
214 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
215 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
221 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
222 // $field['type'] is not always set. Use string type as default if not set.
223 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = isset($field['type']) ?
$field['type'] : 2;
224 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
231 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
241 static function formRule($fields, $files, $self) {
244 $op = CRM_Utils_Array
::value('total_range_op', $fields);
245 $val = CRM_Utils_Array
::value('total_range_value', $fields);
247 if (!in_array($op, array(
249 $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator");
252 if ($val && !CRM_Utils_Rule
::positiveInteger($val)) {
253 $errors['total_range_value'] = ts("Please enter positive number");
260 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
261 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
262 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0
263 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
264 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
265 AND {$this->_aliases['civicrm_email']}.is_primary = 1
266 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
267 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
268 {$this->_aliases['civicrm_phone']}.is_primary = 1
270 $this->addAddressFromClause();
275 $this->_tempClause
= $this->_outerCluase
= $this->_groupLimit
= '';
276 foreach ($this->_columns
as $tableName => $table) {
277 if (array_key_exists('filters', $table)) {
278 foreach ($table['filters'] as $fieldName => $field) {
280 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
281 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
282 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
283 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
285 if ($relative ||
$from ||
$to) {
286 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
290 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
292 $clause = $this->whereClause($field,
294 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
295 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
296 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
301 if (!empty($clause)) {
302 if ($fieldName == 'total_range') {
303 $value = CRM_Utils_Array
::value("total_range_value", $this->_params
);
304 $this->_outerCluase
= " WHERE (( @rows := @rows + 1) <= {$value}) ";
305 $this->_groupLimit
= " LIMIT {$value}";
308 $clauses[] = $clause;
314 if (empty($clauses)) {
315 $this->_where
= "WHERE ( 1 ) ";
318 $this->_where
= "WHERE " . implode(' AND ', $clauses);
321 if ($this->_aclWhere
) {
322 $this->_where
.= " AND {$this->_aclWhere} ";
327 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
330 function postProcess() {
332 $this->beginPostProcess();
334 // get the acl clauses built before we assemble the query
335 $this->buildACLClause($this->_aliases
['civicrm_contact']);
348 //set the variable value rank, rows = 0
349 $setVariable = " SET @rows:=0, @rank=0 ";
350 CRM_Core_DAO
::singleValueQuery($setVariable);
352 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
353 ORDER BY civicrm_contribution_total_amount_sum DESC
354 ) as abc {$this->_outerCluase} $this->_limit
357 $dao = CRM_Core_DAO
::executeQuery($sql);
359 while ($dao->fetch()) {
361 foreach ($this->_columnHeaders
as $key => $value) {
362 if (property_exists($dao, $key)) {
363 $row[$key] = $dao->$key;
368 $this->formatDisplay($rows);
370 $this->doTemplateAssignment($rows);
372 $this->endPostProcess($rows);
378 function add2group($groupID) {
379 if (is_numeric($groupID)) {
382 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
383 ORDER BY civicrm_contribution_total_amount_sum DESC
384 ) as abc {$this->_groupLimit}";
385 $dao = CRM_Core_DAO
::executeQuery($sql);
387 $contact_ids = array();
388 // Add resulting contacts to group
389 while ($dao->fetch()) {
390 $contact_ids[$dao->civicrm_contact_id
] = $dao->civicrm_contact_id
;
393 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
394 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
399 * @param int $rowCount
401 function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
402 // lets do the pager if in html mode
403 $this->_limit
= NULL;
405 // CRM-14115, over-ride row count if rowCount is specified in URL
406 if ($this->_dashBoardRowCount
) {
407 $rowCount = $this->_dashBoardRowCount
;
409 if ($this->_outputMode
== 'html' ||
$this->_outputMode
== 'group') {
410 //replace only first occurence of SELECT
411 $this->_select
= preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
, 1);
412 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
414 if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) {
415 if (!isset($_POST['PagerBottomButton'])) {
416 unset($_POST['crmPID_B']);
419 $pageId = max((int)@$_POST['crmPID_B'], 1);
423 $pageId = $pageId ?
$pageId : 1;
424 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
425 $offset = ($pageId - 1) * $rowCount;
427 $offset = CRM_Utils_Type
::escape($offset, 'Int');
428 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
430 $this->_limit
= " LIMIT $offset, " . $rowCount;
437 function alterDisplay(&$rows) {
438 // custom code to alter rows
443 foreach ($rows as $rowNum => $row) {
445 $rows[$rowNum]['civicrm_donor_rank'] = $rank++
;
446 // convert display name to links
447 if (array_key_exists('civicrm_contact_display_name', $row) &&
448 array_key_exists('civicrm_contact_id', $row) && !empty($row['civicrm_contribution_currency'])) {
449 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
450 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . "¤cy_value=" . $row['civicrm_contribution_currency'],
451 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
453 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
456 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
458 // skip looking further in rows, if first row itself doesn't
459 // have the column we need