CRM-14106 - Regex targeting inline conditonal statements
[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) {
312 if (CRM_Utils_Array::value('required', $field) ||
313 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
314 ) {
315 if ($tableName == 'civicrm_address') {
316 $this->_addressField = TRUE;
317 }
318 elseif ($tableName == 'civicrm_email') {
319 $this->_emailField = TRUE;
320 }
321 elseif ($tableName == 'civicrm_phone') {
322 $this->_phoneField = TRUE;
323 }
324 elseif ($tableName == 'civicrm_contribution') {
325 $this->_contribField = TRUE;
326 }
327 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
328 if (array_key_exists('title', $field)) {
329 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
330 }
331 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
332 }
333 }
334 }
335 }
336
337 $this->_select = "SELECT " . implode(', ', $select) . " ";
338 }
339
340 function from() {
341 $this->_from = NULL;
342
343 $this->_from = "
344 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
f813f78e 345 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
346 ON {$this->_aliases['civicrm_contact']}.id =
6a488035
TO
347 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
348 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
f813f78e 349 ON {$this->_aliases['civicrm_membership_status']}.id =
6a488035
TO
350 {$this->_aliases['civicrm_membership']}.status_id ";
351
352
353 //used when address field is selected
354 if ($this->_addressField) {
355 $this->_from .= "
f813f78e 356 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
357 ON {$this->_aliases['civicrm_contact']}.id =
358 {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
359 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
360 }
361 //used when email field is selected
362 if ($this->_emailField) {
363 $this->_from .= "
f813f78e 364 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
365 ON {$this->_aliases['civicrm_contact']}.id =
366 {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
367 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
368 }
369 //used when phone field is selected
370 if ($this->_phoneField) {
371 $this->_from .= "
f813f78e 372 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
373 ON {$this->_aliases['civicrm_contact']}.id =
374 {$this->_aliases['civicrm_phone']}.contact_id AND
6a488035
TO
375 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
376 }
377 //used when contribution field is selected
378 if ($this->_contribField) {
379 $this->_from .= "
380 LEFT JOIN (
f813f78e 381 SELECT cc.*, cmp.membership_id as membership_id
6a488035 382 FROM civicrm_membership_payment cmp
f813f78e 383 JOIN civicrm_contribution cc
384 ON cc.id = cmp.contribution_id
6a488035 385 ORDER BY cc.receive_date DESC
f813f78e 386 ) {$this->_aliases['civicrm_contribution']}
387 ON {$this->_aliases['civicrm_membership']}.id =
6a488035
TO
388 {$this->_aliases['civicrm_contribution']}.membership_id\n";
389 }
390 }
391
6a488035
TO
392 function groupBy() {
393 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
394 }
395
396 function orderBy() {
397 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
9dad7c4d
DS
398
399 if ($this->_contribField) {
400 $this->_orderBy .= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
2efcf0c2 401 }
6a488035
TO
402 }
403
404 function postProcess() {
405
406 $this->beginPostProcess();
407
408 // get the acl clauses built before we assemble the query
409 $this->buildACLClause($this->_aliases['civicrm_contact']);
410 $sql = $this->buildQuery(TRUE);
411
412 $rows = array();
413 $this->buildRows($sql, $rows);
414
415 $this->formatDisplay($rows);
416 $this->doTemplateAssignment($rows);
417 $this->endPostProcess($rows);
418 }
419
420 function alterDisplay(&$rows) {
421 // custom code to alter rows
422 $entryFound = FALSE;
423 $checkList = array();
f813f78e 424
6a488035
TO
425 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
426 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
427 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
f813f78e 428
6a488035
TO
429 foreach ($rows as $rowNum => $row) {
430
431 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
432 // not repeat contact display names if it matches with the one
433 // in previous row
434 $repeatFound = FALSE;
435 foreach ($row as $colName => $colVal) {
436 if (CRM_Utils_Array::value($colName, $checkList) &&
437 is_array($checkList[$colName]) &&
438 in_array($colVal, $checkList[$colName])
439 ) {
440 $rows[$rowNum][$colName] = "";
441 $repeatFound = TRUE;
442 }
443 if (in_array($colName, $this->_noRepeats)) {
444 $checkList[$colName][] = $colVal;
445 }
446 }
447 }
448
449 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
450 if ($value = $row['civicrm_membership_membership_type_id']) {
451 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE);
452 }
453 $entryFound = TRUE;
454 }
455
456 if (array_key_exists('civicrm_address_state_province_id', $row)) {
457 if ($value = $row['civicrm_address_state_province_id']) {
458 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
459 }
460 $entryFound = TRUE;
461 }
462
463 if (array_key_exists('civicrm_address_country_id', $row)) {
464 if ($value = $row['civicrm_address_country_id']) {
465 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
466 }
467 $entryFound = TRUE;
468 }
469
470 if (array_key_exists('civicrm_contact_sort_name', $row) &&
471 $rows[$rowNum]['civicrm_contact_sort_name'] &&
472 array_key_exists('civicrm_contact_id', $row)
473 ) {
474 $url = CRM_Utils_System::url("civicrm/contact/view",
475 'reset=1&cid=' . $row['civicrm_contact_id'],
476 $this->_absoluteUrl
477 );
478 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
479 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
480 $entryFound = TRUE;
481 }
f813f78e 482
6a488035
TO
483 if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
484 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
485 $entryFound = TRUE;
486 }
487 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
488 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
489 $entryFound = TRUE;
490 }
491 if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) {
492 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
493 $entryFound = TRUE;
494 }
495
0211a554 496 // Convert campaign_id to campaign title
229c2785
CD
497 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
498 if ($value = $row['civicrm_membership_campaign_id']) {
499 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns[$value];
0211a554
DL
500 $entryFound = TRUE;
501 }
502 }
503
6a488035
TO
504 if (!$entryFound) {
505 break;
506 }
507 }
508 }
509}
510