| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
| 32 | */ |
| 33 | class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { |
| 34 | |
| 35 | protected $_charts = array( |
| 36 | '' => 'Tabular', |
| 37 | 'barChart' => 'Bar Chart', |
| 38 | 'pieChart' => 'Pie Chart', |
| 39 | ); |
| 40 | |
| 41 | protected $_customGroupExtends = array( |
| 42 | 'Contact', |
| 43 | 'Individual', |
| 44 | 'Contribution', |
| 45 | ); |
| 46 | |
| 47 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); |
| 48 | |
| 49 | /** |
| 50 | */ |
| 51 | public function __construct() { |
| 52 | $this->_rollup = 'WITH ROLLUP'; |
| 53 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
| 54 | $yearsInPast = 10; |
| 55 | $yearsInFuture = 1; |
| 56 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); |
| 57 | $count = $date['maxYear']; |
| 58 | while ($date['minYear'] <= $count) { |
| 59 | $optionYear[$date['minYear']] = $date['minYear']; |
| 60 | $date['minYear']++; |
| 61 | } |
| 62 | |
| 63 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
| 64 | $config = CRM_Core_Config::singleton(); |
| 65 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); |
| 66 | if ($campaignEnabled) { |
| 67 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); |
| 68 | $this->activeCampaigns = $getCampaigns['campaigns']; |
| 69 | asort($this->activeCampaigns); |
| 70 | } |
| 71 | |
| 72 | $this->_columns = array( |
| 73 | 'civicrm_contact' => array( |
| 74 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 75 | 'grouping' => 'contact-field', |
| 76 | 'fields' => array( |
| 77 | 'sort_name' => array( |
| 78 | 'title' => ts('Donor Name'), |
| 79 | 'required' => TRUE, |
| 80 | ), |
| 81 | 'first_name' => array( |
| 82 | 'title' => ts('First Name'), |
| 83 | ), |
| 84 | 'middle_name' => array( |
| 85 | 'title' => ts('Middle Name'), |
| 86 | ), |
| 87 | 'last_name' => array( |
| 88 | 'title' => ts('Last Name'), |
| 89 | ), |
| 90 | 'id' => array( |
| 91 | 'no_display' => TRUE, |
| 92 | 'required' => TRUE, |
| 93 | ), |
| 94 | 'gender_id' => array( |
| 95 | 'title' => ts('Gender'), |
| 96 | ), |
| 97 | 'birth_date' => array( |
| 98 | 'title' => ts('Birth Date'), |
| 99 | ), |
| 100 | 'age' => array( |
| 101 | 'title' => ts('Age'), |
| 102 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', |
| 103 | ), |
| 104 | 'contact_type' => array( |
| 105 | 'title' => ts('Contact Type'), |
| 106 | ), |
| 107 | 'contact_sub_type' => array( |
| 108 | 'title' => ts('Contact Subtype'), |
| 109 | ), |
| 110 | ), |
| 111 | 'grouping' => 'contact-fields', |
| 112 | 'order_bys' => array( |
| 113 | 'sort_name' => array( |
| 114 | 'title' => ts('Last Name, First Name'), |
| 115 | 'default' => '1', |
| 116 | 'default_weight' => '0', |
| 117 | 'default_order' => 'ASC', |
| 118 | ), |
| 119 | 'first_name' => array( |
| 120 | 'name' => 'first_name', |
| 121 | 'title' => ts('First Name'), |
| 122 | ), |
| 123 | 'gender_id' => array( |
| 124 | 'name' => 'gender_id', |
| 125 | 'title' => ts('Gender'), |
| 126 | ), |
| 127 | 'birth_date' => array( |
| 128 | 'name' => 'birth_date', |
| 129 | 'title' => ts('Birth Date'), |
| 130 | ), |
| 131 | 'age_at_event' => array( |
| 132 | 'name' => 'age_at_event', |
| 133 | 'title' => ts('Age at Event'), |
| 134 | ), |
| 135 | 'contact_type' => array( |
| 136 | 'title' => ts('Contact Type'), |
| 137 | ), |
| 138 | 'contact_sub_type' => array( |
| 139 | 'title' => ts('Contact Subtype'), |
| 140 | ), |
| 141 | ), |
| 142 | 'filters' => array( |
| 143 | 'sort_name' => array( |
| 144 | 'title' => ts('Donor Name'), |
| 145 | 'operator' => 'like', |
| 146 | ), |
| 147 | 'id' => array( |
| 148 | 'title' => ts('Contact ID'), |
| 149 | 'no_display' => TRUE, |
| 150 | ), |
| 151 | 'gender_id' => array( |
| 152 | 'title' => ts('Gender'), |
| 153 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 154 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), |
| 155 | ), |
| 156 | 'birth_date' => array( |
| 157 | 'title' => ts('Birth Date'), |
| 158 | 'operatorType' => CRM_Report_Form::OP_DATE, |
| 159 | ), |
| 160 | 'contact_type' => array( |
| 161 | 'title' => ts('Contact Type'), |
| 162 | ), |
| 163 | 'contact_sub_type' => array( |
| 164 | 'title' => ts('Contact Subtype'), |
| 165 | ), |
| 166 | ), |
| 167 | ), |
| 168 | 'civicrm_line_item' => array( |
| 169 | 'dao' => 'CRM_Price_DAO_LineItem', |
| 170 | ), |
| 171 | 'civicrm_email' => array( |
| 172 | 'dao' => 'CRM_Core_DAO_Email', |
| 173 | 'grouping' => 'contact-field', |
| 174 | 'fields' => array( |
| 175 | 'email' => array( |
| 176 | 'title' => ts('Email'), |
| 177 | 'default' => TRUE, |
| 178 | ), |
| 179 | ), |
| 180 | ), |
| 181 | 'civicrm_phone' => array( |
| 182 | 'dao' => 'CRM_Core_DAO_Phone', |
| 183 | 'grouping' => 'contact-field', |
| 184 | 'fields' => array( |
| 185 | 'phone' => array( |
| 186 | 'title' => ts('Phone'), |
| 187 | 'default' => TRUE, |
| 188 | ), |
| 189 | ), |
| 190 | ), |
| 191 | ); |
| 192 | $this->_columns += $this->addAddressFields(); |
| 193 | $this->_columns += array( |
| 194 | 'civicrm_contribution' => array( |
| 195 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
| 196 | 'fields' => array( |
| 197 | 'contact_id' => array( |
| 198 | 'title' => ts('contactId'), |
| 199 | 'no_display' => TRUE, |
| 200 | 'required' => TRUE, |
| 201 | 'no_repeat' => TRUE, |
| 202 | ), |
| 203 | 'total_amount' => array( |
| 204 | 'title' => ts('Total Amount'), |
| 205 | 'no_display' => TRUE, |
| 206 | 'required' => TRUE, |
| 207 | 'no_repeat' => TRUE, |
| 208 | ), |
| 209 | 'receive_date' => array( |
| 210 | 'title' => ts('Year'), |
| 211 | 'no_display' => TRUE, |
| 212 | 'required' => TRUE, |
| 213 | 'no_repeat' => TRUE, |
| 214 | ), |
| 215 | ), |
| 216 | 'filters' => array( |
| 217 | 'yid' => array( |
| 218 | 'name' => 'receive_date', |
| 219 | 'title' => ts('This Year'), |
| 220 | 'operatorType' => CRM_Report_Form::OP_SELECT, |
| 221 | 'options' => $optionYear, |
| 222 | 'default' => date('Y'), |
| 223 | 'type' => CRM_Utils_Type::T_INT, |
| 224 | ), |
| 225 | 'financial_type_id' => array( |
| 226 | 'title' => ts('Financial Type'), |
| 227 | 'type' => CRM_Utils_Type::T_INT, |
| 228 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 229 | 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(), |
| 230 | ), |
| 231 | 'contribution_status_id' => array( |
| 232 | 'title' => ts('Contribution Status'), |
| 233 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 234 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), |
| 235 | 'default' => array('1'), |
| 236 | ), |
| 237 | ), |
| 238 | ), |
| 239 | ); |
| 240 | |
| 241 | // If we have a campaign, build out the relevant elements |
| 242 | if ($campaignEnabled && !empty($this->activeCampaigns)) { |
| 243 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( |
| 244 | 'title' => ts('Campaign'), |
| 245 | 'default' => 'false', |
| 246 | ); |
| 247 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array( |
| 248 | 'title' => ts('Campaign'), |
| 249 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 250 | 'options' => $this->activeCampaigns, |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | $this->_groupFilter = TRUE; |
| 255 | $this->_tagFilter = TRUE; |
| 256 | parent::__construct(); |
| 257 | } |
| 258 | |
| 259 | public function preProcess() { |
| 260 | parent::preProcess(); |
| 261 | } |
| 262 | |
| 263 | public function select() { |
| 264 | $select = array(); |
| 265 | $this->_columnHeaders = array(); |
| 266 | $current_year = $this->_params['yid_value']; |
| 267 | $previous_year = $current_year - 1; |
| 268 | $previous_pyear = $current_year - 2; |
| 269 | $previous_ppyear = $current_year - 3; |
| 270 | $upTo_year = $current_year - 4; |
| 271 | |
| 272 | foreach ($this->_columns as $tableName => $table) { |
| 273 | if (array_key_exists('fields', $table)) { |
| 274 | foreach ($table['fields'] as $fieldName => $field) { |
| 275 | |
| 276 | if (!empty($field['required']) || |
| 277 | !empty($this->_params['fields'][$fieldName]) |
| 278 | ) { |
| 279 | if ($fieldName == 'total_amount') { |
| 280 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; |
| 281 | |
| 282 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['type'] = $field['type']; |
| 283 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year"; |
| 284 | |
| 285 | $this->_columnHeaders["{$previous_ppyear}"]['type'] = $field['type']; |
| 286 | $this->_columnHeaders["{$previous_ppyear}"]['title'] = $previous_ppyear; |
| 287 | |
| 288 | $this->_columnHeaders["{$previous_pyear}"]['type'] = $field['type']; |
| 289 | $this->_columnHeaders["{$previous_pyear}"]['title'] = $previous_pyear; |
| 290 | |
| 291 | $this->_columnHeaders["{$previous_year}"]['type'] = $field['type']; |
| 292 | $this->_columnHeaders["{$previous_year}"]['title'] = $previous_year; |
| 293 | |
| 294 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; |
| 295 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = 'LifeTime';; |
| 296 | } |
| 297 | elseif ($fieldName == 'receive_date') { |
| 298 | $select[] = self::fiscalYearOffset($field['dbAlias']) . |
| 299 | " as {$tableName}_{$fieldName}"; |
| 300 | } |
| 301 | else { |
| 302 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; |
| 303 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); |
| 304 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; |
| 305 | } |
| 306 | if (!empty($field['no_display'])) { |
| 307 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
| 315 | } |
| 316 | |
| 317 | public function from() { |
| 318 | |
| 319 | $this->_from = " |
| 320 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} |
| 321 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
| 322 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id |
| 323 | {$this->_aclFrom}"; |
| 324 | |
| 325 | if ($this->isTableSelected('civicrm_email')) { |
| 326 | $this->_from .= " |
| 327 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
| 328 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id |
| 329 | AND {$this->_aliases['civicrm_email']}.is_primary = 1"; |
| 330 | } |
| 331 | if ($this->isTableSelected('civicrm_phone')) { |
| 332 | $this->_from .= " |
| 333 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
| 334 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
| 335 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; |
| 336 | } |
| 337 | $this->addAddressFromClause(); |
| 338 | } |
| 339 | |
| 340 | public function where() { |
| 341 | $this->_statusClause = ""; |
| 342 | $clauses = array($this->_aliases['civicrm_contribution'] . '.is_test = 0'); |
| 343 | foreach ($this->_columns as $tableName => $table) { |
| 344 | if (array_key_exists('filters', $table)) { |
| 345 | foreach ($table['filters'] as $fieldName => $field) { |
| 346 | $clause = NULL; |
| 347 | if ($fieldName == 'yid') { |
| 348 | $clause = "contribution_civireport.contact_id NOT IN |
| 349 | (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri |
| 350 | WHERE cont.id = contri.contact_id AND " . |
| 351 | self::fiscalYearOffset('contri.receive_date') . |
| 352 | " = {$this->_params['yid_value']} AND contri.is_test = 0 )"; |
| 353 | } |
| 354 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE |
| 355 | ) { |
| 356 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
| 357 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
| 358 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); |
| 359 | |
| 360 | if ($relative || $from || $to) { |
| 361 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); |
| 362 | } |
| 363 | } |
| 364 | else { |
| 365 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); |
| 366 | if ($op) { |
| 367 | $clause = $this->whereClause($field, |
| 368 | $op, |
| 369 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), |
| 370 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), |
| 371 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) |
| 372 | ); |
| 373 | if (($fieldName == 'contribution_status_id' || |
| 374 | $fieldName == 'financial_type_id') && !empty($clause) |
| 375 | ) { |
| 376 | $this->_statusClause .= " AND " . $clause; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (!empty($clause)) { |
| 382 | $clauses[] = $clause; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | $this->_where = "WHERE " . implode(' AND ', $clauses); |
| 389 | |
| 390 | if ($this->_aclWhere) { |
| 391 | $this->_where .= " AND {$this->_aclWhere} "; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | public function groupBy() { |
| 396 | $this->assign('chartSupported', TRUE); |
| 397 | $this->_groupBy = "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . |
| 398 | self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . |
| 399 | '.receive_date') . " " . " " . $this->_rollup; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @param $rows |
| 404 | * |
| 405 | * @return array |
| 406 | */ |
| 407 | public function statistics(&$rows) { |
| 408 | $statistics = parent::statistics($rows); |
| 409 | |
| 410 | if (!empty($rows)) { |
| 411 | $select = " |
| 412 | SELECT |
| 413 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
| 414 | |
| 415 | $sql = "{$select} {$this->_from} {$this->_where}"; |
| 416 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 417 | if ($dao->fetch()) { |
| 418 | $statistics['counts']['amount'] = array( |
| 419 | 'value' => $dao->amount, |
| 420 | 'title' => 'Total LifeTime', |
| 421 | 'type' => CRM_Utils_Type::T_MONEY, |
| 422 | ); |
| 423 | } |
| 424 | } |
| 425 | return $statistics; |
| 426 | } |
| 427 | |
| 428 | public function postProcess() { |
| 429 | // get ready with post process params |
| 430 | $this->beginPostProcess(); |
| 431 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
| 432 | $this->select(); |
| 433 | $this->from(); |
| 434 | $this->where(); |
| 435 | $this->groupBy(); |
| 436 | $this->getPermissionedFTQuery($this); |
| 437 | |
| 438 | $rows = $contactIds = array(); |
| 439 | if (empty($this->_params['charts'])) { |
| 440 | $this->limit(); |
| 441 | $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid {$this->_from} {$this->_where} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_limit}"; |
| 442 | |
| 443 | $dao = CRM_Core_DAO::executeQuery($getContacts); |
| 444 | |
| 445 | while ($dao->fetch()) { |
| 446 | $contactIds[] = $dao->cid; |
| 447 | } |
| 448 | $dao->free(); |
| 449 | $this->setPager(); |
| 450 | } |
| 451 | |
| 452 | if (!empty($contactIds) || !empty($this->_params['charts'])) { |
| 453 | if (!empty($this->_params['charts'])) { |
| 454 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
| 455 | } |
| 456 | else { |
| 457 | $sql = "" . |
| 458 | "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . |
| 459 | implode(',', $contactIds) . |
| 460 | ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} "; |
| 461 | } |
| 462 | |
| 463 | $current_year = $this->_params['yid_value']; |
| 464 | $previous_year = $current_year - 1; |
| 465 | $previous_pyear = $current_year - 2; |
| 466 | $previous_ppyear = $current_year - 3; |
| 467 | $upTo_year = $current_year - 4; |
| 468 | |
| 469 | $rows = $row = array(); |
| 470 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 471 | $contributionSum = 0; |
| 472 | $yearcal = array(); |
| 473 | while ($dao->fetch()) { |
| 474 | if (!$dao->civicrm_contribution_contact_id) { |
| 475 | continue; |
| 476 | } |
| 477 | $row = array(); |
| 478 | foreach ($this->_columnHeaders as $key => $value) { |
| 479 | if (property_exists($dao, $key)) { |
| 480 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; |
| 481 | } |
| 482 | } |
| 483 | if ($dao->civicrm_contribution_receive_date) { |
| 484 | if ($dao->civicrm_contribution_receive_date > $upTo_year) { |
| 485 | $contributionSum += $dao->civicrm_contribution_total_amount; |
| 486 | $rows[$dao->civicrm_contribution_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; |
| 487 | } |
| 488 | } |
| 489 | else { |
| 490 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; |
| 491 | if (($dao->civicrm_contribution_total_amount - $contributionSum) > 0 |
| 492 | ) { |
| 493 | $rows[$dao->civicrm_contribution_contact_id]["civicrm_upto_{$upTo_year}"] |
| 494 | = $dao->civicrm_contribution_total_amount - $contributionSum; |
| 495 | } |
| 496 | $contributionSum = 0; |
| 497 | } |
| 498 | } |
| 499 | $dao->free(); |
| 500 | } |
| 501 | // format result set. |
| 502 | $this->formatDisplay($rows, FALSE); |
| 503 | |
| 504 | // assign variables to templates |
| 505 | $this->doTemplateAssignment($rows); |
| 506 | |
| 507 | // do print / pdf / instance stuff if needed |
| 508 | $this->endPostProcess($rows); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * @param $rows |
| 513 | */ |
| 514 | public function buildChart(&$rows) { |
| 515 | $graphRows = array(); |
| 516 | $count = 0; |
| 517 | $current_year = $this->_params['yid_value']; |
| 518 | $previous_year = $current_year - 1; |
| 519 | $previous_two_year = $current_year - 2; |
| 520 | $previous_three_year = $current_year - 3; |
| 521 | $upto = $current_year - 4; |
| 522 | |
| 523 | $interval[$previous_year] = $previous_year; |
| 524 | $interval[$previous_two_year] = $previous_two_year; |
| 525 | $interval[$previous_three_year] = $previous_three_year; |
| 526 | $interval["upto_{$upto}"] = "Up To {$upto}"; |
| 527 | |
| 528 | foreach ($rows as $key => $row) { |
| 529 | $display["upto_{$upto}"] |
| 530 | = CRM_Utils_Array::value("upto_{$upto}", $display) |
| 531 | + CRM_Utils_Array::value("civicrm_upto_{$upto}", $row); |
| 532 | $display[$previous_year] |
| 533 | = CRM_Utils_Array::value($previous_year, $display) |
| 534 | + CRM_Utils_Array::value($previous_year, $row); |
| 535 | $display[$previous_two_year] |
| 536 | = CRM_Utils_Array::value($previous_two_year, $display) |
| 537 | + CRM_Utils_Array::value($previous_two_year, $row); |
| 538 | $display[$previous_three_year] |
| 539 | = CRM_Utils_Array::value($previous_three_year, $display) |
| 540 | + CRM_Utils_Array::value($previous_three_year, $row); |
| 541 | } |
| 542 | |
| 543 | $graphRows['value'] = $display; |
| 544 | $config = CRM_Core_Config::Singleton(); |
| 545 | $chartInfo = array( |
| 546 | 'legend' => 'Sybunt Report', |
| 547 | 'xname' => 'Year', |
| 548 | 'yname' => "Amount ({$config->defaultCurrency})", |
| 549 | ); |
| 550 | if ($this->_params['charts']) { |
| 551 | // build the chart. |
| 552 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); |
| 553 | $this->assign('chartType', $this->_params['charts']); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Alter display of rows. |
| 559 | * |
| 560 | * Iterate through the rows retrieved via SQL and make changes for display purposes, |
| 561 | * such as rendering contacts as links. |
| 562 | * |
| 563 | * @param array $rows |
| 564 | * Rows generated by SQL, with an array for each row. |
| 565 | */ |
| 566 | public function alterDisplay(&$rows) { |
| 567 | $entryFound = FALSE; |
| 568 | |
| 569 | foreach ($rows as $rowNum => $row) { |
| 570 | //Convert Display name into link |
| 571 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
| 572 | array_key_exists('civicrm_contribution_contact_id', $row) |
| 573 | ) { |
| 574 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
| 575 | 'reset=1&force=1&id_op=eq&id_value=' . |
| 576 | $row['civicrm_contribution_contact_id'], |
| 577 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
| 578 | ); |
| 579 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; |
| 580 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); |
| 581 | $entryFound = TRUE; |
| 582 | } |
| 583 | |
| 584 | // convert campaign_id to campaign title |
| 585 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { |
| 586 | if ($value = $row['civicrm_contribution_campaign_id']) { |
| 587 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; |
| 588 | $entryFound = TRUE; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
| 593 | |
| 594 | //handle gender |
| 595 | if (array_key_exists('civicrm_contact_gender_id', $row)) { |
| 596 | if ($value = $row['civicrm_contact_gender_id']) { |
| 597 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
| 598 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; |
| 599 | } |
| 600 | $entryFound = TRUE; |
| 601 | } |
| 602 | |
| 603 | // display birthday in the configured custom format |
| 604 | if (array_key_exists('civicrm_contact_birth_date', $row)) { |
| 605 | $birthDate = $row['civicrm_contact_birth_date']; |
| 606 | if ($birthDate) { |
| 607 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); |
| 608 | } |
| 609 | $entryFound = TRUE; |
| 610 | } |
| 611 | |
| 612 | // skip looking further in rows, if first row itself doesn't |
| 613 | // have the column we need |
| 614 | if (!$entryFound) { |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Override "This Year" $op options |
| 622 | * @param string $type |
| 623 | * @param null $fieldName |
| 624 | * |
| 625 | * @return array |
| 626 | */ |
| 627 | public function getOperationPair($type = "string", $fieldName = NULL) { |
| 628 | if ($fieldName == 'yid') { |
| 629 | return array( |
| 630 | 'calendar' => ts('Is Calendar Year'), |
| 631 | 'fiscal' => ts('Fiscal Year Starting'), |
| 632 | ); |
| 633 | } |
| 634 | return parent::getOperationPair($type, $fieldName); |
| 635 | } |
| 636 | |
| 637 | } |