3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Contact_CurrentEmployer
extends CRM_Report_Form
{
20 * This report has not been optimised for group filtering.
22 * The functionality for group filtering has been improved but not
23 * all reports have been adjusted to take care of it. This report has not
24 * and will run an inefficient query until fixed.
27 * @see https://issues.civicrm.org/jira/browse/CRM-19170
29 protected $groupFilterNotOptimised = TRUE;
31 protected $_summary = NULL;
33 protected $_customGroupExtends = [
38 public $_drilldownReport = ['contact/detail' => 'Link to Detail Report'];
43 public function __construct() {
46 'civicrm_employer' => [
47 'dao' => 'CRM_Contact_DAO_Contact',
49 'organization_name' => [
50 'title' => ts('Employer Name'),
60 'organization_name' => [
61 'title' => ts('Employer Name'),
62 'operatorType' => CRM_Report_Form
::OP_STRING
,
66 'civicrm_contact' => [
67 'dao' => 'CRM_Contact_DAO_Contact',
70 'title' => ts('Employee Name'),
74 'title' => ts('First Name'),
77 'title' => ts('Middle Name'),
80 'title' => ts('Last Name'),
87 'title' => ts('Job Title'),
91 'title' => ts('Gender'),
94 'title' => ts('Birth Date'),
98 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
101 'title' => ts('Contact Type'),
103 'contact_sub_type' => [
104 'title' => ts('Contact Subtype'),
107 'filters' => array_merge($this->getBasicContactFilters(), ['sort_name' => ['title' => ts('Employee Name')]]),
108 'grouping' => 'contact-fields',
110 'civicrm_relationship' => [
111 'dao' => 'CRM_Contact_DAO_Relationship',
114 'title' => ts('Employee Since'),
120 'title' => ts('Employee Since'),
121 'operatorType' => CRM_Report_Form
::OP_DATE
,
122 'type' => CRM_Utils_Type
::T_DATE
,
127 'dao' => 'CRM_Core_DAO_Phone',
128 'grouping' => 'contact-fields',
131 'title' => ts('Phone'),
137 'dao' => 'CRM_Core_DAO_Email',
138 'grouping' => 'contact-fields',
141 'title' => ts('Email'),
146 'civicrm_address' => [
147 'dao' => 'CRM_Core_DAO_Address',
148 'grouping' => 'contact-fields',
150 'street_address' => NULL,
152 'postal_code' => NULL,
153 'state_province_id' => [
154 'title' => ts('State/Province'),
157 'title' => ts('Country'),
162 'title' => ts('Country'),
163 'type' => CRM_Utils_Type
::T_INT
,
164 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
165 'options' => CRM_Core_PseudoConstant
::country(NULL, FALSE),
167 'state_province_id' => [
168 'title' => ts('State/Province'),
169 'type' => CRM_Utils_Type
::T_INT
,
170 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
171 'options' => CRM_Core_PseudoConstant
::stateProvince(),
177 $this->_groupFilter
= TRUE;
178 $this->_tagFilter
= TRUE;
179 parent
::__construct();
182 public function preProcess() {
183 parent
::preProcess();
186 public function select() {
188 $select = $this->_columnHeaders
= [];
190 foreach ($this->_columns
as $tableName => $table) {
191 if (array_key_exists('fields', $table)) {
192 foreach ($table['fields'] as $fieldName => $field) {
193 if (!empty($field['required']) ||
194 !empty($this->_params
['fields'][$fieldName])
197 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
198 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
199 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
204 $this->_selectClauses
= $select;
206 $this->_select
= "SELECT " . implode(', ', $select) . " ";
209 public function from() {
210 $relType = civicrm_api3('RelationshipType', 'getvalue', [
212 'name_a_b' => "Employee of",
213 'name_b_a' => "Employer of",
216 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
218 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
219 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
222 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
223 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
224 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
225 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType}) ";
227 $this->joinAddressFromContact();
228 $this->joinPhoneFromContact();
229 $this->joinEmailFromContact();
232 public function where() {
235 foreach ($this->_columns
as $tableName => $table) {
236 if (array_key_exists('filters', $table)) {
237 foreach ($table['filters'] as $fieldName => $field) {
239 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
241 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
242 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
243 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
245 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
248 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
250 $clause = $this->whereClause($field,
252 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
253 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
254 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
259 if (!empty($clause)) {
260 $clauses[$fieldName] = $clause;
266 if (empty($clauses)) {
267 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
270 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
273 if ($this->_aclWhere
) {
274 $this->_where
.= " AND {$this->_aclWhere} ";
278 public function groupBy() {
280 "{$this->_aliases['civicrm_employer']}.id",
281 "{$this->_aliases['civicrm_contact']}.id",
284 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
287 public function orderBy() {
288 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
291 public function postProcess() {
292 // get the acl clauses built before we assemble the query
293 $this->buildACLClause([
294 $this->_aliases
['civicrm_contact'],
295 $this->_aliases
['civicrm_employer'],
297 parent
::postProcess();
301 * Alter display of rows.
303 * Iterate through the rows retrieved via SQL and make changes for display purposes,
304 * such as rendering contacts as links.
307 * Rows generated by SQL, with an array for each row.
309 public function alterDisplay(&$rows) {
313 foreach ($rows as $rowNum => $row) {
315 // convert employer name to links
316 if (array_key_exists('civicrm_employer_organization_name', $row) &&
317 array_key_exists('civicrm_employer_id', $row)
319 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
320 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
321 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
323 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
324 $rows[$rowNum]['civicrm_employer_organization_name_hover'] = ts('View Contact Detail Report for this contact');
328 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
329 // not repeat contact display names if it matches with the one
332 foreach ($row as $colName => $colVal) {
333 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
334 in_array($colVal, $checkList[$colName])
336 $rows[$rowNum][$colName] = "";
338 if (in_array($colName, $this->_noRepeats
)) {
339 $checkList[$colName][] = $colVal;
344 // Handle ID to label conversion for contact fields
345 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/currentEmployer', 'View Contact Detail') ?
TRUE : $entryFound;
346 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
348 // convert employee name to links
349 if (array_key_exists('civicrm_contact_sort_name', $row) &&
350 array_key_exists('civicrm_contact_id', $row)
352 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
353 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
354 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
356 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
357 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Detail Report for this contact');
361 // skip looking further in rows, if first row itself doesn't
362 // have the column we need