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