3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Report_Form_Contact_CurrentEmployer
extends CRM_Report_Form
{
36 * This report has not been optimised for group filtering.
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.
46 protected $groupFilterNotOptimised = TRUE;
48 protected $_summary = NULL;
50 protected $_customGroupExtends = array(
55 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
60 public function __construct() {
62 $this->_columns
= array(
63 'civicrm_employer' => array(
64 'dao' => 'CRM_Contact_DAO_Contact',
66 'organization_name' => array(
67 'title' => ts('Employer Name'),
77 'organization_name' => array(
78 'title' => ts('Employer Name'),
79 'operatorType' => CRM_Report_Form
::OP_STRING
,
83 'civicrm_contact' => array(
84 'dao' => 'CRM_Contact_DAO_Contact',
87 'title' => ts('Employee Name'),
90 'first_name' => array(
91 'title' => ts('First Name'),
93 'middle_name' => array(
94 'title' => ts('Middle Name'),
97 'title' => ts('Last Name'),
100 'no_display' => TRUE,
103 'job_title' => array(
104 'title' => ts('Job Title'),
107 'gender_id' => array(
108 'title' => ts('Gender'),
110 'birth_date' => array(
111 'title' => ts('Birth Date'),
114 'title' => ts('Age'),
115 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
117 'contact_type' => array(
118 'title' => ts('Contact Type'),
120 'contact_sub_type' => array(
121 'title' => ts('Contact Subtype'),
124 'filters' => array_merge($this->getBasicContactFilters(), array('sort_name' => array('title' => ts('Employee Name')))),
125 'grouping' => 'contact-fields',
127 'civicrm_relationship' => array(
128 'dao' => 'CRM_Contact_DAO_Relationship',
130 'start_date' => array(
131 'title' => ts('Employee Since'),
136 'start_date' => array(
137 'title' => ts('Employee Since'),
138 'operatorType' => CRM_Report_Form
::OP_DATE
,
139 'type' => CRM_Utils_Type
::T_DATE
,
143 'civicrm_phone' => array(
144 'dao' => 'CRM_Core_DAO_Phone',
145 'grouping' => 'contact-fields',
148 'title' => ts('Phone'),
153 'civicrm_email' => array(
154 'dao' => 'CRM_Core_DAO_Email',
155 'grouping' => 'contact-fields',
158 'title' => ts('Email'),
163 'civicrm_address' => array(
164 'dao' => 'CRM_Core_DAO_Address',
165 'grouping' => 'contact-fields',
167 'street_address' => NULL,
169 'postal_code' => NULL,
170 'state_province_id' => array(
171 'title' => ts('State/Province'),
173 'country_id' => array(
174 'title' => ts('Country'),
178 'country_id' => array(
179 'title' => ts('Country'),
180 'type' => CRM_Utils_Type
::T_INT
,
181 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
182 'options' => CRM_Core_PseudoConstant
::country(NULL, FALSE),
184 'state_province_id' => array(
185 'title' => ts('State/Province'),
186 'type' => CRM_Utils_Type
::T_INT
,
187 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
188 'options' => CRM_Core_PseudoConstant
::stateProvince(),
192 'civicrm_group' => array(
193 'dao' => 'CRM_Contact_DAO_Group',
197 'name' => 'group_id',
198 'title' => ts('Group'),
199 'type' => CRM_Utils_Type
::T_INT
,
201 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
202 'options' => CRM_Core_PseudoConstant
::staticGroup(),
208 $this->_tagFilter
= TRUE;
209 parent
::__construct();
212 public function preProcess() {
213 parent
::preProcess();
216 public function select() {
218 $select = $this->_columnHeaders
= array();
220 foreach ($this->_columns
as $tableName => $table) {
221 if (array_key_exists('fields', $table)) {
222 foreach ($table['fields'] as $fieldName => $field) {
223 if (!empty($field['required']) ||
224 !empty($this->_params
['fields'][$fieldName])
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);
234 $this->_selectClauses
= $select;
236 $this->_select
= "SELECT " . implode(', ', $select) . " ";
239 public function from() {
240 $relType = civicrm_api3('RelationshipType', 'getvalue', array(
242 'name_a_b' => "Employee of",
243 'name_b_a' => "Employer of",
246 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
248 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
249 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
252 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
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
255 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType})
256 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
257 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
258 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
260 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
261 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
262 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
263 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
264 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
265 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
268 public function where() {
271 foreach ($this->_columns
as $tableName => $table) {
272 if (array_key_exists('filters', $table)) {
273 foreach ($table['filters'] as $fieldName => $field) {
275 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
277 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
278 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
279 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
281 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
284 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
286 $clause = $this->whereClause($field,
288 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
289 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
290 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
295 if (!empty($clause)) {
296 $clauses[$fieldName] = $clause;
302 if (empty($clauses)) {
303 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
306 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
309 if ($this->_aclWhere
) {
310 $this->_where
.= " AND {$this->_aclWhere} ";
314 public function groupBy() {
316 "{$this->_aliases['civicrm_employer']}.id",
317 "{$this->_aliases['civicrm_contact']}.id",
320 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
323 public function orderBy() {
324 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
327 public function postProcess() {
328 // get the acl clauses built before we assemble the query
329 $this->buildACLClause(array(
330 $this->_aliases
['civicrm_contact'],
331 $this->_aliases
['civicrm_employer'],
333 parent
::postProcess();
337 * Alter display of rows.
339 * Iterate through the rows retrieved via SQL and make changes for display purposes,
340 * such as rendering contacts as links.
343 * Rows generated by SQL, with an array for each row.
345 public function alterDisplay(&$rows) {
346 $checkList = array();
349 foreach ($rows as $rowNum => $row) {
351 // convert employer name to links
352 if (array_key_exists('civicrm_employer_organization_name', $row) &&
353 array_key_exists('civicrm_employer_id', $row)
355 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
356 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
357 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
359 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
360 $rows[$rowNum]['civicrm_employer_organization_name_hover'] = ts('View Contact Detail Report for this contact');
364 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
365 // not repeat contact display names if it matches with the one
368 foreach ($row as $colName => $colVal) {
369 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
370 in_array($colVal, $checkList[$colName])
372 $rows[$rowNum][$colName] = "";
374 if (in_array($colName, $this->_noRepeats
)) {
375 $checkList[$colName][] = $colVal;
380 // Handle ID to label conversion for contact fields
381 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/currentEmployer', 'View Contact Detail') ?
TRUE : $entryFound;
383 // display birthday in the configured custom format
384 if (array_key_exists('civicrm_contact_birth_date', $row)) {
385 $birthDate = $row['civicrm_contact_birth_date'];
387 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
392 // convert employee name to links
393 if (array_key_exists('civicrm_contact_sort_name', $row) &&
394 array_key_exists('civicrm_contact_id', $row)
396 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
397 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
398 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
400 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
401 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Detail Report for this contact');
406 if (array_key_exists('civicrm_address_country_id', $row)) {
407 if ($value = $row['civicrm_address_country_id']) {
408 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
413 if (array_key_exists('civicrm_address_state_province_id', $row)) {
414 if ($value = $row['civicrm_address_state_province_id']) {
415 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
420 // skip looking further in rows, if first row itself doesn't
421 // have the column we need