| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Report_Form_Contact_CurrentEmployer extends CRM_Report_Form { |
| 36 | |
| 37 | protected $_summary = NULL; |
| 38 | |
| 39 | protected $_customGroupExtends = array( |
| 40 | 'Contact', |
| 41 | 'Individual', |
| 42 | ); |
| 43 | |
| 44 | public $_drilldownReport = array('contact/detail' => 'Link to Detail Report'); |
| 45 | |
| 46 | /** |
| 47 | */ |
| 48 | /** |
| 49 | */ |
| 50 | public function __construct() { |
| 51 | |
| 52 | $this->_columns = array( |
| 53 | 'civicrm_employer' => array( |
| 54 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 55 | 'fields' => array( |
| 56 | 'organization_name' => array( |
| 57 | 'title' => ts('Employer Name'), |
| 58 | 'required' => TRUE, |
| 59 | 'no_repeat' => TRUE, |
| 60 | ), |
| 61 | 'id' => array( |
| 62 | 'no_display' => TRUE, |
| 63 | 'required' => TRUE, |
| 64 | ), |
| 65 | ), |
| 66 | 'filters' => array( |
| 67 | 'organization_name' => array( |
| 68 | 'title' => ts('Employer Name'), |
| 69 | 'operatorType' => CRM_Report_Form::OP_STRING, |
| 70 | ), |
| 71 | ), |
| 72 | ), |
| 73 | 'civicrm_contact' => array( |
| 74 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 75 | 'fields' => array( |
| 76 | 'sort_name' => array( |
| 77 | 'title' => ts('Employee Name'), |
| 78 | 'required' => TRUE, |
| 79 | ), |
| 80 | 'first_name' => array( |
| 81 | 'title' => ts('First Name'), |
| 82 | ), |
| 83 | 'middle_name' => array( |
| 84 | 'title' => ts('Middle Name'), |
| 85 | ), |
| 86 | 'last_name' => array( |
| 87 | 'title' => ts('Last Name'), |
| 88 | ), |
| 89 | 'id' => array( |
| 90 | 'no_display' => TRUE, |
| 91 | 'required' => TRUE, |
| 92 | ), |
| 93 | 'job_title' => array( |
| 94 | 'title' => ts('Job Title'), |
| 95 | 'default' => TRUE, |
| 96 | ), |
| 97 | 'gender_id' => array( |
| 98 | 'title' => ts('Gender'), |
| 99 | ), |
| 100 | 'birth_date' => array( |
| 101 | 'title' => ts('Birth Date'), |
| 102 | ), |
| 103 | 'age' => array( |
| 104 | 'title' => ts('Age'), |
| 105 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', |
| 106 | ), |
| 107 | 'contact_type' => array( |
| 108 | 'title' => ts('Contact Type'), |
| 109 | ), |
| 110 | 'contact_sub_type' => array( |
| 111 | 'title' => ts('Contact Subtype'), |
| 112 | ), |
| 113 | ), |
| 114 | 'filters' => array( |
| 115 | 'sort_name' => array('title' => ts('Employee Name')), |
| 116 | 'source' => array( |
| 117 | 'title' => ts('Contact Source'), |
| 118 | 'type' => CRM_Utils_Type::T_STRING, |
| 119 | ), |
| 120 | 'id' => array( |
| 121 | 'title' => ts('Contact ID'), |
| 122 | 'no_display' => TRUE, |
| 123 | ), |
| 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'), |
| 128 | ), |
| 129 | 'birth_date' => array( |
| 130 | 'title' => ts('Birth Date'), |
| 131 | 'operatorType' => CRM_Report_Form::OP_DATE, |
| 132 | ), |
| 133 | 'contact_type' => array( |
| 134 | 'title' => ts('Contact Type'), |
| 135 | ), |
| 136 | 'contact_sub_type' => array( |
| 137 | 'title' => ts('Contact Subtype'), |
| 138 | ), |
| 139 | ), |
| 140 | 'grouping' => 'contact-fields', |
| 141 | ), |
| 142 | 'civicrm_relationship' => array( |
| 143 | 'dao' => 'CRM_Contact_DAO_Relationship', |
| 144 | 'fields' => array( |
| 145 | 'start_date' => array( |
| 146 | 'title' => ts('Employee Since'), |
| 147 | 'default' => TRUE, |
| 148 | ), |
| 149 | ), |
| 150 | 'filters' => array( |
| 151 | 'start_date' => array( |
| 152 | 'title' => ts('Employee Since'), |
| 153 | 'operatorType' => CRM_Report_Form::OP_DATE, |
| 154 | 'type' => CRM_Utils_Type::T_DATE, |
| 155 | ), |
| 156 | ), |
| 157 | ), |
| 158 | 'civicrm_phone' => array( |
| 159 | 'dao' => 'CRM_Core_DAO_Phone', |
| 160 | 'grouping' => 'contact-fields', |
| 161 | 'fields' => array( |
| 162 | 'phone' => array( |
| 163 | 'title' => ts('Phone'), |
| 164 | 'default' => TRUE, |
| 165 | ), |
| 166 | ), |
| 167 | ), |
| 168 | 'civicrm_email' => array( |
| 169 | 'dao' => 'CRM_Core_DAO_Email', |
| 170 | 'grouping' => 'contact-fields', |
| 171 | 'fields' => array( |
| 172 | 'email' => array( |
| 173 | 'title' => ts('Email'), |
| 174 | 'default' => TRUE, |
| 175 | ), |
| 176 | ), |
| 177 | ), |
| 178 | 'civicrm_address' => array( |
| 179 | 'dao' => 'CRM_Core_DAO_Address', |
| 180 | 'grouping' => 'contact-fields', |
| 181 | 'fields' => array( |
| 182 | 'street_address' => NULL, |
| 183 | 'city' => NULL, |
| 184 | 'postal_code' => NULL, |
| 185 | 'state_province_id' => array( |
| 186 | 'title' => ts('State/Province'), |
| 187 | ), |
| 188 | 'country_id' => array( |
| 189 | 'title' => ts('Country'), |
| 190 | ), |
| 191 | ), |
| 192 | 'filters' => array( |
| 193 | 'country_id' => array( |
| 194 | 'title' => ts('Country'), |
| 195 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 196 | 'options' => CRM_Core_PseudoConstant::country(NULL, FALSE), |
| 197 | ), |
| 198 | 'state_province_id' => array( |
| 199 | 'title' => ts('State/Province'), |
| 200 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 201 | 'options' => CRM_Core_PseudoConstant::stateProvince(), |
| 202 | ), |
| 203 | ), |
| 204 | ), |
| 205 | 'civicrm_group' => array( |
| 206 | 'dao' => 'CRM_Contact_DAO_Group', |
| 207 | 'alias' => 'cgroup', |
| 208 | 'filters' => array( |
| 209 | 'gid' => array( |
| 210 | 'name' => 'group_id', |
| 211 | 'title' => ts('Group'), |
| 212 | 'group' => TRUE, |
| 213 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 214 | 'options' => CRM_Core_PseudoConstant::staticGroup(), |
| 215 | ), |
| 216 | ), |
| 217 | ), |
| 218 | ); |
| 219 | |
| 220 | $this->_tagFilter = TRUE; |
| 221 | parent::__construct(); |
| 222 | } |
| 223 | |
| 224 | public function preProcess() { |
| 225 | parent::preProcess(); |
| 226 | } |
| 227 | |
| 228 | public function select() { |
| 229 | |
| 230 | $select = $this->_columnHeaders = array(); |
| 231 | |
| 232 | foreach ($this->_columns as $tableName => $table) { |
| 233 | if (array_key_exists('fields', $table)) { |
| 234 | foreach ($table['fields'] as $fieldName => $field) { |
| 235 | if (!empty($field['required']) || |
| 236 | !empty($this->_params['fields'][$fieldName]) |
| 237 | ) { |
| 238 | |
| 239 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; |
| 240 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); |
| 241 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
| 248 | } |
| 249 | |
| 250 | public function from() { |
| 251 | $this->_from = " |
| 252 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} |
| 253 | |
| 254 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']} |
| 255 | ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id |
| 256 | |
| 257 | {$this->_aclFrom} |
| 258 | LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} |
| 259 | ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id |
| 260 | AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id |
| 261 | AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4) |
| 262 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
| 263 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id |
| 264 | AND {$this->_aliases['civicrm_address']}.is_primary = 1 ) |
| 265 | |
| 266 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
| 267 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id |
| 268 | AND {$this->_aliases['civicrm_phone']}.is_primary = 1) |
| 269 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
| 270 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id |
| 271 | AND {$this->_aliases['civicrm_email']}.is_primary = 1) "; |
| 272 | } |
| 273 | |
| 274 | public function where() { |
| 275 | |
| 276 | $clauses = array(); |
| 277 | foreach ($this->_columns as $tableName => $table) { |
| 278 | if (array_key_exists('filters', $table)) { |
| 279 | foreach ($table['filters'] as $fieldName => $field) { |
| 280 | $clause = NULL; |
| 281 | if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE |
| 282 | ) { |
| 283 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
| 284 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
| 285 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); |
| 286 | |
| 287 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']); |
| 288 | } |
| 289 | else { |
| 290 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); |
| 291 | if ($op) { |
| 292 | $clause = $this->whereClause($field, |
| 293 | $op, |
| 294 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), |
| 295 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), |
| 296 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) |
| 297 | ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (!empty($clause)) { |
| 302 | $clauses[$fieldName] = $clause; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (empty($clauses)) { |
| 309 | $this->_where = "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' "; |
| 310 | } |
| 311 | else { |
| 312 | $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses); |
| 313 | } |
| 314 | |
| 315 | if ($this->_aclWhere) { |
| 316 | $this->_where .= " AND {$this->_aclWhere} "; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public function groupBy() { |
| 321 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id"; |
| 322 | } |
| 323 | |
| 324 | public function orderBy() { |
| 325 | $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name"; |
| 326 | } |
| 327 | |
| 328 | public function postProcess() { |
| 329 | // get the acl clauses built before we assemble the query |
| 330 | $this->buildACLClause(array( |
| 331 | $this->_aliases['civicrm_contact'], |
| 332 | $this->_aliases['civicrm_employer'], |
| 333 | )); |
| 334 | parent::postProcess(); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Alter display of rows. |
| 339 | * |
| 340 | * Iterate through the rows retrieved via SQL and make changes for display purposes, |
| 341 | * such as rendering contacts as links. |
| 342 | * |
| 343 | * @param array $rows |
| 344 | * Rows generated by SQL, with an array for each row. |
| 345 | */ |
| 346 | public function alterDisplay(&$rows) { |
| 347 | $checkList = array(); |
| 348 | $entryFound = FALSE; |
| 349 | |
| 350 | foreach ($rows as $rowNum => $row) { |
| 351 | |
| 352 | // convert employer name to links |
| 353 | if (array_key_exists('civicrm_employer_organization_name', $row) && |
| 354 | array_key_exists('civicrm_employer_id', $row) |
| 355 | ) { |
| 356 | $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', |
| 357 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'], |
| 358 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
| 359 | ); |
| 360 | $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url; |
| 361 | $entryFound = TRUE; |
| 362 | } |
| 363 | |
| 364 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { |
| 365 | // not repeat contact display names if it matches with the one |
| 366 | // in previous row |
| 367 | |
| 368 | foreach ($row as $colName => $colVal) { |
| 369 | if (!empty($checkList[$colName]) && is_array($checkList[$colName]) && |
| 370 | in_array($colVal, $checkList[$colName]) |
| 371 | ) { |
| 372 | $rows[$rowNum][$colName] = ""; |
| 373 | } |
| 374 | if (in_array($colName, $this->_noRepeats)) { |
| 375 | $checkList[$colName][] = $colVal; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | //handle gender |
| 381 | if (array_key_exists('civicrm_contact_gender_id', $row)) { |
| 382 | if ($value = $row['civicrm_contact_gender_id']) { |
| 383 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
| 384 | $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value]; |
| 385 | } |
| 386 | $entryFound = TRUE; |
| 387 | } |
| 388 | |
| 389 | // display birthday in the configured custom format |
| 390 | if (array_key_exists('civicrm_contact_birth_date', $row)) { |
| 391 | $birthDate = $row['civicrm_contact_birth_date']; |
| 392 | if ($birthDate) { |
| 393 | $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d'); |
| 394 | } |
| 395 | $entryFound = TRUE; |
| 396 | } |
| 397 | |
| 398 | // convert employee name to links |
| 399 | if (array_key_exists('civicrm_contact_display_name', $row) && |
| 400 | array_key_exists('civicrm_contact_id', $row) |
| 401 | ) { |
| 402 | $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', |
| 403 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'], |
| 404 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
| 405 | ); |
| 406 | $rows[$rowNum]['civicrm_contact_display_name_link'] = $url; |
| 407 | $entryFound = TRUE; |
| 408 | } |
| 409 | |
| 410 | // handle country |
| 411 | if (array_key_exists('civicrm_address_country_id', $row)) { |
| 412 | if ($value = $row['civicrm_address_country_id']) { |
| 413 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); |
| 414 | } |
| 415 | $entryFound = TRUE; |
| 416 | } |
| 417 | |
| 418 | if (array_key_exists('civicrm_address_state_province_id', $row)) { |
| 419 | if ($value = $row['civicrm_address_state_province_id']) { |
| 420 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); |
| 421 | } |
| 422 | $entryFound = TRUE; |
| 423 | } |
| 424 | |
| 425 | // skip looking further in rows, if first row itself doesn't |
| 426 | // have the column we need |
| 427 | if (!$entryFound) { |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | } |