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);
250 $this->_select
= "SELECT " . implode(', ', $select) . " ";
253 public function from() {
255 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
257 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
258 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
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 )
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) ";
277 public function where() {
280 foreach ($this->_columns
as $tableName => $table) {
281 if (array_key_exists('filters', $table)) {
282 foreach ($table['filters'] as $fieldName => $field) {
284 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
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
);
290 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
293 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
295 $clause = $this->whereClause($field,
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
)
304 if (!empty($clause)) {
305 $clauses[$fieldName] = $clause;
311 if (empty($clauses)) {
312 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
315 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
318 if ($this->_aclWhere
) {
319 $this->_where
.= " AND {$this->_aclWhere} ";
323 public function groupBy() {
324 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id";
327 public function orderBy() {
328 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
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'],
337 parent
::postProcess();
341 * Alter display of rows.
343 * Iterate through the rows retrieved via SQL and make changes for display purposes,
344 * such as rendering contacts as links.
347 * Rows generated by SQL, with an array for each row.
349 public function alterDisplay(&$rows) {
350 $checkList = array();
353 foreach ($rows as $rowNum => $row) {
355 // convert employer name to links
356 if (array_key_exists('civicrm_employer_organization_name', $row) &&
357 array_key_exists('civicrm_employer_id', $row)
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
363 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
367 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
368 // not repeat contact display names if it matches with the one
371 foreach ($row as $colName => $colVal) {
372 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
373 in_array($colVal, $checkList[$colName])
375 $rows[$rowNum][$colName] = "";
377 if (in_array($colName, $this->_noRepeats
)) {
378 $checkList[$colName][] = $colVal;
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];
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'];
396 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
401 // convert employee name to links
402 if (array_key_exists('civicrm_contact_display_name', $row) &&
403 array_key_exists('civicrm_contact_id', $row)
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
409 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
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);
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);
428 // skip looking further in rows, if first row itself doesn't
429 // have the column we need