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');
48 public function __construct() {
50 $this->_columns
= array(
51 'civicrm_employer' => array(
52 'dao' => 'CRM_Contact_DAO_Contact',
54 'organization_name' => array(
55 'title' => ts('Employer Name'),
65 'organization_name' => array(
66 'title' => ts('Employer Name'),
67 'operatorType' => CRM_Report_Form
::OP_STRING
,
71 'civicrm_contact' => array(
72 'dao' => 'CRM_Contact_DAO_Contact',
75 'title' => ts('Employee Name'),
78 'first_name' => array(
79 'title' => ts('First Name'),
81 'middle_name' => array(
82 'title' => ts('Middle Name'),
85 'title' => ts('Last Name'),
92 'title' => ts('Job Title'),
96 'title' => ts('Gender'),
98 'birth_date' => array(
99 'title' => ts('Birth Date'),
102 'title' => ts('Age'),
103 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
105 'contact_type' => array(
106 'title' => ts('Contact Type'),
108 'contact_sub_type' => array(
109 'title' => ts('Contact Subtype'),
112 'filters' => array_merge($this->getBasicContactFilters(), array('sort_name' => array('title' => ts('Employee Name')))),
113 'grouping' => 'contact-fields',
115 'civicrm_relationship' => array(
116 'dao' => 'CRM_Contact_DAO_Relationship',
118 'start_date' => array(
119 'title' => ts('Employee Since'),
124 'start_date' => array(
125 'title' => ts('Employee Since'),
126 'operatorType' => CRM_Report_Form
::OP_DATE
,
127 'type' => CRM_Utils_Type
::T_DATE
,
131 'civicrm_phone' => array(
132 'dao' => 'CRM_Core_DAO_Phone',
133 'grouping' => 'contact-fields',
136 'title' => ts('Phone'),
141 'civicrm_email' => array(
142 'dao' => 'CRM_Core_DAO_Email',
143 'grouping' => 'contact-fields',
146 'title' => ts('Email'),
151 'civicrm_address' => array(
152 'dao' => 'CRM_Core_DAO_Address',
153 'grouping' => 'contact-fields',
155 'street_address' => NULL,
157 'postal_code' => NULL,
158 'state_province_id' => array(
159 'title' => ts('State/Province'),
161 'country_id' => array(
162 'title' => ts('Country'),
166 'country_id' => array(
167 'title' => ts('Country'),
168 'type' => CRM_Utils_Type
::T_INT
,
169 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
170 'options' => CRM_Core_PseudoConstant
::country(NULL, FALSE),
172 'state_province_id' => array(
173 'title' => ts('State/Province'),
174 'type' => CRM_Utils_Type
::T_INT
,
175 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
176 'options' => CRM_Core_PseudoConstant
::stateProvince(),
180 'civicrm_group' => array(
181 'dao' => 'CRM_Contact_DAO_Group',
185 'name' => 'group_id',
186 'title' => ts('Group'),
187 'type' => CRM_Utils_Type
::T_INT
,
189 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
190 'options' => CRM_Core_PseudoConstant
::staticGroup(),
196 $this->_tagFilter
= TRUE;
197 parent
::__construct();
200 public function preProcess() {
201 parent
::preProcess();
204 public function select() {
206 $select = $this->_columnHeaders
= array();
208 foreach ($this->_columns
as $tableName => $table) {
209 if (array_key_exists('fields', $table)) {
210 foreach ($table['fields'] as $fieldName => $field) {
211 if (!empty($field['required']) ||
212 !empty($this->_params
['fields'][$fieldName])
215 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
216 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
217 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
222 $this->_selectClauses
= $select;
224 $this->_select
= "SELECT " . implode(', ', $select) . " ";
227 public function from() {
229 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
231 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
232 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
235 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
236 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
237 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
238 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
239 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
240 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
241 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
243 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
244 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
245 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
246 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
247 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
248 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
251 public function where() {
254 foreach ($this->_columns
as $tableName => $table) {
255 if (array_key_exists('filters', $table)) {
256 foreach ($table['filters'] as $fieldName => $field) {
258 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
260 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
261 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
262 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
264 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
267 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
269 $clause = $this->whereClause($field,
271 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
272 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
273 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
278 if (!empty($clause)) {
279 $clauses[$fieldName] = $clause;
285 if (empty($clauses)) {
286 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
289 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
292 if ($this->_aclWhere
) {
293 $this->_where
.= " AND {$this->_aclWhere} ";
297 public function groupBy() {
299 "{$this->_aliases['civicrm_employer']}.id",
300 "{$this->_aliases['civicrm_contact']}.id",
303 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
306 public function orderBy() {
307 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
310 public function postProcess() {
311 // get the acl clauses built before we assemble the query
312 $this->buildACLClause(array(
313 $this->_aliases
['civicrm_contact'],
314 $this->_aliases
['civicrm_employer'],
316 parent
::postProcess();
320 * Alter display of rows.
322 * Iterate through the rows retrieved via SQL and make changes for display purposes,
323 * such as rendering contacts as links.
326 * Rows generated by SQL, with an array for each row.
328 public function alterDisplay(&$rows) {
329 $checkList = array();
332 foreach ($rows as $rowNum => $row) {
334 // convert employer name to links
335 if (array_key_exists('civicrm_employer_organization_name', $row) &&
336 array_key_exists('civicrm_employer_id', $row)
338 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
339 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
340 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
342 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
346 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
347 // not repeat contact display names if it matches with the one
350 foreach ($row as $colName => $colVal) {
351 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
352 in_array($colVal, $checkList[$colName])
354 $rows[$rowNum][$colName] = "";
356 if (in_array($colName, $this->_noRepeats
)) {
357 $checkList[$colName][] = $colVal;
363 if (array_key_exists('civicrm_contact_gender_id', $row)) {
364 if ($value = $row['civicrm_contact_gender_id']) {
365 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
366 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
371 // display birthday in the configured custom format
372 if (array_key_exists('civicrm_contact_birth_date', $row)) {
373 $birthDate = $row['civicrm_contact_birth_date'];
375 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
380 // convert employee name to links
381 if (array_key_exists('civicrm_contact_display_name', $row) &&
382 array_key_exists('civicrm_contact_id', $row)
384 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
385 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
386 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
388 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
393 if (array_key_exists('civicrm_address_country_id', $row)) {
394 if ($value = $row['civicrm_address_country_id']) {
395 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
400 if (array_key_exists('civicrm_address_state_province_id', $row)) {
401 if ($value = $row['civicrm_address_state_province_id']) {
402 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
407 // skip looking further in rows, if first row itself doesn't
408 // have the column we need