Merge pull request #11921 from civicrm/5.0
[civicrm-core.git] / CRM / Report / Form / Member / Lapse.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 * $Id$
33 *
34 */
35 class CRM_Report_Form_Member_Lapse extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38 protected $_charts = array('' => 'Tabular');
39 protected $_customGroupExtends = array(
40 'Membership',
41 );
42 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
43
44 /**
45 * This report has not been optimised for group filtering.
46 *
47 * The functionality for group filtering has been improved but not
48 * all reports have been adjusted to take care of it. This report has not
49 * and will run an inefficient query until fixed.
50 *
51 * CRM-19170
52 *
53 * @var bool
54 */
55 protected $groupFilterNotOptimised = TRUE;
56
57 /**
58 * Class constructor.
59 */
60 public function __construct() {
61
62 // Check if CiviCampaign is a) enabled and b) has active campaigns
63 $config = CRM_Core_Config::singleton();
64 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
65 if ($campaignEnabled) {
66 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
67 $this->activeCampaigns = $getCampaigns['campaigns'];
68 asort($this->activeCampaigns);
69 }
70
71 // UI for selecting columns to appear in the report list
72 // array containing the columns, group_bys and filters build and provided to Form
73 $this->_columns = array(
74 'civicrm_contact' => array(
75 'dao' => 'CRM_Contact_DAO_Contact',
76 'fields' => array(
77 'sort_name' => array(
78 'title' => ts('Member Name'),
79 'no_repeat' => TRUE,
80 'required' => TRUE,
81 ),
82 'id' => array(
83 'no_display' => TRUE,
84 'required' => TRUE,
85 ),
86 'first_name' => array(
87 'title' => ts('First Name'),
88 'no_repeat' => TRUE,
89 ),
90 'last_name' => array(
91 'title' => ts('Last Name'),
92 'no_repeat' => TRUE,
93 ),
94 'contact_type' => array(
95 'title' => ts('Contact Type'),
96 ),
97 'contact_sub_type' => array(
98 'title' => ts('Contact Subtype'),
99 ),
100 ),
101 'grouping' => 'contact-fields',
102 ),
103 'civicrm_membership_type' => array(
104 'dao' => 'CRM_Member_DAO_MembershipType',
105 'grouping' => 'member-fields',
106 'filters' => array(
107 'tid' => array(
108 'name' => 'id',
109 'title' => ts('Membership Types'),
110 'type' => CRM_Utils_Type::T_INT,
111 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
112 'options' => CRM_Member_PseudoConstant::membershipType(),
113 ),
114 ),
115 ),
116 'civicrm_membership' => array(
117 'dao' => 'CRM_Member_DAO_Membership',
118 'grouping' => 'member-fields',
119 'fields' => array(
120 'membership_type_id' => array(
121 'title' => ts('Membership Type'),
122 'required' => TRUE,
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
125 'membership_start_date' => array(
126 'title' => ts('Current Cycle Start Date'),
127 ),
128 'membership_end_date' => array(
129 'title' => ts('Membership Lapse Date'),
130 'required' => TRUE,
131 ),
132 ),
133 'filters' => array(
134 'membership_end_date' => array(
135 'title' => ts('Lapsed Memberships'),
136 'operatorType' => CRM_Report_Form::OP_DATE,
137 ),
138 ),
139 ),
140 'civicrm_membership_status' => array(
141 'dao' => 'CRM_Member_DAO_MembershipStatus',
142 'alias' => 'mem_status',
143 'fields' => array(
144 'name' => array(
145 'title' => ts('Current Status'),
146 'required' => TRUE,
147 ),
148 ),
149 'grouping' => 'member-fields',
150 ),
151 'civicrm_address' => array(
152 'dao' => 'CRM_Core_DAO_Address',
153 'fields' => array(
154 'street_address' => NULL,
155 'city' => NULL,
156 'postal_code' => NULL,
157 'state_province_id' => array(
158 'title' => ts('State/Province'),
159 ),
160 'country_id' => array(
161 'title' => ts('Country'),
162 'default' => TRUE,
163 ),
164 ),
165 'grouping' => 'contact-fields',
166 ),
167 'civicrm_phone' => array(
168 'dao' => 'CRM_Core_DAO_Phone',
169 'alias' => 'phone',
170 'fields' => array('phone' => NULL),
171 'grouping' => 'contact-fields',
172 ),
173 'civicrm_email' => array(
174 'dao' => 'CRM_Core_DAO_Email',
175 'fields' => array('email' => NULL),
176 'grouping' => 'contact-fields',
177 ),
178 );
179
180 // If we have a campaign, build out the relevant elements
181 if ($campaignEnabled && !empty($this->activeCampaigns)) {
182 $this->_columns['civicrm_membership']['fields']['campaign_id'] = array(
183 'title' => ts('Campaign'),
184 'default' => 'false',
185 );
186 $this->_columns['civicrm_membership']['filters']['campaign_id'] = array(
187 'title' => ts('Campaign'),
188 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
189 'options' => $this->activeCampaigns,
190 'type' => CRM_Utils_Type::T_INT,
191 );
192 }
193
194 $this->_groupFilter = TRUE;
195 $this->_tagFilter = TRUE;
196 parent::__construct();
197 }
198
199 public function preProcess() {
200 parent::preProcess();
201 }
202
203 public function select() {
204 $select = array();
205 $this->_columnHeaders = array();
206 foreach ($this->_columns as $tableName => $table) {
207 if (array_key_exists('fields', $table)) {
208 foreach ($table['fields'] as $fieldName => $field) {
209 if (!empty($field['required']) ||
210 !empty($this->_params['fields'][$fieldName])
211 ) {
212 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
213 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
214 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
215 }
216 }
217 }
218 }
219 $this->_select = "SELECT " . implode(', ', $select) . " ";
220 }
221
222 /**
223 * @param $fields
224 * @param $files
225 * @param $self
226 *
227 * @return array
228 */
229 public static function formRule($fields, $files, $self) {
230 $errors = $grouping = array();
231 //check for searching combination of dispaly columns and
232 //grouping criteria
233
234 return $errors;
235 }
236
237 public function from() {
238 $this->_from = "
239 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
240 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
241 ON {$this->_aliases['civicrm_contact']}.id =
242 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
243 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
244 ON {$this->_aliases['civicrm_membership_status']}.id =
245 {$this->_aliases['civicrm_membership']}.status_id
246 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
247 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
248 {$this->_aliases['civicrm_membership_type']}.id";
249
250 $this->joinAddressFromContact();
251 $this->joinPhoneFromContact();
252 $this->joinEmailFromContact();
253 }
254
255 public function where() {
256 $clauses = array();
257 foreach ($this->_columns as $tableName => $table) {
258 if (array_key_exists('filters', $table)) {
259 foreach ($table['filters'] as $fieldName => $field) {
260 $clause = NULL;
261
262 if ($field['operatorType'] & CRM_Utils_Type::T_DATE) {
263 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
264 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
265 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
266
267 if ($relative || $from || $to) {
268 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
269 }
270 }
271 else {
272 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
273 if ($op) {
274 $clause = $this->whereClause($field,
275 $op,
276 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
277 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
278 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
279 );
280 }
281 }
282 if (!empty($clause)) {
283 $clauses[$fieldName] = $clause;
284 }
285 }
286 }
287 }
288
289 if (empty($clauses)) {
290 $this->_where = "WHERE end_date < '" . date('Y-m-d') .
291 "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
292 }
293 else {
294 if (!array_key_exists('end_date', $clauses)) {
295 $this->_where = "WHERE end_date < '" . date('Y-m-d') . "' AND " .
296 implode(' AND ', $clauses);
297 }
298 else {
299 $this->_where = "WHERE " . implode(' AND ', $clauses);
300 }
301 }
302
303 if ($this->_aclWhere) {
304 $this->_where .= " AND {$this->_aclWhere} ";
305 }
306 }
307
308 public function orderBy() {
309 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
310 }
311
312 public function postProcess() {
313 $this->beginPostProcess();
314
315 // get the acl clauses built before we assemble the query
316 $this->buildACLClause($this->_aliases['civicrm_contact']);
317 $sql = $this->buildQuery(TRUE);
318
319 $dao = CRM_Core_DAO::executeQuery($sql);
320 $rows = $graphRows = array();
321 $count = 0;
322 while ($dao->fetch()) {
323 $row = array();
324 foreach ($this->_columnHeaders as $key => $value) {
325 $row[$key] = $dao->$key;
326 }
327
328 $rows[] = $row;
329 }
330 $this->formatDisplay($rows);
331
332 // assign variables to templates
333 $this->doTemplateAssignment($rows);
334
335 $this->endPostProcess($rows);
336 }
337
338 /**
339 * Alter display of rows.
340 *
341 * Iterate through the rows retrieved via SQL and make changes for display purposes,
342 * such as rendering contacts as links.
343 *
344 * @param array $rows
345 * Rows generated by SQL, with an array for each row.
346 */
347 public function alterDisplay(&$rows) {
348 $entryFound = FALSE;
349 $checkList = array();
350
351 foreach ($rows as $rowNum => $row) {
352
353 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
354 // not repeat contact display names if it matches with the one
355 // in previous row
356
357 $repeatFound = FALSE;
358 foreach ($row as $colName => $colVal) {
359 if (!empty($checkList[$colName]) &&
360 is_array($checkList[$colName]) &&
361 in_array($colVal, $checkList[$colName])
362 ) {
363 $rows[$rowNum][$colName] = "";
364 $repeatFound = TRUE;
365 }
366 if (in_array($colName, $this->_noRepeats)) {
367 $checkList[$colName][] = $colVal;
368 }
369 }
370 }
371
372 //handle the Membership Type Ids
373 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
374 if ($value = $row['civicrm_membership_membership_type_id']) {
375 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE);
376 }
377 $entryFound = TRUE;
378 }
379
380 // handle state province
381 if (array_key_exists('civicrm_address_state_province_id', $row)) {
382 if ($value = $row['civicrm_address_state_province_id']) {
383 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
384 }
385 $entryFound = TRUE;
386 }
387
388 // handle country
389 if (array_key_exists('civicrm_address_country_id', $row)) {
390 if ($value = $row['civicrm_address_country_id']) {
391 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
392 }
393 $entryFound = TRUE;
394 }
395
396 // convert display name to links
397 if (array_key_exists('civicrm_contact_sort_name', $row) &&
398 array_key_exists('civicrm_contact_id', $row)
399 ) {
400 $url = CRM_Report_Utils_Report::getNextUrl('member/detail',
401 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
402 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
403 );
404 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
405 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
406 }
407
408 // If using campaigns, convert campaign_id to campaign title
409 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
410 if ($value = $row['civicrm_membership_campaign_id']) {
411 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns[$value];
412 }
413 $entryFound = TRUE;
414 }
415
416 // skip looking further in rows, if first row itself doesn't
417 // have the column we need
418 if (!$entryFound) {
419 break;
420 }
421 }
422 }
423
424 }