Added campaign information to membership report details form. Added a couple of comme...
[civicrm-core.git] / CRM / Report / Form / Member / Detail.php
CommitLineData
6a488035
TO
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 */
37class CRM_Report_Form_Member_Detail extends CRM_Report_Form {
38
39 protected $_addressField = FALSE;
40
41 protected $_emailField = FALSE;
42
43 protected $_phoneField = FALSE;
44
45 protected $_contribField = FALSE;
46
47 protected $_summary = NULL;
48
49 protected $_customGroupExtends = array('Membership', 'Contribution');
50 protected $_customGroupGroupBy = FALSE;
51
0211a554
DL
52 function __construct() {
53
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);
61 }
62
6a488035
TO
63 $this->_columns = array(
64 'civicrm_contact' =>
65 array(
66 'dao' => 'CRM_Contact_DAO_Contact',
67 'fields' =>
68 array(
69 'sort_name' =>
70 array('title' => ts('Contact Name'),
71 'required' => TRUE,
72 'default' => TRUE,
73 'no_repeat' => TRUE,
74 ),
75 'id' =>
76 array(
77 'no_display' => TRUE,
78 'required' => TRUE,
79 ),
80 'first_name' =>
81 array('title' => ts('First Name'),
82 'no_repeat' => TRUE,
83 ),
84 'id' =>
85 array(
86 'no_display' => TRUE,
87 'required' => TRUE,
88 ),
89 'last_name' =>
90 array('title' => ts('Last Name'),
91 'no_repeat' => TRUE,
92 ),
93 'id' =>
94 array(
95 'no_display' => TRUE,
96 'required' => TRUE,
97 ),
98 ),
99 'filters' =>
100 array(
101 'sort_name' =>
102 array('title' => ts('Contact Name'),
103 'operator' => 'like',
104 ),
105 'id' =>
106 array('no_display' => TRUE),
107 ),
108 'grouping' => 'contact-fields',
109 ),
110 'civicrm_membership' =>
111 array(
112 'dao' => 'CRM_Member_DAO_Membership',
113 'fields' =>
114 array(
115 'membership_type_id' => array(
116 'title' => 'Membership Type',
117 'required' => TRUE,
118 'no_repeat' => TRUE,
119 ),
120 'membership_start_date' => array('title' => ts('Start Date'),
121 'default' => TRUE,
122 ),
123 'membership_end_date' => array('title' => ts('End Date'),
124 'default' => TRUE,
125 ),
126 'join_date' => array('title' => ts('Join Date'),
127 'default' => TRUE,
128 ),
129 'source' => array('title' => 'Source'),
130 ),
131 'filters' => array(
132 'join_date' =>
133 array('operatorType' => CRM_Report_Form::OP_DATE),
134 'membership_start_date' =>
135 array('operatorType' => CRM_Report_Form::OP_DATE),
136 'membership_end_date' =>
137 array('operatorType' => CRM_Report_Form::OP_DATE),
138 'owner_membership_id' =>
139 array('title' => ts('Membership Owner ID'),
140 'operatorType' => CRM_Report_Form::OP_INT,
141 ),
142 'tid' =>
143 array(
144 'name' => 'membership_type_id',
145 'title' => ts('Membership Types'),
146 'type' => CRM_Utils_Type::T_INT,
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
148 'options' => CRM_Member_PseudoConstant::membershipType(),
149 ),
150 ),
151 'grouping' => 'member-fields',
152 ),
153 'civicrm_membership_status' =>
154 array(
155 'dao' => 'CRM_Member_DAO_MembershipStatus',
156 'alias' => 'mem_status',
157 'fields' =>
158 array('name' => array('title' => ts('Status'),
159 'default' => TRUE,
160 ),
161 ),
162 'filters' => array(
163 'sid' =>
164 array(
165 'name' => 'id',
166 'title' => ts('Status'),
167 'type' => CRM_Utils_Type::T_INT,
168 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
169 'options' => CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'),
170 ),
171 ),
172 'grouping' => 'member-fields',
173 ),
174 'civicrm_address' =>
175 array(
176 'dao' => 'CRM_Core_DAO_Address',
177 'fields' =>
178 array(
179 'street_address' => NULL,
180 'city' => NULL,
181 'postal_code' => NULL,
182 'state_province_id' =>
183 array('title' => ts('State/Province'),
184 ),
185 'country_id' =>
186 array('title' => ts('Country'),
187 ),
188 ),
189 'grouping' => 'contact-fields',
190 ),
191 'civicrm_email' =>
192 array(
193 'dao' => 'CRM_Core_DAO_Email',
194 'fields' =>
195 array('email' => NULL),
196 'grouping' => 'contact-fields',
197 ),
198 'civicrm_phone' =>
199 array(
200 'dao' => 'CRM_Core_DAO_Phone',
201 'fields' =>
202 array('phone' => NULL),
203 'grouping' => 'contact-fields',
204 ),
205 'civicrm_contribution' =>
206 array(
207 'dao' => 'CRM_Contribute_DAO_Contribution',
208 'fields' =>
209 array(
210 'contribution_id' => array(
211 'name' => 'id',
212 'no_display' => TRUE,
213 'required' => TRUE,
214 ),
215 'financial_type_id' => array('title' => ts('Financial Type')),
216 'contribution_status_id' => array('title' => ts('Contribution Status')),
217 'payment_instrument_id' => array('title' => ts('Payment Type')),
fdb63ad1 218 'currency' => array(
219 'required' => TRUE,
220 'no_display' => TRUE,
221 ),
6a488035
TO
222 'trxn_id' => NULL,
223 'receive_date' => NULL,
224 'receipt_date' => NULL,
225 'fee_amount' => NULL,
226 'net_amount' => NULL,
227 'total_amount' => array('title' => ts('Payment Amount (most recent)'),
228 'statistics' =>
229 array('sum' => ts('Amount')),
230 ),
231 ),
232 'filters' =>
233 array(
234 'receive_date' =>
235 array('operatorType' => CRM_Report_Form::OP_DATE),
236 'financial_type_id' =>
237 array('title' => ts('Financial Type'),
238 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
239 'options' => CRM_Contribute_PseudoConstant::financialType(),
240 'type' => CRM_Utils_Type::T_INT,
241 ),
242 'payment_instrument_id' =>
243 array('title' => ts('Payment Type'),
244 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
245 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
246 'type' => CRM_Utils_Type::T_INT,
247 ),
fdb63ad1 248 'currency' =>
249 array('title' => 'Currency',
250 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
251 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 252 'default' => NULL,
fdb63ad1 253 'type' => CRM_Utils_Type::T_STRING,
254 ),
6a488035
TO
255 'contribution_status_id' =>
256 array('title' => ts('Contribution Status'),
257 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
258 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
259 'type' => CRM_Utils_Type::T_INT,
260 ),
261 'total_amount' =>
262 array('title' => ts('Contribution Amount')),
263 ),
264 'grouping' => 'contri-fields',
265 ),
266 );
267 $this->_groupFilter = TRUE;
268 $this->_tagFilter = TRUE;
fdb63ad1 269
0211a554
DL
270 // If we have active campaigns add those elements to both the fields and filters
271 if ($campaignEnabled && !empty($this->activeCampaigns)) {
272 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
273 'title' => ts('Campaign'),
274 'default' => 'false',
275 );
276 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
277 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
278 'options' => $this->activeCampaigns,
279 );
280 $this->_columns['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
281 }
282
fdb63ad1 283 $this->_currencyColumn = 'civicrm_contribution_currency';
6a488035
TO
284 parent::__construct();
285 }
286
287 function preProcess() {
288 $this->assign('reportTitle', ts('Membership Detail Report'));
289 parent::preProcess();
290 }
291
292 function select() {
293 $select = $this->_columnHeaders = array();
294
295 foreach ($this->_columns as $tableName => $table) {
296 if (array_key_exists('fields', $table)) {
297 foreach ($table['fields'] as $fieldName => $field) {
298 if (CRM_Utils_Array::value('required', $field) ||
299 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
300 ) {
301 if ($tableName == 'civicrm_address') {
302 $this->_addressField = TRUE;
303 }
304 elseif ($tableName == 'civicrm_email') {
305 $this->_emailField = TRUE;
306 }
307 elseif ($tableName == 'civicrm_phone') {
308 $this->_phoneField = TRUE;
309 }
310 elseif ($tableName == 'civicrm_contribution') {
311 $this->_contribField = TRUE;
312 }
313 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
314 if (array_key_exists('title', $field)) {
315 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
316 }
317 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
318 }
319 }
320 }
321 }
322
323 $this->_select = "SELECT " . implode(', ', $select) . " ";
324 }
325
326 function from() {
327 $this->_from = NULL;
328
329 $this->_from = "
330 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
331 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
332 ON {$this->_aliases['civicrm_contact']}.id =
333 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
334 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
335 ON {$this->_aliases['civicrm_membership_status']}.id =
336 {$this->_aliases['civicrm_membership']}.status_id ";
337
338
339 //used when address field is selected
340 if ($this->_addressField) {
341 $this->_from .= "
342 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
343 ON {$this->_aliases['civicrm_contact']}.id =
344 {$this->_aliases['civicrm_address']}.contact_id AND
345 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
346 }
347 //used when email field is selected
348 if ($this->_emailField) {
349 $this->_from .= "
350 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
351 ON {$this->_aliases['civicrm_contact']}.id =
352 {$this->_aliases['civicrm_email']}.contact_id AND
353 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
354 }
355 //used when phone field is selected
356 if ($this->_phoneField) {
357 $this->_from .= "
358 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
359 ON {$this->_aliases['civicrm_contact']}.id =
360 {$this->_aliases['civicrm_phone']}.contact_id AND
361 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
362 }
363 //used when contribution field is selected
364 if ($this->_contribField) {
365 $this->_from .= "
366 LEFT JOIN (
367 SELECT cc.*, cmp.membership_id as membership_id
368 FROM civicrm_membership_payment cmp
369 JOIN civicrm_contribution cc
370 ON cc.id = cmp.contribution_id
371 ORDER BY cc.receive_date DESC
372 ) {$this->_aliases['civicrm_contribution']}
373 ON {$this->_aliases['civicrm_membership']}.id =
374 {$this->_aliases['civicrm_contribution']}.membership_id\n";
375 }
376 }
377
378 function where() {
379 $clauses = array();
380 foreach ($this->_columns as $tableName => $table) {
381 if (array_key_exists('filters', $table)) {
382 foreach ($table['filters'] as $fieldName => $field) {
383 $clause = NULL;
384 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Utils_Type::T_DATE) {
385 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
386 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
387 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
388
389 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
390 }
391 else {
392 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
393 if ($op) {
394 $clause = $this->whereClause($field,
395 $op,
396 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
397 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
398 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
399 );
400 }
401 }
402
403 if (!empty($clause)) {
404 $clauses[] = $clause;
405 }
406 }
407 }
408 }
409
410 if (empty($clauses)) {
411 $this->_where = "WHERE ( 1 ) ";
412 }
413 else {
414 $this->_where = "WHERE " . implode(' AND ', $clauses);
415 }
416
417 if ($this->_aclWhere) {
418 $this->_where .= " AND {$this->_aclWhere} ";
419 }
420 }
421
422 function groupBy() {
423 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
424 }
425
426 function orderBy() {
427 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
428 }
429
430 function postProcess() {
431
432 $this->beginPostProcess();
433
434 // get the acl clauses built before we assemble the query
435 $this->buildACLClause($this->_aliases['civicrm_contact']);
436 $sql = $this->buildQuery(TRUE);
437
438 $rows = array();
439 $this->buildRows($sql, $rows);
440
441 $this->formatDisplay($rows);
442 $this->doTemplateAssignment($rows);
443 $this->endPostProcess($rows);
444 }
445
446 function alterDisplay(&$rows) {
447 // custom code to alter rows
448 $entryFound = FALSE;
449 $checkList = array();
450
451 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
452 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
453 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
454
455 foreach ($rows as $rowNum => $row) {
456
457 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
458 // not repeat contact display names if it matches with the one
459 // in previous row
460 $repeatFound = FALSE;
461 foreach ($row as $colName => $colVal) {
462 if (CRM_Utils_Array::value($colName, $checkList) &&
463 is_array($checkList[$colName]) &&
464 in_array($colVal, $checkList[$colName])
465 ) {
466 $rows[$rowNum][$colName] = "";
467 $repeatFound = TRUE;
468 }
469 if (in_array($colName, $this->_noRepeats)) {
470 $checkList[$colName][] = $colVal;
471 }
472 }
473 }
474
475 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
476 if ($value = $row['civicrm_membership_membership_type_id']) {
477 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE);
478 }
479 $entryFound = TRUE;
480 }
481
482 if (array_key_exists('civicrm_address_state_province_id', $row)) {
483 if ($value = $row['civicrm_address_state_province_id']) {
484 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
485 }
486 $entryFound = TRUE;
487 }
488
489 if (array_key_exists('civicrm_address_country_id', $row)) {
490 if ($value = $row['civicrm_address_country_id']) {
491 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
492 }
493 $entryFound = TRUE;
494 }
495
496 if (array_key_exists('civicrm_contact_sort_name', $row) &&
497 $rows[$rowNum]['civicrm_contact_sort_name'] &&
498 array_key_exists('civicrm_contact_id', $row)
499 ) {
500 $url = CRM_Utils_System::url("civicrm/contact/view",
501 'reset=1&cid=' . $row['civicrm_contact_id'],
502 $this->_absoluteUrl
503 );
504 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
505 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
506 $entryFound = TRUE;
507 }
508
509 if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
510 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
511 $entryFound = TRUE;
512 }
513 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
514 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
515 $entryFound = TRUE;
516 }
517 if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) {
518 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
519 $entryFound = TRUE;
520 }
521
0211a554
DL
522 // Convert campaign_id to campaign title
523 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
524 if ($value = $row['civicrm_contribution_campaign_id']) {
525 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
526 $entryFound = TRUE;
527 }
528 }
529
6a488035
TO
530 if (!$entryFound) {
531 break;
532 }
533 }
534 }
535}
536