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