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