Merge pull request #1496 from ravishnair/CRM-13012fix
[civicrm-core.git] / CRM / Report / Form / Contribute / PCP.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Contribute_PCP extends CRM_Report_Form {
37 function __construct() {
38 $this->_columns = array(
39 'civicrm_contact' =>
40 array(
41 'dao' => 'CRM_Contact_DAO_Contact',
42 'fields' =>
43 array(
44 'sort_name' =>
45 array('title' => ts('Supporter'),
46 'required' => TRUE,
47 'default' => TRUE,
48 ),
49 'id' =>
50 array(
51 'required' => TRUE,
52 'no_display' => TRUE,
53 ),
54 ),
55 'filters' =>
56 array(
57 'sort_name' =>
58 array('title' => ts('Supporter Name'),
59 'type' => CRM_Utils_Type::T_STRING,
60 'operator' => 'like',
61 ),
62 'id' =>
63 array('title' => ts('Contact ID'),
64 'no_display' => TRUE,
65 ),
66 ),
67 'grouping' => 'pcp-fields',
68 ),
69 'civicrm_contribution_page' =>
70 array(
71 'dao' => 'CRM_Contribute_DAO_ContributionPage',
72 'fields' =>
73 array(
74 'page_title' =>
75 array('title' => ts('Contribution Page Title'),
76 'name' => 'title',
77 'default' => TRUE,
78 ),
79 ),
80 'filters' =>
81 array(
82 'page_title' =>
83 array('title' => ts('Contribution Page Title'),
84 'name' => 'title',
85 'type' => CRM_Utils_Type::T_STRING,
86 ),
87 ),
88 'grouping' => 'pcp-fields',
89 ),
90 'civicrm_pcp' =>
91 array(
92 'dao' => 'CRM_PCP_DAO_PCP',
93 'fields' =>
94 array(
95 'title' =>
96 array('title' => ts('Personal Campaign Title'),
97 'default' => TRUE,
98 ),
99 'goal_amount' =>
100 array('title' => ts('Goal Amount'),
101 'type' => CRM_Utils_Type::T_MONEY,
102 'default' => TRUE,
103 ),
104 ),
105 'filters' =>
106 array(
107 'title' =>
108 array('title' => ts('Personal Campaign Title'),
109 'type' => CRM_Utils_Type::T_STRING,
110 ),
111 ),
112 'grouping' => 'pcp-fields',
113 ),
114 'civicrm_contribution_soft' =>
115 array(
116 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
117 'fields' =>
118 array(
119 'amount_1' =>
120 array('title' => ts('Committed Amount'),
121 'name' => 'amount',
122 'type' => CRM_Utils_Type::T_MONEY,
123 'default' => TRUE,
124 'statistics' =>
125 array('sum' => ts('Committed Amount'),
126 ),
127 ),
128 'amount_2' =>
129 array('title' => ts('Amount Received'),
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 ),
136 'soft_id' =>
137 array('title' => ts('Number of Donors'),
138 'name' => 'id',
139 'default' => TRUE,
140 'statistics' =>
141 array('count' => ts('Number of Donors'),
142 ),
143 ),
144 ),
145 'filters' =>
146 array(
147 'amount_2' =>
148 array('title' => ts('Amount Received'),
149 'type' => CRM_Utils_Type::T_MONEY,
150 'dbAlias' => 'SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount))',
151 ),
152 ),
153 'grouping' => 'pcp-fields',
154 ),
155 'civicrm_contribution' =>
156 array(
157 'dao' => 'CRM_Contribute_DAO_Contribution',
158 'fields' =>
159 array(
160 'contribution_id' =>
161 array(
162 'name' => 'id',
163 'no_display' => TRUE,
164 'required' => TRUE,
165 ),
166 'receive_date' =>
167 array('title' => ts('Most Recent Donation'),
168 'default' => TRUE,
169 'statistics' =>
170 array('max' => ts('Most Recent Donation'),
171 ),
172 ),
173 ),
174 'grouping' => 'pcp-fields',
175 ),
176 );
177
178 parent::__construct();
179 }
180
181 function from() {
182 $this->_from = "
183FROM civicrm_pcp {$this->_aliases['civicrm_pcp']}
184
2f4c2f5d 185LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
186 ON {$this->_aliases['civicrm_pcp']}.id =
6a488035
TO
187 {$this->_aliases['civicrm_contribution_soft']}.pcp_id
188
2f4c2f5d 189LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
190 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
6a488035
TO
191 {$this->_aliases['civicrm_contribution']}.id
192
2f4c2f5d 193LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
194 ON {$this->_aliases['civicrm_pcp']}.contact_id =
195 {$this->_aliases['civicrm_contact']}.id
6a488035
TO
196
197LEFT JOIN civicrm_contribution_page {$this->_aliases['civicrm_contribution_page']}
2f4c2f5d 198 ON {$this->_aliases['civicrm_pcp']}.page_id =
6a488035
TO
199 {$this->_aliases['civicrm_contribution_page']}.id";
200 }
201
202 function groupBy() {
203 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_pcp']}.id";
204 }
205
206 function orderBy() {
207 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name ";
208 }
209
210 function where() {
211 $whereClauses = $havingClauses = array();
212
213 foreach ($this->_columns as $tableName => $table) {
214 if (array_key_exists('filters', $table)) {
215 foreach ($table['filters'] as $fieldName => $field) {
216 $clause = NULL;
217
218 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
219 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
220 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
221 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
222 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
223 }
224 else {
225 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
226
227 if ($op) {
228 $clause = $this->whereClause($field,
229 $op,
230 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
231 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
232 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
233 );
234 }
235 }
236
237 if (!empty($clause)) {
238 if ($tableName == 'civicrm_contribution_soft' &&
239 $fieldName == 'amount_2'
240 ) {
241 $havingClauses[] = $clause;
242 }
243 else {
244 $whereClauses[] = $clause;
245 }
246 }
247 }
248 }
249 }
250 if (empty($whereClauses)) {
251 $this->_where = "WHERE ( 1 ) ";
252 }
253 else {
254 $this->_where = "WHERE " . implode(' AND ', $whereClauses);
255 }
256 if ($this->_aclWhere) {
257 $this->_where .= " AND {$this->_aclWhere} ";
258 }
259 $this->_having = "";
260 if (!empty($havingClauses)) {
261 // use this clause to construct group by clause.
262 $this->_having = "HAVING " . implode(' AND ', $havingClauses);
263 }
264 }
265
266 function alterDisplay(&$rows) {
267 // custom code to alter rows
268 $entryFound = FALSE;
269 $checkList = array();
270 foreach ($rows as $rowNum => $row) {
271 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
272 // not repeat contact sort names if it matches with the one
273 // in previous row
274 $repeatFound = FALSE;
275
276 foreach ($row as $colName => $colVal) {
277 if (CRM_Utils_Array::value($colName, $checkList) &&
278 is_array($checkList[$colName]) &&
279 in_array($colVal, $checkList[$colName])
280 ) {
281 $rows[$rowNum][$colName] = "";
282 $repeatFound = TRUE;
283 }
284 if (in_array($colName, $this->_noRepeats)) {
285 $checkList[$colName][] = $colVal;
286 }
287 }
288 }
289
290 if (array_key_exists('civicrm_contact_sort_name', $row) &&
291 $rows[$rowNum]['civicrm_contact_sort_name'] &&
292 array_key_exists('civicrm_contact_id', $row)
293 ) {
294 $url = CRM_Utils_System::url("civicrm/contact/view",
295 'reset=1&cid=' . $row['civicrm_contact_id'],
296 $this->_absoluteUrl
297 );
298 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
299 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
300 $entryFound = TRUE;
301 }
302
303 if (!$entryFound) {
304 break;
305 }
306 }
307 }
308}
309