3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 class CRM_Report_Form_Contact_CurrentEmployer
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 protected $_customGroupExtends = array(
44 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
50 public function __construct() {
52 $this->_columns
= array(
53 'civicrm_employer' => array(
54 'dao' => 'CRM_Contact_DAO_Contact',
56 'organization_name' => array(
57 'title' => ts('Employer Name'),
67 'organization_name' => array(
68 'title' => ts('Employer Name'),
69 'operatorType' => CRM_Report_Form
::OP_STRING
,
73 'civicrm_contact' => array(
74 'dao' => 'CRM_Contact_DAO_Contact',
77 'title' => ts('Employee Name'),
80 'first_name' => array(
81 'title' => ts('First Name'),
83 'middle_name' => array(
84 'title' => ts('Middle Name'),
87 'title' => ts('Last Name'),
94 'title' => ts('Job Title'),
98 'title' => ts('Gender'),
100 'birth_date' => array(
101 'title' => ts('Birth Date'),
104 'title' => ts('Age'),
105 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
107 'contact_type' => array(
108 'title' => ts('Contact Type'),
110 'contact_sub_type' => array(
111 'title' => ts('Contact Subtype'),
115 'sort_name' => array('title' => ts('Employee Name')),
117 'title' => ts('Contact Source'),
118 'type' => CRM_Utils_Type
::T_STRING
,
121 'title' => ts('Contact ID'),
122 'no_display' => TRUE,
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'),
129 'birth_date' => array(
130 'title' => ts('Birth Date'),
131 'operatorType' => CRM_Report_Form
::OP_DATE
,
133 'contact_type' => array(
134 'title' => ts('Contact Type'),
136 'contact_sub_type' => array(
137 'title' => ts('Contact Subtype'),
140 'grouping' => 'contact-fields',
142 'civicrm_relationship' => array(
143 'dao' => 'CRM_Contact_DAO_Relationship',
145 'start_date' => array(
146 'title' => ts('Employee Since'),
151 'start_date' => array(
152 'title' => ts('Employee Since'),
153 'operatorType' => CRM_Report_Form
::OP_DATE
,
154 'type' => CRM_Utils_Type
::T_DATE
,
158 'civicrm_phone' => array(
159 'dao' => 'CRM_Core_DAO_Phone',
160 'grouping' => 'contact-fields',
163 'title' => ts('Phone'),
168 'civicrm_email' => array(
169 'dao' => 'CRM_Core_DAO_Email',
170 'grouping' => 'contact-fields',
173 'title' => ts('Email'),
178 'civicrm_address' => array(
179 'dao' => 'CRM_Core_DAO_Address',
180 'grouping' => 'contact-fields',
182 'street_address' => NULL,
184 'postal_code' => NULL,
185 'state_province_id' => array(
186 'title' => ts('State/Province'),
188 'country_id' => array(
189 'title' => ts('Country'),
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),
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(),
207 'civicrm_group' => array(
208 'dao' => 'CRM_Contact_DAO_Group',
212 'name' => 'group_id',
213 'title' => ts('Group'),
214 'type' => CRM_Utils_Type
::T_INT
,
216 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
217 'options' => CRM_Core_PseudoConstant
::staticGroup(),
223 $this->_tagFilter
= TRUE;
224 parent
::__construct();
227 public function preProcess() {
228 parent
::preProcess();
231 public function select() {
233 $select = $this->_columnHeaders
= array();
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])
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);
249 $this->_selectClauses
= $select;
251 $this->_select
= "SELECT " . implode(', ', $select) . " ";
254 public function from() {
256 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
258 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
259 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
262 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
263 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
264 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
265 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
266 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
267 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
268 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
270 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
271 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
272 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
273 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
274 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
275 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
278 public function where() {
281 foreach ($this->_columns
as $tableName => $table) {
282 if (array_key_exists('filters', $table)) {
283 foreach ($table['filters'] as $fieldName => $field) {
285 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
287 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
288 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
289 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
291 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
294 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
296 $clause = $this->whereClause($field,
298 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
299 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
300 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
305 if (!empty($clause)) {
306 $clauses[$fieldName] = $clause;
312 if (empty($clauses)) {
313 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
316 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
319 if ($this->_aclWhere
) {
320 $this->_where
.= " AND {$this->_aclWhere} ";
324 public function groupBy() {
326 "{$this->_aliases['civicrm_employer']}.id",
327 "{$this->_aliases['civicrm_contact']}.id",
330 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
333 public function orderBy() {
334 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
337 public function postProcess() {
338 // get the acl clauses built before we assemble the query
339 $this->buildACLClause(array(
340 $this->_aliases
['civicrm_contact'],
341 $this->_aliases
['civicrm_employer'],
343 parent
::postProcess();
347 * Alter display of rows.
349 * Iterate through the rows retrieved via SQL and make changes for display purposes,
350 * such as rendering contacts as links.
353 * Rows generated by SQL, with an array for each row.
355 public function alterDisplay(&$rows) {
356 $checkList = array();
359 foreach ($rows as $rowNum => $row) {
361 // convert employer name to links
362 if (array_key_exists('civicrm_employer_organization_name', $row) &&
363 array_key_exists('civicrm_employer_id', $row)
365 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
366 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
367 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
369 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
373 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
374 // not repeat contact display names if it matches with the one
377 foreach ($row as $colName => $colVal) {
378 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
379 in_array($colVal, $checkList[$colName])
381 $rows[$rowNum][$colName] = "";
383 if (in_array($colName, $this->_noRepeats
)) {
384 $checkList[$colName][] = $colVal;
390 if (array_key_exists('civicrm_contact_gender_id', $row)) {
391 if ($value = $row['civicrm_contact_gender_id']) {
392 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
393 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
398 // display birthday in the configured custom format
399 if (array_key_exists('civicrm_contact_birth_date', $row)) {
400 $birthDate = $row['civicrm_contact_birth_date'];
402 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
407 // convert employee name to links
408 if (array_key_exists('civicrm_contact_display_name', $row) &&
409 array_key_exists('civicrm_contact_id', $row)
411 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
412 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
413 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
415 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
420 if (array_key_exists('civicrm_address_country_id', $row)) {
421 if ($value = $row['civicrm_address_country_id']) {
422 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
427 if (array_key_exists('civicrm_address_state_province_id', $row)) {
428 if ($value = $row['civicrm_address_state_province_id']) {
429 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
434 // skip looking further in rows, if first row itself doesn't
435 // have the column we need