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