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