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_Membership_Summary
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 protected $_charts = array(
41 'barChart' => 'Bar Chart',
42 'pieChart' => 'Pie Chart',
49 public function __construct() {
50 // UI for selecting columns to appear in the report list
51 // array conatining the columns, group_bys and filters build and provided to Form
52 $this->_columns
= array(
53 'civicrm_contact' => array(
54 'dao' => 'CRM_Contact_DAO_Contact',
57 'title' => ts('Member Name'),
67 'id' => array('title' => ts('Contact ID')),
68 'display_name' => array(
69 'title' => ts('Contact Name'),
72 'grouping' => 'contact-fields',
74 'civicrm_membership_type' => array(
75 'dao' => 'CRM_Member_DAO_MembershipType',
76 'grouping' => 'member-fields',
80 'title' => ts('Membership Types'),
81 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
82 'options' => CRM_Member_PseudoConstant
::membershipType(),
86 'civicrm_membership' => array(
87 'dao' => 'CRM_Member_DAO_Membership',
88 'grouping' => 'member-fields',
90 'membership_type_id' => array(
91 'title' => 'Membership Type',
95 'start_date' => array(
96 'title' => ts('Current Cycle Start Date'),
99 'title' => ts('Current Cycle End Date'),
102 'group_bys' => array(
103 'membership_type_id' => array('title' => ts('Membership Type')),
106 'join_date' => array('type' => CRM_Utils_Type
::T_DATE
),
109 'civicrm_address' => array(
110 'dao' => 'CRM_Core_DAO_Address',
112 'street_address' => NULL,
114 'postal_code' => NULL,
115 'state_province_id' => array(
116 'title' => ts('State/Province'),
118 'country_id' => array(
119 'title' => ts('Country'),
123 'grouping' => 'contact-fields',
125 'civicrm_email' => array(
126 'dao' => 'CRM_Core_DAO_Email',
127 'fields' => array('email' => NULL),
128 'grouping' => 'contact-fields',
130 'civicrm_contribution' => array(
131 'dao' => 'CRM_Contribute_DAO_Contribution',
133 'total_amount' => array(
134 'title' => ts('Contribution Amount'),
139 parent
::__construct();
142 public function preProcess() {
143 $this->assign('reportTitle', ts('Membership Summary Report'));
144 parent
::preProcess();
150 public function setDefaultValues() {
151 return parent
::setDefaultValues();
154 public function select() {
156 $this->_columnHeaders
= array();
157 foreach ($this->_columns
as $tableName => $table) {
158 if (array_key_exists('fields', $table)) {
159 foreach ($table['fields'] as $fieldName => $field) {
160 if (!empty($field['required']) ||
161 !empty($this->_params
['fields'][$fieldName])
163 // to include optional columns address and email, only if checked
164 if ($tableName == 'civicrm_address') {
165 $this->_addressField
= TRUE;
166 $this->_emailField
= TRUE;
168 elseif ($tableName == 'civicrm_email') {
169 $this->_emailField
= TRUE;
171 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
172 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
173 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
178 $this->_select
= "SELECT " . implode(', ', $select) . " ";
188 public static function formRule($fields, $files, $self) {
189 $errors = $grouping = array();
190 //check for searching combination of dispaly columns and
196 public function from() {
200 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
201 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
202 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
203 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
204 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
205 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
206 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
208 // include address field if address column is to be included
209 if ($this->_addressField
) {
210 $this->_from
.= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
213 // include email field if email column is to be included
214 if ($this->_emailField
) {
215 $this->_from
.= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
219 public function where() {
221 foreach ($this->_columns
as $tableName => $table) {
222 if (array_key_exists('filters', $table)) {
223 foreach ($table['filters'] as $fieldName => $field) {
225 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
226 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
227 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
228 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
230 if ($relative ||
$from ||
$to) {
231 $clause = $this->dateClause($field['name'], $relative, $from, $to);
235 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
237 $clause = $this->whereClause($field,
239 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
240 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
241 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
246 if (!empty($clause)) {
247 $clauses[] = $clause;
253 if (empty($clauses)) {
254 $this->_where
= "WHERE ( 1 ) ";
257 $this->_where
= "WHERE " . implode(' AND ', $clauses);
266 public function statistics(&$rows) {
267 $statistics = array();
268 $statistics[] = array(
269 'title' => ts('Row(s) Listed'),
270 'value' => count($rows),
275 public function groupBy() {
276 $this->_groupBy
= "";
277 if (is_array($this->_params
['group_bys']) &&
278 !empty($this->_params
['group_bys'])
280 foreach ($this->_columns
as $tableName => $table) {
281 if (array_key_exists('group_bys', $table)) {
282 foreach ($table['group_bys'] as $fieldName => $field) {
283 if (!empty($this->_params
['group_bys'][$fieldName])) {
284 $this->_groupBy
[] = $field['dbAlias'];
290 if (!empty($this->_statFields
) &&
291 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
293 $this->_rollup
= " WITH ROLLUP";
295 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
296 " {$this->_rollup} ";
299 $this->_groupBy
= "GROUP BY contact.id";
303 public function postProcess() {
304 $this->_params
= $this->controller
->exportValues($this->_name
);
305 if (empty($this->_params
) &&
308 $this->_params
= $this->_formValues
;
310 $this->_formValues
= $this->_params
;
312 $this->processReportMode();
322 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
324 $dao = CRM_Core_DAO
::executeQuery($sql);
325 $rows = $graphRows = array();
327 while ($dao->fetch()) {
329 foreach ($this->_columnHeaders
as $key => $value) {
330 $row[$key] = $dao->$key;
333 if (!empty($this->_params
['charts']) &&
334 $row['civicrm_contribution_receive_date_subtotal']
336 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
337 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
338 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
344 $this->formatDisplay($rows);
346 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
347 $this->assign_by_ref('rows', $rows);
348 $this->assign('statistics', $this->statistics($rows));
350 if (!empty($this->_params
['charts'])) {
356 unset($graphRows[$ignore][$count - 1]);
360 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
362 parent
::endPostProcess();
368 public function alterDisplay(&$rows) {
369 // custom code to alter rows
371 $checkList = array();
373 foreach ($rows as $rowNum => $row) {
375 if (!empty($this->_noRepeats
)) {
376 // not repeat contact display names if it matches with the one
379 $repeatFound = FALSE;
380 foreach ($row as $colName => $colVal) {
381 if (is_array($checkList[$colName]) &&
382 in_array($colVal, $checkList[$colName])
384 $rows[$rowNum][$colName] = "";
387 if (in_array($colName, $this->_noRepeats
)) {
388 $checkList[$colName][] = $colVal;
393 //handle the Membership Type Ids
394 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
395 if ($value = $row['civicrm_membership_membership_type_id']) {
396 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
401 // handle state province
402 if (array_key_exists('civicrm_address_state_province_id', $row)) {
403 if ($value = $row['civicrm_address_state_province_id']) {
404 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
410 if (array_key_exists('civicrm_address_country_id', $row)) {
411 if ($value = $row['civicrm_address_country_id']) {
412 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
417 // convert display name to links
418 if (array_key_exists('civicrm_contact_sort_name', $row) &&
419 array_key_exists('civicrm_contact_id', $row)
421 $url = CRM_Utils_System
::url('civicrm/report/member/detail',
422 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
425 $rows[$rowNum]['civicrm_contact_sort_name'] =
426 "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
430 // skip looking further in rows, if first row itself doesn't
431 // have the column we need