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(
68 'civicrm_contact' => array(
69 'dao' => 'CRM_Contact_DAO_Contact',
72 'title' => ts('Contact Name'),
81 'first_name' => array(
82 'title' => ts('First Name'),
89 'title' => ts('Last Name'),
95 'contact_type' => array(
96 'title' => ts('Contact Type'),
98 'contact_sub_type' => array(
99 'title' => ts('Contact SubType'),
103 'sort_name' => array(
104 'title' => ts('Contact Name'),
105 'operator' => 'like',
107 'id' => array('no_display' => TRUE),
109 'order_bys' => array(
110 'sort_name' => array(
111 'title' => ts('Last Name, First Name'),
113 'default_weight' => '0',
114 'default_order' => 'ASC'
117 'grouping' => 'contact-fields',
119 'civicrm_membership' => array(
120 'dao' => 'CRM_Member_DAO_Membership',
122 'membership_type_id' => array(
123 'title' => 'Membership Type',
127 'membership_start_date' => array(
128 'title' => ts('Start Date'),
131 'membership_end_date' => array(
132 'title' => ts('End Date'),
135 'join_date' => array(
136 'title' => ts('Join Date'),
139 'source' => array('title' => 'Source'),
142 'join_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
143 'membership_start_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
144 'membership_end_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
145 'owner_membership_id' => array(
146 'title' => ts('Membership Owner ID'),
147 'operatorType' => CRM_Report_Form
::OP_INT
,
150 'name' => 'membership_type_id',
151 'title' => ts('Membership Types'),
152 'type' => CRM_Utils_Type
::T_INT
,
153 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
154 'options' => CRM_Member_PseudoConstant
::membershipType(),
157 'grouping' => 'member-fields',
159 'civicrm_membership_status' => array(
160 'dao' => 'CRM_Member_DAO_MembershipStatus',
161 'alias' => 'mem_status',
164 'title' => ts('Status'),
171 'title' => ts('Status'),
172 'type' => CRM_Utils_Type
::T_INT
,
173 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
174 'options' => CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label'),
177 'grouping' => 'member-fields',
179 'civicrm_address' => array(
180 'dao' => 'CRM_Core_DAO_Address',
182 'street_address' => NULL,
184 'postal_code' => NULL,
185 'state_province_id' => array(
186 'title' => ts('State/Province'),
188 'country_id' => array(
189 'title' => ts('Country'),
192 'grouping' => 'contact-fields',
194 'civicrm_email' => array(
195 'dao' => 'CRM_Core_DAO_Email',
196 'fields' => array('email' => NULL),
197 'grouping' => 'contact-fields',
199 'civicrm_phone' => array(
200 'dao' => 'CRM_Core_DAO_Phone',
201 'fields' => array('phone' => NULL),
202 'grouping' => 'contact-fields',
204 'civicrm_contribution' => array(
205 'dao' => 'CRM_Contribute_DAO_Contribution',
207 'contribution_id' => array(
209 'no_display' => TRUE,
212 'financial_type_id' => array('title' => ts('Financial Type')),
213 'contribution_status_id' => array('title' => ts('Contribution Status')),
214 'payment_instrument_id' => array('title' => ts('Payment Type')),
217 'no_display' => TRUE,
220 'receive_date' => NULL,
221 'receipt_date' => NULL,
222 'fee_amount' => NULL,
223 'net_amount' => NULL,
224 'total_amount' => array(
225 'title' => ts('Payment Amount (most recent)'),
226 'statistics' => array('sum' => ts('Amount')),
230 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
231 'financial_type_id' => array(
232 'title' => ts('Financial Type'),
233 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
234 'options' => CRM_Contribute_PseudoConstant
::financialType(),
235 'type' => CRM_Utils_Type
::T_INT
,
237 'payment_instrument_id' => array(
238 'title' => ts('Payment Type'),
239 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
240 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
241 'type' => CRM_Utils_Type
::T_INT
,
244 'title' => 'Currency',
245 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
246 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
248 'type' => CRM_Utils_Type
::T_STRING
,
250 'contribution_status_id' => array(
251 'title' => ts('Contribution Status'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
254 'type' => CRM_Utils_Type
::T_INT
,
256 'total_amount' => array('title' => ts('Contribution Amount')),
258 'grouping' => 'contri-fields',
261 $this->_groupFilter
= TRUE;
262 $this->_tagFilter
= TRUE;
264 // If we have active campaigns add those elements to both the fields and filters
265 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
266 $this->_columns
['civicrm_membership']['fields']['campaign_id'] = array(
267 'title' => ts('Campaign'),
268 'default' => 'false',
270 $this->_columns
['civicrm_membership']['filters']['campaign_id'] = array(
271 'title' => ts('Campaign'),
272 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
273 'options' => $this->activeCampaigns
,
275 $this->_columns
['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
279 $this->_currencyColumn
= 'civicrm_contribution_currency';
280 parent
::__construct();
283 function preProcess() {
284 $this->assign('reportTitle', ts('Membership Detail Report'));
285 parent
::preProcess();
289 $select = $this->_columnHeaders
= array();
291 foreach ($this->_columns
as $tableName => $table) {
292 if (array_key_exists('fields', $table)) {
293 foreach ($table['fields'] as $fieldName => $field) {
294 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
295 if ($tableName == 'civicrm_address') {
296 $this->_addressField
= TRUE;
298 elseif ($tableName == 'civicrm_email') {
299 $this->_emailField
= TRUE;
301 elseif ($tableName == 'civicrm_phone') {
302 $this->_phoneField
= TRUE;
304 elseif ($tableName == 'civicrm_contribution') {
305 $this->_contribField
= TRUE;
307 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
308 if (array_key_exists('title', $field)) {
309 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
311 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
317 $this->_select
= "SELECT " . implode(', ', $select) . " ";
322 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
323 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
324 ON {$this->_aliases['civicrm_contact']}.id =
325 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
326 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
327 ON {$this->_aliases['civicrm_membership_status']}.id =
328 {$this->_aliases['civicrm_membership']}.status_id ";
331 //used when address field is selected
332 if ($this->_addressField
) {
334 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
335 ON {$this->_aliases['civicrm_contact']}.id =
336 {$this->_aliases['civicrm_address']}.contact_id AND
337 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
339 //used when email field is selected
340 if ($this->_emailField
) {
342 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
343 ON {$this->_aliases['civicrm_contact']}.id =
344 {$this->_aliases['civicrm_email']}.contact_id AND
345 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
347 //used when phone field is selected
348 if ($this->_phoneField
) {
350 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
351 ON {$this->_aliases['civicrm_contact']}.id =
352 {$this->_aliases['civicrm_phone']}.contact_id AND
353 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
355 //used when contribution field is selected
356 if ($this->_contribField
) {
358 LEFT JOIN civicrm_membership_payment cmp
359 ON {$this->_aliases['civicrm_membership']}.id = cmp.membership_id
360 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
361 ON cmp.contribution_id={$this->_aliases['civicrm_contribution']}.id\n";
366 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
370 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
372 if ($this->_contribField
) {
373 $this->_orderBy
.= ", {$this->_aliases['civicrm_contribution']}.receive_date DESC";
377 function postProcess() {
379 $this->beginPostProcess();
381 // get the acl clauses built before we assemble the query
382 $this->buildACLClause($this->_aliases
['civicrm_contact']);
383 $sql = $this->buildQuery(TRUE);
386 $this->buildRows($sql, $rows);
388 $this->formatDisplay($rows);
389 $this->doTemplateAssignment($rows);
390 $this->endPostProcess($rows);
396 function alterDisplay(&$rows) {
397 // custom code to alter rows
399 $checkList = array();
401 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
402 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
403 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
405 foreach ($rows as $rowNum => $row) {
407 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
408 // not repeat contact display names if it matches with the one
410 $repeatFound = FALSE;
411 foreach ($row as $colName => $colVal) {
412 if (!empty($checkList[$colName]) &&
413 is_array($checkList[$colName]) &&
414 in_array($colVal, $checkList[$colName])
416 $rows[$rowNum][$colName] = "";
419 if (in_array($colName, $this->_noRepeats
)) {
420 $checkList[$colName][] = $colVal;
425 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
426 if ($value = $row['civicrm_membership_membership_type_id']) {
427 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
432 if (array_key_exists('civicrm_address_state_province_id', $row)) {
433 if ($value = $row['civicrm_address_state_province_id']) {
434 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
439 if (array_key_exists('civicrm_address_country_id', $row)) {
440 if ($value = $row['civicrm_address_country_id']) {
441 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
446 if (array_key_exists('civicrm_contact_sort_name', $row) &&
447 $rows[$rowNum]['civicrm_contact_sort_name'] &&
448 array_key_exists('civicrm_contact_id', $row)
450 $url = CRM_Utils_System
::url("civicrm/contact/view",
451 'reset=1&cid=' . $row['civicrm_contact_id'],
454 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
455 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
459 if ($value = CRM_Utils_Array
::value('civicrm_contribution_financial_type_id', $row)) {
460 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
463 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
464 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
467 if ($value = CRM_Utils_Array
::value('civicrm_contribution_payment_instrument_id', $row)) {
468 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
472 // Convert campaign_id to campaign title
473 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
474 if ($value = $row['civicrm_membership_campaign_id']) {
475 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns
[$value];