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