4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_Membership_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 protected $_charts = array(
42 'barChart' => 'Bar Chart',
43 'pieChart' => 'Pie Chart',
46 function __construct() {
47 // UI for selecting columns to appear in the report list
48 // array conatining the columns, group_bys and filters build and provided to Form
49 $this->_columns
= array(
52 'dao' => 'CRM_Contact_DAO_Contact',
56 array('title' => ts('Member Name'),
69 array('title' => ts('Contact ID')),
71 array('title' => ts('Contact Name'),
74 'grouping' => 'contact-fields',
76 'civicrm_membership_type' =>
78 'dao' => 'CRM_Member_DAO_MembershipType',
79 'grouping' => 'member-fields',
85 'title' => ts('Membership Types'),
86 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
87 'options' => CRM_Member_PseudoConstant
::membershipType(),
91 'civicrm_membership' =>
93 'dao' => 'CRM_Member_DAO_Membership',
94 'grouping' => 'member-fields',
97 'membership_type_id' =>
99 'title' => 'Membership Type',
103 'start_date' => array('title' => ts('Current Cycle Start Date'),
105 'end_date' => array('title' => ts('Current Cycle End Date'),
110 'membership_type_id' =>
111 array('title' => ts('Membership Type')),
116 array('type' => CRM_Utils_Type
::T_DATE
),
121 'dao' => 'CRM_Core_DAO_Address',
124 'street_address' => NULL,
126 'postal_code' => NULL,
127 'state_province_id' =>
128 array('title' => ts('State/Province'),
131 array('title' => ts('Country'),
135 'grouping' => 'contact-fields',
139 'dao' => 'CRM_Core_DAO_Email',
141 array('email' => NULL),
142 'grouping' => 'contact-fields',
144 'civicrm_contribution' =>
146 'dao' => 'CRM_Contribute_DAO_Contribution',
150 array('title' => ts('Contribution Amount'),
155 parent
::__construct();
158 function preProcess() {
159 $this->assign('reportTitle', ts('Membership Summary Report'));
160 parent
::preProcess();
163 function setDefaultValues() {
164 return parent
::setDefaultValues();
169 $this->_columnHeaders
= array();
170 foreach ($this->_columns
as $tableName => $table) {
171 if (array_key_exists('fields', $table)) {
172 foreach ($table['fields'] as $fieldName => $field) {
173 if (CRM_Utils_Array
::value('required', $field) ||
174 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
176 // to include optional columns address and email, only if checked
177 if ($tableName == 'civicrm_address') {
178 $this->_addressField
= TRUE;
179 $this->_emailField
= TRUE;
181 elseif ($tableName == 'civicrm_email') {
182 $this->_emailField
= TRUE;
184 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
185 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
186 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
191 $this->_select
= "SELECT " . implode(', ', $select) . " ";
194 static function formRule($fields, $files, $self) {
195 $errors = $grouping = array();
196 //check for searching combination of dispaly columns and
206 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
207 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
208 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
209 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
210 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
211 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
212 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
214 // include address field if address column is to be included
215 if ($this->_addressField
) {
216 $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";
219 // include email field if email column is to be included
220 if ($this->_emailField
) {
221 $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";
227 foreach ($this->_columns
as $tableName => $table) {
228 if (array_key_exists('filters', $table)) {
229 foreach ($table['filters'] as $fieldName => $field) {
231 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
232 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
233 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
234 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
236 if ($relative ||
$from ||
$to) {
237 $clause = $this->dateClause($field['name'], $relative, $from, $to);
241 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
243 $clause = $this->whereClause($field,
245 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
246 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
247 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
252 if (!empty($clause)) {
253 $clauses[] = $clause;
259 if (empty($clauses)) {
260 $this->_where
= "WHERE ( 1 ) ";
263 $this->_where
= "WHERE " . implode(' AND ', $clauses);
267 function statistics(&$rows) {
268 $statistics = array();
269 $statistics[] = array('title' => ts('Row(s) Listed'),
270 'value' => count($rows),
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 (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
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
) . " {$this->_rollup} ";
298 $this->_groupBy
= "GROUP BY contact.id";
302 function postProcess() {
303 $this->_params
= $this->controller
->exportValues($this->_name
);
304 if (empty($this->_params
) &&
307 $this->_params
= $this->_formValues
;
309 $this->_formValues
= $this->_params
;
311 $this->processReportMode();
321 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
323 $dao = CRM_Core_DAO
::executeQuery($sql);
324 $rows = $graphRows = array();
326 while ($dao->fetch()) {
328 foreach ($this->_columnHeaders
as $key => $value) {
329 $row[$key] = $dao->$key;
332 if (CRM_Utils_Array
::value('charts', $this->_params
) &&
333 $row['civicrm_contribution_receive_date_subtotal']
335 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
336 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
337 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
343 $this->formatDisplay($rows);
345 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
346 $this->assign_by_ref('rows', $rows);
347 $this->assign('statistics', $this->statistics($rows));
349 if (CRM_Utils_Array
::value('charts', $this->_params
)) {
351 'receive_date', $this->_interval
, 'value') as $ignore) {
352 unset($graphRows[$ignore][$count - 1]);
356 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
358 parent
::endPostProcess();
361 function alterDisplay(&$rows) {
362 // custom code to alter rows
364 $checkList = array();
366 foreach ($rows as $rowNum => $row) {
368 if (!empty($this->_noRepeats
)) {
369 // not repeat contact display names if it matches with the one
372 $repeatFound = FALSE;
373 foreach ($row as $colName => $colVal) {
374 if (is_array($checkList[$colName]) &&
375 in_array($colVal, $checkList[$colName])
377 $rows[$rowNum][$colName] = "";
380 if (in_array($colName, $this->_noRepeats
)) {
381 $checkList[$colName][] = $colVal;
386 //handle the Membership Type Ids
387 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
388 if ($value = $row['civicrm_membership_membership_type_id']) {
389 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
394 // handle state province
395 if (array_key_exists('civicrm_address_state_province_id', $row)) {
396 if ($value = $row['civicrm_address_state_province_id']) {
397 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
403 if (array_key_exists('civicrm_address_country_id', $row)) {
404 if ($value = $row['civicrm_address_country_id']) {
405 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
410 // convert display name to links
411 if (array_key_exists('civicrm_contact_sort_name', $row) &&
412 array_key_exists('civicrm_contact_id', $row)
414 $url = CRM_Utils_System
::url('civicrm/report/member/detail',
415 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
418 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
422 // skip looking further in rows, if first row itself doesn't
423 // have the column we need