762dc41419ca339c969d973280ae02ab6783c5be
[civicrm-core.git] / CRM / Report / Form / Member / Detail.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Member_Detail extends CRM_Report_Form {
36
37 protected $_addressField = FALSE;
38
39 protected $_emailField = FALSE;
40
41 protected $_phoneField = FALSE;
42
43 protected $_contribField = FALSE;
44
45 protected $_summary = NULL;
46
47 protected $_customGroupExtends = array('Membership', 'Contribution');
48 protected $_customGroupGroupBy = FALSE;
49
50 /**
51 */
52 /**
53 */
54 public function __construct() {
55
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);
63 }
64
65 $this->_columns = array(
66 'civicrm_contact' => array(
67 'dao' => 'CRM_Contact_DAO_Contact',
68 'fields' => array(
69 'sort_name' => array(
70 'title' => ts('Contact Name'),
71 'required' => TRUE,
72 'default' => TRUE,
73 ),
74 'id' => array(
75 'no_display' => TRUE,
76 'required' => TRUE,
77 ),
78 'first_name' => array(
79 'title' => ts('First Name'),
80 ),
81 'id' => array(
82 'no_display' => TRUE,
83 'required' => TRUE,
84 ),
85 'last_name' => array(
86 'title' => ts('Last Name'),
87 ),
88 'contact_type' => array(
89 'title' => ts('Contact Type'),
90 ),
91 'contact_sub_type' => array(
92 'title' => ts('Contact Subtype'),
93 ),
94 ),
95 'filters' => array(
96 'sort_name' => array(
97 'title' => ts('Contact Name'),
98 'operator' => 'like',
99 ),
100 'id' => array('no_display' => TRUE),
101 ),
102 'order_bys' => array(
103 'sort_name' => array(
104 'title' => ts('Last Name, First Name'),
105 'default' => '1',
106 'default_weight' => '0',
107 'default_order' => 'ASC',
108 ),
109 ),
110 'grouping' => 'contact-fields',
111 ),
112 'civicrm_membership' => array(
113 'dao' => 'CRM_Member_DAO_Membership',
114 'fields' => array(
115 'membership_type_id' => array(
116 'title' => 'Membership Type',
117 'required' => TRUE,
118 'no_repeat' => TRUE,
119 ),
120 'membership_start_date' => array(
121 'title' => ts('Start Date'),
122 'default' => TRUE,
123 ),
124 'membership_end_date' => array(
125 'title' => ts('End Date'),
126 'default' => TRUE,
127 ),
128 'join_date' => array(
129 'title' => ts('Join Date'),
130 'default' => TRUE,
131 ),
132 'source' => array('title' => 'Source'),
133 ),
134 'filters' => array(
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,
141 ),
142 'tid' => array(
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(),
148 ),
149 ),
150 'order_bys' => array(
151 'membership_type_id' => array(
152 'title' => ts('Membership Type'),
153 'default' => '0',
154 'default_weight' => '1',
155 'default_order' => 'ASC',
156 ),
157 ),
158 'grouping' => 'member-fields',
159 ),
160 'civicrm_membership_status' => array(
161 'dao' => 'CRM_Member_DAO_MembershipStatus',
162 'alias' => 'mem_status',
163 'fields' => array(
164 'name' => array(
165 'title' => ts('Status'),
166 'default' => TRUE,
167 ),
168 ),
169 'filters' => array(
170 'sid' => array(
171 'name' => 'id',
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'),
176 ),
177 ),
178 'grouping' => 'member-fields',
179 ),
180 'civicrm_address' => array(
181 'dao' => 'CRM_Core_DAO_Address',
182 'fields' => array(
183 'street_address' => NULL,
184 'city' => NULL,
185 'postal_code' => NULL,
186 'state_province_id' => array(
187 'title' => ts('State/Province'),
188 ),
189 'country_id' => array(
190 'title' => ts('Country'),
191 ),
192 ),
193 'grouping' => 'contact-fields',
194 ),
195 'civicrm_email' => array(
196 'dao' => 'CRM_Core_DAO_Email',
197 'fields' => array('email' => NULL),
198 'grouping' => 'contact-fields',
199 ),
200 'civicrm_phone' => array(
201 'dao' => 'CRM_Core_DAO_Phone',
202 'fields' => array('phone' => NULL),
203 'grouping' => 'contact-fields',
204 ),
205 'civicrm_contribution' => array(
206 'dao' => 'CRM_Contribute_DAO_Contribution',
207 'fields' => array(
208 'contribution_id' => array(
209 'name' => 'id',
210 'no_display' => TRUE,
211 'required' => TRUE,
212 ),
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')),
216 'currency' => array(
217 'required' => TRUE,
218 'no_display' => TRUE,
219 ),
220 'trxn_id' => NULL,
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')),
228 ),
229 ),
230 'filters' => array(
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,
237 ),
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,
243 ),
244 'currency' => array(
245 'title' => 'Currency',
246 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
247 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
248 'default' => NULL,
249 'type' => CRM_Utils_Type::T_STRING,
250 ),
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,
256 ),
257 'total_amount' => array('title' => ts('Contribution Amount')),
258 ),
259 'order_bys' => array(
260 'receive_date' => array(
261 'title' => ts('Receive Date'),
262 'default_weight' => '2',
263 'default_order' => 'DESC',
264 ),
265 ),
266 'grouping' => 'contri-fields',
267 ),
268 );
269 $this->_groupFilter = TRUE;
270 $this->_tagFilter = TRUE;
271
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',
277 );
278 $this->_columns['civicrm_membership']['filters']['campaign_id'] = array(
279 'title' => ts('Campaign'),
280 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
281 'options' => $this->activeCampaigns,
282 );
283 $this->_columns['civicrm_membership']['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
284
285 }
286
287 $this->_currencyColumn = 'civicrm_contribution_currency';
288 parent::__construct();
289 }
290
291 public function preProcess() {
292 $this->assign('reportTitle', ts('Membership Detail Report'));
293 parent::preProcess();
294 }
295
296 public function select() {
297 $select = $this->_columnHeaders = array();
298
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;
305 }
306 elseif ($tableName == 'civicrm_email') {
307 $this->_emailField = TRUE;
308 }
309 elseif ($tableName == 'civicrm_phone') {
310 $this->_phoneField = TRUE;
311 }
312 elseif ($tableName == 'civicrm_contribution') {
313 $this->_contribField = TRUE;
314 }
315 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
316 if (array_key_exists('title', $field)) {
317 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
318 }
319 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
320 }
321 }
322 }
323 }
324
325 $this->_select = "SELECT " . implode(', ', $select) . " ";
326 }
327
328 public function from() {
329 $this->_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 ";
337
338 //used when address field is selected
339 if ($this->_addressField) {
340 $this->_from .= "
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";
345 }
346 //used when email field is selected
347 if ($this->_emailField) {
348 $this->_from .= "
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";
353 }
354 //used when phone field is selected
355 if ($this->_phoneField) {
356 $this->_from .= "
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";
361 }
362 //used when contribution field is selected
363 if ($this->_contribField) {
364 $this->_from .= "
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";
369 }
370 }
371
372 public function groupBy() {
373 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_membership']}.membership_type_id";
374 }
375
376 public function postProcess() {
377
378 $this->beginPostProcess();
379
380 // get the acl clauses built before we assemble the query
381 $this->buildACLClause($this->_aliases['civicrm_contact']);
382 $sql = $this->buildQuery(TRUE);
383
384 $rows = array();
385 $this->buildRows($sql, $rows);
386
387 $this->formatDisplay($rows);
388 $this->doTemplateAssignment($rows);
389 $this->endPostProcess($rows);
390 }
391
392 /**
393 * Alter display of rows.
394 *
395 * Iterate through the rows retrieved via SQL and make changes for display purposes,
396 * such as rendering contacts as links.
397 *
398 * @param array $rows
399 * Rows generated by SQL, with an array for each row.
400 */
401 public function alterDisplay(&$rows) {
402 $entryFound = FALSE;
403 $checkList = array();
404
405 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
406 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
407 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
408
409 $repeatFound = FALSE;
410 foreach ($rows as $rowNum => $row) {
411 if ($repeatFound == FALSE ||
412 $repeatFound < $rowNum - 1
413 ) {
414 unset($checkList);
415 $checkList = array();
416 }
417 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
418 // not repeat contact display names if it matches with the one
419 // in previous row
420 foreach ($row as $colName => $colVal) {
421 if (in_array($colName, $this->_noRepeats) &&
422 $rowNum > 0
423 ) {
424 if ($rows[$rowNum][$colName] == $rows[$rowNum - 1][$colName] ||
425 (!empty($checkList[$colName]) &&
426 in_array($colVal, $checkList[$colName]))
427 ) {
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'] = "";
432 }
433 $repeatFound = $rowNum;
434 }
435 }
436 if (in_array($colName, $this->_noRepeats)) {
437 $checkList[$colName][] = $colVal;
438 }
439 }
440 }
441
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);
445 }
446 $entryFound = TRUE;
447 }
448
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);
452 }
453 $entryFound = TRUE;
454 }
455
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);
459 }
460 $entryFound = TRUE;
461 }
462
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)
466 ) {
467 $url = CRM_Utils_System::url("civicrm/contact/view",
468 'reset=1&cid=' . $row['civicrm_contact_id'],
469 $this->_absoluteUrl
470 );
471 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
473 $entryFound = TRUE;
474 }
475
476 if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
477 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
478 $entryFound = TRUE;
479 }
480 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
481 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
482 $entryFound = TRUE;
483 }
484 if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) {
485 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
486 $entryFound = TRUE;
487 }
488
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];
493 $entryFound = TRUE;
494 }
495 }
496
497 if (!$entryFound) {
498 break;
499 }
500 }
501 }
502
503 }