4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contact_CurrentEmployer
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 protected $_customGroupExtends = array(
41 'Contact', 'Individual');
43 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
45 function __construct() {
47 $this->_columns
= array(
50 'dao' => 'CRM_Contact_DAO_Contact',
53 'organization_name' =>
54 array('title' => ts('Employer Name'),
66 'organization_name' =>
67 array('title' => ts('Employer Name'),
68 'operatorType' => CRM_Report_Form
::OP_STRING
,
74 'dao' => 'CRM_Contact_DAO_Contact',
78 array('title' => ts('Employee Name'),
81 'first_name' => array('title' => ts('First Name'),
83 'last_name' => array('title' => ts('Last Name'),
86 array('title' => ts('Job Title'),
90 array('title' => ts('Gender'),
99 'title' => ts('Contact Type'),
101 'contact_sub_type' =>
103 'title' => ts('Contact SubType'),
109 array('title' => ts('Employee Name')),
111 array('no_display' => TRUE),
113 'grouping' => 'contact-fields',
115 'civicrm_relationship' =>
117 'dao' => 'CRM_Contact_DAO_Relationship',
121 array('title' => ts('Employee Since'),
128 array('title' => ts('Employee Since'),
129 'operatorType' => CRM_Report_Form
::OP_DATE
,
130 'type' => CRM_Utils_Type
::T_DATE
,
136 'dao' => 'CRM_Core_DAO_Email',
137 'grouping' => 'contact-fields',
141 array('title' => ts('Email'),
148 'dao' => 'CRM_Core_DAO_Address',
149 'grouping' => 'contact-fields',
152 'street_address' => NULL,
154 'postal_code' => NULL,
155 'state_province_id' =>
156 array('title' => ts('State/Province'),
159 array('title' => ts('Country'),
165 array('title' => ts('Country'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Core_PseudoConstant
::country(NULL, FALSE),
169 'state_province_id' =>
170 array('title' => ts('State/Province'),
171 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
172 'options' => CRM_Core_PseudoConstant
::stateProvince(),
178 'dao' => 'CRM_Contact_DAO_Group',
184 'name' => 'group_id',
185 'title' => ts('Group'),
187 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
188 'options' => CRM_Core_PseudoConstant
::staticGroup(),
194 $this->_tagFilter
= TRUE;
195 parent
::__construct();
198 function preProcess() {
199 parent
::preProcess();
204 $select = $this->_columnHeaders
= array();
206 foreach ($this->_columns
as $tableName => $table) {
207 if (array_key_exists('fields', $table)) {
208 foreach ($table['fields'] as $fieldName => $field) {
209 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
211 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
212 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
213 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
219 $this->_select
= "SELECT " . implode(', ', $select) . " ";
224 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
226 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
227 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
230 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
231 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
232 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
233 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
234 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
235 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
236 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
238 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
239 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
240 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
246 foreach ($this->_columns
as $tableName => $table) {
247 if (array_key_exists('filters', $table)) {
248 foreach ($table['filters'] as $fieldName => $field) {
250 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
) {
251 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
252 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
253 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
255 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
258 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
260 $clause = $this->whereClause($field,
262 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
263 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
264 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
269 if (!empty($clause)) {
270 $clauses[$fieldName] = $clause;
276 if (empty($clauses)) {
277 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
280 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
283 if ($this->_aclWhere
) {
284 $this->_where
.= " AND {$this->_aclWhere} ";
289 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id";
293 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
296 function postProcess() {
297 // get the acl clauses built before we assemble the query
298 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_employer']));
299 parent
::postProcess();
302 function alterDisplay(&$rows) {
303 // custom code to alter rows
304 $checkList = array();
307 foreach ($rows as $rowNum => $row) {
309 // convert employer name to links
310 if (array_key_exists('civicrm_employer_organization_name', $row) &&
311 array_key_exists('civicrm_employer_id', $row)
313 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
314 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
315 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
317 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
321 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
322 // not repeat contact display names if it matches with the one
325 foreach ($row as $colName => $colVal) {
326 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
327 in_array($colVal, $checkList[$colName])
329 $rows[$rowNum][$colName] = "";
331 if (in_array($colName, $this->_noRepeats
)) {
332 $checkList[$colName][] = $colVal;
338 if (array_key_exists('civicrm_contact_gender_id', $row)) {
339 if ($value = $row['civicrm_contact_gender_id']) {
340 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
341 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
346 // convert employee name to links
347 if (array_key_exists('civicrm_contact_display_name', $row) &&
348 array_key_exists('civicrm_contact_id', $row)
350 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
351 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
352 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
354 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
359 if (array_key_exists('civicrm_address_country_id', $row)) {
360 if ($value = $row['civicrm_address_country_id']) {
361 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
366 if (array_key_exists('civicrm_address_state_province_id', $row)) {
367 if ($value = $row['civicrm_address_state_province_id']) {
368 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
373 // skip looking further in rows, if first row itself doesn't
374 // have the column we need