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