3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Report_Form_Member_Detail
extends CRM_Report_Form
{
37 protected $_addressField = FALSE;
39 protected $_emailField = FALSE;
41 protected $_phoneField = FALSE;
43 protected $_contribField = FALSE;
45 protected $_summary = NULL;
47 protected $_customGroupExtends = array('Membership', 'Contribution');
48 protected $_customGroupGroupBy = FALSE;
50 function __construct() {
52 // Check if CiviCampaign is a) enabled and b) has active campaigns
53 $config = CRM_Core_Config
::singleton();
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
);
61 $this->_columns
= array(
64 'dao' => 'CRM_Contact_DAO_Contact',
68 array('title' => ts('Contact Name'),
79 array('title' => ts('First Name'),
88 array('title' => ts('Last Name'),
100 array('title' => ts('Contact Name'),
101 'operator' => 'like',
104 array('no_display' => TRUE),
108 'sort_name' => array(
109 'title' => ts('Last Name, First Name'),
111 'default_weight' => '0',
112 'default_order' => 'ASC'
115 'grouping' => 'contact-fields',
117 'civicrm_membership' =>
119 'dao' => 'CRM_Member_DAO_Membership',
122 'membership_type_id' => array(
123 'title' => 'Membership Type',
127 'membership_start_date' => array('title' => ts('Start Date'),
130 'membership_end_date' => array('title' => ts('End Date'),
133 'join_date' => array('title' => ts('Join Date'),
136 'source' => array('title' => 'Source'),
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
,
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(),
158 'grouping' => 'member-fields',
160 'civicrm_membership_status' =>
162 'dao' => 'CRM_Member_DAO_MembershipStatus',
163 'alias' => 'mem_status',
165 array('name' => array('title' => ts('Status'),
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'),
179 'grouping' => 'member-fields',
183 'dao' => 'CRM_Core_DAO_Address',
186 'street_address' => NULL,
188 'postal_code' => NULL,
189 'state_province_id' =>
190 array('title' => ts('State/Province'),
193 array('title' => ts('Country'),
196 'grouping' => 'contact-fields',
200 'dao' => 'CRM_Core_DAO_Email',
202 array('email' => NULL),
203 'grouping' => 'contact-fields',
207 'dao' => 'CRM_Core_DAO_Phone',
209 array('phone' => NULL),
210 'grouping' => 'contact-fields',
212 'civicrm_contribution' =>
214 'dao' => 'CRM_Contribute_DAO_Contribution',
217 'contribution_id' => array(
219 'no_display' => TRUE,
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')),
227 'no_display' => TRUE,
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)'),
236 array('sum' => ts('Amount')),
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
,
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
,
256 array('title' => 'Currency',
257 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
258 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
260 'type' => CRM_Utils_Type
::T_STRING
,
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
,
269 array('title' => ts('Contribution Amount')),
271 'grouping' => 'contri-fields',
274 $this->_groupFilter
= TRUE;
275 $this->_tagFilter
= TRUE;
277 // If we have active campaigns add those elements to both the fields and filters
278 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
279 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
280 'title' => ts('Campaign'),
281 'default' => 'false',
283 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
284 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
285 'options' => $this->activeCampaigns
,
287 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
291 $this->_currencyColumn
= 'civicrm_contribution_currency';
292 parent
::__construct();
295 function preProcess() {
296 $this->assign('reportTitle', ts('Membership Detail Report'));
297 parent
::preProcess();
301 $select = $this->_columnHeaders
= array();
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'])
309 if ($tableName == 'civicrm_address') {
310 $this->_addressField
= TRUE;
312 elseif ($tableName == 'civicrm_email') {
313 $this->_emailField
= TRUE;
315 elseif ($tableName == 'civicrm_phone') {
316 $this->_phoneField
= TRUE;
318 elseif ($tableName == 'civicrm_contribution') {
319 $this->_contribField
= TRUE;
321 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
322 if (array_key_exists('title', $field)) {
323 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
325 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
331 $this->_select
= "SELECT " . implode(', ', $select) . " ";
338 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
339 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
340 ON {$this->_aliases['civicrm_contact']}.id =
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']}
343 ON {$this->_aliases['civicrm_membership_status']}.id =
344 {$this->_aliases['civicrm_membership']}.status_id ";
347 //used when address field is selected
348 if ($this->_addressField
) {
350 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
351 ON {$this->_aliases['civicrm_contact']}.id =
352 {$this->_aliases['civicrm_address']}.contact_id AND
353 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
355 //used when email field is selected
356 if ($this->_emailField
) {
358 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
359 ON {$this->_aliases['civicrm_contact']}.id =
360 {$this->_aliases['civicrm_email']}.contact_id AND
361 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
363 //used when phone field is selected
364 if ($this->_phoneField
) {
366 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
367 ON {$this->_aliases['civicrm_contact']}.id =
368 {$this->_aliases['civicrm_phone']}.contact_id AND
369 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
371 //used when contribution field is selected
372 if ($this->_contribField
) {
375 SELECT cc.*, cmp.membership_id as membership_id
376 FROM civicrm_membership_payment cmp
377 JOIN civicrm_contribution cc
378 ON cc.id = cmp.contribution_id
379 ORDER BY cc.receive_date DESC
380 ) {$this->_aliases['civicrm_contribution']}
381 ON {$this->_aliases['civicrm_membership']}.id =
382 {$this->_aliases['civicrm_contribution']}.membership_id\n";
388 foreach ($this->_columns
as $tableName => $table) {
389 if (array_key_exists('filters', $table)) {
390 foreach ($table['filters'] as $fieldName => $field) {
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
);
397 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
400 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
402 $clause = $this->whereClause($field,
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
)
411 if (!empty($clause)) {
412 $clauses[] = $clause;
418 if (empty($clauses)) {
419 $this->_where
= "WHERE ( 1 ) ";
422 $this->_where
= "WHERE " . implode(' AND ', $clauses);
425 if ($this->_aclWhere
) {
426 $this->_where
.= " AND {$this->_aclWhere} ";
431 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
435 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
437 if ($this->_contribField
) {
438 $this->_orderBy
.= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
442 function postProcess() {
444 $this->beginPostProcess();
446 // get the acl clauses built before we assemble the query
447 $this->buildACLClause($this->_aliases
['civicrm_contact']);
448 $sql = $this->buildQuery(TRUE);
451 $this->buildRows($sql, $rows);
453 $this->formatDisplay($rows);
454 $this->doTemplateAssignment($rows);
455 $this->endPostProcess($rows);
458 function alterDisplay(&$rows) {
459 // custom code to alter rows
461 $checkList = array();
463 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
464 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
465 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
467 foreach ($rows as $rowNum => $row) {
469 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
470 // not repeat contact display names if it matches with the one
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])
478 $rows[$rowNum][$colName] = "";
481 if (in_array($colName, $this->_noRepeats
)) {
482 $checkList[$colName][] = $colVal;
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);
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);
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);
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)
512 $url = CRM_Utils_System
::url("civicrm/contact/view",
513 'reset=1&cid=' . $row['civicrm_contact_id'],
516 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
517 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
521 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
522 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
525 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
526 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
529 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
530 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
534 // Convert campaign_id to campaign title
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];