| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for relationship |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Contact_Form_Relationship extends CRM_Core_Form { |
| 41 | |
| 42 | /** |
| 43 | * max number of contacts we will display for a relationship |
| 44 | */ |
| 45 | CONST MAX_RELATIONSHIPS = 50; |
| 46 | |
| 47 | /** |
| 48 | * The relationship id, used when editing the relationship |
| 49 | * |
| 50 | * @var int |
| 51 | */ |
| 52 | protected $_relationshipId; |
| 53 | |
| 54 | /** |
| 55 | * The contact id, used when add/edit relationship |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
| 59 | protected $_contactId; |
| 60 | |
| 61 | /** |
| 62 | * This is a string which is either a_b or b_a used to determine the relationship between to contacts |
| 63 | * |
| 64 | */ |
| 65 | protected $_rtype; |
| 66 | |
| 67 | /** |
| 68 | * This is a string which is used to determine the relationship between to contacts |
| 69 | * |
| 70 | */ |
| 71 | protected $_rtypeId; |
| 72 | |
| 73 | /** |
| 74 | * Display name of contact a |
| 75 | * |
| 76 | */ |
| 77 | protected $_display_name_a; |
| 78 | |
| 79 | /** |
| 80 | * Display name of contact b |
| 81 | * |
| 82 | */ |
| 83 | protected $_display_name_b; |
| 84 | |
| 85 | /** |
| 86 | * The relationship type id |
| 87 | * |
| 88 | * @var int |
| 89 | */ |
| 90 | protected $_relationshipTypeId; |
| 91 | |
| 92 | /** |
| 93 | * an array of all relationship names |
| 94 | * |
| 95 | * @var array |
| 96 | */ |
| 97 | protected $_allRelationshipNames; |
| 98 | |
| 99 | /** |
| 100 | * The relationship values if Updating relationship |
| 101 | */ |
| 102 | protected $_values; |
| 103 | |
| 104 | /** |
| 105 | * casid if it called from case context |
| 106 | */ |
| 107 | protected $_caseId; |
| 108 | |
| 109 | function preProcess() { |
| 110 | //custom data related code |
| 111 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
| 112 | $this->assign('cdType', FALSE); |
| 113 | if ($this->_cdType) { |
| 114 | $this->assign('cdType', TRUE); |
| 115 | return CRM_Custom_Form_CustomData::preProcess($this); |
| 116 | } |
| 117 | |
| 118 | $this->_contactId = $this->get('contactId'); |
| 119 | |
| 120 | $this->_relationshipId = $this->get('id'); |
| 121 | |
| 122 | $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); |
| 123 | |
| 124 | $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); |
| 125 | |
| 126 | $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); |
| 127 | |
| 128 | $this->assign('sort_name_a', $this->_display_name_a); |
| 129 | CRM_Utils_System::setTitle(ts('Relationships for') . ' ' . $this->_display_name_a); |
| 130 | |
| 131 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); |
| 132 | |
| 133 | //get the relationship values. |
| 134 | $this->_values = array(); |
| 135 | if ($this->_relationshipId) { |
| 136 | $params = array('id' => $this->_relationshipId); |
| 137 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); |
| 138 | } |
| 139 | |
| 140 | if (!$this->_rtypeId) { |
| 141 | $params = $this->controller->exportValues($this->_name); |
| 142 | if (isset($params['relationship_type_id'])) { |
| 143 | $this->_rtypeId = $params['relationship_type_id']; |
| 144 | } |
| 145 | elseif (!empty($this->_values)) { |
| 146 | $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | //get the relationship type id |
| 151 | $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); |
| 152 | |
| 153 | //get the relationship type |
| 154 | if (!$this->_rtype) { |
| 155 | $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); |
| 156 | } |
| 157 | $this->assign('rtype', $this->_rtype); |
| 158 | |
| 159 | |
| 160 | //use name as it remain constant, CRM-3336 |
| 161 | $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); |
| 162 | |
| 163 | // when custom data is included in this page |
| 164 | if (CRM_Utils_Array::value('hidden_custom', $_POST)) { |
| 165 | CRM_Custom_Form_CustomData::preProcess($this); |
| 166 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 167 | CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This function sets the default values for the form. Relationship that in edit/view mode |
| 173 | * the default values are retrieved from the database |
| 174 | * |
| 175 | * @access public |
| 176 | * |
| 177 | * @return None |
| 178 | */ |
| 179 | function setDefaultValues() { |
| 180 | if ($this->_cdType) { |
| 181 | return CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 182 | } |
| 183 | |
| 184 | $defaults = array(); |
| 185 | |
| 186 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 187 | if (!empty($this->_values)) { |
| 188 | $defaults['relationship_type_id'] = $this->_rtypeId; |
| 189 | if (CRM_Utils_Array::value('start_date', $this->_values)) { |
| 190 | list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults($this->_values['start_date']); |
| 191 | } |
| 192 | if (CRM_Utils_Array::value('end_date', $this->_values)) { |
| 193 | list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_values['end_date']); |
| 194 | } |
| 195 | $defaults['description'] = CRM_Utils_Array::value('description', $this->_values); |
| 196 | $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values); |
| 197 | $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $this->_values); |
| 198 | $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $this->_values); |
| 199 | $contact = new CRM_Contact_DAO_Contact(); |
| 200 | if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) { |
| 201 | $contact->id = $this->_values['contact_id_b']; |
| 202 | } |
| 203 | else { |
| 204 | $contact->id = $this->_values['contact_id_a']; |
| 205 | } |
| 206 | if ($contact->find(TRUE)) { |
| 207 | $this->_display_name_b = $contact->display_name; |
| 208 | $this->assign('sort_name_b', $this->_display_name_b); |
| 209 | |
| 210 | //is current employee/employer. |
| 211 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of' && |
| 212 | $contact->id == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id') |
| 213 | ) { |
| 214 | $defaults['is_current_employer'] = 1; |
| 215 | $this->_values['current_employee_id'] = $this->_contactId; |
| 216 | $this->_values['current_employer_id'] = $contact->id; |
| 217 | } |
| 218 | elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of' && |
| 219 | $this->_contactId == $contact->employer_id |
| 220 | ) { |
| 221 | $defaults['is_current_employer'] = 1; |
| 222 | $this->_values['current_employee_id'] = $contact->id; |
| 223 | $this->_values['current_employer_id'] = $this->_contactId; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | $relationshipID = $this->_values['id']; |
| 228 | $query = "SELECT id, note FROM civicrm_note where entity_table = 'civicrm_relationship' and entity_id = $relationshipID order by modified_date desc"; |
| 229 | $dao = new CRM_Core_DAO(); |
| 230 | $dao->query($query); |
| 231 | if ($dao->fetch($query)) { |
| 232 | $defaults['note'] = $dao->note; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | else { |
| 237 | $defaults['is_active'] = 1; |
| 238 | $defaults['relationship_type_id'] = $this->_rtypeId; |
| 239 | } |
| 240 | |
| 241 | $this->_enabled = $defaults['is_active']; |
| 242 | return $defaults; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * This function is used to add the rules for form. |
| 247 | * |
| 248 | * @return None |
| 249 | * @access public |
| 250 | */ |
| 251 | function addRules() { |
| 252 | if ($this->_cdType) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
| 257 | $this->addRule('relationship_type_id', ts('Please select a relationship type.'), 'required'); |
| 258 | |
| 259 | // add a form rule only when creating a new relationship |
| 260 | // edit is severely limited, so add a simpleer form rule |
| 261 | if ($this->_action & CRM_Core_Action::ADD) { |
| 262 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'formRule'), $this); |
| 263 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); |
| 264 | } |
| 265 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
| 266 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Function to build the form |
| 273 | * |
| 274 | * @return None |
| 275 | * @access public |
| 276 | */ |
| 277 | public function buildQuickForm() { |
| 278 | if ($this->_cdType) { |
| 279 | return CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 280 | } |
| 281 | |
| 282 | $relTypeID = explode('_', $this->_rtypeId, 3); |
| 283 | |
| 284 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 285 | |
| 286 | $this->addButtons(array( |
| 287 | array( |
| 288 | 'type' => 'next', |
| 289 | 'name' => ts('Delete'), |
| 290 | 'isDefault' => TRUE, |
| 291 | ), |
| 292 | array( |
| 293 | 'type' => 'cancel', |
| 294 | 'name' => ts('Cancel'), |
| 295 | ), |
| 296 | ) |
| 297 | ); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | $callAjax = $this->get('callAjax'); |
| 302 | |
| 303 | $searchRows = NULL; |
| 304 | if (!$callAjax) { |
| 305 | $searchRows = $this->get('searchRows'); |
| 306 | } |
| 307 | else { |
| 308 | |
| 309 | $this->addElement('hidden', 'store_contacts', '', array('id' => 'store_contacts')); |
| 310 | $sourceUrl = 'snippet=4&relType=' . $this->get('relType'); |
| 311 | $sourceUrl .= '&relContact=' . $this->get('relContact'); |
| 312 | $sourceUrl .= '&cid=' . $this->_contactId; |
| 313 | |
| 314 | $this->assign('searchCount', TRUE); |
| 315 | |
| 316 | // To handle employee of and employer of |
| 317 | if (!empty($this->_relationshipTypeId) && |
| 318 | !empty($this->_rtype) |
| 319 | ) { |
| 320 | $sourceUrl .= '&typeName=' . $this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"]; |
| 321 | } |
| 322 | $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/relationshipcontacts', $sourceUrl, FALSE, NULL, FALSE)); |
| 323 | } |
| 324 | |
| 325 | $this->assign('callAjax', $callAjax); |
| 326 | $this->_callAjax = $callAjax; |
| 327 | |
| 328 | $this->addElement('select', |
| 329 | 'relationship_type_id', |
| 330 | ts('Relationship Type'), |
| 331 | array( |
| 332 | '' => ts('- select -')) + |
| 333 | CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, |
| 334 | $this->_rtype, |
| 335 | $this->_relationshipId, |
| 336 | NULL, FALSE, 'label' |
| 337 | ) |
| 338 | ); |
| 339 | |
| 340 | // add a ajax facility for searching contacts |
| 341 | $dataUrl = CRM_Utils_System::url('civicrm/ajax/search', 'reset=1', TRUE, NULL, FALSE); |
| 342 | $this->assign('dataUrl', $dataUrl); |
| 343 | CRM_Contact_Form_NewContact::buildQuickForm($this); |
| 344 | |
| 345 | $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate')); |
| 346 | $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate')); |
| 347 | $this->addElement('checkbox', 'is_active', ts('Enabled?'), NULL, NULL); |
| 348 | |
| 349 | $this->addElement('checkbox', 'is_permission_a_b', ts('Permission for contact a to view and update information for contact b'), NULL); |
| 350 | $this->addElement('checkbox', 'is_permission_b_a', ts('permission for contact b to view and update information for contact a'), NULL); |
| 351 | |
| 352 | $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Relationship', 'description')); |
| 353 | |
| 354 | CRM_Contact_Form_Edit_Notes::buildQuickForm($this); |
| 355 | |
| 356 | $searchCount = $this->get('searchCount'); |
| 357 | $duplicateRelationship = $this->get('duplicateRelationship'); |
| 358 | $searchDone = $this->get('searchDone'); |
| 359 | |
| 360 | $isEmployeeOf = $isEmployerOf = FALSE; |
| 361 | if (!empty($this->_relationshipTypeId) && |
| 362 | !empty($this->_rtype) |
| 363 | ) { |
| 364 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { |
| 365 | $isEmployeeOf = TRUE; |
| 366 | } |
| 367 | elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') { |
| 368 | $isEmployerOf = TRUE; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | $employers = $checkBoxes = $employees = array(); |
| 373 | if ($searchRows) { |
| 374 | foreach ($searchRows as $id => $row) { |
| 375 | $checkBoxes[$id] = $this->createElement('checkbox', $id, NULL, ''); |
| 376 | if ($isEmployeeOf) { |
| 377 | $employers[$id] = $this->createElement('radio', NULL, $id, NULL, $id); |
| 378 | } |
| 379 | elseif ($isEmployerOf) { |
| 380 | $employees[$id] = $this->createElement('checkbox', $id, NULL, ''); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | $this->addGroup($checkBoxes, 'contact_check'); |
| 385 | $this->assign('searchRows', $searchRows); |
| 386 | } |
| 387 | |
| 388 | if ($isEmployeeOf) { |
| 389 | $this->assign('isEmployeeOf', $isEmployeeOf); |
| 390 | if (!$callAjax) { |
| 391 | $this->addGroup($employers, 'employee_of'); |
| 392 | } |
| 393 | } |
| 394 | elseif ($isEmployerOf) { |
| 395 | $this->assign('isEmployerOf', $isEmployerOf); |
| 396 | if (!$callAjax) { |
| 397 | $this->addGroup($employees, 'employer_of'); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if ($callAjax && ($isEmployeeOf || $isEmployerOf)) { |
| 402 | $this->addElement('hidden', 'store_employers', '', array('id' => 'store_employers')); |
| 403 | } |
| 404 | |
| 405 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 406 | $this->addElement('checkbox', 'is_current_employer'); |
| 407 | } |
| 408 | |
| 409 | $this->assign('duplicateRelationship', $duplicateRelationship); |
| 410 | $this->assign('searchCount', $searchCount); |
| 411 | $this->assign('searchDone', $searchDone); |
| 412 | |
| 413 | if ($this->get('contact_type')) { |
| 414 | $typeLabel = CRM_Contact_BAO_ContactType::getLabel($this->get('contact_type')); |
| 415 | $this->assign('contact_type', $this->get('contact_type')); |
| 416 | $this->assign('contact_type_display', $typeLabel); |
| 417 | } |
| 418 | |
| 419 | if ($searchDone) { |
| 420 | $searchBtn = ts('Search Again'); |
| 421 | } |
| 422 | else { |
| 423 | $searchBtn = ts('Search'); |
| 424 | } |
| 425 | $this->addElement('submit', $this->getButtonName('refresh'), $searchBtn, array('class' => 'form-submit', 'id' => 'search-button')); |
| 426 | $this->addElement('submit', $this->getButtonName('refresh', 'save'), 'Quick Save', array('class' => 'form-submit', 'id' => 'quick-save')); |
| 427 | $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'form-submit')); |
| 428 | |
| 429 | $this->addElement('submit', $this->getButtonName('refresh', 'savedetails'), 'Save Relationship', array('class' => 'form-submit hiddenElement', 'id' => 'details-save')); |
| 430 | $this->addElement('checkbox', 'add_current_employer', ts('Current Employer'), NULL); |
| 431 | $this->addElement('checkbox', 'add_current_employee', ts('Current Employee'), NULL); |
| 432 | |
| 433 | //need to assign custom data type and subtype to the template |
| 434 | $this->assign('customDataType', 'Relationship'); |
| 435 | $this->assign('customDataSubType', $this->_relationshipTypeId); |
| 436 | $this->assign('entityID', $this->_relationshipId); |
| 437 | |
| 438 | // make this form an upload since we dont know if the custom data injected dynamically |
| 439 | // is of type file etc $uploadNames = $this->get( |
| 440 | // 'uploadNames' ); |
| 441 | $buttonParams = array( |
| 442 | 'type' => 'upload', |
| 443 | 'name' => ts('Save Relationship'), |
| 444 | 'isDefault' => TRUE, |
| 445 | ); |
| 446 | if ($callAjax) { |
| 447 | $buttonParams['js'] = array('onclick' => ' submitAjaxData();'); |
| 448 | } |
| 449 | |
| 450 | $this->addButtons(array( |
| 451 | $buttonParams |
| 452 | , |
| 453 | array( |
| 454 | 'type' => 'cancel', |
| 455 | 'name' => ts('Cancel'), |
| 456 | ), |
| 457 | ) |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * This function is called when the form is submitted |
| 463 | * |
| 464 | * @access public |
| 465 | * |
| 466 | * @return None |
| 467 | */ |
| 468 | public function postProcess() { |
| 469 | // store the submitted values in an array |
| 470 | $params = $this->controller->exportValues($this->_name); |
| 471 | $quickSave = FALSE; |
| 472 | if (CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) || |
| 473 | CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST) |
| 474 | ) { |
| 475 | $quickSave = TRUE; |
| 476 | } |
| 477 | |
| 478 | $this->set('searchDone', 0); |
| 479 | $this->set('callAjax', FALSE); |
| 480 | if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST) || $quickSave) { |
| 481 | if (is_numeric($params['contact_select_id'][1])) { |
| 482 | if ($quickSave) { |
| 483 | $params['contact_check'] = array($params['contact_select_id'][1] => 1); |
| 484 | } |
| 485 | } |
| 486 | else { |
| 487 | $this->set('callAjax', TRUE); |
| 488 | $this->set('relType', $params['relationship_type_id']); |
| 489 | $this->set('relContact', $params['contact'][1]); |
| 490 | $quickSave = FALSE; |
| 491 | } |
| 492 | $this->set('searchDone', 1); |
| 493 | if (!$quickSave) { |
| 494 | return; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // action is taken depending upon the mode |
| 499 | $ids = array(); |
| 500 | $ids['contact'] = $this->_contactId; |
| 501 | |
| 502 | // modify params for ajax call |
| 503 | $this->modifyParams($params); |
| 504 | |
| 505 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 506 | CRM_Contact_BAO_Relationship::del($this->_relationshipId); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $params['relationship_type_id']); |
| 511 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 512 | $ids['relationship'] = $this->_relationshipId; |
| 513 | $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId); |
| 514 | $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a; |
| 515 | |
| 516 | //if relationship type change and previously it was |
| 517 | //employer / emplyee relationship with current employer |
| 518 | //than clear the current employer. CRM-3235. |
| 519 | |
| 520 | //make sure we has to have employer id before firing queries, CRM-7306 |
| 521 | $employerId = CRM_Utils_Array::value('current_employee_id', $this->_values); |
| 522 | $isDisabled = TRUE; |
| 523 | if (CRM_Utils_Array::value('is_active', $params)) { |
| 524 | $isDisabled = FALSE; |
| 525 | } |
| 526 | $relChanged = TRUE; |
| 527 | if ($relationshipTypeId == $this->_values['relationship_type_id']) { |
| 528 | $relChanged = FALSE; |
| 529 | } |
| 530 | if ($employerId && ($isDisabled || $relChanged)) { |
| 531 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['current_employee_id']); |
| 532 | } |
| 533 | |
| 534 | //if field key doesn't exists in params that means the user has unchecked checkbox, |
| 535 | //hence fill FALSE to params |
| 536 | $params['is_active'] = $isDisabled ? FALSE : TRUE; |
| 537 | $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, FALSE); |
| 538 | $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, FALSE); |
| 539 | } |
| 540 | elseif ($quickSave) { |
| 541 | if (CRM_Utils_Array::value('add_current_employee', $params) && |
| 542 | $this->_allRelationshipNames[$relationshipTypeId]['name_a_b'] == 'Employee of' |
| 543 | ) { |
| 544 | $params['employee_of'] = $params['contact_select_id'][1]; |
| 545 | } |
| 546 | elseif (CRM_Utils_Array::value('add_current_employer', $params) && |
| 547 | $this->_allRelationshipNames[$relationshipTypeId]['name_b_a'] == 'Employer of' |
| 548 | ) { |
| 549 | $params['employer_of'] = array($params['contact_select_id'][1] => 1); |
| 550 | } |
| 551 | if (!$this->_rtype) { |
| 552 | $this->_rtype = str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (!$params['note']) { |
| 557 | $params['note'] = 'null'; |
| 558 | } |
| 559 | $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE); |
| 560 | $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE); |
| 561 | |
| 562 | //special case to handle if all checkboxes are unchecked |
| 563 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId); |
| 564 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
| 565 | $customFields, |
| 566 | $this->_relationshipId, |
| 567 | 'Relationship' |
| 568 | ); |
| 569 | |
| 570 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids); |
| 571 | |
| 572 | // if this is called from case view, |
| 573 | //create an activity for case role removal.CRM-4480 |
| 574 | if ($this->_caseId) { |
| 575 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); |
| 576 | } |
| 577 | |
| 578 | $status = ''; |
| 579 | if ($valid) { |
| 580 | CRM_Core_Session::setStatus(ts('New relationship created.', array('count' => $valid, 'plural' => '%count new relationships created.')), ts('Saved'), 'success'); |
| 581 | } |
| 582 | if ($invalid) { |
| 583 | CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid target contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid target contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records'))); |
| 584 | } |
| 585 | if ($duplicate) { |
| 586 | CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array('count' => $duplicate, 'plural' => '%count relationships were not created because they already exist.')), ts('%count duplicate relationship', array('count' => $duplicate, 'plural' => '%count duplicate relationships'))); |
| 587 | } |
| 588 | if ($saved) { |
| 589 | CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); |
| 590 | } |
| 591 | |
| 592 | if (!empty($relationshipIds)) { |
| 593 | $note = new CRM_Core_DAO_Note(); |
| 594 | $note->entity_id = $relationshipIds[0]; |
| 595 | $note->entity_table = 'civicrm_relationship'; |
| 596 | $noteIds = array(); |
| 597 | if ($note->find(TRUE)) { |
| 598 | $id = $note->id; |
| 599 | $noteIds['id'] = $id; |
| 600 | } |
| 601 | |
| 602 | $noteParams = array( |
| 603 | 'entity_id' => $relationshipIds[0], |
| 604 | 'entity_table' => 'civicrm_relationship', |
| 605 | 'note' => $params['note'], |
| 606 | 'contact_id' => $this->_contactId, |
| 607 | ); |
| 608 | CRM_Core_BAO_Note::add($noteParams, $noteIds); |
| 609 | |
| 610 | $params['relationship_ids'] = $relationshipIds; |
| 611 | } |
| 612 | |
| 613 | // Membership for related contacts CRM-1657 |
| 614 | if (CRM_Core_Permission::access('CiviMember') && (!$duplicate)) { |
| 615 | if ($this->_action & CRM_Core_Action::ADD) { |
| 616 | CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, |
| 617 | $params, $ids, |
| 618 | $this->_action |
| 619 | ); |
| 620 | } |
| 621 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
| 622 | //fixes for CRM-7985 |
| 623 | //only if the relationship has been toggled to enable /disable |
| 624 | if (CRM_Utils_Array::value('is_active', $params) != $this->_enabled) { |
| 625 | $active = CRM_Utils_Array::value('is_active', $params) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; |
| 626 | CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_relationshipId, $active); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | //handle current employee/employer relationship, CRM-3532 |
| 631 | if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { |
| 632 | $orgId = NULL; |
| 633 | if (CRM_Utils_Array::value('employee_of', $params)) { |
| 634 | $orgId = $params['employee_of']; |
| 635 | } |
| 636 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
| 637 | if (CRM_Utils_Array::value('is_current_employer', $params) && |
| 638 | CRM_Utils_Array::value('is_active', $params) |
| 639 | ) { |
| 640 | if (CRM_Utils_Array::value('contactTarget', $ids) != |
| 641 | CRM_Utils_Array::value('current_employer_id', $this->_values) |
| 642 | ) { |
| 643 | $orgId = CRM_Utils_Array::value('contactTarget', $ids); |
| 644 | } |
| 645 | } |
| 646 | elseif (CRM_Utils_Array::value('contactTarget', $ids) == |
| 647 | CRM_Utils_Array::value('current_employer_id', $this->_values) |
| 648 | ) { |
| 649 | //clear current employer. |
| 650 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_contactId); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | //set current employer |
| 655 | if ($orgId) { |
| 656 | $currentEmpParams[$this->_contactId] = $orgId; |
| 657 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); |
| 658 | } |
| 659 | } |
| 660 | elseif ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') { |
| 661 | $individualIds = array(); |
| 662 | if (CRM_Utils_Array::value('employer_of', $params)) { |
| 663 | $individualIds = array_keys($params['employer_of']); |
| 664 | } |
| 665 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
| 666 | if (CRM_Utils_Array::value('is_current_employer', $params)) { |
| 667 | if (CRM_Utils_Array::value('contactTarget', $ids) != |
| 668 | CRM_Utils_Array::value('current_employee_id', $this->_values) |
| 669 | ) { |
| 670 | $individualIds[] = CRM_Utils_Array::value('contactTarget', $ids); |
| 671 | } |
| 672 | } |
| 673 | elseif (CRM_Utils_Array::value('contactTarget', $ids) == |
| 674 | CRM_Utils_Array::value('current_employee_id', $this->_values) |
| 675 | ) { |
| 676 | // clear current employee |
| 677 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($ids['contactTarget']); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | //set current employee |
| 682 | if (!empty($individualIds)) { |
| 683 | |
| 684 | //build the employee params. |
| 685 | foreach ($individualIds as $key => $Id) { |
| 686 | $currentEmpParams[$Id] = $this->_contactId; |
| 687 | } |
| 688 | |
| 689 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if ($quickSave) { |
| 694 | $session = CRM_Core_Session::singleton(); |
| 695 | CRM_Utils_System::redirect($session->popUserContext()); |
| 696 | } |
| 697 | } |
| 698 | //end of function |
| 699 | |
| 700 | /** |
| 701 | * function for validation |
| 702 | * |
| 703 | * @param array $params (reference ) an assoc array of name/value pairs |
| 704 | * |
| 705 | * @return mixed true or array of errors |
| 706 | * @access public |
| 707 | * @static |
| 708 | */ |
| 709 | static function formRule($params, $files, $form) { |
| 710 | // hack, no error check for refresh |
| 711 | if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST) || |
| 712 | CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) || |
| 713 | CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST) |
| 714 | ) { |
| 715 | return TRUE; |
| 716 | } |
| 717 | |
| 718 | $form->modifyParams($params); |
| 719 | |
| 720 | $ids = array(); |
| 721 | $session = CRM_Core_Session::singleton(); |
| 722 | $ids['contact'] = $form->get('contactId'); |
| 723 | $ids['relationship'] = $form->get('relationshipId'); |
| 724 | |
| 725 | $errors = array(); |
| 726 | $employerId = NULL; |
| 727 | if (CRM_Utils_Array::value('contact_check', $params) && is_array($params['contact_check'])) { |
| 728 | foreach ($params['contact_check'] as $cid => $dontCare) { |
| 729 | $message = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $cid); |
| 730 | if ($message) { |
| 731 | $errors['relationship_type_id'] = $message; |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | if ($cid == CRM_Utils_Array::value('employee_of', $params)) { |
| 736 | $employerId = $cid; |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | else { |
| 741 | if ($form->_callAjax) { |
| 742 | $errors['store_contacts'] = ts('Select select at least one contact from Target Contact(s).'); |
| 743 | } |
| 744 | else { |
| 745 | $errors['contact_check'] = ts('Please select at least one contact.'); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (CRM_Utils_Array::value('employee_of', $params) && |
| 750 | !$employerId |
| 751 | ) { |
| 752 | if ($form->_callAjax) { |
| 753 | $errors['store_employer'] = ts('Current employer should be one of the selected contacts.'); |
| 754 | } |
| 755 | else { |
| 756 | $errors['employee_of'] = ts('Current employer should be one of the selected contacts.'); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (CRM_Utils_Array::value('employer_of', $params) && |
| 761 | CRM_Utils_Array::value('contact_check', $params) && |
| 762 | array_diff(array_keys($params['employer_of']), array_keys($params['contact_check'])) |
| 763 | ) { |
| 764 | if ($form->_callAjax) { |
| 765 | $errors['store_employer'] = ts('Current employee should be among the selected contacts.'); |
| 766 | } |
| 767 | else { |
| 768 | $errors['employer_of'] = ts('Current employee should be among the selected contacts.'); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return empty($errors) ? TRUE : $errors; |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * function for date validation |
| 777 | * |
| 778 | * @param array $params (reference ) an assoc array of name/value pairs |
| 779 | * |
| 780 | * @return mixed true or array of errors |
| 781 | * @access public |
| 782 | * @static |
| 783 | */ |
| 784 | static function dateRule($params) { |
| 785 | $errors = array(); |
| 786 | |
| 787 | // check start and end date |
| 788 | if (CRM_Utils_Array::value('start_date', $params) && |
| 789 | CRM_Utils_Array::value('end_date', $params) |
| 790 | ) { |
| 791 | $start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params)); |
| 792 | $end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params)); |
| 793 | if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) { |
| 794 | $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.'); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return empty($errors) ? TRUE : $errors; |
| 799 | } |
| 800 | |
| 801 | function modifyParams(&$params) { |
| 802 | if (!$this->_callAjax) { |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | if (CRM_Utils_Array::value('store_contacts', $params)) { |
| 807 | $storedContacts = array(); |
| 808 | foreach (explode(',', $params['store_contacts']) as $value) { |
| 809 | if ($value) { |
| 810 | $storedContacts[$value] = 1; |
| 811 | } |
| 812 | } |
| 813 | $params['contact_check'] = $storedContacts; |
| 814 | } |
| 815 | |
| 816 | if (CRM_Utils_Array::value('store_employers', $params)) { |
| 817 | $employeeContacts = array(); |
| 818 | foreach (explode(',', $params['store_employers']) as $value) { |
| 819 | if ($value) { |
| 820 | $employeeContacts[$value] = $value; |
| 821 | } |
| 822 | } |
| 823 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { |
| 824 | $params['employee_of'] = current($employeeContacts); |
| 825 | } |
| 826 | else { |
| 827 | $params['employer_of'] = $employeeContacts; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |