5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Member_Lapse
extends CRM_Report_Form
{
39 protected $_summary = NULL;
40 protected $_addressField = FALSE;
41 protected $_emailField = FALSE;
42 protected $_phoneField = FALSE;
43 protected $_charts = array('' => 'Tabular');
44 protected $_customGroupExtends = array(
46 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
48 function __construct() {
50 // Check if CiviCampaign is a) enabled and b) has active campaigns
51 $config = CRM_Core_Config
::singleton();
52 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
53 if ($campaignEnabled) {
54 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
55 $this->activeCampaigns
= $getCampaigns['campaigns'];
56 asort($this->activeCampaigns
);
59 // UI for selecting columns to appear in the report list
60 // array conatining the columns, group_bys and filters build and provided to Form
61 $this->_columns
= array(
64 'dao' => 'CRM_Contact_DAO_Contact',
68 array('title' => ts('Member Name'),
78 array('title' => ts('First Name'),
87 array('title' => ts('Last Name'),
96 'grouping' => 'contact-fields',
98 'civicrm_membership_type' =>
100 'dao' => 'CRM_Member_DAO_MembershipType',
101 'grouping' => 'member-fields',
107 'title' => ts('Membership Types'),
108 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
109 'options' => CRM_Member_PseudoConstant
::membershipType(),
113 'civicrm_membership' =>
115 'dao' => 'CRM_Member_DAO_Membership',
116 'grouping' => 'member-fields',
119 'membership_type_id' =>
121 'title' => 'Membership Type',
123 'type' => CRM_Utils_Type
::T_STRING
,
125 'membership_start_date' => array('title' => ts('Current Cycle Start Date'),
127 'membership_end_date' => array('title' => ts('Membership Lapse Date'),
133 'membership_end_date' =>
135 'title' => 'Lapsed Memberships',
136 'operatorType' => CRM_Report_Form
::OP_DATE
,
140 'civicrm_membership_status' =>
142 'dao' => 'CRM_Member_DAO_MembershipStatus',
143 'alias' => 'mem_status',
146 'name' => array('title' => ts('Current Status'),
150 'grouping' => 'member-fields',
154 'dao' => 'CRM_Core_DAO_Address',
157 'street_address' => NULL,
159 'postal_code' => NULL,
160 'state_province_id' =>
161 array('title' => ts('State/Province'),
164 array('title' => ts('Country'),
168 'grouping' => 'contact-fields',
172 'dao' => 'CRM_Core_DAO_Phone',
175 array('phone' => NULL),
176 'grouping' => 'contact-fields',
180 'dao' => 'CRM_Core_DAO_Email',
182 array('email' => NULL),
183 'grouping' => 'contact-fields',
187 'dao' => 'CRM_Contact_DAO_GroupContact',
193 'name' => 'group_id',
194 'title' => ts('Group'),
195 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
197 'options' => CRM_Core_PseudoConstant
::group(),
203 // If we have a campaign, build out the relevant elements
204 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
205 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
206 'title' => 'Campaign',
207 'default' => 'false',
209 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
210 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
211 'options' => $this->activeCampaigns
,
215 $this->_tagFilter
= TRUE;
216 parent
::__construct();
219 function preProcess() {
220 parent
::preProcess();
225 $this->_columnHeaders
= array();
226 foreach ($this->_columns
as $tableName => $table) {
227 if (array_key_exists('fields', $table)) {
228 foreach ($table['fields'] as $fieldName => $field) {
229 if (CRM_Utils_Array
::value('required', $field) ||
230 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
232 // to include optional columns address ,email and phone only if checked
233 if ($tableName == 'civicrm_address') {
234 $this->_addressField
= TRUE;
236 elseif ($tableName == 'civicrm_email') {
237 $this->_emailField
= TRUE;
239 elseif ($tableName == 'civicrm_phone') {
240 $this->_phoneField
= TRUE;
242 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
243 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
244 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
249 $this->_select
= "SELECT " . implode(', ', $select) . " ";
252 static function formRule($fields, $files, $self) {
253 $errors = $grouping = array();
254 //check for searching combination of dispaly columns and
264 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
265 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
266 ON {$this->_aliases['civicrm_contact']}.id =
267 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
268 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
269 ON {$this->_aliases['civicrm_membership_status']}.id =
270 {$this->_aliases['civicrm_membership']}.status_id
271 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
272 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
273 {$this->_aliases['civicrm_membership_type']}.id";
275 // include address field if address column is to be included
276 if ($this->_addressField
) {
278 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
279 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
282 // include email field if email column is to be included
283 if ($this->_emailField
) {
285 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
286 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
289 // include phone field if phone column is to be included
290 if ($this->_phoneField
) {
292 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
293 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
294 AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
300 foreach ($this->_columns
as $tableName => $table) {
301 if (array_key_exists('filters', $table)) {
302 foreach ($table['filters'] as $fieldName => $field) {
305 if ($field['operatorType'] & CRM_Utils_Type
::T_DATE
) {
306 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
307 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
308 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
310 if ($relative ||
$from ||
$to) {
311 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
315 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
317 $clause = $this->whereClause($field,
319 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
320 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
321 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
325 if (!empty($clause)) {
326 $clauses[$fieldName] = $clause;
332 if (empty($clauses)) {
333 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
336 if (!array_key_exists('end_date', $clauses)) {
337 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " . implode(' AND ', $clauses);
340 $this->_where
= "WHERE " . implode(' AND ', $clauses);
344 if ($this->_aclWhere
) {
345 $this->_where
.= " AND {$this->_aclWhere} ";
350 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
353 function postProcess() {
354 $this->beginPostProcess();
356 // get the acl clauses built before we assemble the query
357 $this->buildACLClause($this->_aliases
['civicrm_contact']);
358 $sql = $this->buildQuery(TRUE);
360 $dao = CRM_Core_DAO
::executeQuery($sql);
361 $rows = $graphRows = array();
363 while ($dao->fetch()) {
365 foreach ($this->_columnHeaders
as $key => $value) {
366 $row[$key] = $dao->$key;
371 $this->formatDisplay($rows);
373 // assign variables to templates
374 $this->doTemplateAssignment($rows);
376 $this->endPostProcess($rows);
379 function alterDisplay(&$rows) {
380 // custom code to alter rows
382 $checkList = array();
384 foreach ($rows as $rowNum => $row) {
386 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
387 // not repeat contact display names if it matches with the one
390 $repeatFound = FALSE;
391 foreach ($row as $colName => $colVal) {
392 if (CRM_Utils_Array
::value($colName, $checkList) &&
393 is_array($checkList[$colName]) &&
394 in_array($colVal, $checkList[$colName])
396 $rows[$rowNum][$colName] = "";
399 if (in_array($colName, $this->_noRepeats
)) {
400 $checkList[$colName][] = $colVal;
405 //handle the Membership Type Ids
406 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
407 if ($value = $row['civicrm_membership_membership_type_id']) {
408 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
413 // handle state province
414 if (array_key_exists('civicrm_address_state_province_id', $row)) {
415 if ($value = $row['civicrm_address_state_province_id']) {
416 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
422 if (array_key_exists('civicrm_address_country_id', $row)) {
423 if ($value = $row['civicrm_address_country_id']) {
424 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
429 // convert display name to links
430 if (array_key_exists('civicrm_contact_sort_name', $row) &&
431 array_key_exists('civicrm_contact_id', $row)
433 $url = CRM_Report_Utils_Report
::getNextUrl('member/detail',
434 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
435 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
437 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
438 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
441 // If using campaigns, convert campaign_id to campaign title
442 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
443 if ($value = $row['civicrm_membership_campaign_id']) {
444 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];
449 // skip looking further in rows, if first row itself doesn't
450 // have the column we need