-- CRM-12528 Added campaign column and filter to reports
[civicrm-core.git] / CRM / Report / Form / Member / Lapse.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
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. |
15 | |
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. |
20 | |
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 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37 class CRM_Report_Form_Member_Lapse extends CRM_Report_Form {
38
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(
45 'Membership');
46 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
47
48 function __construct() {
49
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);
57 }
58
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(
62 'civicrm_contact' =>
63 array(
64 'dao' => 'CRM_Contact_DAO_Contact',
65 'fields' =>
66 array(
67 'sort_name' =>
68 array('title' => ts('Member Name'),
69 'no_repeat' => TRUE,
70 'required' => TRUE,
71 ),
72 'id' =>
73 array(
74 'no_display' => TRUE,
75 'required' => TRUE,
76 ),
77 'first_name' =>
78 array('title' => ts('First Name'),
79 'no_repeat' => TRUE,
80 ),
81 'id' =>
82 array(
83 'no_display' => TRUE,
84 'required' => TRUE,
85 ),
86 'last_name' =>
87 array('title' => ts('Last Name'),
88 'no_repeat' => TRUE,
89 ),
90 'id' =>
91 array(
92 'no_display' => TRUE,
93 'required' => TRUE,
94 ),
95 ),
96 'grouping' => 'contact-fields',
97 ),
98 'civicrm_membership_type' =>
99 array(
100 'dao' => 'CRM_Member_DAO_MembershipType',
101 'grouping' => 'member-fields',
102 'filters' =>
103 array(
104 'tid' =>
105 array(
106 'name' => 'id',
107 'title' => ts('Membership Types'),
108 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
109 'options' => CRM_Member_PseudoConstant::membershipType(),
110 ),
111 ),
112 ),
113 'civicrm_membership' =>
114 array(
115 'dao' => 'CRM_Member_DAO_Membership',
116 'grouping' => 'member-fields',
117 'fields' =>
118 array(
119 'membership_type_id' =>
120 array(
121 'title' => 'Membership Type',
122 'required' => TRUE,
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
125 'membership_start_date' => array('title' => ts('Current Cycle Start Date'),
126 ),
127 'membership_end_date' => array('title' => ts('Membership Lapse Date'),
128 'required' => TRUE,
129 ),
130 ),
131 'filters' =>
132 array(
133 'membership_end_date' =>
134 array(
135 'title' => 'Lapsed Memberships',
136 'operatorType' => CRM_Report_Form::OP_DATE,
137 ),
138 ),
139 ),
140 'civicrm_membership_status' =>
141 array(
142 'dao' => 'CRM_Member_DAO_MembershipStatus',
143 'alias' => 'mem_status',
144 'fields' =>
145 array(
146 'name' => array('title' => ts('Current Status'),
147 'required' => TRUE,
148 ),
149 ),
150 'grouping' => 'member-fields',
151 ),
152 'civicrm_address' =>
153 array(
154 'dao' => 'CRM_Core_DAO_Address',
155 'fields' =>
156 array(
157 'street_address' => NULL,
158 'city' => NULL,
159 'postal_code' => NULL,
160 'state_province_id' =>
161 array('title' => ts('State/Province'),
162 ),
163 'country_id' =>
164 array('title' => ts('Country'),
165 'default' => TRUE,
166 ),
167 ),
168 'grouping' => 'contact-fields',
169 ),
170 'civicrm_phone' =>
171 array(
172 'dao' => 'CRM_Core_DAO_Phone',
173 'alias' => 'phone',
174 'fields' =>
175 array('phone' => NULL),
176 'grouping' => 'contact-fields',
177 ),
178 'civicrm_email' =>
179 array(
180 'dao' => 'CRM_Core_DAO_Email',
181 'fields' =>
182 array('email' => NULL),
183 'grouping' => 'contact-fields',
184 ),
185 'civicrm_group' =>
186 array(
187 'dao' => 'CRM_Contact_DAO_GroupContact',
188 'alias' => 'cgroup',
189 'filters' =>
190 array(
191 'gid' =>
192 array(
193 'name' => 'group_id',
194 'title' => ts('Group'),
195 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
196 'group' => TRUE,
197 'options' => CRM_Core_PseudoConstant::group(),
198 ),
199 ),
200 ),
201 );
202
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',
208 );
209 $this->_columns['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
210 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
211 'options' => $this->activeCampaigns,
212 );
213 }
214
215 $this->_tagFilter = TRUE;
216 parent::__construct();
217 }
218
219 function preProcess() {
220 parent::preProcess();
221 }
222
223 function select() {
224 $select = array();
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'])
231 ) {
232 // to include optional columns address ,email and phone only if checked
233 if ($tableName == 'civicrm_address') {
234 $this->_addressField = TRUE;
235 }
236 elseif ($tableName == 'civicrm_email') {
237 $this->_emailField = TRUE;
238 }
239 elseif ($tableName == 'civicrm_phone') {
240 $this->_phoneField = TRUE;
241 }
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'];
245 }
246 }
247 }
248 }
249 $this->_select = "SELECT " . implode(', ', $select) . " ";
250 }
251
252 static function formRule($fields, $files, $self) {
253 $errors = $grouping = array();
254 //check for searching combination of dispaly columns and
255 //grouping criteria
256
257 return $errors;
258 }
259
260 function from() {
261 $this->_from = NULL;
262
263 $this->_from = "
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";
274
275 // include address field if address column is to be included
276 if ($this->_addressField) {
277 $this->_from .= "
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";
280 }
281
282 // include email field if email column is to be included
283 if ($this->_emailField) {
284 $this->_from .= "
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";
287 }
288
289 // include phone field if phone column is to be included
290 if ($this->_phoneField) {
291 $this->_from .= "
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";
295 }
296 }
297
298 function where() {
299 $clauses = array();
300 foreach ($this->_columns as $tableName => $table) {
301 if (array_key_exists('filters', $table)) {
302 foreach ($table['filters'] as $fieldName => $field) {
303 $clause = NULL;
304
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);
309
310 if ($relative || $from || $to) {
311 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
312 }
313 }
314 else {
315 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
316 if ($op) {
317 $clause = $this->whereClause($field,
318 $op,
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)
322 );
323 }
324 }
325 if (!empty($clause)) {
326 $clauses[$fieldName] = $clause;
327 }
328 }
329 }
330 }
331
332 if (empty($clauses)) {
333 $this->_where = "WHERE end_date < '" . date('Y-m-d') . "' AND {$this->_aliases['civicrm_membership_status']}.name = 'Expired'";
334 }
335 else {
336 if (!array_key_exists('end_date', $clauses)) {
337 $this->_where = "WHERE end_date < '" . date('Y-m-d') . "' AND " . implode(' AND ', $clauses);
338 }
339 else {
340 $this->_where = "WHERE " . implode(' AND ', $clauses);
341 }
342 }
343
344 if ($this->_aclWhere) {
345 $this->_where .= " AND {$this->_aclWhere} ";
346 }
347 }
348
349 function orderBy() {
350 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
351 }
352
353 function postProcess() {
354 $this->beginPostProcess();
355
356 // get the acl clauses built before we assemble the query
357 $this->buildACLClause($this->_aliases['civicrm_contact']);
358 $sql = $this->buildQuery(TRUE);
359
360 $dao = CRM_Core_DAO::executeQuery($sql);
361 $rows = $graphRows = array();
362 $count = 0;
363 while ($dao->fetch()) {
364 $row = array();
365 foreach ($this->_columnHeaders as $key => $value) {
366 $row[$key] = $dao->$key;
367 }
368
369 $rows[] = $row;
370 }
371 $this->formatDisplay($rows);
372
373 // assign variables to templates
374 $this->doTemplateAssignment($rows);
375
376 $this->endPostProcess($rows);
377 }
378
379 function alterDisplay(&$rows) {
380 // custom code to alter rows
381 $entryFound = FALSE;
382 $checkList = array();
383
384 foreach ($rows as $rowNum => $row) {
385
386 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
387 // not repeat contact display names if it matches with the one
388 // in previous row
389
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])
395 ) {
396 $rows[$rowNum][$colName] = "";
397 $repeatFound = TRUE;
398 }
399 if (in_array($colName, $this->_noRepeats)) {
400 $checkList[$colName][] = $colVal;
401 }
402 }
403 }
404
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);
409 }
410 $entryFound = TRUE;
411 }
412
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);
417 }
418 $entryFound = TRUE;
419 }
420
421 // handle country
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);
425 }
426 $entryFound = TRUE;
427 }
428
429 // convert display name to links
430 if (array_key_exists('civicrm_contact_sort_name', $row) &&
431 array_key_exists('civicrm_contact_id', $row)
432 ) {
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
436 );
437 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
438 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Membership Detail for this Contact.");
439 }
440
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];
445 }
446 $entryFound = TRUE;
447 }
448
449 // skip looking further in rows, if first row itself doesn't
450 // have the column we need
451 if (!$entryFound) {
452 break;
453 }
454 }
455 }
456 }
457