-- CRM-13250 Added Contact Type and Contact SubType columns in report
[civicrm-core.git] / CRM / Report / Form / Contribute / PCP.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
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 */
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 ),
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 ),
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
193 LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
194 ON {$this->_aliases['civicrm_pcp']}.id =
195 {$this->_aliases['civicrm_contribution_soft']}.pcp_id
196
197 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
198 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
199 {$this->_aliases['civicrm_contribution']}.id
200
201 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
202 ON {$this->_aliases['civicrm_pcp']}.contact_id =
203 {$this->_aliases['civicrm_contact']}.id
204
205 LEFT JOIN civicrm_contribution_page {$this->_aliases['civicrm_contribution_page']}
206 ON {$this->_aliases['civicrm_pcp']}.page_id =
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
274 function alterDisplay(&$rows) {
275 // custom code to alter rows
276 $entryFound = FALSE;
277 $checkList = array();
278 foreach ($rows as $rowNum => $row) {
279 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
280 // not repeat contact sort names if it matches with the one
281 // in previous row
282 $repeatFound = FALSE;
283
284 foreach ($row as $colName => $colVal) {
285 if (CRM_Utils_Array::value($colName, $checkList) &&
286 is_array($checkList[$colName]) &&
287 in_array($colVal, $checkList[$colName])
288 ) {
289 $rows[$rowNum][$colName] = "";
290 $repeatFound = TRUE;
291 }
292 if (in_array($colName, $this->_noRepeats)) {
293 $checkList[$colName][] = $colVal;
294 }
295 }
296 }
297
298 if (array_key_exists('civicrm_contact_sort_name', $row) &&
299 $rows[$rowNum]['civicrm_contact_sort_name'] &&
300 array_key_exists('civicrm_contact_id', $row)
301 ) {
302 $url = CRM_Utils_System::url("civicrm/contact/view",
303 'reset=1&cid=' . $row['civicrm_contact_id'],
304 $this->_absoluteUrl
305 );
306 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
307 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
308 $entryFound = TRUE;
309 }
310
311 if (!$entryFound) {
312 break;
313 }
314 }
315 }
316 }
317