4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
174 // to include optional columns address and email, only if checked
175 if ($tableName == 'civicrm_address') {
176 $this->_addressField
= TRUE;
177 $this->_emailField
= TRUE;
179 elseif ($tableName == 'civicrm_email') {
180 $this->_emailField
= TRUE;
182 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
183 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
184 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
189 $this->_select
= "SELECT " . implode(', ', $select) . " ";
192 static function formRule($fields, $files, $self) {
193 $errors = $grouping = array();
194 //check for searching combination of dispaly columns and
204 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
205 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
206 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
207 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
208 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
209 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
210 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
212 // include address field if address column is to be included
213 if ($this->_addressField
) {
214 $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";
217 // include email field if email column is to be included
218 if ($this->_emailField
) {
219 $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";
225 foreach ($this->_columns
as $tableName => $table) {
226 if (array_key_exists('filters', $table)) {
227 foreach ($table['filters'] as $fieldName => $field) {
229 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
230 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
231 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
232 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
234 if ($relative ||
$from ||
$to) {
235 $clause = $this->dateClause($field['name'], $relative, $from, $to);
239 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
241 $clause = $this->whereClause($field,
243 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
244 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
245 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
250 if (!empty($clause)) {
251 $clauses[] = $clause;
257 if (empty($clauses)) {
258 $this->_where
= "WHERE ( 1 ) ";
261 $this->_where
= "WHERE " . implode(' AND ', $clauses);
265 function statistics(&$rows) {
266 $statistics = array();
267 $statistics[] = array('title' => ts('Row(s) Listed'),
268 'value' => count($rows),
274 $this->_groupBy
= "";
275 if (is_array($this->_params
['group_bys']) &&
276 !empty($this->_params
['group_bys'])
278 foreach ($this->_columns
as $tableName => $table) {
279 if (array_key_exists('group_bys', $table)) {
280 foreach ($table['group_bys'] as $fieldName => $field) {
281 if (!empty($this->_params
['group_bys'][$fieldName])) {
282 $this->_groupBy
[] = $field['dbAlias'];
288 if (!empty($this->_statFields
) &&
289 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
291 $this->_rollup
= " WITH ROLLUP";
293 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) . " {$this->_rollup} ";
296 $this->_groupBy
= "GROUP BY contact.id";
300 function postProcess() {
301 $this->_params
= $this->controller
->exportValues($this->_name
);
302 if (empty($this->_params
) &&
305 $this->_params
= $this->_formValues
;
307 $this->_formValues
= $this->_params
;
309 $this->processReportMode();
319 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
321 $dao = CRM_Core_DAO
::executeQuery($sql);
322 $rows = $graphRows = array();
324 while ($dao->fetch()) {
326 foreach ($this->_columnHeaders
as $key => $value) {
327 $row[$key] = $dao->$key;
330 if (!empty($this->_params
['charts']) &&
331 $row['civicrm_contribution_receive_date_subtotal']
333 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
334 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
335 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
341 $this->formatDisplay($rows);
343 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
344 $this->assign_by_ref('rows', $rows);
345 $this->assign('statistics', $this->statistics($rows));
347 if (!empty($this->_params
['charts'])) {
349 'receive_date', $this->_interval
, 'value') as $ignore) {
350 unset($graphRows[$ignore][$count - 1]);
354 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
356 parent
::endPostProcess();
359 function alterDisplay(&$rows) {
360 // custom code to alter rows
362 $checkList = array();
364 foreach ($rows as $rowNum => $row) {
366 if (!empty($this->_noRepeats
)) {
367 // not repeat contact display names if it matches with the one
370 $repeatFound = FALSE;
371 foreach ($row as $colName => $colVal) {
372 if (is_array($checkList[$colName]) &&
373 in_array($colVal, $checkList[$colName])
375 $rows[$rowNum][$colName] = "";
378 if (in_array($colName, $this->_noRepeats
)) {
379 $checkList[$colName][] = $colVal;
384 //handle the Membership Type Ids
385 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
386 if ($value = $row['civicrm_membership_membership_type_id']) {
387 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
392 // handle state province
393 if (array_key_exists('civicrm_address_state_province_id', $row)) {
394 if ($value = $row['civicrm_address_state_province_id']) {
395 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
401 if (array_key_exists('civicrm_address_country_id', $row)) {
402 if ($value = $row['civicrm_address_country_id']) {
403 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
408 // convert display name to links
409 if (array_key_exists('civicrm_contact_sort_name', $row) &&
410 array_key_exists('civicrm_contact_id', $row)
412 $url = CRM_Utils_System
::url('civicrm/report/member/detail',
413 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
416 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
420 // skip looking further in rows, if first row itself doesn't
421 // have the column we need