Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
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_Membership_Summary extends CRM_Report_Form { | |
37 | ||
38 | protected $_summary = NULL; | |
39 | ||
40 | protected $_charts = array( | |
41 | '' => 'Tabular', | |
42 | 'barChart' => 'Bar Chart', | |
43 | 'pieChart' => 'Pie Chart', | |
44 | ); | |
2f4c2f5d | 45 | |
6a488035 TO |
46 | function __construct() { |
47 | // UI for selecting columns to appear in the report list | |
48 | // array conatining the columns, group_bys and filters build and provided to Form | |
49 | $this->_columns = array( | |
50 | 'civicrm_contact' => | |
51 | array( | |
52 | 'dao' => 'CRM_Contact_DAO_Contact', | |
53 | 'fields' => | |
54 | array( | |
55 | 'sort_name' => | |
56 | array('title' => ts('Member Name'), | |
57 | 'no_repeat' => TRUE, | |
58 | 'required' => TRUE, | |
59 | ), | |
60 | 'id' => | |
61 | array( | |
62 | 'no_display' => TRUE, | |
63 | 'required' => TRUE, | |
64 | ), | |
65 | ), | |
66 | 'group_bys' => | |
67 | array( | |
68 | 'id' => | |
69 | array('title' => ts('Contact ID')), | |
70 | 'display_name' => | |
71 | array('title' => ts('Contact Name'), | |
72 | ), | |
73 | ), | |
74 | 'grouping' => 'contact-fields', | |
75 | ), | |
76 | 'civicrm_membership_type' => | |
77 | array( | |
78 | 'dao' => 'CRM_Member_DAO_MembershipType', | |
79 | 'grouping' => 'member-fields', | |
80 | 'filters' => | |
81 | array( | |
82 | 'gid' => | |
83 | array( | |
84 | 'name' => 'id', | |
85 | 'title' => ts('Membership Types'), | |
86 | 'type' => CRM_Utils_Type::T_INT + CRM_Utils_Type::T_ENUM, | |
87 | 'options' => CRM_Member_PseudoConstant::membershipType(), | |
88 | ), | |
89 | ), | |
90 | ), | |
91 | 'civicrm_membership' => | |
92 | array( | |
93 | 'dao' => 'CRM_Member_DAO_Membership', | |
94 | 'grouping' => 'member-fields', | |
95 | 'fields' => | |
96 | array( | |
97 | 'membership_type_id' => | |
98 | array( | |
99 | 'title' => 'Membership Type', | |
100 | 'required' => TRUE, | |
101 | ), | |
102 | 'join_date' => NULL, | |
103 | 'start_date' => array('title' => ts('Current Cycle Start Date'), | |
104 | ), | |
105 | 'end_date' => array('title' => ts('Current Cycle End Date'), | |
106 | ), | |
107 | ), | |
108 | 'group_bys' => | |
109 | array( | |
110 | 'membership_type_id' => | |
111 | array('title' => ts('Membership Type')), | |
112 | ), | |
113 | 'filters' => | |
114 | array( | |
115 | 'join_date' => | |
116 | array('type' => CRM_Utils_Type::T_DATE), | |
117 | ), | |
118 | ), | |
119 | 'civicrm_address' => | |
120 | array( | |
121 | 'dao' => 'CRM_Core_DAO_Address', | |
122 | 'fields' => | |
123 | array( | |
124 | 'street_address' => NULL, | |
125 | 'city' => NULL, | |
126 | 'postal_code' => NULL, | |
127 | 'state_province_id' => | |
128 | array('title' => ts('State/Province'), | |
129 | ), | |
130 | 'country_id' => | |
131 | array('title' => ts('Country'), | |
132 | 'default' => TRUE, | |
133 | ), | |
134 | ), | |
135 | 'grouping' => 'contact-fields', | |
136 | ), | |
137 | 'civicrm_email' => | |
138 | array( | |
139 | 'dao' => 'CRM_Core_DAO_Email', | |
140 | 'fields' => | |
141 | array('email' => NULL), | |
142 | 'grouping' => 'contact-fields', | |
143 | ), | |
144 | 'civicrm_contribution' => | |
145 | array( | |
146 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
147 | 'filters' => | |
148 | array( | |
149 | 'total_amount' => | |
150 | array('title' => ts('Contribution Amount'), | |
151 | ), | |
152 | ), | |
153 | ), | |
154 | ); | |
155 | parent::__construct(); | |
156 | } | |
157 | ||
158 | function preProcess() { | |
159 | $this->assign('reportTitle', ts('Membership Summary Report')); | |
160 | parent::preProcess(); | |
161 | } | |
162 | ||
163 | function setDefaultValues() { | |
164 | return parent::setDefaultValues(); | |
165 | } | |
166 | ||
167 | function select() { | |
168 | $select = array(); | |
169 | $this->_columnHeaders = array(); | |
170 | foreach ($this->_columns as $tableName => $table) { | |
171 | if (array_key_exists('fields', $table)) { | |
172 | foreach ($table['fields'] as $fieldName => $field) { | |
173 | if (CRM_Utils_Array::value('required', $field) || | |
174 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
175 | ) { | |
176 | // to include optional columns address and email, only if checked | |
177 | if ($tableName == 'civicrm_address') { | |
178 | $this->_addressField = TRUE; | |
179 | $this->_emailField = TRUE; | |
180 | } | |
181 | elseif ($tableName == 'civicrm_email') { | |
182 | $this->_emailField = TRUE; | |
183 | } | |
184 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
185 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type']; | |
186 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
192 | } | |
193 | ||
194 | static function formRule($fields, $files, $self) { | |
195 | $errors = $grouping = array(); | |
196 | //check for searching combination of dispaly columns and | |
197 | //grouping criteria | |
198 | ||
199 | return $errors; | |
200 | } | |
201 | ||
202 | function from() { | |
203 | $this->_from = NULL; | |
204 | ||
205 | $this->_from = " | |
206 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
2f4c2f5d | 207 | INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']} |
6a488035 | 208 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id |
2f4c2f5d | 209 | LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']} |
6a488035 | 210 | ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id |
2f4c2f5d | 211 | LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
212 | ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id |
213 | "; | |
214 | // include address field if address column is to be included | |
215 | if ($this->_addressField) { | |
216 | $this->_from .= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n"; | |
217 | } | |
218 | ||
219 | // include email field if email column is to be included | |
220 | if ($this->_emailField) { | |
221 | $this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n"; | |
222 | } | |
223 | } | |
224 | ||
225 | function where() { | |
226 | $clauses = array(); | |
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 | if ($field['type'] & CRM_Utils_Type::T_DATE) { | |
232 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
233 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
234 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
235 | ||
236 | if ($relative || $from || $to) { | |
237 | $clause = $this->dateClause($field['name'], $relative, $from, $to); | |
238 | } | |
239 | } | |
240 | else { | |
241 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
242 | if ($op) { | |
243 | $clause = $this->whereClause($field, | |
244 | $op, | |
245 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
246 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
247 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
248 | ); | |
249 | } | |
250 | } | |
251 | ||
252 | if (!empty($clause)) { | |
253 | $clauses[] = $clause; | |
254 | } | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | if (empty($clauses)) { | |
260 | $this->_where = "WHERE ( 1 ) "; | |
261 | } | |
262 | else { | |
263 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
264 | } | |
265 | } | |
266 | ||
267 | function statistics(&$rows) { | |
268 | $statistics = array(); | |
269 | $statistics[] = array('title' => ts('Row(s) Listed'), | |
270 | 'value' => count($rows), | |
271 | ); | |
272 | return $statistics; | |
273 | } | |
274 | ||
275 | function groupBy() { | |
276 | $this->_groupBy = ""; | |
277 | if (is_array($this->_params['group_bys']) && | |
278 | !empty($this->_params['group_bys']) | |
279 | ) { | |
280 | foreach ($this->_columns as $tableName => $table) { | |
281 | if (array_key_exists('group_bys', $table)) { | |
282 | foreach ($table['group_bys'] as $fieldName => $field) { | |
283 | if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) { | |
284 | $this->_groupBy[] = $field['dbAlias']; | |
285 | } | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | if (!empty($this->_statFields) && | |
291 | (($append && count($this->_groupBy) <= 1) || (!$append)) | |
292 | ) { | |
293 | $this->_rollup = " WITH ROLLUP"; | |
294 | } | |
295 | $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy) . " {$this->_rollup} "; | |
296 | } | |
297 | else { | |
298 | $this->_groupBy = "GROUP BY contact.id"; | |
299 | } | |
300 | } | |
301 | ||
302 | function postProcess() { | |
303 | $this->_params = $this->controller->exportValues($this->_name); | |
304 | if (empty($this->_params) && | |
305 | $this->_force | |
306 | ) { | |
307 | $this->_params = $this->_formValues; | |
308 | } | |
309 | $this->_formValues = $this->_params; | |
310 | ||
311 | $this->processReportMode(); | |
312 | ||
313 | $this->select(); | |
314 | ||
315 | $this->from(); | |
316 | ||
317 | $this->where(); | |
318 | ||
319 | $this->groupBy(); | |
320 | ||
321 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; | |
322 | ||
323 | $dao = CRM_Core_DAO::executeQuery($sql); | |
324 | $rows = $graphRows = array(); | |
325 | $count = 0; | |
326 | while ($dao->fetch()) { | |
327 | $row = array(); | |
328 | foreach ($this->_columnHeaders as $key => $value) { | |
329 | $row[$key] = $dao->$key; | |
330 | } | |
331 | ||
332 | if (CRM_Utils_Array::value('charts', $this->_params) && | |
333 | $row['civicrm_contribution_receive_date_subtotal'] | |
334 | ) { | |
335 | $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start']; | |
336 | $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval']; | |
337 | $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum']; | |
338 | $count++; | |
339 | } | |
340 | ||
341 | $rows[] = $row; | |
342 | } | |
343 | $this->formatDisplay($rows); | |
344 | ||
345 | $this->assign_by_ref('columnHeaders', $this->_columnHeaders); | |
346 | $this->assign_by_ref('rows', $rows); | |
347 | $this->assign('statistics', $this->statistics($rows)); | |
348 | ||
349 | if (CRM_Utils_Array::value('charts', $this->_params)) { | |
350 | foreach (array( | |
351 | 'receive_date', $this->_interval, 'value') as $ignore) { | |
352 | unset($graphRows[$ignore][$count - 1]); | |
353 | } | |
354 | ||
355 | // build chart. | |
356 | CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); | |
357 | } | |
358 | parent::endPostProcess(); | |
359 | } | |
360 | ||
361 | function alterDisplay(&$rows) { | |
362 | // custom code to alter rows | |
363 | $entryFound = FALSE; | |
364 | $checkList = array(); | |
365 | ||
366 | foreach ($rows as $rowNum => $row) { | |
367 | ||
368 | if (!empty($this->_noRepeats)) { | |
369 | // not repeat contact display names if it matches with the one | |
370 | // in previous row | |
371 | ||
372 | $repeatFound = FALSE; | |
373 | foreach ($row as $colName => $colVal) { | |
374 | if (is_array($checkList[$colName]) && | |
375 | in_array($colVal, $checkList[$colName]) | |
376 | ) { | |
377 | $rows[$rowNum][$colName] = ""; | |
378 | $repeatFound = TRUE; | |
379 | } | |
380 | if (in_array($colName, $this->_noRepeats)) { | |
381 | $checkList[$colName][] = $colVal; | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | //handle the Membership Type Ids | |
387 | if (array_key_exists('civicrm_membership_membership_type_id', $row)) { | |
388 | if ($value = $row['civicrm_membership_membership_type_id']) { | |
389 | $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE); | |
390 | } | |
391 | $entryFound = TRUE; | |
392 | } | |
393 | ||
394 | // handle state province | |
395 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
396 | if ($value = $row['civicrm_address_state_province_id']) { | |
397 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE); | |
398 | } | |
399 | $entryFound = TRUE; | |
400 | } | |
401 | ||
402 | // handle country | |
403 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
404 | if ($value = $row['civicrm_address_country_id']) { | |
405 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
406 | } | |
407 | $entryFound = TRUE; | |
408 | } | |
409 | ||
410 | // convert display name to links | |
411 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
412 | array_key_exists('civicrm_contact_id', $row) | |
413 | ) { | |
414 | $url = CRM_Utils_System::url('civicrm/report/member/detail', | |
415 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], | |
416 | $this->_absoluteUrl | |
417 | ); | |
418 | $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>'; | |
419 | $entryFound = TRUE; | |
420 | } | |
421 | ||
422 | // skip looking further in rows, if first row itself doesn't | |
423 | // have the column we need | |
424 | if (!$entryFound) { | |
425 | break; | |
426 | } | |
427 | } | |
428 | } | |
429 | } | |
430 |