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 'dao' => 'CRM_Contact_DAO_GroupContact',
205 'name' => 'group_id',
206 'title' => ts('Group'),
207 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
209 'options' => CRM_Core_PseudoConstant
::group(),
215 // If we have a campaign, build out the relevant elements
216 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
217 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
218 'title' => 'Campaign',
219 'default' => 'false',
221 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
222 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
223 'options' => $this->activeCampaigns
,
227 $this->_tagFilter
= TRUE;
228 parent
::__construct();
231 function preProcess() {
232 parent
::preProcess();
237 $this->_columnHeaders
= array();
238 foreach ($this->_columns
as $tableName => $table) {
239 if (array_key_exists('fields', $table)) {
240 foreach ($table['fields'] as $fieldName => $field) {
241 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
242 // to include optional columns address ,email and phone only if checked
243 if ($tableName == 'civicrm_address') {
244 $this->_addressField
= TRUE;
246 elseif ($tableName == 'civicrm_email') {
247 $this->_emailField
= TRUE;
249 elseif ($tableName == 'civicrm_phone') {
250 $this->_phoneField
= TRUE;
252 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
253 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
254 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
259 $this->_select
= "SELECT " . implode(', ', $select) . " ";
269 static function formRule($fields, $files, $self) {
270 $errors = $grouping = array();
271 //check for searching combination of dispaly columns and
281 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
282 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
283 ON {$this->_aliases['civicrm_contact']}.id =
284 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
285 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
286 ON {$this->_aliases['civicrm_membership_status']}.id =
287 {$this->_aliases['civicrm_membership']}.status_id
288 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
289 ON {$this->_aliases['civicrm_membership']}.membership_type_id =
290 {$this->_aliases['civicrm_membership_type']}.id";
292 // include address field if address column is to be included
293 if ($this->_addressField
) {
295 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
296 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
299 // include email field if email column is to be included
300 if ($this->_emailField
) {
302 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
303 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
306 // include phone field if phone column is to be included
307 if ($this->_phoneField
) {
309 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
310 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
311 AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
317 foreach ($this->_columns
as $tableName => $table) {
318 if (array_key_exists('filters', $table)) {
319 foreach ($table['filters'] as $fieldName => $field) {
322 if ($field['operatorType'] & CRM_Utils_Type
::T_DATE
) {
323 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
324 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
325 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
327 if ($relative ||
$from ||
$to) {
328 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
332 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
334 $clause = $this->whereClause($field,
336 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
337 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
338 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
342 if (!empty($clause)) {
343 $clauses[$fieldName] = $clause;
349 if (empty($clauses)) {
350 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
353 if (!array_key_exists('end_date', $clauses)) {
354 $this->_where
= "WHERE end_date < '" . date('Y-m-d') . "' AND " . implode(' AND ', $clauses);
357 $this->_where
= "WHERE " . implode(' AND ', $clauses);
361 if ($this->_aclWhere
) {
362 $this->_where
.= " AND {$this->_aclWhere} ";
367 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
370 function postProcess() {
371 $this->beginPostProcess();
373 // get the acl clauses built before we assemble the query
374 $this->buildACLClause($this->_aliases
['civicrm_contact']);
375 $sql = $this->buildQuery(TRUE);
377 $dao = CRM_Core_DAO
::executeQuery($sql);
378 $rows = $graphRows = array();
380 while ($dao->fetch()) {
382 foreach ($this->_columnHeaders
as $key => $value) {
383 $row[$key] = $dao->$key;
388 $this->formatDisplay($rows);
390 // assign variables to templates
391 $this->doTemplateAssignment($rows);
393 $this->endPostProcess($rows);
399 function alterDisplay(&$rows) {
400 // custom code to alter rows
402 $checkList = array();
404 foreach ($rows as $rowNum => $row) {
406 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
407 // not repeat contact display names if it matches with the one
410 $repeatFound = FALSE;
411 foreach ($row as $colName => $colVal) {
412 if (!empty($checkList[$colName]) &&
413 is_array($checkList[$colName]) &&
414 in_array($colVal, $checkList[$colName])
416 $rows[$rowNum][$colName] = "";
419 if (in_array($colName, $this->_noRepeats
)) {
420 $checkList[$colName][] = $colVal;
425 //handle the Membership Type Ids
426 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
427 if ($value = $row['civicrm_membership_membership_type_id']) {
428 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
433 // handle state province
434 if (array_key_exists('civicrm_address_state_province_id', $row)) {
435 if ($value = $row['civicrm_address_state_province_id']) {
436 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
442 if (array_key_exists('civicrm_address_country_id', $row)) {
443 if ($value = $row['civicrm_address_country_id']) {
444 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
449 // convert display name to links
450 if (array_key_exists('civicrm_contact_sort_name', $row) &&
451 array_key_exists('civicrm_contact_id', $row)
453 $url = CRM_Report_Utils_Report
::getNextUrl('member/detail',
454 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
455 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
457 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
458 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
461 // If using campaigns, convert campaign_id to campaign title
462 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
463 if ($value = $row['civicrm_membership_campaign_id']) {
464 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];
469 // skip looking further in rows, if first row itself doesn't
470 // have the column we need