3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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;
54 public function __construct() {
56 // Check if CiviCampaign is a) enabled and b) has active campaigns
57 $config = CRM_Core_Config
::singleton();
58 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
59 if ($campaignEnabled) {
60 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
61 $this->activeCampaigns
= $getCampaigns['campaigns'];
62 asort($this->activeCampaigns
);
65 $this->_columns
= array(
66 'civicrm_contact' => array(
67 'dao' => 'CRM_Contact_DAO_Contact',
70 'title' => ts('Contact Name'),
78 'first_name' => array(
79 'title' => ts('First Name'),
86 'title' => ts('Last Name'),
88 'contact_type' => array(
89 'title' => ts('Contact Type'),
91 'contact_sub_type' => array(
92 'title' => ts('Contact Subtype'),
97 'title' => ts('Contact Name'),
100 'id' => array('no_display' => TRUE),
102 'order_bys' => array(
103 'sort_name' => array(
104 'title' => ts('Last Name, First Name'),
106 'default_weight' => '0',
107 'default_order' => 'ASC',
110 'grouping' => 'contact-fields',
112 'civicrm_membership' => array(
113 'dao' => 'CRM_Member_DAO_Membership',
115 'membership_type_id' => array(
116 'title' => 'Membership Type',
120 'membership_start_date' => array(
121 'title' => ts('Start Date'),
124 'membership_end_date' => array(
125 'title' => ts('End Date'),
128 'join_date' => array(
129 'title' => ts('Join Date'),
132 'source' => array('title' => 'Source'),
135 'join_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
136 'membership_start_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
137 'membership_end_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
138 'owner_membership_id' => array(
139 'title' => ts('Membership Owner ID'),
140 'operatorType' => CRM_Report_Form
::OP_INT
,
143 'name' => 'membership_type_id',
144 'title' => ts('Membership Types'),
145 'type' => CRM_Utils_Type
::T_INT
,
146 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
147 'options' => CRM_Member_PseudoConstant
::membershipType(),
150 'order_bys' => array(
151 'membership_type_id' => array(
152 'title' => ts('Membership Type'),
154 'default_weight' => '1',
155 'default_order' => 'ASC',
158 'grouping' => 'member-fields',
160 'civicrm_membership_status' => array(
161 'dao' => 'CRM_Member_DAO_MembershipStatus',
162 'alias' => 'mem_status',
165 'title' => ts('Status'),
172 'title' => ts('Status'),
173 'type' => CRM_Utils_Type
::T_INT
,
174 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
175 'options' => CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label'),
178 'grouping' => 'member-fields',
180 'civicrm_address' => array(
181 'dao' => 'CRM_Core_DAO_Address',
183 'street_address' => NULL,
185 'postal_code' => NULL,
186 'state_province_id' => array(
187 'title' => ts('State/Province'),
189 'country_id' => array(
190 'title' => ts('Country'),
193 'grouping' => 'contact-fields',
195 'civicrm_email' => array(
196 'dao' => 'CRM_Core_DAO_Email',
197 'fields' => array('email' => NULL),
198 'grouping' => 'contact-fields',
200 'civicrm_phone' => array(
201 'dao' => 'CRM_Core_DAO_Phone',
202 'fields' => array('phone' => NULL),
203 'grouping' => 'contact-fields',
205 'civicrm_contribution' => array(
206 'dao' => 'CRM_Contribute_DAO_Contribution',
208 'contribution_id' => array(
210 'no_display' => TRUE,
213 'financial_type_id' => array('title' => ts('Financial Type')),
214 'contribution_status_id' => array('title' => ts('Contribution Status')),
215 'payment_instrument_id' => array('title' => ts('Payment Type')),
218 'no_display' => TRUE,
221 'receive_date' => NULL,
222 'receipt_date' => NULL,
223 'fee_amount' => NULL,
224 'net_amount' => NULL,
225 'total_amount' => array(
226 'title' => ts('Payment Amount (most recent)'),
227 'statistics' => array('sum' => ts('Amount')),
231 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
232 'financial_type_id' => array(
233 'title' => ts('Financial Type'),
234 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
235 'options' => CRM_Contribute_PseudoConstant
::financialType(),
236 'type' => CRM_Utils_Type
::T_INT
,
238 'payment_instrument_id' => array(
239 'title' => ts('Payment Type'),
240 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
241 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
242 'type' => CRM_Utils_Type
::T_INT
,
245 'title' => 'Currency',
246 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
247 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
249 'type' => CRM_Utils_Type
::T_STRING
,
251 'contribution_status_id' => array(
252 'title' => ts('Contribution Status'),
253 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
254 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
255 'type' => CRM_Utils_Type
::T_INT
,
257 'total_amount' => array('title' => ts('Contribution Amount')),
259 'order_bys' => array(
260 'receive_date' => array(
261 'title' => ts('Receive Date'),
262 'default_weight' => '2',
263 'default_order' => 'DESC',
266 'grouping' => 'contri-fields',
269 $this->_groupFilter
= TRUE;
270 $this->_tagFilter
= TRUE;
272 // If we have active campaigns add those elements to both the fields and filters
273 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
274 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
275 'title' => ts('Campaign'),
276 'default' => 'false',
278 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array(
279 'title' => ts('Campaign'),
280 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
281 'options' => $this->activeCampaigns
,
283 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
287 $this->_currencyColumn
= 'civicrm_contribution_currency';
288 parent
::__construct();
291 public function preProcess() {
292 $this->assign('reportTitle', ts('Membership Detail Report'));
293 parent
::preProcess();
296 public function select() {
297 $select = $this->_columnHeaders
= array();
299 foreach ($this->_columns
as $tableName => $table) {
300 if (array_key_exists('fields', $table)) {
301 foreach ($table['fields'] as $fieldName => $field) {
302 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
303 if ($tableName == 'civicrm_address') {
304 $this->_addressField
= TRUE;
306 elseif ($tableName == 'civicrm_email') {
307 $this->_emailField
= TRUE;
309 elseif ($tableName == 'civicrm_phone') {
310 $this->_phoneField
= TRUE;
312 elseif ($tableName == 'civicrm_contribution') {
313 $this->_contribField
= TRUE;
315 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
316 if (array_key_exists('title', $field)) {
317 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
319 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
325 $this->_select
= "SELECT " . implode(', ', $select) . " ";
328 public function from() {
330 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
331 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
332 ON {$this->_aliases['civicrm_contact']}.id =
333 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
334 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
335 ON {$this->_aliases['civicrm_membership_status']}.id =
336 {$this->_aliases['civicrm_membership']}.status_id ";
338 //used when address field is selected
339 if ($this->_addressField
) {
341 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
342 ON {$this->_aliases['civicrm_contact']}.id =
343 {$this->_aliases['civicrm_address']}.contact_id AND
344 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
346 //used when email field is selected
347 if ($this->_emailField
) {
349 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
350 ON {$this->_aliases['civicrm_contact']}.id =
351 {$this->_aliases['civicrm_email']}.contact_id AND
352 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
354 //used when phone field is selected
355 if ($this->_phoneField
) {
357 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
358 ON {$this->_aliases['civicrm_contact']}.id =
359 {$this->_aliases['civicrm_phone']}.contact_id AND
360 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
362 //used when contribution field is selected
363 if ($this->_contribField
) {
365 LEFT JOIN civicrm_membership_payment cmp
366 ON {$this->_aliases['civicrm_membership']}.id = cmp.membership_id
367 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
368 ON cmp.contribution_id={$this->_aliases['civicrm_contribution']}.id\n";
372 public function groupBy() {
373 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
376 public function postProcess() {
378 $this->beginPostProcess();
380 // get the acl clauses built before we assemble the query
381 $this->buildACLClause($this->_aliases
['civicrm_contact']);
382 $sql = $this->buildQuery(TRUE);
385 $this->buildRows($sql, $rows);
387 $this->formatDisplay($rows);
388 $this->doTemplateAssignment($rows);
389 $this->endPostProcess($rows);
393 * Alter display of rows.
395 * Iterate through the rows retrieved via SQL and make changes for display purposes,
396 * such as rendering contacts as links.
399 * Rows generated by SQL, with an array for each row.
401 public function alterDisplay(&$rows) {
403 $checkList = array();
405 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
406 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
407 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
409 $repeatFound = FALSE;
410 foreach ($rows as $rowNum => $row) {
411 if ($repeatFound == FALSE ||
412 $repeatFound < $rowNum - 1
415 $checkList = array();
417 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
418 // not repeat contact display names if it matches with the one
420 foreach ($row as $colName => $colVal) {
421 if (in_array($colName, $this->_noRepeats
) &&
424 if ($rows[$rowNum][$colName] == $rows[$rowNum - 1][$colName] ||
425 (!empty($checkList[$colName]) &&
426 in_array($colVal, $checkList[$colName]))
428 $rows[$rowNum][$colName] = "";
429 // CRM-15917: Don't blank the name if it's a different contact
430 if ($colName == 'civicrm_contact_exposed_id') {
431 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
433 $repeatFound = $rowNum;
436 if (in_array($colName, $this->_noRepeats
)) {
437 $checkList[$colName][] = $colVal;
442 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
443 if ($value = $row['civicrm_membership_membership_type_id']) {
444 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
449 if (array_key_exists('civicrm_address_state_province_id', $row)) {
450 if ($value = $row['civicrm_address_state_province_id']) {
451 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
456 if (array_key_exists('civicrm_address_country_id', $row)) {
457 if ($value = $row['civicrm_address_country_id']) {
458 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
463 if (array_key_exists('civicrm_contact_sort_name', $row) &&
464 $rows[$rowNum]['civicrm_contact_sort_name'] &&
465 array_key_exists('civicrm_contact_id', $row)
467 $url = CRM_Utils_System
::url("civicrm/contact/view",
468 'reset=1&cid=' . $row['civicrm_contact_id'],
471 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
476 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
477 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
480 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
481 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
484 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
485 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
489 // Convert campaign_id to campaign title
490 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
491 if ($value = $row['civicrm_membership_campaign_id']) {
492 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];