3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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'),
98 'title' => ts('Contact Type'),
100 'contact_sub_type' =>
102 'title' => ts('Contact SubType'),
108 array('title' => ts('Contact Name'),
109 'operator' => 'like',
112 array('no_display' => TRUE),
116 'sort_name' => array(
117 'title' => ts('Last Name, First Name'),
119 'default_weight' => '0',
120 'default_order' => 'ASC'
123 'grouping' => 'contact-fields',
125 'civicrm_membership' =>
127 'dao' => 'CRM_Member_DAO_Membership',
130 'membership_type_id' => array(
131 'title' => 'Membership Type',
135 'membership_start_date' => array('title' => ts('Start Date'),
138 'membership_end_date' => array('title' => ts('End Date'),
141 'join_date' => array('title' => ts('Join Date'),
144 'source' => array('title' => 'Source'),
148 array('operatorType' => CRM_Report_Form
::OP_DATE
),
149 'membership_start_date' =>
150 array('operatorType' => CRM_Report_Form
::OP_DATE
),
151 'membership_end_date' =>
152 array('operatorType' => CRM_Report_Form
::OP_DATE
),
153 'owner_membership_id' =>
154 array('title' => ts('Membership Owner ID'),
155 'operatorType' => CRM_Report_Form
::OP_INT
,
159 'name' => 'membership_type_id',
160 'title' => ts('Membership Types'),
161 'type' => CRM_Utils_Type
::T_INT
,
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Member_PseudoConstant
::membershipType(),
166 'grouping' => 'member-fields',
168 'civicrm_membership_status' =>
170 'dao' => 'CRM_Member_DAO_MembershipStatus',
171 'alias' => 'mem_status',
173 array('name' => array('title' => ts('Status'),
181 'title' => ts('Status'),
182 'type' => CRM_Utils_Type
::T_INT
,
183 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
184 'options' => CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label'),
187 'grouping' => 'member-fields',
191 'dao' => 'CRM_Core_DAO_Address',
194 'street_address' => NULL,
196 'postal_code' => NULL,
197 'state_province_id' =>
198 array('title' => ts('State/Province'),
201 array('title' => ts('Country'),
204 'grouping' => 'contact-fields',
208 'dao' => 'CRM_Core_DAO_Email',
210 array('email' => NULL),
211 'grouping' => 'contact-fields',
215 'dao' => 'CRM_Core_DAO_Phone',
217 array('phone' => NULL),
218 'grouping' => 'contact-fields',
220 'civicrm_contribution' =>
222 'dao' => 'CRM_Contribute_DAO_Contribution',
225 'contribution_id' => array(
227 'no_display' => TRUE,
230 'financial_type_id' => array('title' => ts('Financial Type')),
231 'contribution_status_id' => array('title' => ts('Contribution Status')),
232 'payment_instrument_id' => array('title' => ts('Payment Type')),
235 'no_display' => TRUE,
238 'receive_date' => NULL,
239 'receipt_date' => NULL,
240 'fee_amount' => NULL,
241 'net_amount' => NULL,
242 'total_amount' => array('title' => ts('Payment Amount (most recent)'),
244 array('sum' => ts('Amount')),
250 array('operatorType' => CRM_Report_Form
::OP_DATE
),
251 'financial_type_id' =>
252 array('title' => ts('Financial Type'),
253 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
254 'options' => CRM_Contribute_PseudoConstant
::financialType(),
255 'type' => CRM_Utils_Type
::T_INT
,
257 'payment_instrument_id' =>
258 array('title' => ts('Payment Type'),
259 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
260 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
261 'type' => CRM_Utils_Type
::T_INT
,
264 array('title' => 'Currency',
265 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
266 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
268 'type' => CRM_Utils_Type
::T_STRING
,
270 'contribution_status_id' =>
271 array('title' => ts('Contribution Status'),
272 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
273 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
274 'type' => CRM_Utils_Type
::T_INT
,
277 array('title' => ts('Contribution Amount')),
279 'grouping' => 'contri-fields',
282 $this->_groupFilter
= TRUE;
283 $this->_tagFilter
= TRUE;
285 // If we have active campaigns add those elements to both the fields and filters
286 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
287 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
288 'title' => ts('Campaign'),
289 'default' => 'false',
291 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
292 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
293 'options' => $this->activeCampaigns
,
295 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
299 $this->_currencyColumn
= 'civicrm_contribution_currency';
300 parent
::__construct();
303 function preProcess() {
304 $this->assign('reportTitle', ts('Membership Detail Report'));
305 parent
::preProcess();
309 $select = $this->_columnHeaders
= array();
311 foreach ($this->_columns
as $tableName => $table) {
312 if (array_key_exists('fields', $table)) {
313 foreach ($table['fields'] as $fieldName => $field) {
314 if (CRM_Utils_Array
::value('required', $field) ||
315 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
317 if ($tableName == 'civicrm_address') {
318 $this->_addressField
= TRUE;
320 elseif ($tableName == 'civicrm_email') {
321 $this->_emailField
= TRUE;
323 elseif ($tableName == 'civicrm_phone') {
324 $this->_phoneField
= TRUE;
326 elseif ($tableName == 'civicrm_contribution') {
327 $this->_contribField
= TRUE;
329 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
330 if (array_key_exists('title', $field)) {
331 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
333 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
339 $this->_select
= "SELECT " . implode(', ', $select) . " ";
346 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
347 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
348 ON {$this->_aliases['civicrm_contact']}.id =
349 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
350 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
351 ON {$this->_aliases['civicrm_membership_status']}.id =
352 {$this->_aliases['civicrm_membership']}.status_id ";
355 //used when address field is selected
356 if ($this->_addressField
) {
358 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
359 ON {$this->_aliases['civicrm_contact']}.id =
360 {$this->_aliases['civicrm_address']}.contact_id AND
361 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
363 //used when email field is selected
364 if ($this->_emailField
) {
366 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
367 ON {$this->_aliases['civicrm_contact']}.id =
368 {$this->_aliases['civicrm_email']}.contact_id AND
369 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
371 //used when phone field is selected
372 if ($this->_phoneField
) {
374 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
375 ON {$this->_aliases['civicrm_contact']}.id =
376 {$this->_aliases['civicrm_phone']}.contact_id AND
377 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
379 //used when contribution field is selected
380 if ($this->_contribField
) {
383 SELECT cc.*, cmp.membership_id as membership_id
384 FROM civicrm_membership_payment cmp
385 JOIN civicrm_contribution cc
386 ON cc.id = cmp.contribution_id
387 ORDER BY cc.receive_date DESC
388 ) {$this->_aliases['civicrm_contribution']}
389 ON {$this->_aliases['civicrm_membership']}.id =
390 {$this->_aliases['civicrm_contribution']}.membership_id\n";
395 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
399 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
401 if ($this->_contribField
) {
402 $this->_orderBy
.= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
406 function postProcess() {
408 $this->beginPostProcess();
410 // get the acl clauses built before we assemble the query
411 $this->buildACLClause($this->_aliases
['civicrm_contact']);
412 $sql = $this->buildQuery(TRUE);
415 $this->buildRows($sql, $rows);
417 $this->formatDisplay($rows);
418 $this->doTemplateAssignment($rows);
419 $this->endPostProcess($rows);
422 function alterDisplay(&$rows) {
423 // custom code to alter rows
425 $checkList = array();
427 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
428 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
429 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
431 foreach ($rows as $rowNum => $row) {
433 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
434 // not repeat contact display names if it matches with the one
436 $repeatFound = FALSE;
437 foreach ($row as $colName => $colVal) {
438 if (CRM_Utils_Array
::value($colName, $checkList) &&
439 is_array($checkList[$colName]) &&
440 in_array($colVal, $checkList[$colName])
442 $rows[$rowNum][$colName] = "";
445 if (in_array($colName, $this->_noRepeats
)) {
446 $checkList[$colName][] = $colVal;
451 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
452 if ($value = $row['civicrm_membership_membership_type_id']) {
453 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
458 if (array_key_exists('civicrm_address_state_province_id', $row)) {
459 if ($value = $row['civicrm_address_state_province_id']) {
460 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
465 if (array_key_exists('civicrm_address_country_id', $row)) {
466 if ($value = $row['civicrm_address_country_id']) {
467 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
472 if (array_key_exists('civicrm_contact_sort_name', $row) &&
473 $rows[$rowNum]['civicrm_contact_sort_name'] &&
474 array_key_exists('civicrm_contact_id', $row)
476 $url = CRM_Utils_System
::url("civicrm/contact/view",
477 'reset=1&cid=' . $row['civicrm_contact_id'],
480 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
481 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
485 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
486 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
489 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
490 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
493 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
494 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
498 // Convert campaign_id to campaign title
499 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
500 if ($value = $row['civicrm_membership_campaign_id']) {
501 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];