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