CRM-12595 fix formatting in CRM/Upgrade files
[civicrm-core.git] / CRM / Report / Form / Contribute / History.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
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. |
15 | |
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. |
20 | |
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 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Form_Contribute_History extends CRM_Report_Form {
38 // Primary Contacts count limitCONSTROW_COUNT_LIMIT = 10;
39
40 protected $_addressField = FALSE;
41 protected $_emailField = FALSE;
42 protected $_phoneField = FALSE;
43 protected $_relationshipColumns = array();
44
45 protected $_customGroupExtends = array('Contribution');
46
47 protected $_referenceYear = array(
48 'this_year' => '',
49 'other_year' => '',
50 );
51 protected $_yearStatisticsFrom = '';
52
53 protected $_yearStatisticsTo = '';
54 function __construct() {
55 $yearsInPast = 4;
56 $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, 0);
57 $count = $date['maxYear'];
58 $optionYear = array('' => ts('-- select --'));
59
60 $this->_yearStatisticsFrom = $date['minYear'];
61 $this->_yearStatisticsTo = $date['maxYear'];
62
63 while ($date['minYear'] <= $count) {
64 $optionYear[$date['minYear']] = $date['minYear'];
65 $date['minYear']++;
66 }
67
68 $relationTypeOp = array();
69 $relationshipTypes = CRM_Core_PseudoConstant::relationshipType();
70 foreach ($relationshipTypes as $rid => $rtype) {
71 if ($rtype['label_a_b'] != $rtype['label_b_a']) {
72 $relationTypeOp[$rid] = "{$rtype['label_a_b']}/{$rtype['label_b_a']}";
73 }
74 else {
75 $relationTypeOp[$rid] = $rtype['label_a_b'];
76 }
77 }
78
79 $this->_columns = array(
80 'civicrm_contact' =>
81 array(
82 'dao' => 'CRM_Contact_DAO_Contact',
83 'fields' =>
84 array(
85 'sort_name' =>
86 array('title' => ts('Contact Name'),
87 'default' => TRUE,
88 'required' => TRUE,
89 'no_repeat' => TRUE,
90 ),
91 'id' =>
92 array(
93 'no_display' => TRUE,
94 'default' => TRUE,
95 'required' => TRUE,
96 ),
97 ),
98 'grouping' => 'contact-fields',
99 'filters' =>
100 array(
101 'sort_name' =>
102 array('title' => ts('Contact Name')),
103 'id' =>
104 array('title' => ts('Contact ID'),
105 'no_display' => TRUE,
106 ),
107 ),
108 ),
109 'civicrm_email' =>
110 array(
111 'dao' => 'CRM_Core_DAO_Email',
112 'fields' =>
113 array(
114 'email' =>
115 array('title' => ts('Email'),
116 'no_repeat' => TRUE,
117 ),
118 ),
119 'grouping' => 'contact-fields',
120 ),
121 'civicrm_phone' =>
122 array(
123 'dao' => 'CRM_Core_DAO_Phone',
124 'fields' =>
125 array(
126 'phone' =>
127 array('title' => ts('Phone'),
128 'no_repeat' => TRUE,
129 ),
130 ),
131 'grouping' => 'contact-fields',
132 ),
133 ) + $this->addAddressFields(FALSE, FALSE, FALSE, array(
134 )) + array('civicrm_relationship' =>
135 array(
136 'dao' => 'CRM_Contact_DAO_Relationship',
137 'fields' =>
138 array(
139 'relationship_type_id' =>
140 array('title' => ts('Relationship Type'),
141 'default' => TRUE,
142 ),
143 'contact_id_a' =>
144 array('no_display' => TRUE),
145 'contact_id_b' =>
146 array('no_display' => TRUE),
147 ),
148 'filters' =>
149 array(
150 'relationship_type_id' =>
151 array('title' => ts('Relationship Type'),
152 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
153 'options' => $relationTypeOp,
154 'type' => CRM_Utils_Type::T_STRING,
155 ),
156 ),
157 ),
158 ) + array(
159 'civicrm_contribution' =>
160 array(
161 'dao' => 'CRM_Contribute_DAO_Contribution',
162 'fields' =>
163 array(
164 'total_amount' =>
165 array('title' => ts('Amount Statistics'),
166 'default' => TRUE,
167 'required' => TRUE,
168 'no_display' => TRUE,
169 'statistics' =>
170 array('sum' => ts('Aggregate Amount')),
171 ),
172 'receive_date' =>
173 array(
174 'required' => TRUE,
175 'default' => TRUE,
176 'no_display' => TRUE,
177 ),
178 ),
179 'grouping' => 'contri-fields',
180 'filters' =>
181 array(
182 'this_year' =>
183 array(
184 'title' => ts('This Year'),
185 'operatorType' => CRM_Report_Form::OP_SELECT,
186 'options' => $optionYear,
187 'default' => '',
188 ),
189 'other_year' =>
190 array(
191 'title' => ts('Other Years'),
192 'operatorType' => CRM_Report_Form::OP_SELECT,
193 'options' => $optionYear,
194 'default' => '',
195 ),
196 'receive_date' =>
197 array('operatorType' => CRM_Report_Form::OP_DATE),
198 'contribution_status_id' =>
199 array('title' => ts('Donation Status'),
200 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
201 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
202 'default' => array(1),
203 ),
b914f4e8
PN
204 'financial_type_id' => array(
205 'title' => ts('Financial Type'),
6a488035 206 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
b914f4e8 207 'options' => CRM_Contribute_PseudoConstant::financialType(),
6a488035
TO
208 ),
209 'total_amount' =>
210 array('title' => ts('Donation Amount'),
211 ),
212 'total_sum' =>
213 array('title' => ts('Aggregate Amount'),
214 'type' => CRM_Report_Form::OP_INT,
215 'dbAlias' => 'civicrm_contribution_total_amount_sum',
216 'having' => TRUE,
217 ),
218 ),
219 ),
220 ) + array(
221 'civicrm_group' =>
222 array(
223 'dao' => 'CRM_Contact_DAO_GroupContact',
224 'alias' => 'cgroup',
225 'filters' =>
226 array(
227 'gid' =>
228 array(
229 'name' => 'group_id',
230 'title' => ts('Group'),
231 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
232 'group' => TRUE,
233 'options' => CRM_Core_PseudoConstant::group(),
234 ),
235 ),
236 ),
237 );
238
239 $this->_columns['civicrm_contribution']['fields']['civicrm_upto_' . $this->_yearStatisticsFrom] = array('title' => ts('Up To %1 Donation', array(1 => $this->_yearStatisticsFrom)),
240 'default' => TRUE,
241 'type' => CRM_Utils_Type::T_MONEY,
242 'is_statistics' => TRUE,
243 );
244
245 $yearConter = $this->_yearStatisticsFrom;
246 $yearConter++;
247 while ($yearConter <= $this->_yearStatisticsTo) {
248 $this->_columns['civicrm_contribution']['fields'][$yearConter] = array('title' => ts('%1 Donation', array(1 => $yearConter)),
249 'default' => TRUE,
250 'type' => CRM_Utils_Type::T_MONEY,
251 'is_statistics' => TRUE,
252 );
253 $yearConter++;
254 }
255
256 $this->_columns['civicrm_contribution']['fields']['aggregate_amount'] = array('title' => ts('Aggregate Amount'),
257 'type' => CRM_Utils_Type::T_MONEY,
258 'is_statistics' => TRUE,
259 );
260
261 $this->_tagFilter = TRUE;
262 parent::__construct();
263 }
264
265 function preProcess() {
266 parent::preProcess();
267 }
268
269 function select() {
270 $select = array();
271 $this->_columnHeaders = array();
272
273 foreach ($this->_columns as $tableName => $table) {
274 if (array_key_exists('fields', $table)) {
275 foreach ($table['fields'] as $fieldName => $field) {
276
277 if (CRM_Utils_Array::value('required', $field) ||
278 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
279 ) {
280 if ($tableName == 'civicrm_address') {
281 $this->_addressField = TRUE;
282 }
283 if ($tableName == 'civicrm_email') {
284 $this->_emailField = TRUE;
285 }
286 if ($tableName == 'civicrm_phone') {
287 $this->_phoneField = TRUE;
288 }
289 if ($tableName == 'civicrm_relationship') {
290 $this->_relationshipColumns["{$tableName}_{$fieldName}"] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
291 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
292 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
293 continue;
294 }
295
296 if ($fieldName == 'total_amount') {
297 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
298 }
299
300 if (CRM_Utils_Array::value('is_statistics', $field)) {
301 $this->_columnHeaders[$fieldName]['type'] = $field['type'];
302 $this->_columnHeaders[$fieldName]['title'] = $field['title'];
303 continue;
304 }
305 elseif ($fieldName == 'receive_date') {
306 if ((CRM_Utils_Array::value('this_year_op', $this->_params) == 'fiscal' &&
307 CRM_Utils_Array::value('this_year_value', $this->_params)) ||
308 (CRM_Utils_Array::value('other_year_op', $this->_params == 'fiscal') &&
309 CRM_Utils_Array::value('other_year_value', $this->_params)
310 )){
311 $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}";
312 }else{
313 $select[] = " YEAR(".$field['dbAlias'].")" . " as {$tableName}_{$fieldName}";
314 }
315 }
316 else {
317 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
318 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
319 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
320 }
321 if (CRM_Utils_Array::value('no_display', $field)) {
322 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
323 }
324 }
325 }
326 }
327 }
328
329 $this->_select = "SELECT " . implode(', ', $select) . " ";
330 }
331
332 function from() {
333 $this->_from = "
334 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
335 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
336 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
337 {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
338
339 if ($this->_emailField) {
340 $this->_from .= " LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
341 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
342 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
343 }
344
345 if ($this->_phoneField) {
346 $this->_from .= " LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
347 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
348 {$this->_aliases['civicrm_phone']}.is_primary = 1) ";
349 }
350
351 $relContacAlias = 'contact_relationship';
352 $this->_relationshipFrom = " INNER JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
353 ON (({$this->_aliases['civicrm_relationship']}.contact_id_a = {$relContacAlias}.id OR {$this->_aliases['civicrm_relationship']}.contact_id_b = {$relContacAlias}.id ) AND {$this->_aliases['civicrm_relationship']}.is_active = 1) ";
354
355 if ($this->_addressField) {
356 $this->_from .= "
357 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
358 ON {$this->_aliases['civicrm_contact']}.id =
359 {$this->_aliases['civicrm_address']}.contact_id AND
360 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
361 }
362 }
363
364 function where() {
365 $whereClauses = $havingClauses = $relationshipWhere = array();
366 $this->_relationshipWhere = '';
367 $this->_statusClause = '';
368
369 foreach ($this->_columns as $tableName => $table) {
370 if (array_key_exists('filters', $table)) {
371 foreach ($table['filters'] as $fieldName => $field) {
372 $clause = NULL;
373 if ($fieldName == 'this_year' || $fieldName == 'other_year') {
374 continue;
375 }elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
376 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
377 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
378 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
379
380 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
381 }
382 else {
383 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
384 if ($op) {
385 $clause = $this->whereClause($field,
386 $op,
387 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
388 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
389 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
390 );
391 }
392 }
393
394 if (!empty($clause)) {
395 if ($tableName == 'civicrm_relationship') {
396 $relationshipWhere[] = $clause;
397 continue;
398 }
399
400 if ($fieldName == 'contribution_status_id') {
401 $this->_statusClause = " AND " . $clause;
402 }
403
404 if (CRM_Utils_Array::value('having', $field)) {
405 $havingClauses[] = $clause;
406 }
407 else {
408 $whereClauses[] = $clause;
409 }
410 }
411 }
412 }
413 }
414
415 if (empty($whereClauses)) {
416 $this->_where = "WHERE ( 1 ) ";
417 $this->_having = "";
418 }
419 else {
420 $this->_where = "WHERE " . implode(' AND ', $whereClauses);
421 }
422
423 if ($this->_aclWhere) {
424 $this->_where .= " AND {$this->_aclWhere} ";
425 }
426
427 if (!empty($havingClauses)) {
428 // use this clause to construct group by clause.
429 $this->_having = "HAVING " . implode(' AND ', $havingClauses);
430 }
431
432 if (!empty($relationshipWhere)) {
433 $this->_relationshipWhere = ' AND ' . implode(' AND ', $relationshipWhere);
434 }
435 }
436
437 function groupBy() {
438 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, YEAR({$this->_aliases['civicrm_contribution']}.receive_date)";
439 }
440
441 //Override to set limit is 10
442 function limit($rowCount = self::ROW_COUNT_LIMIT) {
443 parent::limit($rowCount);
444 }
445
446 //Override to set pager with limit is 10
447 function setPager($rowCount = self::ROW_COUNT_LIMIT) {
448 parent::setPager($rowCount);
449 }
450
451 function statistics(&$rows) {
452 $statistics = parent::statistics($rows);
453 $count = 0;
454 foreach ($rows as $rownum => $row) {
455 if (is_numeric($rownum)) {
456 $count++;
457 }
458 }
459 $statistics['counts']['rowCount'] = array('title' => ts('Primary Contact(s) Listed'),
460 'value' => $count,
461 );
462
463 if ($this->_rowsFound && ($this->_rowsFound > $count)) {
464 $statistics['counts']['rowsFound'] = array('title' => ts('Total Primary Contact(s)'),
465 'value' => $this->_rowsFound,
466 );
467 }
468
469 return $statistics;
470 }
471
472 static function formRule($fields, $files, $self) {
473 $errors = array();
474 if (CRM_Utils_Array::value('this_year_value', $fields) &&
475 CRM_Utils_Array::value('other_year_value', $fields) &&
476 ($fields['this_year_value'] == $fields['other_year_value'])
477 ) {
478 $errors['other_year_value'] = ts("Value for filters 'This Year' and 'Other Years' can not be same.");
479 }
480 return $errors;
481 }
482
483 function postProcess() {
484 // get ready with post process params
485 $this->beginPostProcess();
486 $this->fixReportParams();
487
488 $this->buildACLClause($this->_aliases['civicrm_contact']);
489 $this->select();
490 $this->where();
491 $this->from();
492 $this->groupBy();
493
494 $rows = array();
495
496 // build array of result based on column headers. This method also allows
497 // modifying column headers before using it to build result set i.e $rows.
498 $this->buildRows($rows);
499
500 // format result set.
501 $this->formatDisplay($rows, FALSE);
502
503 // assign variables to templates
504 $this->doTemplateAssignment($rows);
505
506 // do print / pdf / instance stuff if needed
507 $this->endPostProcess($rows);
508 }
509
510 function fixReportParams() {
511 if (CRM_Utils_Array::value('this_year_value', $this->_params)) {
512 $this->_referenceYear['this_year'] = $this->_params['this_year_value'];
513 }
514 if (CRM_Utils_Array::value('other_year_value', $this->_params)) {
515 $this->_referenceYear['other_year'] = $this->_params['other_year_value'];
516 }
517 }
518
519 function buildRows(&$rows) {
520 $contactIds = array();
521
522 $addWhere = '';
523
524 if (CRM_Utils_Array::value('other_year', $this->_referenceYear)) {
525 (CRM_Utils_Array::value('other_year_op', $this->_params) == 'calendar') ? $other_receive_date = 'YEAR (contri.receive_date)' : $other_receive_date = self::fiscalYearOffset('contri.receive_date');
526 $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id NOT IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$other_receive_date} = {$this->_referenceYear['other_year']} AND contri.is_test = 0 ) ";
527 }
528 if (CRM_Utils_Array::value('this_year', $this->_referenceYear)) {
529 (CRM_Utils_Array::value('this_year_op', $this->_params) == 'calendar') ? $receive_date = 'YEAR (contri.receive_date)' : $receive_date = self::fiscalYearOffset('contri.receive_date');
530 $addWhere .= " AND {$this->_aliases['civicrm_contact']}.id IN ( SELECT DISTINCT cont.id FROM civicrm_contact cont, civicrm_contribution contri WHERE cont.id = contri.contact_id AND {$receive_date} = {$this->_referenceYear['this_year']} AND contri.is_test = 0 ) ";
531 }
532 $this->limit();
533 $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid, SUM({$this->_aliases['civicrm_contribution']}.total_amount) as civicrm_contribution_total_amount_sum {$this->_from} {$this->_where} {$addWhere} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_having} {$this->_limit}";
534
535 $dao = CRM_Core_DAO::executeQuery($getContacts);
536
537 while ($dao->fetch()) {
538 $contactIds[] = $dao->cid;
539 }
540 $dao->free();
541 $this->setPager();
542
543 $relationshipRows = array();
544 if (empty($contactIds)) {
545 return;
546 }
547
548 $primaryContributions = $this->buildContributionRows($contactIds);
549
550 list($relationshipRows, $relatedContactIds) = $this->buildRelationshipRows($contactIds);
551
552 if (empty($relatedContactIds)) {
553 $rows = $primaryContributions;
554 return;
555 }
556
557 $relatedContributions = $this->buildContributionRows($relatedContactIds);
558
559 $summaryYears = array();
560 $summaryYears[] = "civicrm_upto_{$this->_yearStatisticsFrom}";
561 $yearConter = $this->_yearStatisticsFrom;
562 $yearConter++;
563 while ($yearConter <= $this->_yearStatisticsTo) {
564 $summaryYears[] = $yearConter;
565 $yearConter++;
566 }
567 $summaryYears[] = 'aggregate_amount';
568
569 foreach ($primaryContributions as $cid => $primaryRow) {
570 $row = $primaryRow;
571 if (!isset($relationshipRows[$cid])) {
572 $rows[$cid] = $row;
573 continue;
574 }
575 $total = array();
576 $total['civicrm_contact_sort_name'] = ts('Total');
577 foreach ($summaryYears as $year) {
578 $total[$year] = CRM_Utils_Array::value($year, $primaryRow, 0);
579 }
580
581 $relatedContact = FALSE;
582 $rows[$cid] = $row;
583 foreach ($relationshipRows[$cid] as $relcid => $relRow) {
584 if (!isset($relatedContributions[$relcid])) {
585 continue;
586 }
587 $relatedContact = TRUE;
588 $relatedRow = $relatedContributions[$relcid];
589 foreach ($summaryYears as $year) {
590 $total[$year] += CRM_Utils_Array::value($year, $relatedRow, 0);
591 }
592
593 foreach (array_keys($this->_relationshipColumns) as $col) {
594 if (CRM_Utils_Array::value($col, $relRow)) {
595 $relatedRow[$col] = $relRow[$col];
596 }
597 }
598 $rows["{$cid}_{$relcid}"] = $relatedRow;
599 }
600 if ($relatedContact) {
601 $rows["{$cid}_total"] = $total;
602 $rows["{$cid}_bank"] = array('civicrm_contact_sort_name' => '&nbsp;');
603 }
604 }
605 }
606
607 function buildContributionRows($contactIds) {
608 $rows = array();
609 if (empty($contactIds)) {
610 return $rows;
611 }
612
613 $sqlContribution = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} ";
614
615 $dao = CRM_Core_DAO::executeQuery($sqlContribution);
616 $contributionSum = 0;
617 $yearcal = array();
618 while ($dao->fetch()) {
619 if (!$dao->civicrm_contact_id) {
620 continue;
621 }
622
623 foreach ($this->_columnHeaders as $key => $value) {
624 if (property_exists($dao, $key)) {
625 $rows[$dao->civicrm_contact_id][$key] = $dao->$key;
626 }
627 }
628 if ($dao->civicrm_contribution_receive_date) {
629 if ($dao->civicrm_contribution_receive_date > $this->_yearStatisticsFrom) {
630 $rows[$dao->civicrm_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount;
631 }
632 else {
633 if (!isset($rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"])) {
634 $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] = 0;
635 }
636
637 $rows[$dao->civicrm_contact_id]["civicrm_upto_{$this->_yearStatisticsFrom}"] += $dao->civicrm_contribution_total_amount;
638 }
639 }
640
641 if (!isset($rows[$dao->civicrm_contact_id]['aggregate_amount'])) {
642 $rows[$dao->civicrm_contact_id]['aggregate_amount'] = 0;
643 }
644 $rows[$dao->civicrm_contact_id]['aggregate_amount'] += $dao->civicrm_contribution_total_amount;
645 }
646 $dao->free();
647 return $rows;
648 }
649
650 function buildRelationshipRows($contactIds) {
651 $relationshipRows = $relatedContactIds = array();
652 if (empty($contactIds)) {
653 return array($relationshipRows, $relatedContactIds);
654 }
655
656 $relContactAlias = 'contact_relationship';
657 $addRelSelect = '';
658 if (!empty($this->_relationshipColumns)) {
659 $addRelSelect = ', ' . implode(', ', $this->_relationshipColumns);
660 }
661 $sqlRelationship = "SELECT {$this->_aliases['civicrm_relationship']}.relationship_type_id as relationship_type_id, {$this->_aliases['civicrm_relationship']}.contact_id_a as contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b as contact_id_b {$addRelSelect} FROM civicrm_contact {$relContactAlias} {$this->_relationshipFrom} WHERE {$relContactAlias}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_relationship']}.is_active = 1 {$this->_relationshipWhere} GROUP BY {$this->_aliases['civicrm_relationship']}.contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b";
662 $relationshipTypes = CRM_Core_PseudoConstant::relationshipType();
663
664 $dao = CRM_Core_DAO::executeQuery($sqlRelationship);
665 while ($dao->fetch()) {
666 $row = array();
667 foreach (array_keys($this->_relationshipColumns) as $rel_column) {
668 $row[$rel_column] = $dao->$rel_column;
669 }
670 if (in_array($dao->contact_id_a, $contactIds)) {
671 $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_a_b'];
672 $row['civicrm_relationship_contact_id'] = $dao->contact_id_b;
673 $relationshipRows[$dao->contact_id_a][$dao->contact_id_b] = $row;
674 $relatedContactIds[$dao->contact_id_b] = $dao->contact_id_b;
675 }
676 if (in_array($dao->contact_id_b, $contactIds)) {
677 $row['civicrm_relationship_contact_id'] = $dao->contact_id_a;
678 $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_b_a'];
679 $relationshipRows[$dao->contact_id_b][$dao->contact_id_a] = $row;
680 $relatedContactIds[$dao->contact_id_a] = $dao->contact_id_a;
681 }
682 }
683 $dao->free();
684 return array($relationshipRows, $relatedContactIds);
685 }
686
687 // Override "This Year" $op options
688 function getOperationPair($type = "string", $fieldName = NULL) {
689 if ($fieldName == 'this_year' || $fieldName == 'other_year') {
690 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
691 }
692 return parent::getOperationPair($type, $fieldName);
693 }
694
695
696
697 function alterDisplay(&$rows) {
698 if (empty($rows)) {
699 return;
700 }
701
702
703 $last_primary = NULL;
704 foreach ($rows as $rowNum => $row) {
705 // Highlight primary contact and amount row
706 if (is_numeric($rowNum) ||
707 ($last_primary && ($rowNum == "{$last_primary}_total"))
708 ) {
709 if (is_numeric($rowNum)) {
710 $last_primary = $rowNum;
711 }
712 foreach ($row as $key => $value) {
713 if ($key == 'civicrm_contact_id') {
714 continue;
715 }
716 if (empty($value)) {
717 $row[$key] = '';
718 continue;
719 }
720
721 if ($last_primary && ($rowNum == "{$last_primary}_total")) {
722 $value = CRM_Utils_Money::format($value, ' ');
723 }
724 $row[$key] = '<strong>' . $value . '</strong>';
725 }
726 $rows[$rowNum] = $row;
727 }
728
729 // Convert Display name into link
730 if (CRM_Utils_Array::value('civicrm_contact_sort_name', $row) &&
731 CRM_Utils_Array::value('civicrm_contact_id', $row)
732 ) {
733 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
734 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
735 $this->_absoluteUrl, $this->_id
736 );
737 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
738 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
739 }
740 }
741 }
742 }
743