Merge pull request #15214 from civicrm/5.17
[civicrm-core.git] / CRM / Report / Form / Membership / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33 class CRM_Report_Form_Membership_Summary extends CRM_Report_Form {
34
35 protected $_summary = NULL;
36
37 protected $_charts = [
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 = [
50 'civicrm_contact' => [
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'fields' => [
53 'sort_name' => [
54 'title' => ts('Member Name'),
55 'no_repeat' => TRUE,
56 'required' => TRUE,
57 ],
58 'id' => [
59 'no_display' => TRUE,
60 'required' => TRUE,
61 ],
62 ],
63 'group_bys' => [
64 'id' => ['title' => ts('Contact ID')],
65 'display_name' => [
66 'title' => ts('Contact Name'),
67 ],
68 ],
69 'grouping' => 'contact-fields',
70 ],
71 'civicrm_membership_type' => [
72 'dao' => 'CRM_Member_DAO_MembershipType',
73 'grouping' => 'member-fields',
74 'filters' => [
75 'gid' => [
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' => [
84 'dao' => 'CRM_Member_DAO_Membership',
85 'grouping' => 'member-fields',
86 'fields' => [
87 'membership_type_id' => [
88 'title' => ts('Membership Type'),
89 'required' => TRUE,
90 ],
91 'join_date' => NULL,
92 'start_date' => [
93 'title' => ts('Current Cycle Start Date'),
94 ],
95 'end_date' => [
96 'title' => ts('Current Cycle End Date'),
97 ],
98 ],
99 'group_bys' => [
100 'membership_type_id' => ['title' => ts('Membership Type')],
101 ],
102 'filters' => [
103 'membership_join_date' => ['type' => CRM_Utils_Type::T_DATE],
104 ],
105 ],
106 'civicrm_address' => [
107 'dao' => 'CRM_Core_DAO_Address',
108 'fields' => [
109 'street_address' => NULL,
110 'city' => NULL,
111 'postal_code' => NULL,
112 'state_province_id' => [
113 'title' => ts('State/Province'),
114 ],
115 'country_id' => [
116 'title' => ts('Country'),
117 'default' => TRUE,
118 ],
119 ],
120 'grouping' => 'contact-fields',
121 ],
122 'civicrm_email' => [
123 'dao' => 'CRM_Core_DAO_Email',
124 'fields' => ['email' => NULL],
125 'grouping' => 'contact-fields',
126 ],
127 'civicrm_contribution' => [
128 'dao' => 'CRM_Contribute_DAO_Contribution',
129 'filters' => [
130 'total_amount' => [
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 // @todo remove this in favour of just using parent.
152 $select = [];
153 $this->_columnHeaders = [];
154 foreach ($this->_columns as $tableName => $table) {
155 if (array_key_exists('fields', $table)) {
156 foreach ($table['fields'] as $fieldName => $field) {
157 if (!empty($field['required']) ||
158 !empty($this->_params['fields'][$fieldName])
159 ) {
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
170 /**
171 * Generate from clause.
172 */
173 public function from() {
174 $this->_from = NULL;
175
176 $this->_from = "
177 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
178 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
179 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
180 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
181 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
182 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
183 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
184 ";
185 $this->joinAddressFromContact();
186 $this->joinEmailFromContact();
187 }
188
189 /**
190 * Generate where clause.
191 *
192 * @todo this looks like it duplicates the parent & could go.
193 */
194 public function where() {
195 $clauses = [];
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);
202 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
203 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
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
236 /**
237 * Generate statistics (bottom section of the report).
238 *
239 * @param array $rows
240 *
241 * @return array
242 */
243 public function statistics(&$rows) {
244 $statistics = [];
245 $statistics[] = [
246 'title' => ts('Row(s) Listed'),
247 'value' => count($rows),
248 ];
249 return $statistics;
250 }
251
252 /**
253 * Generate group by clause.
254 *
255 * @todo looks like a broken duplicate of the parent.
256 */
257 public function groupBy() {
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) {
265 if (!empty($this->_params['group_bys'][$fieldName])) {
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 }
277 $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy) .
278 " {$this->_rollup} ";
279 }
280 else {
281 $this->_groupBy = "GROUP BY contact.id";
282 }
283 }
284
285 /**
286 * PostProcess function.
287 */
288 public function postProcess() {
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
309 $dao = CRM_Core_DAO::executeQuery($sql);
310 $rows = $graphRows = [];
311 $count = 0;
312 while ($dao->fetch()) {
313 $row = [];
314 foreach ($this->_columnHeaders as $key => $value) {
315 $row[$key] = $dao->$key;
316 }
317
318 if (!empty($this->_params['charts']) &&
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
335 if (!empty($this->_params['charts'])) {
336 foreach ([
337 'receive_date',
338 $this->_interval,
339 'value',
340 ] as $ignore) {
341 unset($graphRows[$ignore][$count - 1]);
342 }
343
344 // build chart.
345 CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval);
346 }
347 parent::endPostProcess();
348 }
349
350 /**
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.
355 *
356 * @param array $rows
357 * Rows generated by SQL, with an array for each row.
358 */
359 public function alterDisplay(&$rows) {
360 $entryFound = FALSE;
361 $checkList = [];
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
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 ) {
395 $url = CRM_Utils_System::url(
396 'civicrm/report/member/detail',
397 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
398 $this->_absoluteUrl
399 );
400 $rows[$rowNum]['civicrm_contact_sort_name']
401 = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
402 $entryFound = TRUE;
403 }
404
405 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
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 }
413
414 }