3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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(
44 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
52 function __construct() {
54 // Check if CiviCampaign is a) enabled and b) has active campaigns
55 $config = CRM_Core_Config
::singleton();
56 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
57 if ($campaignEnabled) {
58 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
59 $this->activeCampaigns
= $getCampaigns['campaigns'];
60 asort($this->activeCampaigns
);
63 // UI for selecting columns to appear in the report list
64 // array conatining the columns, group_bys and filters build and provided to Form
65 $this->_columns
= array(
68 'dao' => 'CRM_Contact_DAO_Contact',
72 array('title' => ts('Member Name'),
82 array('title' => ts('First Name'),
91 array('title' => ts('Last Name'),
101 'title' => ts('Contact Type'),
103 'contact_sub_type' =>
105 'title' => ts('Contact SubType'),
108 'grouping' => 'contact-fields',
110 'civicrm_membership_type' =>
112 'dao' => 'CRM_Member_DAO_MembershipType',
113 'grouping' => 'member-fields',
119 'title' => ts('Membership Types'),
120 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
121 'options' => CRM_Member_PseudoConstant
::membershipType(),
125 'civicrm_membership' =>
127 'dao' => 'CRM_Member_DAO_Membership',
128 'grouping' => 'member-fields',
131 'membership_type_id' =>
133 'title' => 'Membership Type',
135 'type' => CRM_Utils_Type
::T_STRING
,
137 'membership_start_date' => array('title' => ts('Current Cycle Start Date'),
139 'membership_end_date' => array('title' => ts('Membership Lapse Date'),
145 'membership_end_date' =>
147 'title' => 'Lapsed Memberships',
148 'operatorType' => CRM_Report_Form
::OP_DATE
,
152 'civicrm_membership_status' =>
154 'dao' => 'CRM_Member_DAO_MembershipStatus',
155 'alias' => 'mem_status',
158 'name' => array('title' => ts('Current Status'),
162 'grouping' => 'member-fields',
166 'dao' => 'CRM_Core_DAO_Address',
169 'street_address' => NULL,
171 'postal_code' => NULL,
172 'state_province_id' =>
173 array('title' => ts('State/Province'),
176 array('title' => ts('Country'),
180 'grouping' => 'contact-fields',
184 'dao' => 'CRM_Core_DAO_Phone',
187 array('phone' => NULL),
188 'grouping' => 'contact-fields',
192 'dao' => 'CRM_Core_DAO_Email',
194 array('email' => NULL),
195 'grouping' => 'contact-fields',
199 // If we have a campaign, build out the relevant elements
200 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
201 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
202 'title' => 'Campaign',
203 'default' => 'false',
205 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
206 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
207 'options' => $this->activeCampaigns
,
211 $this->_groupFilter
= TRUE;
212 $this->_tagFilter
= TRUE;
213 parent
::__construct();
216 function preProcess() {
217 parent
::preProcess();
222 $this->_columnHeaders
= array();
223 foreach ($this->_columns
as $tableName => $table) {
224 if (array_key_exists('fields', $table)) {
225 foreach ($table['fields'] as $fieldName => $field) {
226 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
227 // to include optional columns address ,email and phone only if checked
228 if ($tableName == 'civicrm_address') {
229 $this->_addressField
= TRUE;
231 elseif ($tableName == 'civicrm_email') {
232 $this->_emailField
= TRUE;
234 elseif ($tableName == 'civicrm_phone') {
235 $this->_phoneField
= TRUE;
237 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
238 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
239 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
244 $this->_select
= "SELECT " . implode(', ', $select) . " ";
254 static function formRule($fields, $files, $self) {
255 $errors = $grouping = array();
256 //check for searching combination of dispaly columns and
266 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
267 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
268 ON {$this->_aliases['civicrm_contact']}.id =
269 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
270 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
271 ON {$this->_aliases['civicrm_membership_status']}.id =
272 {$this->_aliases['civicrm_membership']}.status_id
273 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
274 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
275 {$this->_aliases['civicrm_membership_type']}.id";
277 // include address field if address column is to be included
278 if ($this->_addressField
) {
280 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
281 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
284 // include email field if email column is to be included
285 if ($this->_emailField
) {
287 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
288 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
291 // include phone field if phone column is to be included
292 if ($this->_phoneField
) {
294 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
295 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
296 AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
302 foreach ($this->_columns
as $tableName => $table) {
303 if (array_key_exists('filters', $table)) {
304 foreach ($table['filters'] as $fieldName => $field) {
307 if ($field['operatorType'] & CRM_Utils_Type
::T_DATE
) {
308 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
309 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
310 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
312 if ($relative ||
$from ||
$to) {
313 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
317 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
319 $clause = $this->whereClause($field,
321 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
322 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
323 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
327 if (!empty($clause)) {
328 $clauses[$fieldName] = $clause;
334 if (empty($clauses)) {
335 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
338 if (!array_key_exists('end_date', $clauses)) {
339 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " . implode(' AND ', $clauses);
342 $this->_where
= "WHERE " . implode(' AND ', $clauses);
346 if ($this->_aclWhere
) {
347 $this->_where
.= " AND {$this->_aclWhere} ";
352 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
355 function postProcess() {
356 $this->beginPostProcess();
358 // get the acl clauses built before we assemble the query
359 $this->buildACLClause($this->_aliases
['civicrm_contact']);
360 $sql = $this->buildQuery(TRUE);
362 $dao = CRM_Core_DAO
::executeQuery($sql);
363 $rows = $graphRows = array();
365 while ($dao->fetch()) {
367 foreach ($this->_columnHeaders
as $key => $value) {
368 $row[$key] = $dao->$key;
373 $this->formatDisplay($rows);
375 // assign variables to templates
376 $this->doTemplateAssignment($rows);
378 $this->endPostProcess($rows);
384 function alterDisplay(&$rows) {
385 // custom code to alter rows
387 $checkList = array();
389 foreach ($rows as $rowNum => $row) {
391 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
392 // not repeat contact display names if it matches with the one
395 $repeatFound = FALSE;
396 foreach ($row as $colName => $colVal) {
397 if (!empty($checkList[$colName]) &&
398 is_array($checkList[$colName]) &&
399 in_array($colVal, $checkList[$colName])
401 $rows[$rowNum][$colName] = "";
404 if (in_array($colName, $this->_noRepeats
)) {
405 $checkList[$colName][] = $colVal;
410 //handle the Membership Type Ids
411 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
412 if ($value = $row['civicrm_membership_membership_type_id']) {
413 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
418 // handle state province
419 if (array_key_exists('civicrm_address_state_province_id', $row)) {
420 if ($value = $row['civicrm_address_state_province_id']) {
421 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
427 if (array_key_exists('civicrm_address_country_id', $row)) {
428 if ($value = $row['civicrm_address_country_id']) {
429 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
434 // convert display name to links
435 if (array_key_exists('civicrm_contact_sort_name', $row) &&
436 array_key_exists('civicrm_contact_id', $row)
438 $url = CRM_Report_Utils_Report
::getNextUrl('member/detail',
439 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
440 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
442 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
443 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
446 // If using campaigns, convert campaign_id to campaign title
447 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
448 if ($value = $row['civicrm_membership_campaign_id']) {
449 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];
454 // skip looking further in rows, if first row itself doesn't
455 // have the column we need