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