3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
56 function __construct() {
58 // Check if CiviCampaign is a) enabled and b) has active campaigns
59 $config = CRM_Core_Config
::singleton();
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
);
67 $this->_columns
= array(
70 'dao' => 'CRM_Contact_DAO_Contact',
74 array('title' => ts('Contact Name'),
85 array('title' => ts('First Name'),
93 array('title' => ts('Last Name'),
102 'title' => ts('Contact Type'),
104 'contact_sub_type' =>
106 'title' => ts('Contact SubType'),
112 array('title' => ts('Contact Name'),
113 'operator' => 'like',
116 array('no_display' => TRUE),
120 'sort_name' => array(
121 'title' => ts('Last Name, First Name'),
123 'default_weight' => '0',
124 'default_order' => 'ASC'
127 'grouping' => 'contact-fields',
129 'civicrm_membership' =>
131 'dao' => 'CRM_Member_DAO_Membership',
134 'membership_type_id' => array(
135 'title' => 'Membership Type',
139 'membership_start_date' => array('title' => ts('Start Date'),
142 'membership_end_date' => array('title' => ts('End Date'),
145 'join_date' => array('title' => ts('Join Date'),
148 'source' => array('title' => 'Source'),
152 array('operatorType' => CRM_Report_Form
::OP_DATE
),
153 'membership_start_date' =>
154 array('operatorType' => CRM_Report_Form
::OP_DATE
),
155 'membership_end_date' =>
156 array('operatorType' => CRM_Report_Form
::OP_DATE
),
157 'owner_membership_id' =>
158 array('title' => ts('Membership Owner ID'),
159 'operatorType' => CRM_Report_Form
::OP_INT
,
163 'name' => 'membership_type_id',
164 'title' => ts('Membership Types'),
165 'type' => CRM_Utils_Type
::T_INT
,
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Member_PseudoConstant
::membershipType(),
170 'grouping' => 'member-fields',
172 'civicrm_membership_status' =>
174 'dao' => 'CRM_Member_DAO_MembershipStatus',
175 'alias' => 'mem_status',
177 array('name' => array('title' => ts('Status'),
185 'title' => ts('Status'),
186 'type' => CRM_Utils_Type
::T_INT
,
187 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
188 'options' => CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label'),
191 'grouping' => 'member-fields',
195 'dao' => 'CRM_Core_DAO_Address',
198 'street_address' => NULL,
200 'postal_code' => NULL,
201 'state_province_id' =>
202 array('title' => ts('State/Province'),
205 array('title' => ts('Country'),
208 'grouping' => 'contact-fields',
212 'dao' => 'CRM_Core_DAO_Email',
214 array('email' => NULL),
215 'grouping' => 'contact-fields',
219 'dao' => 'CRM_Core_DAO_Phone',
221 array('phone' => NULL),
222 'grouping' => 'contact-fields',
224 'civicrm_contribution' =>
226 'dao' => 'CRM_Contribute_DAO_Contribution',
229 'contribution_id' => array(
231 'no_display' => TRUE,
234 'financial_type_id' => array('title' => ts('Financial Type')),
235 'contribution_status_id' => array('title' => ts('Contribution Status')),
236 'payment_instrument_id' => array('title' => ts('Payment Type')),
239 'no_display' => TRUE,
242 'receive_date' => NULL,
243 'receipt_date' => NULL,
244 'fee_amount' => NULL,
245 'net_amount' => NULL,
246 'total_amount' => array('title' => ts('Payment Amount (most recent)'),
248 array('sum' => ts('Amount')),
254 array('operatorType' => CRM_Report_Form
::OP_DATE
),
255 'financial_type_id' =>
256 array('title' => ts('Financial Type'),
257 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
258 'options' => CRM_Contribute_PseudoConstant
::financialType(),
259 'type' => CRM_Utils_Type
::T_INT
,
261 'payment_instrument_id' =>
262 array('title' => ts('Payment Type'),
263 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
264 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
265 'type' => CRM_Utils_Type
::T_INT
,
268 array('title' => 'Currency',
269 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
270 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
272 'type' => CRM_Utils_Type
::T_STRING
,
274 'contribution_status_id' =>
275 array('title' => ts('Contribution Status'),
276 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
277 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
278 'type' => CRM_Utils_Type
::T_INT
,
281 array('title' => ts('Contribution Amount')),
283 'grouping' => 'contri-fields',
286 $this->_groupFilter
= TRUE;
287 $this->_tagFilter
= TRUE;
289 // If we have active campaigns add those elements to both the fields and filters
290 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
291 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
292 'title' => ts('Campaign'),
293 'default' => 'false',
295 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
296 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
297 'options' => $this->activeCampaigns
,
299 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
303 $this->_currencyColumn
= 'civicrm_contribution_currency';
304 parent
::__construct();
307 function preProcess() {
308 $this->assign('reportTitle', ts('Membership Detail Report'));
309 parent
::preProcess();
313 $select = $this->_columnHeaders
= array();
315 foreach ($this->_columns
as $tableName => $table) {
316 if (array_key_exists('fields', $table)) {
317 foreach ($table['fields'] as $fieldName => $field) {
318 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
319 if ($tableName == 'civicrm_address') {
320 $this->_addressField
= TRUE;
322 elseif ($tableName == 'civicrm_email') {
323 $this->_emailField
= TRUE;
325 elseif ($tableName == 'civicrm_phone') {
326 $this->_phoneField
= TRUE;
328 elseif ($tableName == 'civicrm_contribution') {
329 $this->_contribField
= TRUE;
331 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
332 if (array_key_exists('title', $field)) {
333 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
335 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
341 $this->_select
= "SELECT " . implode(', ', $select) . " ";
348 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
349 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
350 ON {$this->_aliases['civicrm_contact']}.id =
351 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
352 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
353 ON {$this->_aliases['civicrm_membership_status']}.id =
354 {$this->_aliases['civicrm_membership']}.status_id ";
357 //used when address field is selected
358 if ($this->_addressField
) {
360 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
361 ON {$this->_aliases['civicrm_contact']}.id =
362 {$this->_aliases['civicrm_address']}.contact_id AND
363 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
365 //used when email field is selected
366 if ($this->_emailField
) {
368 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
369 ON {$this->_aliases['civicrm_contact']}.id =
370 {$this->_aliases['civicrm_email']}.contact_id AND
371 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
373 //used when phone field is selected
374 if ($this->_phoneField
) {
376 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
377 ON {$this->_aliases['civicrm_contact']}.id =
378 {$this->_aliases['civicrm_phone']}.contact_id AND
379 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
381 //used when contribution field is selected
382 if ($this->_contribField
) {
385 SELECT cc.*, cmp.membership_id as membership_id
386 FROM civicrm_membership_payment cmp
387 JOIN civicrm_contribution cc
388 ON cc.id = cmp.contribution_id
389 ORDER BY cc.receive_date DESC
390 ) {$this->_aliases['civicrm_contribution']}
391 ON {$this->_aliases['civicrm_membership']}.id =
392 {$this->_aliases['civicrm_contribution']}.membership_id\n";
397 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
401 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
403 if ($this->_contribField
) {
404 $this->_orderBy
.= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
408 function postProcess() {
410 $this->beginPostProcess();
412 // get the acl clauses built before we assemble the query
413 $this->buildACLClause($this->_aliases
['civicrm_contact']);
414 $sql = $this->buildQuery(TRUE);
417 $this->buildRows($sql, $rows);
419 $this->formatDisplay($rows);
420 $this->doTemplateAssignment($rows);
421 $this->endPostProcess($rows);
427 function alterDisplay(&$rows) {
428 // custom code to alter rows
430 $checkList = array();
432 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
433 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
434 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
436 foreach ($rows as $rowNum => $row) {
438 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
439 // not repeat contact display names if it matches with the one
441 $repeatFound = FALSE;
442 foreach ($row as $colName => $colVal) {
443 if (!empty($checkList[$colName]) &&
444 is_array($checkList[$colName]) &&
445 in_array($colVal, $checkList[$colName])
447 $rows[$rowNum][$colName] = "";
450 if (in_array($colName, $this->_noRepeats
)) {
451 $checkList[$colName][] = $colVal;
456 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
457 if ($value = $row['civicrm_membership_membership_type_id']) {
458 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
463 if (array_key_exists('civicrm_address_state_province_id', $row)) {
464 if ($value = $row['civicrm_address_state_province_id']) {
465 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
470 if (array_key_exists('civicrm_address_country_id', $row)) {
471 if ($value = $row['civicrm_address_country_id']) {
472 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
477 if (array_key_exists('civicrm_contact_sort_name', $row) &&
478 $rows[$rowNum]['civicrm_contact_sort_name'] &&
479 array_key_exists('civicrm_contact_id', $row)
481 $url = CRM_Utils_System
::url("civicrm/contact/view",
482 'reset=1&cid=' . $row['civicrm_contact_id'],
485 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
486 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
490 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
491 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
494 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
495 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
498 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
499 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
503 // Convert campaign_id to campaign title
504 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
505 if ($value = $row['civicrm_membership_campaign_id']) {
506 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];