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',
48 function __construct() {
49 $this->_columns
= array(
52 'dao' => 'CRM_Contact_DAO_Contact',
61 array('title' => ts('Contact Name'),
65 'first_name' => array(
66 'title' => ts('First Name'),
69 'title' => ts('Last Name'),
73 'title' => ts('Contact Type'),
77 'title' => ts('Contact SubType'),
82 +
$this->getAddressColumns()
84 'civicrm_contribution' =>
86 'dao' => 'CRM_Contribute_DAO_Contribution',
90 array('title' => ts('Amount Statistics'),
93 array('sum' => ts('Aggregate Amount'),
94 'count' => ts('Donations'),
95 'avg' => ts('Average'),
99 array('required' => TRUE,
100 'no_display' => TRUE,
107 'default' => 'this.year',
108 'operatorType' => CRM_Report_Form
::OP_DATE
,
111 array('title' => 'Currency',
112 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
113 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
115 'type' => CRM_Utils_Type
::T_STRING
,
118 array('title' => ts('Show no. of Top Donors'),
119 'type' => CRM_Utils_Type
::T_INT
,
120 'default_op' => 'eq',
122 'financial_type_id' =>
123 array('name' => 'financial_type_id',
124 'title' => ts('Financial Type'),
125 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
126 'options' => CRM_Contribute_PseudoConstant
::financialType() ,
128 'contribution_status_id' =>
129 array('title' => ts('Donation Status'),
130 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
131 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
132 'default' => array(1),
138 'dao' => 'CRM_Core_DAO_Email',
142 array('title' => ts('Email'),
147 'grouping' => 'email-fields',
152 'dao' => 'CRM_Core_DAO_Phone',
156 array('title' => ts('Phone'),
161 'grouping' => 'phone-fields',
165 'dao' => 'CRM_Contact_DAO_GroupContact',
171 'name' => 'group_id',
172 'title' => ts('Group'),
173 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
175 'options' => CRM_Core_PseudoConstant
::group(),
181 $this->_tagFilter
= TRUE;
182 $this->_currencyColumn
= 'civicrm_contribution_currency';
183 parent
::__construct();
186 function preProcess() {
187 parent
::preProcess();
192 $this->_columnHeaders
= array();
193 //Headers for Rank column
194 $this->_columnHeaders
["civicrm_donor_rank"]['title'] = ts('Rank');
195 $this->_columnHeaders
["civicrm_donor_rank"]['type'] = 1;
196 //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank ";
198 foreach ($this->_columns
as $tableName => $table) {
199 if (array_key_exists('fields', $table)) {
200 foreach ($table['fields'] as $fieldName => $field) {
201 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
202 // only include statistics columns if set
203 if (!empty($field['statistics'])) {
204 foreach ($field['statistics'] as $stat => $label) {
205 switch (strtolower($stat)) {
207 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
208 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
209 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
210 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
214 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
215 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
216 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
217 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
221 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
222 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
223 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
224 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
231 // $field['type'] is not always set. Use string type as default if not set.
232 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = isset($field['type']) ?
$field['type'] : 2;
233 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
240 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
243 static function formRule($fields, $files, $self) {
246 $op = CRM_Utils_Array
::value('total_range_op', $fields);
247 $val = CRM_Utils_Array
::value('total_range_value', $fields);
249 if (!in_array($op, array(
251 $errors['total_range_op'] = ts("Please select 'Is equal to' OR 'Is Less than or equal to' operator");
254 if ($val && !CRM_Utils_Rule
::positiveInteger($val)) {
255 $errors['total_range_value'] = ts("Please enter positive number");
262 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
263 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
264 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0
265 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
266 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
267 AND {$this->_aliases['civicrm_email']}.is_primary = 1
268 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
269 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
270 {$this->_aliases['civicrm_phone']}.is_primary = 1
272 $this->addAddressFromClause();
277 $this->_tempClause
= $this->_outerCluase
= $this->_groupLimit
= '';
278 foreach ($this->_columns
as $tableName => $table) {
279 if (array_key_exists('filters', $table)) {
280 foreach ($table['filters'] as $fieldName => $field) {
282 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
283 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
284 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
285 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
287 if ($relative ||
$from ||
$to) {
288 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
292 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
294 $clause = $this->whereClause($field,
296 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
297 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
298 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
303 if (!empty($clause)) {
304 if ($fieldName == 'total_range') {
305 $value = CRM_Utils_Array
::value("total_range_value", $this->_params
);
306 $this->_outerCluase
= " WHERE (( @rows := @rows + 1) <= {$value}) ";
307 $this->_groupLimit
= " LIMIT {$value}";
310 $clauses[] = $clause;
316 if (empty($clauses)) {
317 $this->_where
= "WHERE ( 1 ) ";
320 $this->_where
= "WHERE " . implode(' AND ', $clauses);
323 if ($this->_aclWhere
) {
324 $this->_where
.= " AND {$this->_aclWhere} ";
329 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
332 function postProcess() {
334 $this->beginPostProcess();
336 // get the acl clauses built before we assemble the query
337 $this->buildACLClause($this->_aliases
['civicrm_contact']);
350 //set the variable value rank, rows = 0
351 $setVariable = " SET @rows:=0, @rank=0 ";
352 CRM_Core_DAO
::singleValueQuery($setVariable);
354 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
355 ORDER BY civicrm_contribution_total_amount_sum DESC
356 ) as abc {$this->_outerCluase} $this->_limit
359 $dao = CRM_Core_DAO
::executeQuery($sql);
361 while ($dao->fetch()) {
363 foreach ($this->_columnHeaders
as $key => $value) {
364 if (property_exists($dao, $key)) {
365 $row[$key] = $dao->$key;
370 $this->formatDisplay($rows);
372 $this->doTemplateAssignment($rows);
374 $this->endPostProcess($rows);
377 function add2group($groupID) {
378 if (is_numeric($groupID)) {
381 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
382 ORDER BY civicrm_contribution_total_amount_sum DESC
383 ) as abc {$this->_groupLimit}";
384 $dao = CRM_Core_DAO
::executeQuery($sql);
386 $contact_ids = array();
387 // Add resulting contacts to group
388 while ($dao->fetch()) {
389 $contact_ids[$dao->civicrm_contact_id
] = $dao->civicrm_contact_id
;
392 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
393 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
397 function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
398 // lets do the pager if in html mode
399 $this->_limit
= NULL;
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;
425 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) && !empty($row['civicrm_contribution_currency'])) {
437 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
438 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . "¤cy_value=" . $row['civicrm_contribution_currency'],
439 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
441 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
444 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
446 // skip looking further in rows, if first row itself doesn't
447 // have the column we need