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