Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Member_Lapse extends CRM_Report_Form { | |
36 | ||
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( | |
21dfd5f5 | 43 | 'Membership', |
9d72cede | 44 | ); |
6a488035 | 45 | public $_drilldownReport = array('member/detail' => 'Link to Detail Report'); |
f813f78e | 46 | |
74cf4551 | 47 | /** |
74cf4551 EM |
48 | */ |
49 | /** | |
74cf4551 | 50 | */ |
00be9182 | 51 | public function __construct() { |
d62fab33 RN |
52 | |
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); | |
f813f78e | 60 | } |
d62fab33 | 61 | |
6a488035 | 62 | // UI for selecting columns to appear in the report list |
9d72cede | 63 | // array containing the columns, group_bys and filters build and provided to Form |
6a488035 | 64 | $this->_columns = array( |
9d72cede | 65 | 'civicrm_contact' => array( |
6a488035 | 66 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
67 | 'fields' => array( |
68 | 'sort_name' => array( | |
69 | 'title' => ts('Member Name'), | |
6a488035 TO |
70 | 'no_repeat' => TRUE, |
71 | 'required' => TRUE, | |
72 | ), | |
9d72cede | 73 | 'id' => array( |
6a488035 TO |
74 | 'no_display' => TRUE, |
75 | 'required' => TRUE, | |
76 | ), | |
9d72cede EM |
77 | 'first_name' => array( |
78 | 'title' => ts('First Name'), | |
6a488035 TO |
79 | 'no_repeat' => TRUE, |
80 | ), | |
9d72cede | 81 | 'id' => array( |
6a488035 TO |
82 | 'no_display' => TRUE, |
83 | 'required' => TRUE, | |
84 | ), | |
9d72cede EM |
85 | 'last_name' => array( |
86 | 'title' => ts('Last Name'), | |
6a488035 TO |
87 | 'no_repeat' => TRUE, |
88 | ), | |
9d72cede | 89 | 'contact_type' => array( |
30f85891 RN |
90 | 'title' => ts('Contact Type'), |
91 | ), | |
9d72cede | 92 | 'contact_sub_type' => array( |
b8f96eb8 | 93 | 'title' => ts('Contact Subtype'), |
30f85891 | 94 | ), |
6a488035 TO |
95 | ), |
96 | 'grouping' => 'contact-fields', | |
97 | ), | |
9d72cede | 98 | 'civicrm_membership_type' => array( |
6a488035 TO |
99 | 'dao' => 'CRM_Member_DAO_MembershipType', |
100 | 'grouping' => 'member-fields', | |
9d72cede EM |
101 | 'filters' => array( |
102 | 'tid' => array( | |
6a488035 TO |
103 | 'name' => 'id', |
104 | 'title' => ts('Membership Types'), | |
105 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
106 | 'options' => CRM_Member_PseudoConstant::membershipType(), | |
107 | ), | |
108 | ), | |
109 | ), | |
9d72cede | 110 | 'civicrm_membership' => array( |
6a488035 TO |
111 | 'dao' => 'CRM_Member_DAO_Membership', |
112 | 'grouping' => 'member-fields', | |
9d72cede EM |
113 | 'fields' => array( |
114 | 'membership_type_id' => array( | |
6a488035 TO |
115 | 'title' => 'Membership Type', |
116 | 'required' => TRUE, | |
117 | 'type' => CRM_Utils_Type::T_STRING, | |
118 | ), | |
9d72cede EM |
119 | 'membership_start_date' => array( |
120 | 'title' => ts('Current Cycle Start Date'), | |
6a488035 | 121 | ), |
9d72cede EM |
122 | 'membership_end_date' => array( |
123 | 'title' => ts('Membership Lapse Date'), | |
6a488035 TO |
124 | 'required' => TRUE, |
125 | ), | |
126 | ), | |
9d72cede EM |
127 | 'filters' => array( |
128 | 'membership_end_date' => array( | |
6a488035 TO |
129 | 'title' => 'Lapsed Memberships', |
130 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
131 | ), | |
132 | ), | |
133 | ), | |
9d72cede | 134 | 'civicrm_membership_status' => array( |
6a488035 TO |
135 | 'dao' => 'CRM_Member_DAO_MembershipStatus', |
136 | 'alias' => 'mem_status', | |
9d72cede EM |
137 | 'fields' => array( |
138 | 'name' => array( | |
139 | 'title' => ts('Current Status'), | |
6a488035 TO |
140 | 'required' => TRUE, |
141 | ), | |
142 | ), | |
143 | 'grouping' => 'member-fields', | |
144 | ), | |
9d72cede | 145 | 'civicrm_address' => array( |
6a488035 | 146 | 'dao' => 'CRM_Core_DAO_Address', |
9d72cede | 147 | 'fields' => array( |
6a488035 TO |
148 | 'street_address' => NULL, |
149 | 'city' => NULL, | |
150 | 'postal_code' => NULL, | |
9d72cede EM |
151 | 'state_province_id' => array( |
152 | 'title' => ts('State/Province'), | |
6a488035 | 153 | ), |
9d72cede EM |
154 | 'country_id' => array( |
155 | 'title' => ts('Country'), | |
6a488035 TO |
156 | 'default' => TRUE, |
157 | ), | |
158 | ), | |
159 | 'grouping' => 'contact-fields', | |
160 | ), | |
9d72cede | 161 | 'civicrm_phone' => array( |
6a488035 TO |
162 | 'dao' => 'CRM_Core_DAO_Phone', |
163 | 'alias' => 'phone', | |
9d72cede | 164 | 'fields' => array('phone' => NULL), |
6a488035 TO |
165 | 'grouping' => 'contact-fields', |
166 | ), | |
9d72cede | 167 | 'civicrm_email' => array( |
6a488035 | 168 | 'dao' => 'CRM_Core_DAO_Email', |
9d72cede | 169 | 'fields' => array('email' => NULL), |
6a488035 TO |
170 | 'grouping' => 'contact-fields', |
171 | ), | |
6a488035 TO |
172 | ); |
173 | ||
d62fab33 RN |
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', | |
179 | ); | |
9d72cede EM |
180 | $this->_columns['civicrm_membership']['filters']['campaign_id'] = array( |
181 | 'title' => ts('Campaign'), | |
d62fab33 RN |
182 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
183 | 'options' => $this->activeCampaigns, | |
184 | ); | |
f813f78e | 185 | } |
d62fab33 | 186 | |
16e2e80c | 187 | $this->_groupFilter = TRUE; |
6a488035 TO |
188 | $this->_tagFilter = TRUE; |
189 | parent::__construct(); | |
190 | } | |
191 | ||
00be9182 | 192 | public function preProcess() { |
6a488035 TO |
193 | parent::preProcess(); |
194 | } | |
195 | ||
00be9182 | 196 | public function select() { |
6a488035 TO |
197 | $select = array(); |
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) { | |
9d72cede EM |
202 | if (!empty($field['required']) || |
203 | !empty($this->_params['fields'][$fieldName]) | |
204 | ) { | |
6a488035 TO |
205 | // to include optional columns address ,email and phone only if checked |
206 | if ($tableName == 'civicrm_address') { | |
207 | $this->_addressField = TRUE; | |
208 | } | |
209 | elseif ($tableName == 'civicrm_email') { | |
210 | $this->_emailField = TRUE; | |
211 | } | |
212 | elseif ($tableName == 'civicrm_phone') { | |
213 | $this->_phoneField = TRUE; | |
214 | } | |
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']; | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
223 | } | |
224 | ||
74cf4551 EM |
225 | /** |
226 | * @param $fields | |
227 | * @param $files | |
228 | * @param $self | |
229 | * | |
230 | * @return array | |
231 | */ | |
00be9182 | 232 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
233 | $errors = $grouping = array(); |
234 | //check for searching combination of dispaly columns and | |
235 | //grouping criteria | |
236 | ||
237 | return $errors; | |
238 | } | |
239 | ||
00be9182 | 240 | public function from() { |
6a488035 TO |
241 | $this->_from = NULL; |
242 | ||
243 | $this->_from = " | |
244 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
f813f78e | 245 | INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']} |
246 | ON {$this->_aliases['civicrm_contact']}.id = | |
6a488035 TO |
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']} | |
f813f78e | 249 | ON {$this->_aliases['civicrm_membership_status']}.id = |
6a488035 | 250 | {$this->_aliases['civicrm_membership']}.status_id |
f813f78e | 251 | LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']} |
6a488035 TO |
252 | ON {$this->_aliases['civicrm_membership']}.membership_type_id = |
253 | {$this->_aliases['civicrm_membership_type']}.id"; | |
254 | ||
255 | // include address field if address column is to be included | |
256 | if ($this->_addressField) { | |
257 | $this->_from .= " | |
f813f78e | 258 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
6a488035 TO |
259 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
260 | } | |
261 | ||
262 | // include email field if email column is to be included | |
263 | if ($this->_emailField) { | |
264 | $this->_from .= " | |
f813f78e | 265 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
6a488035 TO |
266 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
267 | } | |
268 | ||
269 | // include phone field if phone column is to be included | |
270 | if ($this->_phoneField) { | |
271 | $this->_from .= " | |
f813f78e | 272 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
273 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id | |
6a488035 TO |
274 | AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n"; |
275 | } | |
276 | } | |
277 | ||
00be9182 | 278 | public function where() { |
6a488035 TO |
279 | $clauses = array(); |
280 | foreach ($this->_columns as $tableName => $table) { | |
281 | if (array_key_exists('filters', $table)) { | |
282 | foreach ($table['filters'] as $fieldName => $field) { | |
283 | $clause = NULL; | |
284 | ||
285 | if ($field['operatorType'] & CRM_Utils_Type::T_DATE) { | |
286 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
287 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
288 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
289 | |
290 | if ($relative || $from || $to) { | |
291 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
292 | } | |
293 | } | |
294 | else { | |
295 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
296 | if ($op) { | |
297 | $clause = $this->whereClause($field, | |
298 | $op, | |
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) | |
302 | ); | |
303 | } | |
304 | } | |
305 | if (!empty($clause)) { | |
306 | $clauses[$fieldName] = $clause; | |
307 | } | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | if (empty($clauses)) { | |
9d72cede EM |
313 | $this->_where = "WHERE end_date < '" . date('Y-m-d') . |
314 | "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'"; | |
6a488035 TO |
315 | } |
316 | else { | |
317 | if (!array_key_exists('end_date', $clauses)) { | |
9d72cede EM |
318 | $this->_where = "WHERE end_date < '" . date('Y-m-d') . "' AND " . |
319 | implode(' AND ', $clauses); | |
6a488035 TO |
320 | } |
321 | else { | |
322 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
323 | } | |
324 | } | |
325 | ||
326 | if ($this->_aclWhere) { | |
327 | $this->_where .= " AND {$this->_aclWhere} "; | |
328 | } | |
329 | } | |
330 | ||
00be9182 | 331 | public function orderBy() { |
6a488035 TO |
332 | $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id"; |
333 | } | |
334 | ||
00be9182 | 335 | public function postProcess() { |
6a488035 TO |
336 | $this->beginPostProcess(); |
337 | ||
338 | // get the acl clauses built before we assemble the query | |
339 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
340 | $sql = $this->buildQuery(TRUE); | |
341 | ||
9d72cede EM |
342 | $dao = CRM_Core_DAO::executeQuery($sql); |
343 | $rows = $graphRows = array(); | |
6a488035 TO |
344 | $count = 0; |
345 | while ($dao->fetch()) { | |
346 | $row = array(); | |
347 | foreach ($this->_columnHeaders as $key => $value) { | |
348 | $row[$key] = $dao->$key; | |
349 | } | |
350 | ||
351 | $rows[] = $row; | |
352 | } | |
353 | $this->formatDisplay($rows); | |
354 | ||
355 | // assign variables to templates | |
356 | $this->doTemplateAssignment($rows); | |
357 | ||
358 | $this->endPostProcess($rows); | |
359 | } | |
360 | ||
74cf4551 | 361 | /** |
ced9bfed EM |
362 | * Alter display of rows. |
363 | * | |
364 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
365 | * such as rendering contacts as links. | |
366 | * | |
367 | * @param array $rows | |
368 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 369 | */ |
00be9182 | 370 | public function alterDisplay(&$rows) { |
6a488035 TO |
371 | $entryFound = FALSE; |
372 | $checkList = array(); | |
373 | ||
374 | foreach ($rows as $rowNum => $row) { | |
375 | ||
376 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { | |
377 | // not repeat contact display names if it matches with the one | |
378 | // in previous row | |
379 | ||
380 | $repeatFound = FALSE; | |
381 | foreach ($row as $colName => $colVal) { | |
a7488080 | 382 | if (!empty($checkList[$colName]) && |
6a488035 TO |
383 | is_array($checkList[$colName]) && |
384 | in_array($colVal, $checkList[$colName]) | |
385 | ) { | |
386 | $rows[$rowNum][$colName] = ""; | |
387 | $repeatFound = TRUE; | |
388 | } | |
389 | if (in_array($colName, $this->_noRepeats)) { | |
390 | $checkList[$colName][] = $colVal; | |
391 | } | |
392 | } | |
393 | } | |
394 | ||
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); | |
399 | } | |
400 | $entryFound = TRUE; | |
401 | } | |
402 | ||
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); | |
407 | } | |
408 | $entryFound = TRUE; | |
409 | } | |
410 | ||
411 | // handle country | |
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); | |
415 | } | |
416 | $entryFound = TRUE; | |
417 | } | |
418 | ||
419 | // convert display name to links | |
420 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
421 | array_key_exists('civicrm_contact_id', $row) | |
422 | ) { | |
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 | |
426 | ); | |
427 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
428 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact."); | |
429 | } | |
430 | ||
d62fab33 RN |
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]; | |
435 | } | |
436 | $entryFound = TRUE; | |
437 | } | |
438 | ||
6a488035 TO |
439 | // skip looking further in rows, if first row itself doesn't |
440 | // have the column we need | |
441 | if (!$entryFound) { | |
442 | break; | |
443 | } | |
444 | } | |
445 | } | |
96025800 | 446 | |
6a488035 | 447 | } |