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