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