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