5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Contribute_TopDonor
extends CRM_Report_Form
{
39 protected $_summary = NULL;
41 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43 protected $_charts = array(
45 'barChart' => 'Bar Chart',
46 'pieChart' => 'Pie Chart',
49 function __construct() {
50 $this->_columns
= array(
53 'dao' => 'CRM_Contact_DAO_Contact',
62 array('title' => ts('Contact Name'),
66 'first_name' => array('title' => ts('First Name'),
68 'last_name' => array('title' => ts('Last Name'),
72 'civicrm_contribution' =>
74 'dao' => 'CRM_Contribute_DAO_Contribution',
78 array('title' => ts('Amount Statistics'),
81 array('sum' => ts('Aggregate Amount'),
82 'count' => ts('Donations'),
83 'avg' => ts('Average'),
87 array('required' => TRUE,
95 'default' => 'this.year',
96 'operatorType' => CRM_Report_Form
::OP_DATE
,
99 array('title' => 'Currency',
100 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
101 'options' => CRM_Core_PseudoConstant
::currencySymbols('name','name'),
102 'type' => CRM_Utils_Type
::T_STRING
,
105 array('title' => ts('Show no. of Top Donors'),
106 'type' => CRM_Utils_Type
::T_INT
,
107 'default_op' => 'eq',
109 'financial_type_id' =>
110 array('name' => 'financial_type_id',
111 'title' => ts('Financial Type'),
112 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
113 'options' => CRM_Contribute_PseudoConstant
::financialType() ,
115 'contribution_status_id' =>
116 array('title' => ts('Donation Status'),
117 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
118 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
119 'default' => array(1),
125 'dao' => 'CRM_Core_DAO_Email',
129 array('title' => ts('Email'),
134 'grouping' => 'email-fields',
139 'dao' => 'CRM_Core_DAO_Phone',
143 array('title' => ts('Phone'),
148 'grouping' => 'phone-fields',
152 'dao' => 'CRM_Contact_DAO_GroupContact',
158 'name' => 'group_id',
159 'title' => ts('Group'),
160 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
162 'options' => CRM_Core_PseudoConstant
::group(),
168 $this->_tagFilter
= TRUE;
169 $this->_currencyColumn
= 'civicrm_contribution_currency';
170 parent
::__construct();
173 function preProcess() {
174 parent
::preProcess();
179 $this->_columnHeaders
= array();
180 //Headers for Rank column
181 $this->_columnHeaders
["civicrm_donor_rank"]['title'] = ts('Rank');
182 $this->_columnHeaders
["civicrm_donor_rank"]['type'] = 1;
183 //$select[] ="(@rank:=@rank+1) as civicrm_donor_rank ";
185 foreach ($this->_columns
as $tableName => $table) {
186 if (array_key_exists('fields', $table)) {
187 foreach ($table['fields'] as $fieldName => $field) {
188 if (CRM_Utils_Array
::value('required', $field) ||
189 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
191 // only include statistics columns if set
192 if (CRM_Utils_Array
::value('statistics', $field)) {
193 foreach ($field['statistics'] as $stat => $label) {
194 switch (strtolower($stat)) {
196 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
197 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
198 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
199 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
203 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
204 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
205 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
206 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
210 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
211 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
212 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
213 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
219 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
220 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
221 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
227 $this->_select
= " SELECT * FROM ( SELECT " . implode(', ', $select) . " ";
230 static function formRule($fields, $files, $self) {
233 $op = CRM_Utils_Array
::value('total_range_op', $fields);
234 $val = CRM_Utils_Array
::value('total_range_value', $fields);
236 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");
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
263 $this->_tempClause
= $this->_outerCluase
= '';
264 foreach ($this->_columns
as $tableName => $table) {
265 if (array_key_exists('filters', $table)) {
266 foreach ($table['filters'] as $fieldName => $field) {
268 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
269 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
270 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
271 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
273 if ($relative ||
$from ||
$to) {
274 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
278 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
280 $clause = $this->whereClause($field,
282 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
283 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
284 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
289 if (!empty($clause)) {
290 if ($fieldName == 'total_range') {
291 $value = CRM_Utils_Array
::value("total_range_value", $this->_params
);
292 $this->_outerCluase
= " WHERE (( @rows := @rows + 1) <= {$value}) ";
295 $clauses[] = $clause;
301 if (empty($clauses)) {
302 $this->_where
= "WHERE ( 1 ) ";
305 $this->_where
= "WHERE " . implode(' AND ', $clauses);
308 if ($this->_aclWhere
) {
309 $this->_where
.= " AND {$this->_aclWhere} ";
314 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.currency";
317 function postProcess() {
319 $this->beginPostProcess();
321 // get the acl clauses built before we assemble the query
322 $this->buildACLClause($this->_aliases
['civicrm_contact']);
335 //set the variable value rank, rows = 0
336 $setVariable = " SET @rows:=0, @rank=0 ";
337 CRM_Core_DAO
::singleValueQuery($setVariable);
339 $sql = " {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
340 ORDER BY civicrm_contribution_total_amount_sum DESC
341 ) as abc {$this->_outerCluase} $this->_limit
344 $dao = CRM_Core_DAO
::executeQuery($sql);
346 while ($dao->fetch()) {
348 foreach ($this->_columnHeaders
as $key => $value) {
349 if (property_exists($dao, $key)) {
350 $row[$key] = $dao->$key;
355 $this->formatDisplay($rows);
357 $this->doTemplateAssignment($rows);
359 $this->endPostProcess($rows);
362 function add2group($groupID) {
363 if (is_numeric($groupID)) {
366 {$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}
367 ORDER BY civicrm_contribution_total_amount_sum DESC
368 ) as abc {$this->_outerCluase}";
369 $dao = CRM_Core_DAO
::executeQuery($sql);
371 $contact_ids = array();
372 // Add resulting contacts to group
373 while ($dao->fetch()) {
374 $contact_ids[$dao->civicrm_contact_id
] = $dao->civicrm_contact_id
;
377 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
378 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
382 function limit($rowCount = CRM_Report_Form
::ROW_COUNT_LIMIT
) {
383 // lets do the pager if in html mode
384 $this->_limit
= NULL;
385 if ($this->_outputMode
== 'html' ||
$this->_outputMode
== 'group') {
386 //replace only first occurence of SELECT
387 $this->_select
= preg_replace('/SELECT/', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
, 1);
388 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
390 if (!$pageId && !empty($_POST) && isset($_POST['crmPID_B'])) {
391 if (!isset($_POST['PagerBottomButton'])) {
392 unset($_POST['crmPID_B']);
395 $pageId = max((int)@$_POST['crmPID_B'], 1);
399 $pageId = $pageId ?
$pageId : 1;
400 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
401 $offset = ($pageId - 1) * $rowCount;
403 $this->_limit
= " LIMIT $offset, " . $rowCount;
407 function alterDisplay(&$rows) {
408 // custom code to alter rows
413 foreach ($rows as $rowNum => $row) {
415 $rows[$rowNum]['civicrm_donor_rank'] = $rank++
;
416 // convert display name to links
417 if (array_key_exists('civicrm_contact_display_name', $row) &&
418 array_key_exists('civicrm_contact_id', $row) &&
419 CRM_Utils_Array
::value('civicrm_contribution_currency', $row)
421 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
422 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'] . "¤cy_value=" . $row['civicrm_contribution_currency'],
423 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
425 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
429 // skip looking further in rows, if first row itself doesn't
430 // have the column we need