security/core#65 Fix Display of Report title that contains XSS in it when shown on...
[civicrm-core.git] / CRM / Report / Form / Pledge / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Pledge_Summary extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38 protected $_totalPaid = FALSE;
be2fb01f 39 protected $_customGroupExtends = ['Pledge', 'Individual'];
6a488035 40 protected $_customGroupGroupBy = TRUE;
6a488035 41
74cf4551 42 /**
1728e9a0 43 * This report has not been optimised for group filtering.
44 *
45 * The functionality for group filtering has been improved but not
46 * all reports have been adjusted to take care of it. This report has not
47 * and will run an inefficient query until fixed.
48 *
49 * CRM-19170
50 *
51 * @var bool
74cf4551 52 */
1728e9a0 53 protected $groupFilterNotOptimised = TRUE;
54
74cf4551 55 /**
73b448bf 56 * Class constructor.
74cf4551 57 */
00be9182 58 public function __construct() {
be2fb01f
CW
59 $this->_columns = [
60 'civicrm_contact' => [
c301f76e 61 'dao' => 'CRM_Contact_DAO_Contact',
be2fb01f
CW
62 'fields' => [
63 'sort_name' => [
c301f76e 64 'title' => ts('Contact Name'),
65 'no_repeat' => TRUE,
be2fb01f
CW
66 ],
67 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
68 'id' => [
c301f76e 69 'no_display' => TRUE,
70 'required' => TRUE,
be2fb01f
CW
71 ],
72 ],
c301f76e 73 'grouping' => 'contact-fields',
be2fb01f
CW
74 'group_bys' => [
75 'id' => ['title' => ts('Contact ID')],
76 'sort_name' => [
c301f76e 77 'title' => ts('Contact Name'),
be2fb01f
CW
78 ],
79 ],
80 ],
81 'civicrm_email' => [
c301f76e 82 'dao' => 'CRM_Core_DAO_Email',
be2fb01f
CW
83 'fields' => [
84 'email' => [
c301f76e 85 'no_repeat' => TRUE,
86 'title' => ts('email'),
be2fb01f
CW
87 ],
88 ],
c301f76e 89 'grouping' => 'contact-fields',
be2fb01f
CW
90 ],
91 'civicrm_pledge' => [
c301f76e 92 'dao' => 'CRM_Pledge_DAO_Pledge',
be2fb01f
CW
93 'fields' => [
94 'id' => [
c301f76e 95 'no_display' => TRUE,
96 'required' => FALSE,
be2fb01f
CW
97 ],
98 'financial_type_id' => [
9e335308 99 'title' => ts('Financial Type'),
be2fb01f
CW
100 ],
101 'currency' => [
c301f76e 102 'required' => TRUE,
103 'no_display' => TRUE,
be2fb01f
CW
104 ],
105 'amount' => [
c301f76e 106 'title' => ts('Pledge Amount'),
107 'required' => TRUE,
108 'type' => CRM_Utils_Type::T_MONEY,
be2fb01f 109 'statistics' => [
c301f76e 110 'sum' => ts('Aggregate Amount Pledged'),
111 'count' => ts('Pledges'),
112 'avg' => ts('Average'),
be2fb01f
CW
113 ],
114 ],
115 'frequency_unit' => [
c301f76e 116 'title' => ts('Frequency Unit'),
be2fb01f
CW
117 ],
118 'installments' => [
c301f76e 119 'title' => ts('Installments'),
be2fb01f
CW
120 ],
121 'pledge_create_date' => [
c301f76e 122 'title' => ts('Pledge Made Date'),
be2fb01f
CW
123 ],
124 'start_date' => [
c301f76e 125 'title' => ts('Pledge Start Date'),
126 'type' => CRM_Utils_Type::T_DATE,
be2fb01f
CW
127 ],
128 'end_date' => [
c301f76e 129 'title' => ts('Pledge End Date'),
130 'type' => CRM_Utils_Type::T_DATE,
be2fb01f
CW
131 ],
132 'status_id' => [
c301f76e 133 'title' => ts('Pledge Status'),
be2fb01f
CW
134 ],
135 ],
136 'filters' => [
137 'pledge_create_date' => [
ccc29f8e 138 'title' => ts('Pledge Made Date'),
c301f76e 139 'operatorType' => CRM_Report_Form::OP_DATE,
be2fb01f
CW
140 ],
141 'pledge_amount' => [
c301f76e 142 'title' => ts('Pledged Amount'),
143 'operatorType' => CRM_Report_Form::OP_INT,
be2fb01f
CW
144 ],
145 'currency' => [
ccc29f8e 146 'title' => ts('Currency'),
c301f76e 147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
148 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
149 'default' => NULL,
150 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
151 ],
152 'sid' => [
c301f76e 153 'name' => 'status_id',
154 'title' => ts('Pledge Status'),
525ae77a 155 'type' => CRM_Utils_Type::T_INT,
c301f76e 156 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
01dac399 157 'options' => CRM_Core_OptionGroup::values('pledge_status'),
be2fb01f
CW
158 ],
159 'financial_type_id' => [
9e335308 160 'title' => ts('Financial Type'),
525ae77a 161 'type' => CRM_Utils_Type::T_INT,
9e335308 162 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
163 'options' => CRM_Contribute_PseudoConstant::financialType(),
be2fb01f
CW
164 ],
165 ],
166 'group_bys' => [
167 'pledge_create_date' => [
c301f76e 168 'frequency' => TRUE,
169 'default' => TRUE,
170 'chart' => TRUE,
be2fb01f
CW
171 ],
172 'frequency_unit' => [
c301f76e 173 'title' => ts('Frequency Unit'),
be2fb01f
CW
174 ],
175 'status_id' => [
c301f76e 176 'title' => ts('Pledge Status'),
be2fb01f
CW
177 ],
178 'financial_type_id' => [
a00ead4d 179 'title' => ts('Financial Type'),
be2fb01f
CW
180 ],
181 ],
182 ],
183 'civicrm_pledge_payment' => [
c301f76e 184 'dao' => 'CRM_Pledge_DAO_PledgePayment',
be2fb01f
CW
185 'fields' => [
186 'total_paid' => [
c301f76e 187 'title' => ts('Total Amount Paid'),
188 'type' => CRM_Utils_Type::T_STRING,
189 'dbAlias' => 'sum(pledge_payment_civireport.actual_amount)',
be2fb01f
CW
190 ],
191 ],
192 ],
193 ] + $this->addAddressFields();
6a488035 194
16e2e80c 195 $this->_groupFilter = TRUE;
6a488035 196 $this->_tagFilter = TRUE;
9bf1940a 197 $this->_currencyColumn = 'civicrm_pledge_currency';
6a488035
TO
198 parent::__construct();
199 }
200
00be9182 201 public function preProcess() {
6a488035
TO
202 parent::preProcess();
203 }
204
00be9182 205 public function select() {
6a488035
TO
206 parent::select();
207 }
208
00be9182 209 public function from() {
6a488035
TO
210 $this->_from = "
211 FROM civicrm_pledge {$this->_aliases['civicrm_pledge']}
2f4c2f5d 212 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
213 ON ({$this->_aliases['civicrm_contact']}.id =
6a488035
TO
214 {$this->_aliases['civicrm_pledge']}.contact_id )
215 {$this->_aclFrom} ";
216
cd2426ba 217 $this->joinAddressFromContact();
218 $this->joinEmailFromContact();
6a488035 219
9d72cede 220 if (!empty($this->_params['fields']['total_paid'])) {
6a488035
TO
221 $this->_from .= "
222 LEFT JOIN civicrm_pledge_payment {$this->_aliases['civicrm_pledge_payment']} ON
223 {$this->_aliases['civicrm_pledge']}.id = {$this->_aliases['civicrm_pledge_payment']}.pledge_id
224 AND {$this->_aliases['civicrm_pledge_payment']}.status_id = 1
225 ";
226 }
227 }
228
00be9182 229 public function groupBy() {
47c0ee09 230 $this->_groupBy = "";
6a488035
TO
231 $append = FALSE;
232
233 if (is_array($this->_params['group_bys']) &&
234 !empty($this->_params['group_bys'])
235 ) {
236 foreach ($this->_columns as $tableName => $table) {
237 if (array_key_exists('group_bys', $table)) {
238 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080
CW
239 if (!empty($this->_params['group_bys'][$fieldName])) {
240 if (!empty($field['chart'])) {
6a488035
TO
241 $this->assign('chartSupported', TRUE);
242 }
243
9d72cede
EM
244 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
245 !empty($this->_params['group_bys_freq'][$fieldName])
246 ) {
6a488035
TO
247
248 $append = "YEAR({$field['dbAlias']}),";
249 if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
be2fb01f 250 ['year']
9d72cede 251 )) {
6a488035
TO
252 $append = '';
253 }
47c0ee09 254 $this->_groupByArray[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
6a488035
TO
255 $append = TRUE;
256 }
257 else {
47c0ee09 258 $this->_groupByArray[] = $field['dbAlias'];
6a488035
TO
259 }
260 }
261 }
262 }
263 }
264
265 if (!empty($this->_statFields) &&
47c0ee09 266 (($append && count($this->_groupByArray) <= 1) || (!$append)) &&
9d72cede 267 !$this->_having
6a488035
TO
268 ) {
269 $this->_rollup = " WITH ROLLUP";
270 }
47c0ee09
SL
271 $groupBy = $this->_groupByArray;
272 $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupByArray);
6a488035
TO
273 }
274 else {
d1641c51 275 $groupBy = "{$this->_aliases['civicrm_contact']}.id";
276 $this->_groupBy = "GROUP BY {$groupBy}";
6a488035 277 }
36d2f4d5 278 $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, $groupBy);
d1641c51 279 $this->_groupBy .= " {$this->_rollup}";
6a488035
TO
280 }
281
74cf4551
EM
282 /**
283 * @param $rows
284 *
285 * @return array
286 */
00be9182 287 public function statistics(&$rows) {
6a488035
TO
288 $statistics = parent::statistics($rows);
289
290 if (!$this->_having) {
291 $select = "
292 SELECT COUNT({$this->_aliases['civicrm_pledge']}.amount ) as count,
293 SUM({$this->_aliases['civicrm_pledge']}.amount ) as amount,
294 ROUND(AVG({$this->_aliases['civicrm_pledge']}.amount), 2) as avg
295 ";
296
297 $sql = "{$select} {$this->_from} {$this->_where}";
9bf1940a 298
6a488035
TO
299 $dao = CRM_Core_DAO::executeQuery($sql);
300
301 if ($dao->fetch()) {
be2fb01f 302 $statistics['count']['amount'] = [
6a488035 303 'value' => $dao->amount,
ccc29f8e 304 'title' => ts('Total Pledged'),
6a488035 305 'type' => CRM_Utils_Type::T_MONEY,
be2fb01f
CW
306 ];
307 $statistics['count']['count '] = [
6a488035 308 'value' => $dao->count,
ccc29f8e 309 'title' => ts('Total No Pledges'),
be2fb01f
CW
310 ];
311 $statistics['count']['avg '] = [
6a488035 312 'value' => $dao->avg,
ccc29f8e 313 'title' => ts('Average'),
6a488035 314 'type' => CRM_Utils_Type::T_MONEY,
be2fb01f 315 ];
6a488035
TO
316 }
317 }
318 return $statistics;
319 }
320
00be9182 321 public function where() {
be2fb01f 322 $clauses = [];
6a488035
TO
323 foreach ($this->_columns as $tableName => $table) {
324 if (array_key_exists('filters', $table)) {
325 foreach ($table['filters'] as $fieldName => $field) {
326 $clause = NULL;
327 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
328 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
329 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
330 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
331
332 if ($relative || $from || $to) {
333 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
334 }
335 }
336 else {
337 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
338 if ($op) {
339 $clause = $this->whereClause($field,
340 $op,
341 CRM_Utils_Array::value("{$fieldName}_value",
342 $this->_params
343 ),
344 CRM_Utils_Array::value("{$fieldName}_min",
345 $this->_params
346 ),
347 CRM_Utils_Array::value("{$fieldName}_max",
348 $this->_params
349 )
350 );
351 }
352 }
353
354 if (!empty($clause)) {
355 $clauses[] = $clause;
356 }
357 }
358 }
359 }
360 if (empty($clauses)) {
361 $this->_where = "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) ";
362 }
363 else {
2f4c2f5d 364 $this->_where = "WHERE ({$this->_aliases['civicrm_pledge']}.is_test=0 ) AND
6a488035
TO
365 " . implode(' AND ', $clauses);
366 }
367
368 if ($this->_aclWhere) {
369 $this->_where .= " AND {$this->_aclWhere} ";
370 }
371 }
372
00be9182 373 public function postProcess() {
6a488035
TO
374 parent::postProcess();
375 }
376
74cf4551 377 /**
ced9bfed
EM
378 * Alter display of rows.
379 *
380 * Iterate through the rows retrieved via SQL and make changes for display purposes,
381 * such as rendering contacts as links.
382 *
383 * @param array $rows
384 * Rows generated by SQL, with an array for each row.
74cf4551 385 */
00be9182 386 public function alterDisplay(&$rows) {
9d72cede 387 $entryFound = FALSE;
be2fb01f 388 $checkList = [];
6a488035 389 $display_flag = $prev_cid = $cid = 0;
6a488035
TO
390 foreach ($rows as $rowNum => $row) {
391
392 // convert display name to links
393 if (array_key_exists('civicrm_contact_sort_name', $row) &&
394 array_key_exists('civicrm_contact_id', $row)
395 ) {
396 $url = CRM_Utils_System::url("civicrm/contact/view",
397 'reset=1&cid=' . $row['civicrm_contact_id'],
398 $this->_absoluteUrl
399 );
400 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
401 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
402 $entryFound = TRUE;
403 }
404
9e335308 405 if (array_key_exists('civicrm_pledge_financial_type_id', $row)) {
406 if ($value = $row['civicrm_pledge_financial_type_id']) {
407 $rows[$rowNum]['civicrm_pledge_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value, FALSE);
408 }
409 $entryFound = TRUE;
410 }
411
6a488035
TO
412 //handle status id
413 if (array_key_exists('civicrm_pledge_status_id', $row)) {
414 if ($value = $row['civicrm_pledge_status_id']) {
01dac399 415 $rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
6a488035
TO
416 }
417 $entryFound = TRUE;
418 }
419
420 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'pledge/summary', 'List all pledge(s) for this ') ? TRUE : $entryFound;
421
422 // skip looking further in rows, if first row itself doesn't
423 // have the column we need
424 if (!$entryFound) {
425 break;
426 }
427 }
428 }
96025800 429
6a488035 430}