Merge pull request #4737 from eileenmcnaughton/master
[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(
9d72cede 45 'civicrm_contact' => array(
6a488035 46 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
47 'fields' => array(
48 'sort_name' => array(
49 'title' => ts('Supporter'),
6a488035
TO
50 'required' => TRUE,
51 'default' => TRUE,
52 ),
9d72cede 53 'id' => array(
6a488035
TO
54 'required' => TRUE,
55 'no_display' => TRUE,
56 ),
9d72cede 57 'contact_type' => array(
30f85891
RN
58 'title' => ts('Supporter Contact Type'),
59 ),
9d72cede 60 'contact_sub_type' => array(
b8f96eb8 61 'title' => ts('Supporter Contact Subtype'),
30f85891 62 ),
6a488035 63 ),
9d72cede
EM
64 'filters' => array(
65 'sort_name' => array(
66 'title' => ts('Supporter Name'),
6a488035
TO
67 'type' => CRM_Utils_Type::T_STRING,
68 'operator' => 'like',
69 ),
9d72cede
EM
70 'id' => array(
71 'title' => ts('Contact ID'),
6a488035
TO
72 'no_display' => TRUE,
73 ),
74 ),
75 'grouping' => 'pcp-fields',
76 ),
9d72cede 77 'civicrm_contribution_page' => array(
6a488035 78 'dao' => 'CRM_Contribute_DAO_ContributionPage',
9d72cede
EM
79 'fields' => array(
80 'page_title' => array(
81 'title' => ts('Contribution Page Title'),
6a488035
TO
82 'name' => 'title',
83 'default' => TRUE,
84 ),
85 ),
9d72cede
EM
86 'filters' => array(
87 'page_title' => array(
88 'title' => ts('Contribution Page Title'),
6a488035
TO
89 'name' => 'title',
90 'type' => CRM_Utils_Type::T_STRING,
91 ),
92 ),
93 'grouping' => 'pcp-fields',
94 ),
9d72cede 95 'civicrm_pcp' => array(
6a488035 96 'dao' => 'CRM_PCP_DAO_PCP',
9d72cede
EM
97 'fields' => array(
98 'title' => array(
99 'title' => ts('Personal Campaign Title'),
6a488035
TO
100 'default' => TRUE,
101 ),
9d72cede
EM
102 'goal_amount' => array(
103 'title' => ts('Goal Amount'),
6a488035
TO
104 'type' => CRM_Utils_Type::T_MONEY,
105 'default' => TRUE,
106 ),
107 ),
9d72cede
EM
108 'filters' => array(
109 'title' => array(
110 'title' => ts('Personal Campaign Title'),
6a488035
TO
111 'type' => CRM_Utils_Type::T_STRING,
112 ),
113 ),
114 'grouping' => 'pcp-fields',
115 ),
9d72cede 116 'civicrm_contribution_soft' => array(
6a488035 117 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
9d72cede
EM
118 'fields' => array(
119 'amount_1' => array(
120 'title' => ts('Committed Amount'),
6a488035
TO
121 'name' => 'amount',
122 'type' => CRM_Utils_Type::T_MONEY,
123 'default' => TRUE,
9d72cede
EM
124 'statistics' => array(
125 'sum' => ts('Committed Amount'),
6a488035
TO
126 ),
127 ),
9d72cede
EM
128 'amount_2' => array(
129 'title' => ts('Amount Received'),
6a488035
TO
130 'name' => 'amount',
131 'type' => CRM_Utils_Type::T_MONEY,
132 'default' => TRUE,
133 // nice trick with dbAlias
134 'dbAlias' => 'SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount))',
135 ),
9d72cede
EM
136 'soft_id' => array(
137 'title' => ts('Number of Donors'),
6a488035
TO
138 'name' => 'id',
139 'default' => TRUE,
9d72cede
EM
140 'statistics' => array(
141 'count' => ts('Number of Donors'),
6a488035
TO
142 ),
143 ),
144 ),
9d72cede
EM
145 'filters' => array(
146 'amount_2' => array(
147 'title' => ts('Amount Received'),
6a488035
TO
148 'type' => CRM_Utils_Type::T_MONEY,
149 'dbAlias' => 'SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount))',
150 ),
151 ),
152 'grouping' => 'pcp-fields',
153 ),
9d72cede 154 'civicrm_contribution' => array(
6a488035 155 'dao' => 'CRM_Contribute_DAO_Contribution',
9d72cede
EM
156 'fields' => array(
157 'contribution_id' => array(
6a488035
TO
158 'name' => 'id',
159 'no_display' => TRUE,
160 'required' => TRUE,
161 ),
9d72cede
EM
162 'receive_date' => array(
163 'title' => ts('Most Recent Contribution'),
6a488035 164 'default' => TRUE,
9d72cede
EM
165 'statistics' => array(
166 'max' => ts('Most Recent Contribution'),
6a488035
TO
167 ),
168 ),
169 ),
170 'grouping' => 'pcp-fields',
171 ),
172 );
173
174 parent::__construct();
175 }
176
177 function from() {
178 $this->_from = "
179FROM civicrm_pcp {$this->_aliases['civicrm_pcp']}
180
2f4c2f5d 181LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
182 ON {$this->_aliases['civicrm_pcp']}.id =
6a488035
TO
183 {$this->_aliases['civicrm_contribution_soft']}.pcp_id
184
2f4c2f5d 185LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
186 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
6a488035
TO
187 {$this->_aliases['civicrm_contribution']}.id
188
2f4c2f5d 189LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
190 ON {$this->_aliases['civicrm_pcp']}.contact_id =
191 {$this->_aliases['civicrm_contact']}.id
6a488035
TO
192
193LEFT JOIN civicrm_contribution_page {$this->_aliases['civicrm_contribution_page']}
2f4c2f5d 194 ON {$this->_aliases['civicrm_pcp']}.page_id =
6a488035
TO
195 {$this->_aliases['civicrm_contribution_page']}.id";
196 }
197
198 function groupBy() {
199 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_pcp']}.id";
200 }
201
202 function orderBy() {
203 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name ";
204 }
205
206 function where() {
207 $whereClauses = $havingClauses = array();
208
209 foreach ($this->_columns as $tableName => $table) {
210 if (array_key_exists('filters', $table)) {
211 foreach ($table['filters'] as $fieldName => $field) {
212 $clause = NULL;
213
214 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
215 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
216 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
217 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
218 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
6a488035
TO
219 }
220 else {
221 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
222
223 if ($op) {
224 $clause = $this->whereClause($field,
225 $op,
226 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
227 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
228 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
229 );
230 }
231 }
232
233 if (!empty($clause)) {
234 if ($tableName == 'civicrm_contribution_soft' &&
235 $fieldName == 'amount_2'
236 ) {
237 $havingClauses[] = $clause;
238 }
239 else {
240 $whereClauses[] = $clause;
241 }
242 }
243 }
244 }
245 }
246 if (empty($whereClauses)) {
247 $this->_where = "WHERE ( 1 ) ";
248 }
249 else {
250 $this->_where = "WHERE " . implode(' AND ', $whereClauses);
251 }
252 if ($this->_aclWhere) {
253 $this->_where .= " AND {$this->_aclWhere} ";
254 }
255 $this->_having = "";
256 if (!empty($havingClauses)) {
257 // use this clause to construct group by clause.
258 $this->_having = "HAVING " . implode(' AND ', $havingClauses);
259 }
260 }
261
74cf4551
EM
262 /**
263 * @param $rows
264 *
265 * @return array
266 */
ecd4d80f
JM
267 function statistics(&$rows) {
268 $statistics = parent::statistics($rows);
269
9d72cede
EM
270 $select =
271 "SELECT SUM({$this->_aliases['civicrm_pcp']}.goal_amount) as goal_total, " .
272 "SUM({$this->_aliases['civicrm_contribution_soft']}.amount) as committed_total, " .
273 "COUNT({$this->_aliases['civicrm_contribution_soft']}.id) as donors_total, " .
ecd4d80f
JM
274 "SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount)) AS received_total ";
275 $sql = "{$select} {$this->_from} {$this->_where}";
276 $dao = CRM_Core_DAO::executeQuery($sql);
277 $dao->fetch();
278 $statistics['counts']['goal_total'] = array(
279 'title' => ts('Goal Total'),
280 'value' => $dao->goal_total,
281 'type' => CRM_Utils_Type::T_MONEY
282 );
283 $statistics['counts']['committed_total'] = array(
284 'title' => ts('Total Committed'),
285 'value' => $dao->committed_total,
286 'type' => CRM_Utils_Type::T_MONEY
287 );
288 $statistics['counts']['received_total'] = array(
289 'title' => ts('Total Received'),
290 'value' => $dao->received_total,
291 'type' => CRM_Utils_Type::T_MONEY
292 );
293 $statistics['counts']['donors_total'] = array(
294 'title' => ts('Total Donors'),
295 'value' => $dao->donors_total,
296 'type' => CRM_Utils_Type::T_INT
297 );
298 return $statistics;
299 }
74cf4551
EM
300
301 /**
302 * @param $rows
303 */
6a488035
TO
304 function alterDisplay(&$rows) {
305 // custom code to alter rows
306 $entryFound = FALSE;
307 $checkList = array();
308 foreach ($rows as $rowNum => $row) {
309 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
310 // not repeat contact sort names if it matches with the one
311 // in previous row
312 $repeatFound = FALSE;
313
314 foreach ($row as $colName => $colVal) {
a7488080 315 if (!empty($checkList[$colName]) &&
6a488035
TO
316 is_array($checkList[$colName]) &&
317 in_array($colVal, $checkList[$colName])
318 ) {
319 $rows[$rowNum][$colName] = "";
320 $repeatFound = TRUE;
321 }
322 if (in_array($colName, $this->_noRepeats)) {
323 $checkList[$colName][] = $colVal;
324 }
325 }
326 }
327
328 if (array_key_exists('civicrm_contact_sort_name', $row) &&
329 $rows[$rowNum]['civicrm_contact_sort_name'] &&
330 array_key_exists('civicrm_contact_id', $row)
331 ) {
332 $url = CRM_Utils_System::url("civicrm/contact/view",
333 'reset=1&cid=' . $row['civicrm_contact_id'],
334 $this->_absoluteUrl
335 );
336 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
337 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
338 $entryFound = TRUE;
339 }
340
341 if (!$entryFound) {
342 break;
343 }
344 }
345 }
346}
347