Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Report / Form / Contact / Summary.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Form_Contact_Summary extends CRM_Report_Form {
38
39 protected $_summary = NULL;
40
41 protected $_emailField = FALSE;
42
43 protected $_phoneField = FALSE;
44
45 protected $_customGroupExtends = array(
46 'Contact', 'Individual', 'Household', 'Organization');
47
48 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
49
50 function __construct() {
51 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
52 $this->_columns = array(
53 'civicrm_contact' =>
54 array(
55 'dao' => 'CRM_Contact_DAO_Contact',
56 'fields' =>
57 array(
58 'sort_name' =>
59 array('title' => ts('Contact Name'),
60 'required' => TRUE,
61 'no_repeat' => TRUE,
62 ),
63 'first_name' => array('title' => ts('First Name'),
64 ),
65 'last_name' => array('title' => ts('Last Name'),
66 ),
67 'id' =>
68 array(
69 'no_display' => TRUE,
70 'required' => TRUE,
71 ),
72 ),
73 'filters' =>
74 array(
75 'sort_name' =>
76 array('title' => ts('Contact Name')),
77 'source' =>
78 array('title' => ts('Contact Source'),
79 'type' => CRM_Utils_Type::T_STRING,
80 ),
81 'id' =>
82 array('title' => ts('Contact ID'),
83 'no_display' => TRUE,
84 ),
85 ),
86 'grouping' => 'contact-fields',
87 'order_bys' =>
88 array(
89 'sort_name' =>
90 array(
91 'title' => ts('Last Name, First Name'), 'default' => '1', 'default_weight' => '0', 'default_order' => 'ASC',
92 ),
93 ),
94 ),
95 'civicrm_email' =>
96 array(
97 'dao' => 'CRM_Core_DAO_Email',
98 'fields' =>
99 array(
100 'email' =>
101 array('title' => ts('Email'),
102 'no_repeat' => TRUE,
103 ),
104 ),
105 'grouping' => 'contact-fields',
106 'order_bys' =>
107 array(
108 'email' =>
109 array('title' => ts('Email'),
110 ),
111 ),
112 ),
113 'civicrm_address' =>
114 array(
115 'dao' => 'CRM_Core_DAO_Address',
116 'grouping' => 'contact-fields',
117 'fields' =>
118 array(
119 'street_address' =>
120 array('default' => TRUE),
121 'city' =>
122 array('default' => TRUE),
123 'postal_code' => NULL,
124 'state_province_id' =>
125 array('title' => ts('State/Province'),
126 ),
127 ),
128 'filters' =>
129 array(
130 'country_id' =>
131 array('title' => ts('Country'),
132 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
133 'options' => CRM_Core_PseudoConstant::country(),
134 ),
135 'state_province_id' =>
136 array('title' => ts('State / Province'),
137 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
138 'options' => CRM_Core_PseudoConstant::stateProvince(),
139 ),
140 ),
141 'order_bys' =>
142 array('state_province_id' => array('title' => 'State/Province'),
143 'city' => array('title' => 'City'),
144 'postal_code' => array('title' => 'Postal Code'),
145 ),
146 ),
147 'civicrm_country' =>
148 array(
149 'dao' => 'CRM_Core_DAO_Country',
150 'fields' =>
151 array(
152 'name' =>
153 array('title' => 'Country', 'default' => TRUE),
154 ),
155 'order_bys' =>
156 array(
157 'name' =>
158 array('title' => 'Country'),
159 ),
160 'grouping' => 'contact-fields',
161 ),
162 'civicrm_phone' =>
163 array(
164 'dao' => 'CRM_Core_DAO_Phone',
165 'fields' =>
166 array('phone' => NULL),
167 'grouping' => 'contact-fields',
168 ),
169 'civicrm_group' =>
170 array(
171 'dao' => 'CRM_Contact_DAO_Group',
172 'alias' => 'cgroup',
173 'filters' =>
174 array(
175 'gid' =>
176 array(
177 'name' => 'group_id',
178 'title' => ts('Group'),
179 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
180 'group' => TRUE,
181 'options' => CRM_Core_PseudoConstant::group(),
182 ),
183 ),
184 ),
185 );
186
187 $this->_tagFilter = TRUE;
188 parent::__construct();
189 }
190
191 function preProcess() {
192 parent::preProcess();
193 }
194
195 function select() {
196 $select = array();
197 $this->_columnHeaders = array();
198 foreach ($this->_columns as $tableName => $table) {
199 if (array_key_exists('fields', $table)) {
200 foreach ($table['fields'] as $fieldName => $field) {
201 if (CRM_Utils_Array::value('required', $field) ||
202 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
203 ) {
204 if ($tableName == 'civicrm_email') {
205 $this->_emailField = TRUE;
206 }
207 elseif ($tableName == 'civicrm_phone') {
208 $this->_phoneField = TRUE;
209 }
210 elseif ($tableName == 'civicrm_country') {
211 $this->_countryField = TRUE;
212 }
213
214 $alias = "{$tableName}_{$fieldName}";
215 $select[] = "{$field['dbAlias']} as {$alias}";
216 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
217 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
218 $this->_selectAliases[] = $alias;
219 }
220 }
221 }
222 }
223
224 $this->_select = "SELECT " . implode(', ', $select) . " ";
225 }
226
227 static function formRule($fields, $files, $self) {
228 $errors = $grouping = array();
229 return $errors;
230 }
231
232 function from() {
233 $this->_from = "
234 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
235 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
236 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
237 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
238
239 if ($this->isTableSelected('civicrm_email')) {
240 $this->_from .= "
241 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
242 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
243 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
244 }
245
246 if ($this->_phoneField) {
247 $this->_from .= "
248 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
249 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
250 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
251 }
252
253 if ($this->isTableSelected('civicrm_country')) {
254 $this->_from .= "
255 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
256 ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
257 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
258 }
259 }
260
261 function postProcess() {
262
263 $this->beginPostProcess();
264
265 // get the acl clauses built before we assemble the query
266 $this->buildACLClause($this->_aliases['civicrm_contact']);
267
268 $sql = $this->buildQuery(TRUE);
269
270 $rows = $graphRows = array();
271 $this->buildRows($sql, $rows);
272
273 $this->formatDisplay($rows);
274 $this->doTemplateAssignment($rows);
275 $this->endPostProcess($rows);
276 }
277
278 function alterDisplay(&$rows) {
279 // custom code to alter rows
280 $entryFound = FALSE;
281 foreach ($rows as $rowNum => $row) {
282 // make count columns point to detail report
283 // convert sort name to links
284 if (array_key_exists('civicrm_contact_sort_name', $row) &&
285 array_key_exists('civicrm_contact_id', $row)
286 ) {
287 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
288 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
289 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
290 );
291 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
292 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Constituent Detail Report for this contact.");
293 $entryFound = TRUE;
294 }
295
296 if (array_key_exists('civicrm_address_state_province_id', $row)) {
297 if ($value = $row['civicrm_address_state_province_id']) {
298 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
299 }
300 $entryFound = TRUE;
301 }
302
303
304 // skip looking further in rows, if first row itself doesn't
305 // have the column we need
306 if (!$entryFound) {
307 break;
308 }
309 }
310 }
311}
312