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