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'),
79 'first_name' => array(
80 'title' => ts('First Name'),
87 'title' => ts('Last Name'),
89 'contact_type' => array(
90 'title' => ts('Contact Type'),
92 'contact_sub_type' => array(
93 'title' => ts('Contact Subtype'),
98 'title' => ts('Contact Name'),
101 'id' => array('no_display' => TRUE),
103 'order_bys' => array(
104 'sort_name' => array(
105 'title' => ts('Last Name, First Name'),
107 'default_weight' => '0',
108 'default_order' => 'ASC',
111 'grouping' => 'contact-fields',
113 'civicrm_membership' => array(
114 'dao' => 'CRM_Member_DAO_Membership',
116 'membership_type_id' => array(
117 'title' => 'Membership Type',
121 'membership_start_date' => array(
122 'title' => ts('Start Date'),
125 'membership_end_date' => array(
126 'title' => ts('End Date'),
129 'join_date' => array(
130 'title' => ts('Join Date'),
133 'source' => array('title' => 'Source'),
136 'join_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
137 'membership_start_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
138 'membership_end_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
139 'owner_membership_id' => array(
140 'title' => ts('Membership Owner ID'),
141 'operatorType' => CRM_Report_Form
::OP_INT
,
144 'name' => 'membership_type_id',
145 'title' => ts('Membership Types'),
146 'type' => CRM_Utils_Type
::T_INT
,
147 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
148 'options' => CRM_Member_PseudoConstant
::membershipType(),
151 'grouping' => 'member-fields',
153 'civicrm_membership_status' => array(
154 'dao' => 'CRM_Member_DAO_MembershipStatus',
155 'alias' => 'mem_status',
158 'title' => ts('Status'),
165 'title' => ts('Status'),
166 'type' => CRM_Utils_Type
::T_INT
,
167 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
168 'options' => CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label'),
171 'grouping' => 'member-fields',
173 'civicrm_address' => array(
174 'dao' => 'CRM_Core_DAO_Address',
176 'street_address' => NULL,
178 'postal_code' => NULL,
179 'state_province_id' => array(
180 'title' => ts('State/Province'),
182 'country_id' => array(
183 'title' => ts('Country'),
186 'grouping' => 'contact-fields',
188 'civicrm_email' => array(
189 'dao' => 'CRM_Core_DAO_Email',
190 'fields' => array('email' => NULL),
191 'grouping' => 'contact-fields',
193 'civicrm_phone' => array(
194 'dao' => 'CRM_Core_DAO_Phone',
195 'fields' => array('phone' => NULL),
196 'grouping' => 'contact-fields',
198 'civicrm_contribution' => array(
199 'dao' => 'CRM_Contribute_DAO_Contribution',
201 'contribution_id' => array(
203 'no_display' => TRUE,
206 'financial_type_id' => array('title' => ts('Financial Type')),
207 'contribution_status_id' => array('title' => ts('Contribution Status')),
208 'payment_instrument_id' => array('title' => ts('Payment Type')),
211 'no_display' => TRUE,
214 'receive_date' => NULL,
215 'receipt_date' => NULL,
216 'fee_amount' => NULL,
217 'net_amount' => NULL,
218 'total_amount' => array(
219 'title' => ts('Payment Amount (most recent)'),
220 'statistics' => array('sum' => ts('Amount')),
224 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
225 'financial_type_id' => array(
226 'title' => ts('Financial Type'),
227 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
228 'options' => CRM_Contribute_PseudoConstant
::financialType(),
229 'type' => CRM_Utils_Type
::T_INT
,
231 'payment_instrument_id' => array(
232 'title' => ts('Payment Type'),
233 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
234 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
235 'type' => CRM_Utils_Type
::T_INT
,
238 'title' => 'Currency',
239 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
240 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
242 'type' => CRM_Utils_Type
::T_STRING
,
244 'contribution_status_id' => array(
245 'title' => ts('Contribution Status'),
246 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
247 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
248 'type' => CRM_Utils_Type
::T_INT
,
250 'total_amount' => array('title' => ts('Contribution Amount')),
252 'grouping' => 'contri-fields',
255 $this->_groupFilter
= TRUE;
256 $this->_tagFilter
= TRUE;
258 // If we have active campaigns add those elements to both the fields and filters
259 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
260 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
261 'title' => ts('Campaign'),
262 'default' => 'false',
264 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array(
265 'title' => ts('Campaign'),
266 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
267 'options' => $this->activeCampaigns
,
269 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
273 $this->_currencyColumn
= 'civicrm_contribution_currency';
274 parent
::__construct();
277 public function preProcess() {
278 $this->assign('reportTitle', ts('Membership Detail Report'));
279 parent
::preProcess();
282 public function select() {
283 $select = $this->_columnHeaders
= array();
285 foreach ($this->_columns
as $tableName => $table) {
286 if (array_key_exists('fields', $table)) {
287 foreach ($table['fields'] as $fieldName => $field) {
288 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
289 if ($tableName == 'civicrm_address') {
290 $this->_addressField
= TRUE;
292 elseif ($tableName == 'civicrm_email') {
293 $this->_emailField
= TRUE;
295 elseif ($tableName == 'civicrm_phone') {
296 $this->_phoneField
= TRUE;
298 elseif ($tableName == 'civicrm_contribution') {
299 $this->_contribField
= TRUE;
301 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
302 if (array_key_exists('title', $field)) {
303 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
305 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
311 $this->_select
= "SELECT " . implode(', ', $select) . " ";
314 public function from() {
316 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
317 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
318 ON {$this->_aliases['civicrm_contact']}.id =
319 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
320 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
321 ON {$this->_aliases['civicrm_membership_status']}.id =
322 {$this->_aliases['civicrm_membership']}.status_id ";
324 //used when address field is selected
325 if ($this->_addressField
) {
327 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
328 ON {$this->_aliases['civicrm_contact']}.id =
329 {$this->_aliases['civicrm_address']}.contact_id AND
330 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
332 //used when email field is selected
333 if ($this->_emailField
) {
335 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
336 ON {$this->_aliases['civicrm_contact']}.id =
337 {$this->_aliases['civicrm_email']}.contact_id AND
338 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
340 //used when phone field is selected
341 if ($this->_phoneField
) {
343 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
344 ON {$this->_aliases['civicrm_contact']}.id =
345 {$this->_aliases['civicrm_phone']}.contact_id AND
346 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
348 //used when contribution field is selected
349 if ($this->_contribField
) {
351 LEFT JOIN civicrm_membership_payment cmp
352 ON {$this->_aliases['civicrm_membership']}.id = cmp.membership_id
353 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
354 ON cmp.contribution_id={$this->_aliases['civicrm_contribution']}.id\n";
358 public function groupBy() {
359 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
362 public function orderBy() {
363 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
365 if ($this->_contribField
) {
366 $this->_orderBy
.= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
370 public function postProcess() {
372 $this->beginPostProcess();
374 // get the acl clauses built before we assemble the query
375 $this->buildACLClause($this->_aliases
['civicrm_contact']);
376 $sql = $this->buildQuery(TRUE);
379 $this->buildRows($sql, $rows);
381 $this->formatDisplay($rows);
382 $this->doTemplateAssignment($rows);
383 $this->endPostProcess($rows);
389 public function alterDisplay(&$rows) {
390 // custom code to alter rows
392 $checkList = array();
394 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
395 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
396 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
398 foreach ($rows as $rowNum => $row) {
400 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
401 // not repeat contact display names if it matches with the one
403 $repeatFound = FALSE;
404 foreach ($row as $colName => $colVal) {
405 if (!empty($checkList[$colName]) &&
406 is_array($checkList[$colName]) &&
407 in_array($colVal, $checkList[$colName])
409 $rows[$rowNum][$colName] = "";
412 if (in_array($colName, $this->_noRepeats
)) {
413 $checkList[$colName][] = $colVal;
418 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
419 if ($value = $row['civicrm_membership_membership_type_id']) {
420 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
425 if (array_key_exists('civicrm_address_state_province_id', $row)) {
426 if ($value = $row['civicrm_address_state_province_id']) {
427 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
432 if (array_key_exists('civicrm_address_country_id', $row)) {
433 if ($value = $row['civicrm_address_country_id']) {
434 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
439 if (array_key_exists('civicrm_contact_sort_name', $row) &&
440 $rows[$rowNum]['civicrm_contact_sort_name'] &&
441 array_key_exists('civicrm_contact_id', $row)
443 $url = CRM_Utils_System
::url("civicrm/contact/view",
444 'reset=1&cid=' . $row['civicrm_contact_id'],
447 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
448 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
452 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
453 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
456 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
457 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
460 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
461 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
465 // Convert campaign_id to campaign title
466 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
467 if ($value = $row['civicrm_membership_campaign_id']) {
468 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];