3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 class CRM_Report_Form_Member_Lapse
extends CRM_Report_Form
{
37 protected $_summary = NULL;
38 protected $_charts = array('' => 'Tabular');
39 protected $_customGroupExtends = array(
42 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
45 * This report has not been optimised for group filtering.
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.
55 protected $groupFilterNotOptimised = TRUE;
60 public function __construct() {
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
);
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',
78 'title' => ts('Member Name'),
86 'first_name' => array(
87 'title' => ts('First Name'),
91 'title' => ts('Last Name'),
94 'contact_type' => array(
95 'title' => ts('Contact Type'),
97 'contact_sub_type' => array(
98 'title' => ts('Contact Subtype'),
101 'grouping' => 'contact-fields',
103 'civicrm_membership_type' => array(
104 'dao' => 'CRM_Member_DAO_MembershipType',
105 'grouping' => 'member-fields',
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(),
116 'civicrm_membership' => array(
117 'dao' => 'CRM_Member_DAO_Membership',
118 'grouping' => 'member-fields',
120 'membership_type_id' => array(
121 'title' => ts('Membership Type'),
123 'type' => CRM_Utils_Type
::T_STRING
,
125 'membership_start_date' => array(
126 'title' => ts('Current Cycle Start Date'),
128 'membership_end_date' => array(
129 'title' => ts('Membership Lapse Date'),
134 'membership_end_date' => array(
135 'title' => ts('Lapsed Memberships'),
136 'operatorType' => CRM_Report_Form
::OP_DATE
,
140 'civicrm_membership_status' => array(
141 'dao' => 'CRM_Member_DAO_MembershipStatus',
142 'alias' => 'mem_status',
145 'title' => ts('Current Status'),
149 'grouping' => 'member-fields',
151 'civicrm_address' => array(
152 'dao' => 'CRM_Core_DAO_Address',
154 'street_address' => NULL,
156 'postal_code' => NULL,
157 'state_province_id' => array(
158 'title' => ts('State/Province'),
160 'country_id' => array(
161 'title' => ts('Country'),
165 'grouping' => 'contact-fields',
167 'civicrm_phone' => array(
168 'dao' => 'CRM_Core_DAO_Phone',
170 'fields' => array('phone' => NULL),
171 'grouping' => 'contact-fields',
173 'civicrm_email' => array(
174 'dao' => 'CRM_Core_DAO_Email',
175 'fields' => array('email' => NULL),
176 'grouping' => 'contact-fields',
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',
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
,
194 $this->_groupFilter
= TRUE;
195 $this->_tagFilter
= TRUE;
196 parent
::__construct();
199 public function preProcess() {
200 parent
::preProcess();
203 public function select() {
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])
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'];
219 $this->_select
= "SELECT " . implode(', ', $select) . " ";
229 public static function formRule($fields, $files, $self) {
230 $errors = $grouping = array();
231 //check for searching combination of dispaly columns and
237 public function 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";
250 $this->joinAddressFromContact();
251 $this->joinPhoneFromContact();
252 $this->joinEmailFromContact();
255 public function where() {
257 foreach ($this->_columns
as $tableName => $table) {
258 if (array_key_exists('filters', $table)) {
259 foreach ($table['filters'] as $fieldName => $field) {
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
);
267 if ($relative ||
$from ||
$to) {
268 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
272 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
274 $clause = $this->whereClause($field,
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
)
282 if (!empty($clause)) {
283 $clauses[$fieldName] = $clause;
289 if (empty($clauses)) {
290 $this->_where
= "WHERE end_date < '" . date('Y-m-d') .
291 "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
294 if (!array_key_exists('end_date', $clauses)) {
295 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " .
296 implode(' AND ', $clauses);
299 $this->_where
= "WHERE " . implode(' AND ', $clauses);
303 if ($this->_aclWhere
) {
304 $this->_where
.= " AND {$this->_aclWhere} ";
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";
312 public function postProcess() {
313 $this->beginPostProcess();
315 // get the acl clauses built before we assemble the query
316 $this->buildACLClause($this->_aliases
['civicrm_contact']);
317 $sql = $this->buildQuery(TRUE);
319 $dao = CRM_Core_DAO
::executeQuery($sql);
320 $rows = $graphRows = array();
322 while ($dao->fetch()) {
324 foreach ($this->_columnHeaders
as $key => $value) {
325 $row[$key] = $dao->$key;
330 $this->formatDisplay($rows);
332 // assign variables to templates
333 $this->doTemplateAssignment($rows);
335 $this->endPostProcess($rows);
339 * Alter display of rows.
341 * Iterate through the rows retrieved via SQL and make changes for display purposes,
342 * such as rendering contacts as links.
345 * Rows generated by SQL, with an array for each row.
347 public function alterDisplay(&$rows) {
349 $checkList = array();
351 foreach ($rows as $rowNum => $row) {
353 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
354 // not repeat contact display names if it matches with the one
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])
363 $rows[$rowNum][$colName] = "";
366 if (in_array($colName, $this->_noRepeats
)) {
367 $checkList[$colName][] = $colVal;
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);
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);
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);
396 // convert display name to links
397 if (array_key_exists('civicrm_contact_sort_name', $row) &&
398 array_key_exists('civicrm_contact_id', $row)
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
404 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
405 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
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];
416 // skip looking further in rows, if first row itself doesn't
417 // have the column we need