3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Report_Form_Member_Lapse
extends CRM_Report_Form
{
37 protected $_summary = NULL;
38 protected $_addressField = FALSE;
39 protected $_emailField = FALSE;
40 protected $_phoneField = FALSE;
41 protected $_charts = array('' => 'Tabular');
42 protected $_customGroupExtends = array(
45 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
51 public function __construct() {
53 // Check if CiviCampaign is a) enabled and b) has active campaigns
54 $config = CRM_Core_Config
::singleton();
55 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
56 if ($campaignEnabled) {
57 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
58 $this->activeCampaigns
= $getCampaigns['campaigns'];
59 asort($this->activeCampaigns
);
62 // UI for selecting columns to appear in the report list
63 // array containing the columns, group_bys and filters build and provided to Form
64 $this->_columns
= array(
65 'civicrm_contact' => array(
66 'dao' => 'CRM_Contact_DAO_Contact',
69 'title' => ts('Member Name'),
77 'first_name' => array(
78 'title' => ts('First Name'),
86 'title' => ts('Last Name'),
89 'contact_type' => array(
90 'title' => ts('Contact Type'),
92 'contact_sub_type' => array(
93 'title' => ts('Contact Subtype'),
96 'grouping' => 'contact-fields',
98 'civicrm_membership_type' => array(
99 'dao' => 'CRM_Member_DAO_MembershipType',
100 'grouping' => 'member-fields',
104 'title' => ts('Membership Types'),
105 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
106 'options' => CRM_Member_PseudoConstant
::membershipType(),
110 'civicrm_membership' => array(
111 'dao' => 'CRM_Member_DAO_Membership',
112 'grouping' => 'member-fields',
114 'membership_type_id' => array(
115 'title' => 'Membership Type',
117 'type' => CRM_Utils_Type
::T_STRING
,
119 'membership_start_date' => array(
120 'title' => ts('Current Cycle Start Date'),
122 'membership_end_date' => array(
123 'title' => ts('Membership Lapse Date'),
128 'membership_end_date' => array(
129 'title' => 'Lapsed Memberships',
130 'operatorType' => CRM_Report_Form
::OP_DATE
,
134 'civicrm_membership_status' => array(
135 'dao' => 'CRM_Member_DAO_MembershipStatus',
136 'alias' => 'mem_status',
139 'title' => ts('Current Status'),
143 'grouping' => 'member-fields',
145 'civicrm_address' => array(
146 'dao' => 'CRM_Core_DAO_Address',
148 'street_address' => NULL,
150 'postal_code' => NULL,
151 'state_province_id' => array(
152 'title' => ts('State/Province'),
154 'country_id' => array(
155 'title' => ts('Country'),
159 'grouping' => 'contact-fields',
161 'civicrm_phone' => array(
162 'dao' => 'CRM_Core_DAO_Phone',
164 'fields' => array('phone' => NULL),
165 'grouping' => 'contact-fields',
167 'civicrm_email' => array(
168 'dao' => 'CRM_Core_DAO_Email',
169 'fields' => array('email' => NULL),
170 'grouping' => 'contact-fields',
174 // If we have a campaign, build out the relevant elements
175 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
176 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
177 'title' => 'Campaign',
178 'default' => 'false',
180 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array(
181 'title' => ts('Campaign'),
182 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
183 'options' => $this->activeCampaigns
,
187 $this->_groupFilter
= TRUE;
188 $this->_tagFilter
= TRUE;
189 parent
::__construct();
192 public function preProcess() {
193 parent
::preProcess();
196 public function select() {
198 $this->_columnHeaders
= array();
199 foreach ($this->_columns
as $tableName => $table) {
200 if (array_key_exists('fields', $table)) {
201 foreach ($table['fields'] as $fieldName => $field) {
202 if (!empty($field['required']) ||
203 !empty($this->_params
['fields'][$fieldName])
205 // to include optional columns address ,email and phone only if checked
206 if ($tableName == 'civicrm_address') {
207 $this->_addressField
= TRUE;
209 elseif ($tableName == 'civicrm_email') {
210 $this->_emailField
= TRUE;
212 elseif ($tableName == 'civicrm_phone') {
213 $this->_phoneField
= TRUE;
215 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
216 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
217 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
222 $this->_select
= "SELECT " . implode(', ', $select) . " ";
232 public static function formRule($fields, $files, $self) {
233 $errors = $grouping = array();
234 //check for searching combination of dispaly columns and
240 public function from() {
244 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
245 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
246 ON {$this->_aliases['civicrm_contact']}.id =
247 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
248 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
249 ON {$this->_aliases['civicrm_membership_status']}.id =
250 {$this->_aliases['civicrm_membership']}.status_id
251 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
252 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
253 {$this->_aliases['civicrm_membership_type']}.id";
255 // include address field if address column is to be included
256 if ($this->_addressField
) {
258 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
259 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
262 // include email field if email column is to be included
263 if ($this->_emailField
) {
265 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
266 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
269 // include phone field if phone column is to be included
270 if ($this->_phoneField
) {
272 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
273 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
274 AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
278 public function where() {
280 foreach ($this->_columns
as $tableName => $table) {
281 if (array_key_exists('filters', $table)) {
282 foreach ($table['filters'] as $fieldName => $field) {
285 if ($field['operatorType'] & CRM_Utils_Type
::T_DATE
) {
286 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
287 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
288 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
290 if ($relative ||
$from ||
$to) {
291 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
295 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
297 $clause = $this->whereClause($field,
299 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
300 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
301 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
305 if (!empty($clause)) {
306 $clauses[$fieldName] = $clause;
312 if (empty($clauses)) {
313 $this->_where
= "WHERE end_date < '" . date('Y-m-d') .
314 "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
317 if (!array_key_exists('end_date', $clauses)) {
318 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " .
319 implode(' AND ', $clauses);
322 $this->_where
= "WHERE " . implode(' AND ', $clauses);
326 if ($this->_aclWhere
) {
327 $this->_where
.= " AND {$this->_aclWhere} ";
331 public function orderBy() {
332 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
335 public function postProcess() {
336 $this->beginPostProcess();
338 // get the acl clauses built before we assemble the query
339 $this->buildACLClause($this->_aliases
['civicrm_contact']);
340 $sql = $this->buildQuery(TRUE);
342 $dao = CRM_Core_DAO
::executeQuery($sql);
343 $rows = $graphRows = array();
345 while ($dao->fetch()) {
347 foreach ($this->_columnHeaders
as $key => $value) {
348 $row[$key] = $dao->$key;
353 $this->formatDisplay($rows);
355 // assign variables to templates
356 $this->doTemplateAssignment($rows);
358 $this->endPostProcess($rows);
362 * Alter display of rows.
364 * Iterate through the rows retrieved via SQL and make changes for display purposes,
365 * such as rendering contacts as links.
368 * Rows generated by SQL, with an array for each row.
370 public function alterDisplay(&$rows) {
372 $checkList = array();
374 foreach ($rows as $rowNum => $row) {
376 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
377 // not repeat contact display names if it matches with the one
380 $repeatFound = FALSE;
381 foreach ($row as $colName => $colVal) {
382 if (!empty($checkList[$colName]) &&
383 is_array($checkList[$colName]) &&
384 in_array($colVal, $checkList[$colName])
386 $rows[$rowNum][$colName] = "";
389 if (in_array($colName, $this->_noRepeats
)) {
390 $checkList[$colName][] = $colVal;
395 //handle the Membership Type Ids
396 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
397 if ($value = $row['civicrm_membership_membership_type_id']) {
398 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
403 // handle state province
404 if (array_key_exists('civicrm_address_state_province_id', $row)) {
405 if ($value = $row['civicrm_address_state_province_id']) {
406 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
412 if (array_key_exists('civicrm_address_country_id', $row)) {
413 if ($value = $row['civicrm_address_country_id']) {
414 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
419 // convert display name to links
420 if (array_key_exists('civicrm_contact_sort_name', $row) &&
421 array_key_exists('civicrm_contact_id', $row)
423 $url = CRM_Report_Utils_Report
::getNextUrl('member/detail',
424 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
425 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
427 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
428 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
431 // If using campaigns, convert campaign_id to campaign title
432 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
433 if ($value = $row['civicrm_membership_campaign_id']) {
434 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];
439 // skip looking further in rows, if first row itself doesn't
440 // have the column we need