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