3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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');
46 function __construct() {
48 // Check if CiviCampaign is a) enabled and b) has active campaigns
49 $config = CRM_Core_Config
::singleton();
50 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
51 if ($campaignEnabled) {
52 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
53 $this->activeCampaigns
= $getCampaigns['campaigns'];
54 asort($this->activeCampaigns
);
57 // UI for selecting columns to appear in the report list
58 // array conatining the columns, group_bys and filters build and provided to Form
59 $this->_columns
= array(
62 'dao' => 'CRM_Contact_DAO_Contact',
66 array('title' => ts('Member Name'),
76 array('title' => ts('First Name'),
85 array('title' => ts('Last Name'),
95 'title' => ts('Contact Type'),
99 'title' => ts('Contact SubType'),
102 'grouping' => 'contact-fields',
104 'civicrm_membership_type' =>
106 'dao' => 'CRM_Member_DAO_MembershipType',
107 'grouping' => 'member-fields',
113 'title' => ts('Membership Types'),
114 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
115 'options' => CRM_Member_PseudoConstant
::membershipType(),
119 'civicrm_membership' =>
121 'dao' => 'CRM_Member_DAO_Membership',
122 'grouping' => 'member-fields',
125 'membership_type_id' =>
127 'title' => 'Membership Type',
129 'type' => CRM_Utils_Type
::T_STRING
,
131 'membership_start_date' => array('title' => ts('Current Cycle Start Date'),
133 'membership_end_date' => array('title' => ts('Membership Lapse Date'),
139 'membership_end_date' =>
141 'title' => 'Lapsed Memberships',
142 'operatorType' => CRM_Report_Form
::OP_DATE
,
146 'civicrm_membership_status' =>
148 'dao' => 'CRM_Member_DAO_MembershipStatus',
149 'alias' => 'mem_status',
152 'name' => array('title' => ts('Current Status'),
156 'grouping' => 'member-fields',
160 'dao' => 'CRM_Core_DAO_Address',
163 'street_address' => NULL,
165 'postal_code' => NULL,
166 'state_province_id' =>
167 array('title' => ts('State/Province'),
170 array('title' => ts('Country'),
174 'grouping' => 'contact-fields',
178 'dao' => 'CRM_Core_DAO_Phone',
181 array('phone' => NULL),
182 'grouping' => 'contact-fields',
186 'dao' => 'CRM_Core_DAO_Email',
188 array('email' => NULL),
189 'grouping' => 'contact-fields',
193 'dao' => 'CRM_Contact_DAO_GroupContact',
199 'name' => 'group_id',
200 'title' => ts('Group'),
201 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
203 'options' => CRM_Core_PseudoConstant
::group(),
209 // If we have a campaign, build out the relevant elements
210 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
211 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
212 'title' => 'Campaign',
213 'default' => 'false',
215 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
216 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
217 'options' => $this->activeCampaigns
,
221 $this->_tagFilter
= TRUE;
222 parent
::__construct();
225 function preProcess() {
226 parent
::preProcess();
231 $this->_columnHeaders
= array();
232 foreach ($this->_columns
as $tableName => $table) {
233 if (array_key_exists('fields', $table)) {
234 foreach ($table['fields'] as $fieldName => $field) {
235 if (!empty($field['required']) ||
236 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
238 // to include optional columns address ,email and phone only if checked
239 if ($tableName == 'civicrm_address') {
240 $this->_addressField
= TRUE;
242 elseif ($tableName == 'civicrm_email') {
243 $this->_emailField
= TRUE;
245 elseif ($tableName == 'civicrm_phone') {
246 $this->_phoneField
= TRUE;
248 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
249 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
250 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
255 $this->_select
= "SELECT " . implode(', ', $select) . " ";
258 static function formRule($fields, $files, $self) {
259 $errors = $grouping = array();
260 //check for searching combination of dispaly columns and
270 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
271 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
272 ON {$this->_aliases['civicrm_contact']}.id =
273 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
274 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
275 ON {$this->_aliases['civicrm_membership_status']}.id =
276 {$this->_aliases['civicrm_membership']}.status_id
277 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
278 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
279 {$this->_aliases['civicrm_membership_type']}.id";
281 // include address field if address column is to be included
282 if ($this->_addressField
) {
284 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
285 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
288 // include email field if email column is to be included
289 if ($this->_emailField
) {
291 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
292 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
295 // include phone field if phone column is to be included
296 if ($this->_phoneField
) {
298 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
299 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
300 AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
306 foreach ($this->_columns
as $tableName => $table) {
307 if (array_key_exists('filters', $table)) {
308 foreach ($table['filters'] as $fieldName => $field) {
311 if ($field['operatorType'] & CRM_Utils_Type
::T_DATE
) {
312 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
313 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
314 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
316 if ($relative ||
$from ||
$to) {
317 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
321 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
323 $clause = $this->whereClause($field,
325 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
326 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
327 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
331 if (!empty($clause)) {
332 $clauses[$fieldName] = $clause;
338 if (empty($clauses)) {
339 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
342 if (!array_key_exists('end_date', $clauses)) {
343 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " . implode(' AND ', $clauses);
346 $this->_where
= "WHERE " . implode(' AND ', $clauses);
350 if ($this->_aclWhere
) {
351 $this->_where
.= " AND {$this->_aclWhere} ";
356 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
359 function postProcess() {
360 $this->beginPostProcess();
362 // get the acl clauses built before we assemble the query
363 $this->buildACLClause($this->_aliases
['civicrm_contact']);
364 $sql = $this->buildQuery(TRUE);
366 $dao = CRM_Core_DAO
::executeQuery($sql);
367 $rows = $graphRows = array();
369 while ($dao->fetch()) {
371 foreach ($this->_columnHeaders
as $key => $value) {
372 $row[$key] = $dao->$key;
377 $this->formatDisplay($rows);
379 // assign variables to templates
380 $this->doTemplateAssignment($rows);
382 $this->endPostProcess($rows);
385 function alterDisplay(&$rows) {
386 // custom code to alter rows
388 $checkList = array();
390 foreach ($rows as $rowNum => $row) {
392 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
393 // not repeat contact display names if it matches with the one
396 $repeatFound = FALSE;
397 foreach ($row as $colName => $colVal) {
398 if (!empty($checkList[$colName]) &&
399 is_array($checkList[$colName]) &&
400 in_array($colVal, $checkList[$colName])
402 $rows[$rowNum][$colName] = "";
405 if (in_array($colName, $this->_noRepeats
)) {
406 $checkList[$colName][] = $colVal;
411 //handle the Membership Type Ids
412 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
413 if ($value = $row['civicrm_membership_membership_type_id']) {
414 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
419 // handle state province
420 if (array_key_exists('civicrm_address_state_province_id', $row)) {
421 if ($value = $row['civicrm_address_state_province_id']) {
422 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
428 if (array_key_exists('civicrm_address_country_id', $row)) {
429 if ($value = $row['civicrm_address_country_id']) {
430 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
435 // convert display name to links
436 if (array_key_exists('civicrm_contact_sort_name', $row) &&
437 array_key_exists('civicrm_contact_id', $row)
439 $url = CRM_Report_Utils_Report
::getNextUrl('member/detail',
440 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
441 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
443 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
444 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
447 // If using campaigns, convert campaign_id to campaign title
448 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
449 if ($value = $row['civicrm_membership_campaign_id']) {
450 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];
455 // skip looking further in rows, if first row itself doesn't
456 // have the column we need