CRM-15603 - More consistent use of 'Donations' vs 'Contributions'
[civicrm-core.git] / CRM / Report / Form / Contribute / PCP.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Contribute_PCP extends CRM_Report_Form {
74cf4551
EM
37 /**
38 *
39 */
40 /**
41 *
42 */
6a488035
TO
43 function __construct() {
44 $this->_columns = array(
45 'civicrm_contact' =>
46 array(
47 'dao' => 'CRM_Contact_DAO_Contact',
48 'fields' =>
49 array(
50 'sort_name' =>
51 array('title' => ts('Supporter'),
52 'required' => TRUE,
53 'default' => TRUE,
54 ),
55 'id' =>
56 array(
57 'required' => TRUE,
58 'no_display' => TRUE,
59 ),
30f85891
RN
60 'contact_type' =>
61 array(
62 'title' => ts('Supporter Contact Type'),
63 ),
64 'contact_sub_type' =>
65 array(
b8f96eb8 66 'title' => ts('Supporter Contact Subtype'),
30f85891 67 ),
6a488035
TO
68 ),
69 'filters' =>
70 array(
71 'sort_name' =>
72 array('title' => ts('Supporter Name'),
73 'type' => CRM_Utils_Type::T_STRING,
74 'operator' => 'like',
75 ),
76 'id' =>
77 array('title' => ts('Contact ID'),
78 'no_display' => TRUE,
79 ),
80 ),
81 'grouping' => 'pcp-fields',
82 ),
83 'civicrm_contribution_page' =>
84 array(
85 'dao' => 'CRM_Contribute_DAO_ContributionPage',
86 'fields' =>
87 array(
88 'page_title' =>
89 array('title' => ts('Contribution Page Title'),
90 'name' => 'title',
91 'default' => TRUE,
92 ),
93 ),
94 'filters' =>
95 array(
96 'page_title' =>
97 array('title' => ts('Contribution Page Title'),
98 'name' => 'title',
99 'type' => CRM_Utils_Type::T_STRING,
100 ),
101 ),
102 'grouping' => 'pcp-fields',
103 ),
104 'civicrm_pcp' =>
105 array(
106 'dao' => 'CRM_PCP_DAO_PCP',
107 'fields' =>
108 array(
109 'title' =>
110 array('title' => ts('Personal Campaign Title'),
111 'default' => TRUE,
112 ),
113 'goal_amount' =>
114 array('title' => ts('Goal Amount'),
115 'type' => CRM_Utils_Type::T_MONEY,
116 'default' => TRUE,
117 ),
118 ),
119 'filters' =>
120 array(
121 'title' =>
122 array('title' => ts('Personal Campaign Title'),
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
125 ),
126 'grouping' => 'pcp-fields',
127 ),
128 'civicrm_contribution_soft' =>
129 array(
130 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
131 'fields' =>
132 array(
133 'amount_1' =>
134 array('title' => ts('Committed Amount'),
135 'name' => 'amount',
136 'type' => CRM_Utils_Type::T_MONEY,
137 'default' => TRUE,
138 'statistics' =>
139 array('sum' => ts('Committed Amount'),
140 ),
141 ),
142 'amount_2' =>
143 array('title' => ts('Amount Received'),
144 'name' => 'amount',
145 'type' => CRM_Utils_Type::T_MONEY,
146 'default' => TRUE,
147 // nice trick with dbAlias
148 'dbAlias' => 'SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount))',
149 ),
150 'soft_id' =>
151 array('title' => ts('Number of Donors'),
152 'name' => 'id',
153 'default' => TRUE,
154 'statistics' =>
155 array('count' => ts('Number of Donors'),
156 ),
157 ),
158 ),
159 'filters' =>
160 array(
161 'amount_2' =>
162 array('title' => ts('Amount Received'),
163 'type' => CRM_Utils_Type::T_MONEY,
164 'dbAlias' => 'SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount))',
165 ),
166 ),
167 'grouping' => 'pcp-fields',
168 ),
169 'civicrm_contribution' =>
170 array(
171 'dao' => 'CRM_Contribute_DAO_Contribution',
172 'fields' =>
173 array(
174 'contribution_id' =>
175 array(
176 'name' => 'id',
177 'no_display' => TRUE,
178 'required' => TRUE,
179 ),
180 'receive_date' =>
be205937 181 array('title' => ts('Most Recent Contribution'),
6a488035
TO
182 'default' => TRUE,
183 'statistics' =>
be205937 184 array('max' => ts('Most Recent Contribution'),
6a488035
TO
185 ),
186 ),
187 ),
188 'grouping' => 'pcp-fields',
189 ),
190 );
191
192 parent::__construct();
193 }
194
195 function from() {
196 $this->_from = "
197FROM civicrm_pcp {$this->_aliases['civicrm_pcp']}
198
2f4c2f5d 199LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
200 ON {$this->_aliases['civicrm_pcp']}.id =
6a488035
TO
201 {$this->_aliases['civicrm_contribution_soft']}.pcp_id
202
2f4c2f5d 203LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
204 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
6a488035
TO
205 {$this->_aliases['civicrm_contribution']}.id
206
2f4c2f5d 207LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
208 ON {$this->_aliases['civicrm_pcp']}.contact_id =
209 {$this->_aliases['civicrm_contact']}.id
6a488035
TO
210
211LEFT JOIN civicrm_contribution_page {$this->_aliases['civicrm_contribution_page']}
2f4c2f5d 212 ON {$this->_aliases['civicrm_pcp']}.page_id =
6a488035
TO
213 {$this->_aliases['civicrm_contribution_page']}.id";
214 }
215
216 function groupBy() {
217 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_pcp']}.id";
218 }
219
220 function orderBy() {
221 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name ";
222 }
223
224 function where() {
225 $whereClauses = $havingClauses = array();
226
227 foreach ($this->_columns as $tableName => $table) {
228 if (array_key_exists('filters', $table)) {
229 foreach ($table['filters'] as $fieldName => $field) {
230 $clause = NULL;
231
232 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
233 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
234 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
235 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
236 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
237 }
238 else {
239 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
240
241 if ($op) {
242 $clause = $this->whereClause($field,
243 $op,
244 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
245 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
246 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
247 );
248 }
249 }
250
251 if (!empty($clause)) {
252 if ($tableName == 'civicrm_contribution_soft' &&
253 $fieldName == 'amount_2'
254 ) {
255 $havingClauses[] = $clause;
256 }
257 else {
258 $whereClauses[] = $clause;
259 }
260 }
261 }
262 }
263 }
264 if (empty($whereClauses)) {
265 $this->_where = "WHERE ( 1 ) ";
266 }
267 else {
268 $this->_where = "WHERE " . implode(' AND ', $whereClauses);
269 }
270 if ($this->_aclWhere) {
271 $this->_where .= " AND {$this->_aclWhere} ";
272 }
273 $this->_having = "";
274 if (!empty($havingClauses)) {
275 // use this clause to construct group by clause.
276 $this->_having = "HAVING " . implode(' AND ', $havingClauses);
277 }
278 }
279
74cf4551
EM
280 /**
281 * @param $rows
282 *
283 * @return array
284 */
ecd4d80f
JM
285 function statistics(&$rows) {
286 $statistics = parent::statistics($rows);
287
288 $select = "SELECT SUM({$this->_aliases['civicrm_pcp']}.goal_amount) as goal_total, ".
289 "SUM({$this->_aliases['civicrm_contribution_soft']}.amount) as committed_total, ".
290 "COUNT({$this->_aliases['civicrm_contribution_soft']}.id) as donors_total, ".
291 "SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount)) AS received_total ";
292 $sql = "{$select} {$this->_from} {$this->_where}";
293 $dao = CRM_Core_DAO::executeQuery($sql);
294 $dao->fetch();
295 $statistics['counts']['goal_total'] = array(
296 'title' => ts('Goal Total'),
297 'value' => $dao->goal_total,
298 'type' => CRM_Utils_Type::T_MONEY
299 );
300 $statistics['counts']['committed_total'] = array(
301 'title' => ts('Total Committed'),
302 'value' => $dao->committed_total,
303 'type' => CRM_Utils_Type::T_MONEY
304 );
305 $statistics['counts']['received_total'] = array(
306 'title' => ts('Total Received'),
307 'value' => $dao->received_total,
308 'type' => CRM_Utils_Type::T_MONEY
309 );
310 $statistics['counts']['donors_total'] = array(
311 'title' => ts('Total Donors'),
312 'value' => $dao->donors_total,
313 'type' => CRM_Utils_Type::T_INT
314 );
315 return $statistics;
316 }
74cf4551
EM
317
318 /**
319 * @param $rows
320 */
6a488035
TO
321 function alterDisplay(&$rows) {
322 // custom code to alter rows
323 $entryFound = FALSE;
324 $checkList = array();
325 foreach ($rows as $rowNum => $row) {
326 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
327 // not repeat contact sort names if it matches with the one
328 // in previous row
329 $repeatFound = FALSE;
330
331 foreach ($row as $colName => $colVal) {
a7488080 332 if (!empty($checkList[$colName]) &&
6a488035
TO
333 is_array($checkList[$colName]) &&
334 in_array($colVal, $checkList[$colName])
335 ) {
336 $rows[$rowNum][$colName] = "";
337 $repeatFound = TRUE;
338 }
339 if (in_array($colName, $this->_noRepeats)) {
340 $checkList[$colName][] = $colVal;
341 }
342 }
343 }
344
345 if (array_key_exists('civicrm_contact_sort_name', $row) &&
346 $rows[$rowNum]['civicrm_contact_sort_name'] &&
347 array_key_exists('civicrm_contact_id', $row)
348 ) {
349 $url = CRM_Utils_System::url("civicrm/contact/view",
350 'reset=1&cid=' . $row['civicrm_contact_id'],
351 $this->_absoluteUrl
352 );
353 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
354 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
355 $entryFound = TRUE;
356 }
357
358 if (!$entryFound) {
359 break;
360 }
361 }
362 }
363}
364