| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 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. | |
| 14 | | | |
| 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. | |
| 19 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 33 | * $Id$ |
| 34 | * |
| 35 | */ |
| 36 | class CRM_Report_Form_Contact_CurrentEmployer extends CRM_Report_Form { |
| 37 | |
| 38 | protected $_summary = NULL; |
| 39 | |
| 40 | protected $_customGroupExtends = array( |
| 41 | 'Contact', 'Individual'); |
| 42 | |
| 43 | public $_drilldownReport = array('contact/detail' => 'Link to Detail Report'); |
| 44 | |
| 45 | /** |
| 46 | * |
| 47 | */ |
| 48 | /** |
| 49 | * |
| 50 | */ |
| 51 | function __construct() { |
| 52 | |
| 53 | $this->_columns = array( |
| 54 | 'civicrm_employer' => |
| 55 | array( |
| 56 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 57 | 'fields' => |
| 58 | array( |
| 59 | 'organization_name' => |
| 60 | array('title' => ts('Employer Name'), |
| 61 | 'required' => TRUE, |
| 62 | 'no_repeat' => TRUE, |
| 63 | ), |
| 64 | 'id' => |
| 65 | array( |
| 66 | 'no_display' => TRUE, |
| 67 | 'required' => TRUE, |
| 68 | ), |
| 69 | ), |
| 70 | 'filters' => |
| 71 | array( |
| 72 | 'organization_name' => |
| 73 | array('title' => ts('Employer Name'), |
| 74 | 'operatorType' => CRM_Report_Form::OP_STRING, |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | 'civicrm_contact' => |
| 79 | array( |
| 80 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 81 | 'fields' => |
| 82 | array( |
| 83 | 'sort_name' => |
| 84 | array('title' => ts('Employee Name'), |
| 85 | 'required' => TRUE, |
| 86 | ), |
| 87 | 'first_name' => array('title' => ts('First Name'), |
| 88 | ), |
| 89 | 'last_name' => array('title' => ts('Last Name'), |
| 90 | ), |
| 91 | 'job_title' => |
| 92 | array('title' => ts('Job Title'), |
| 93 | 'default' => TRUE, |
| 94 | ), |
| 95 | 'gender_id' => |
| 96 | array('title' => ts('Gender'), |
| 97 | ), |
| 98 | 'id' => |
| 99 | array( |
| 100 | 'no_display' => TRUE, |
| 101 | 'required' => TRUE, |
| 102 | ), |
| 103 | 'contact_type' => |
| 104 | array( |
| 105 | 'title' => ts('Contact Type'), |
| 106 | ), |
| 107 | 'contact_sub_type' => |
| 108 | array( |
| 109 | 'title' => ts('Contact SubType'), |
| 110 | ), |
| 111 | ), |
| 112 | 'filters' => |
| 113 | array( |
| 114 | 'sort_name' => |
| 115 | array('title' => ts('Employee Name')), |
| 116 | 'id' => |
| 117 | array('no_display' => TRUE), |
| 118 | ), |
| 119 | 'grouping' => 'contact-fields', |
| 120 | ), |
| 121 | 'civicrm_relationship' => |
| 122 | array( |
| 123 | 'dao' => 'CRM_Contact_DAO_Relationship', |
| 124 | 'fields' => |
| 125 | array( |
| 126 | 'start_date' => |
| 127 | array('title' => ts('Employee Since'), |
| 128 | 'default' => TRUE, |
| 129 | ), |
| 130 | ), |
| 131 | 'filters' => |
| 132 | array( |
| 133 | 'start_date' => |
| 134 | array('title' => ts('Employee Since'), |
| 135 | 'operatorType' => CRM_Report_Form::OP_DATE, |
| 136 | 'type' => CRM_Utils_Type::T_DATE, |
| 137 | ), |
| 138 | ), |
| 139 | ), |
| 140 | 'civicrm_phone' => |
| 141 | array( |
| 142 | 'dao' => 'CRM_Core_DAO_Phone', |
| 143 | 'grouping' => 'contact-fields', |
| 144 | 'fields' => |
| 145 | array( |
| 146 | 'phone' => |
| 147 | array('title' => ts('Phone'), |
| 148 | 'default' => TRUE, |
| 149 | ), |
| 150 | ), |
| 151 | ), |
| 152 | 'civicrm_email' => |
| 153 | array( |
| 154 | 'dao' => 'CRM_Core_DAO_Email', |
| 155 | 'grouping' => 'contact-fields', |
| 156 | 'fields' => |
| 157 | array( |
| 158 | 'email' => |
| 159 | array('title' => ts('Email'), |
| 160 | 'default' => TRUE, |
| 161 | ), |
| 162 | ), |
| 163 | ), |
| 164 | 'civicrm_address' => |
| 165 | array( |
| 166 | 'dao' => 'CRM_Core_DAO_Address', |
| 167 | 'grouping' => 'contact-fields', |
| 168 | 'fields' => |
| 169 | array( |
| 170 | 'street_address' => NULL, |
| 171 | 'city' => NULL, |
| 172 | 'postal_code' => NULL, |
| 173 | 'state_province_id' => |
| 174 | array('title' => ts('State/Province'), |
| 175 | ), |
| 176 | 'country_id' => |
| 177 | array('title' => ts('Country'), |
| 178 | ), |
| 179 | ), |
| 180 | 'filters' => |
| 181 | array( |
| 182 | 'country_id' => |
| 183 | array('title' => ts('Country'), |
| 184 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 185 | 'options' => CRM_Core_PseudoConstant::country(NULL, FALSE), |
| 186 | ), |
| 187 | 'state_province_id' => |
| 188 | array('title' => ts('State/Province'), |
| 189 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 190 | 'options' => CRM_Core_PseudoConstant::stateProvince(), |
| 191 | ), |
| 192 | ), |
| 193 | ), |
| 194 | 'civicrm_group' => |
| 195 | array( |
| 196 | 'dao' => 'CRM_Contact_DAO_Group', |
| 197 | 'alias' => 'cgroup', |
| 198 | 'filters' => |
| 199 | array( |
| 200 | 'gid' => |
| 201 | array( |
| 202 | 'name' => 'group_id', |
| 203 | 'title' => ts('Group'), |
| 204 | 'group' => TRUE, |
| 205 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 206 | 'options' => CRM_Core_PseudoConstant::staticGroup(), |
| 207 | ), |
| 208 | ), |
| 209 | ), |
| 210 | ); |
| 211 | |
| 212 | $this->_tagFilter = TRUE; |
| 213 | parent::__construct(); |
| 214 | } |
| 215 | |
| 216 | function preProcess() { |
| 217 | parent::preProcess(); |
| 218 | } |
| 219 | |
| 220 | function select() { |
| 221 | |
| 222 | $select = $this->_columnHeaders = array(); |
| 223 | |
| 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])) { |
| 228 | |
| 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); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
| 238 | } |
| 239 | |
| 240 | function from() { |
| 241 | $this->_from = " |
| 242 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} |
| 243 | |
| 244 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']} |
| 245 | ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id |
| 246 | |
| 247 | {$this->_aclFrom} |
| 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 ) |
| 255 | |
| 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) "; |
| 262 | } |
| 263 | |
| 264 | function where() { |
| 265 | |
| 266 | $clauses = array(); |
| 267 | foreach ($this->_columns as $tableName => $table) { |
| 268 | if (array_key_exists('filters', $table)) { |
| 269 | foreach ($table['filters'] as $fieldName => $field) { |
| 270 | $clause = NULL; |
| 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); |
| 275 | |
| 276 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); |
| 277 | } |
| 278 | else { |
| 279 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); |
| 280 | if ($op) { |
| 281 | $clause = $this->whereClause($field, |
| 282 | $op, |
| 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) |
| 286 | ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (!empty($clause)) { |
| 291 | $clauses[$fieldName] = $clause; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (empty($clauses)) { |
| 298 | $this->_where = "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' "; |
| 299 | } |
| 300 | else { |
| 301 | $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses); |
| 302 | } |
| 303 | |
| 304 | if ($this->_aclWhere) { |
| 305 | $this->_where .= " AND {$this->_aclWhere} "; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | function groupBy() { |
| 310 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id"; |
| 311 | } |
| 312 | |
| 313 | function orderBy() { |
| 314 | $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name"; |
| 315 | } |
| 316 | |
| 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(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * @param $rows |
| 325 | */ |
| 326 | function alterDisplay(&$rows) { |
| 327 | // custom code to alter rows |
| 328 | $checkList = array(); |
| 329 | $entryFound = FALSE; |
| 330 | |
| 331 | foreach ($rows as $rowNum => $row) { |
| 332 | |
| 333 | // convert employer name to links |
| 334 | if (array_key_exists('civicrm_employer_organization_name', $row) && |
| 335 | array_key_exists('civicrm_employer_id', $row) |
| 336 | ) { |
| 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 |
| 340 | ); |
| 341 | $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url; |
| 342 | $entryFound = TRUE; |
| 343 | } |
| 344 | |
| 345 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { |
| 346 | // not repeat contact display names if it matches with the one |
| 347 | // in previous row |
| 348 | |
| 349 | foreach ($row as $colName => $colVal) { |
| 350 | if (!empty($checkList[$colName]) && is_array($checkList[$colName]) && |
| 351 | in_array($colVal, $checkList[$colName]) |
| 352 | ) { |
| 353 | $rows[$rowNum][$colName] = ""; |
| 354 | } |
| 355 | if (in_array($colName, $this->_noRepeats)) { |
| 356 | $checkList[$colName][] = $colVal; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | //handle gender |
| 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]; |
| 366 | } |
| 367 | $entryFound = TRUE; |
| 368 | } |
| 369 | |
| 370 | // convert employee name to links |
| 371 | if (array_key_exists('civicrm_contact_display_name', $row) && |
| 372 | array_key_exists('civicrm_contact_id', $row) |
| 373 | ) { |
| 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 |
| 377 | ); |
| 378 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; |
| 379 | $entryFound = TRUE; |
| 380 | } |
| 381 | |
| 382 | // handle country |
| 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); |
| 386 | } |
| 387 | $entryFound = TRUE; |
| 388 | } |
| 389 | |
| 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); |
| 393 | } |
| 394 | $entryFound = TRUE; |
| 395 | } |
| 396 | |
| 397 | // skip looking further in rows, if first row itself doesn't |
| 398 | // have the column we need |
| 399 | if (!$entryFound) { |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |