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