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');
51 function __construct() {
53 $this->_columns
= array(
56 'dao' => 'CRM_Contact_DAO_Contact',
59 'organization_name' =>
60 array('title' => ts('Employer Name'),
72 'organization_name' =>
73 array('title' => ts('Employer Name'),
74 'operatorType' => CRM_Report_Form
::OP_STRING
,
80 'dao' => 'CRM_Contact_DAO_Contact',
84 array('title' => ts('Employee Name'),
87 'first_name' => array('title' => ts('First Name'),
89 'last_name' => array('title' => ts('Last Name'),
92 array('title' => ts('Job Title'),
96 array('title' => ts('Gender'),
100 'no_display' => TRUE,
105 'title' => ts('Contact Type'),
107 'contact_sub_type' =>
109 'title' => ts('Contact SubType'),
115 array('title' => ts('Employee Name')),
117 array('no_display' => TRUE),
119 'grouping' => 'contact-fields',
121 'civicrm_relationship' =>
123 'dao' => 'CRM_Contact_DAO_Relationship',
127 array('title' => ts('Employee Since'),
134 array('title' => ts('Employee Since'),
135 'operatorType' => CRM_Report_Form
::OP_DATE
,
136 'type' => CRM_Utils_Type
::T_DATE
,
142 'dao' => 'CRM_Core_DAO_Phone',
143 'grouping' => 'contact-fields',
147 array('title' => ts('Phone'),
154 'dao' => 'CRM_Core_DAO_Email',
155 'grouping' => 'contact-fields',
159 array('title' => ts('Email'),
166 'dao' => 'CRM_Core_DAO_Address',
167 'grouping' => 'contact-fields',
170 'street_address' => NULL,
172 'postal_code' => NULL,
173 'state_province_id' =>
174 array('title' => ts('State/Province'),
177 array('title' => ts('Country'),
183 array('title' => ts('Country'),
184 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
185 'options' => CRM_Core_PseudoConstant
::country(NULL, FALSE),
187 'state_province_id' =>
188 array('title' => ts('State/Province'),
189 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
190 'options' => CRM_Core_PseudoConstant
::stateProvince(),
196 'dao' => 'CRM_Contact_DAO_Group',
202 'name' => 'group_id',
203 'title' => ts('Group'),
205 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
206 'options' => CRM_Core_PseudoConstant
::staticGroup(),
212 $this->_tagFilter
= TRUE;
213 parent
::__construct();
216 function preProcess() {
217 parent
::preProcess();
222 $select = $this->_columnHeaders
= array();
224 foreach ($this->_columns
as $tableName => $table) {
225 if (array_key_exists('fields', $table)) {
226 foreach ($table['fields'] as $fieldName => $field) {
227 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
229 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
230 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
231 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
237 $this->_select
= "SELECT " . implode(', ', $select) . " ";
242 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
244 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
245 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
248 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
249 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
250 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
251 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
252 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
253 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
254 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
256 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
257 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
258 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
259 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
260 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
261 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
267 foreach ($this->_columns
as $tableName => $table) {
268 if (array_key_exists('filters', $table)) {
269 foreach ($table['filters'] as $fieldName => $field) {
271 if (CRM_Utils_Array
::value('operatorType', $field) & CRM_Report_Form
::OP_DATE
) {
272 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
273 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
274 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
276 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
279 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
281 $clause = $this->whereClause($field,
283 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
284 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
285 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
290 if (!empty($clause)) {
291 $clauses[$fieldName] = $clause;
297 if (empty($clauses)) {
298 $this->_where
= "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
301 $this->_where
= "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
304 if ($this->_aclWhere
) {
305 $this->_where
.= " AND {$this->_aclWhere} ";
310 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id";
314 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
317 function postProcess() {
318 // get the acl clauses built before we assemble the query
319 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_employer']));
320 parent
::postProcess();
326 function alterDisplay(&$rows) {
327 // custom code to alter rows
328 $checkList = array();
331 foreach ($rows as $rowNum => $row) {
333 // convert employer name to links
334 if (array_key_exists('civicrm_employer_organization_name', $row) &&
335 array_key_exists('civicrm_employer_id', $row)
337 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
338 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
339 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
341 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
345 if (!empty($this->_noRepeats
) && $this->_outputMode
!= 'csv') {
346 // not repeat contact display names if it matches with the one
349 foreach ($row as $colName => $colVal) {
350 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
351 in_array($colVal, $checkList[$colName])
353 $rows[$rowNum][$colName] = "";
355 if (in_array($colName, $this->_noRepeats
)) {
356 $checkList[$colName][] = $colVal;
362 if (array_key_exists('civicrm_contact_gender_id', $row)) {
363 if ($value = $row['civicrm_contact_gender_id']) {
364 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
365 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
370 // convert employee name to links
371 if (array_key_exists('civicrm_contact_display_name', $row) &&
372 array_key_exists('civicrm_contact_id', $row)
374 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
375 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
376 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
378 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
383 if (array_key_exists('civicrm_address_country_id', $row)) {
384 if ($value = $row['civicrm_address_country_id']) {
385 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
390 if (array_key_exists('civicrm_address_state_province_id', $row)) {
391 if ($value = $row['civicrm_address_state_province_id']) {
392 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
397 // skip looking further in rows, if first row itself doesn't
398 // have the column we need