Merge pull request #7047 from johanv/CRM-17430-dont_change_domain_version_1st_attempt
[civicrm-core.git] / CRM / Report / Form / Contact / CurrentEmployer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
31 * @copyright CiviCRM LLC (c) 2004-2016
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Contact_CurrentEmployer extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38
39 protected $_customGroupExtends = array(
40 'Contact',
41 'Individual',
42 );
43
44 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
45
46 /**
47 */
48 /**
49 */
50 public function __construct() {
51
52 $this->_columns = array(
53 'civicrm_employer' => array(
54 'dao' => 'CRM_Contact_DAO_Contact',
55 'fields' => array(
56 'organization_name' => array(
57 'title' => ts('Employer Name'),
58 'required' => TRUE,
59 'no_repeat' => TRUE,
60 ),
61 'id' => array(
62 'no_display' => TRUE,
63 'required' => TRUE,
64 ),
65 ),
66 'filters' => array(
67 'organization_name' => array(
68 'title' => ts('Employer Name'),
69 'operatorType' => CRM_Report_Form::OP_STRING,
70 ),
71 ),
72 ),
73 'civicrm_contact' => array(
74 'dao' => 'CRM_Contact_DAO_Contact',
75 'fields' => array(
76 'sort_name' => array(
77 'title' => ts('Employee Name'),
78 'required' => TRUE,
79 ),
80 'first_name' => array(
81 'title' => ts('First Name'),
82 ),
83 'middle_name' => array(
84 'title' => ts('Middle Name'),
85 ),
86 'last_name' => array(
87 'title' => ts('Last Name'),
88 ),
89 'id' => array(
90 'no_display' => TRUE,
91 'required' => TRUE,
92 ),
93 'job_title' => array(
94 'title' => ts('Job Title'),
95 'default' => TRUE,
96 ),
97 'gender_id' => array(
98 'title' => ts('Gender'),
99 ),
100 'birth_date' => array(
101 'title' => ts('Birth Date'),
102 ),
103 'age' => array(
104 'title' => ts('Age'),
105 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
106 ),
107 'contact_type' => array(
108 'title' => ts('Contact Type'),
109 ),
110 'contact_sub_type' => array(
111 'title' => ts('Contact Subtype'),
112 ),
113 ),
114 'filters' => array(
115 'sort_name' => array('title' => ts('Employee Name')),
116 'source' => array(
117 'title' => ts('Contact Source'),
118 'type' => CRM_Utils_Type::T_STRING,
119 ),
120 'id' => array(
121 'title' => ts('Contact ID'),
122 'no_display' => TRUE,
123 ),
124 'gender_id' => array(
125 'title' => ts('Gender'),
126 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
127 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
128 ),
129 'birth_date' => array(
130 'title' => ts('Birth Date'),
131 'operatorType' => CRM_Report_Form::OP_DATE,
132 ),
133 'contact_type' => array(
134 'title' => ts('Contact Type'),
135 ),
136 'contact_sub_type' => array(
137 'title' => ts('Contact Subtype'),
138 ),
139 ),
140 'grouping' => 'contact-fields',
141 ),
142 'civicrm_relationship' => array(
143 'dao' => 'CRM_Contact_DAO_Relationship',
144 'fields' => array(
145 'start_date' => array(
146 'title' => ts('Employee Since'),
147 'default' => TRUE,
148 ),
149 ),
150 'filters' => array(
151 'start_date' => array(
152 'title' => ts('Employee Since'),
153 'operatorType' => CRM_Report_Form::OP_DATE,
154 'type' => CRM_Utils_Type::T_DATE,
155 ),
156 ),
157 ),
158 'civicrm_phone' => array(
159 'dao' => 'CRM_Core_DAO_Phone',
160 'grouping' => 'contact-fields',
161 'fields' => array(
162 'phone' => array(
163 'title' => ts('Phone'),
164 'default' => TRUE,
165 ),
166 ),
167 ),
168 'civicrm_email' => array(
169 'dao' => 'CRM_Core_DAO_Email',
170 'grouping' => 'contact-fields',
171 'fields' => array(
172 'email' => array(
173 'title' => ts('Email'),
174 'default' => TRUE,
175 ),
176 ),
177 ),
178 'civicrm_address' => array(
179 'dao' => 'CRM_Core_DAO_Address',
180 'grouping' => 'contact-fields',
181 'fields' => array(
182 'street_address' => NULL,
183 'city' => NULL,
184 'postal_code' => NULL,
185 'state_province_id' => array(
186 'title' => ts('State/Province'),
187 ),
188 'country_id' => array(
189 'title' => ts('Country'),
190 ),
191 ),
192 'filters' => array(
193 'country_id' => array(
194 'title' => ts('Country'),
195 'type' => CRM_Utils_Type::T_INT,
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => CRM_Core_PseudoConstant::country(NULL, FALSE),
198 ),
199 'state_province_id' => array(
200 'title' => ts('State/Province'),
201 'type' => CRM_Utils_Type::T_INT,
202 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
203 'options' => CRM_Core_PseudoConstant::stateProvince(),
204 ),
205 ),
206 ),
207 'civicrm_group' => array(
208 'dao' => 'CRM_Contact_DAO_Group',
209 'alias' => 'cgroup',
210 'filters' => array(
211 'gid' => array(
212 'name' => 'group_id',
213 'title' => ts('Group'),
214 'type' => CRM_Utils_Type::T_INT,
215 'group' => TRUE,
216 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
217 'options' => CRM_Core_PseudoConstant::staticGroup(),
218 ),
219 ),
220 ),
221 );
222
223 $this->_tagFilter = TRUE;
224 parent::__construct();
225 }
226
227 public function preProcess() {
228 parent::preProcess();
229 }
230
231 public function select() {
232
233 $select = $this->_columnHeaders = array();
234
235 foreach ($this->_columns as $tableName => $table) {
236 if (array_key_exists('fields', $table)) {
237 foreach ($table['fields'] as $fieldName => $field) {
238 if (!empty($field['required']) ||
239 !empty($this->_params['fields'][$fieldName])
240 ) {
241
242 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
243 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
244 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
245 }
246 }
247 }
248 }
249
250 $this->_select = "SELECT " . implode(', ', $select) . " ";
251 }
252
253 public function from() {
254 $this->_from = "
255 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
256
257 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
258 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
259
260 {$this->_aclFrom}
261 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
262 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
263 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
264 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
265 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
266 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
267 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
268
269 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
270 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
271 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
272 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
273 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
274 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
275 }
276
277 public function where() {
278
279 $clauses = array();
280 foreach ($this->_columns as $tableName => $table) {
281 if (array_key_exists('filters', $table)) {
282 foreach ($table['filters'] as $fieldName => $field) {
283 $clause = NULL;
284 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE
285 ) {
286 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
287 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
288 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
289
290 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
291 }
292 else {
293 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
294 if ($op) {
295 $clause = $this->whereClause($field,
296 $op,
297 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
298 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
299 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
300 );
301 }
302 }
303
304 if (!empty($clause)) {
305 $clauses[$fieldName] = $clause;
306 }
307 }
308 }
309 }
310
311 if (empty($clauses)) {
312 $this->_where = "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
313 }
314 else {
315 $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
316 }
317
318 if ($this->_aclWhere) {
319 $this->_where .= " AND {$this->_aclWhere} ";
320 }
321 }
322
323 public function groupBy() {
324 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id";
325 }
326
327 public function orderBy() {
328 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
329 }
330
331 public function postProcess() {
332 // get the acl clauses built before we assemble the query
333 $this->buildACLClause(array(
334 $this->_aliases['civicrm_contact'],
335 $this->_aliases['civicrm_employer'],
336 ));
337 parent::postProcess();
338 }
339
340 /**
341 * Alter display of rows.
342 *
343 * Iterate through the rows retrieved via SQL and make changes for display purposes,
344 * such as rendering contacts as links.
345 *
346 * @param array $rows
347 * Rows generated by SQL, with an array for each row.
348 */
349 public function alterDisplay(&$rows) {
350 $checkList = array();
351 $entryFound = FALSE;
352
353 foreach ($rows as $rowNum => $row) {
354
355 // convert employer name to links
356 if (array_key_exists('civicrm_employer_organization_name', $row) &&
357 array_key_exists('civicrm_employer_id', $row)
358 ) {
359 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
360 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
361 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
362 );
363 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
364 $entryFound = TRUE;
365 }
366
367 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
368 // not repeat contact display names if it matches with the one
369 // in previous row
370
371 foreach ($row as $colName => $colVal) {
372 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
373 in_array($colVal, $checkList[$colName])
374 ) {
375 $rows[$rowNum][$colName] = "";
376 }
377 if (in_array($colName, $this->_noRepeats)) {
378 $checkList[$colName][] = $colVal;
379 }
380 }
381 }
382
383 //handle gender
384 if (array_key_exists('civicrm_contact_gender_id', $row)) {
385 if ($value = $row['civicrm_contact_gender_id']) {
386 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
387 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
388 }
389 $entryFound = TRUE;
390 }
391
392 // display birthday in the configured custom format
393 if (array_key_exists('civicrm_contact_birth_date', $row)) {
394 $birthDate = $row['civicrm_contact_birth_date'];
395 if ($birthDate) {
396 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
397 }
398 $entryFound = TRUE;
399 }
400
401 // convert employee name to links
402 if (array_key_exists('civicrm_contact_display_name', $row) &&
403 array_key_exists('civicrm_contact_id', $row)
404 ) {
405 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
406 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
407 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
408 );
409 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
410 $entryFound = TRUE;
411 }
412
413 // handle country
414 if (array_key_exists('civicrm_address_country_id', $row)) {
415 if ($value = $row['civicrm_address_country_id']) {
416 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
417 }
418 $entryFound = TRUE;
419 }
420
421 if (array_key_exists('civicrm_address_state_province_id', $row)) {
422 if ($value = $row['civicrm_address_state_province_id']) {
423 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
424 }
425 $entryFound = TRUE;
426 }
427
428 // skip looking further in rows, if first row itself doesn't
429 // have the column we need
430 if (!$entryFound) {
431 break;
432 }
433 }
434 }
435
436 }