| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 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 | * The relationship id, used when editing the relationship |
| 44 | * |
| 45 | * @var int |
| 46 | */ |
| 47 | public $_relationshipId; |
| 48 | |
| 49 | /** |
| 50 | * The contact id, used when add/edit relationship |
| 51 | * |
| 52 | * @var int |
| 53 | */ |
| 54 | public $_contactId; |
| 55 | |
| 56 | /** |
| 57 | * This is a string which is either a_b or b_a used to determine the relationship between to contacts |
| 58 | * |
| 59 | */ |
| 60 | public $_rtype; |
| 61 | |
| 62 | /** |
| 63 | * This is a string which is used to determine the relationship between to contacts |
| 64 | * |
| 65 | */ |
| 66 | public $_rtypeId; |
| 67 | |
| 68 | /** |
| 69 | * Display name of contact a |
| 70 | * |
| 71 | */ |
| 72 | public $_display_name_a; |
| 73 | |
| 74 | /** |
| 75 | * Display name of contact b |
| 76 | * |
| 77 | */ |
| 78 | public $_display_name_b; |
| 79 | |
| 80 | /** |
| 81 | * The relationship type id |
| 82 | * |
| 83 | * @var int |
| 84 | */ |
| 85 | public $_relationshipTypeId; |
| 86 | |
| 87 | /** |
| 88 | * an array of all relationship names |
| 89 | * |
| 90 | * @var array |
| 91 | */ |
| 92 | public $_allRelationshipNames; |
| 93 | |
| 94 | /** |
| 95 | * @var bool |
| 96 | */ |
| 97 | public $_enabled; |
| 98 | |
| 99 | /** |
| 100 | * @var bool |
| 101 | */ |
| 102 | public $_isCurrentEmployer; |
| 103 | |
| 104 | /** |
| 105 | * @var string |
| 106 | */ |
| 107 | public $_contactType; |
| 108 | |
| 109 | /** |
| 110 | * The relationship values if Updating relationship |
| 111 | */ |
| 112 | public $_values; |
| 113 | |
| 114 | /** |
| 115 | * casid if it called from case context |
| 116 | */ |
| 117 | public $_caseId; |
| 118 | |
| 119 | /** |
| 120 | * @var mixed |
| 121 | */ |
| 122 | public $_cdType; |
| 123 | |
| 124 | function preProcess() { |
| 125 | //custom data related code |
| 126 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
| 127 | $this->assign('cdType', FALSE); |
| 128 | if ($this->_cdType) { |
| 129 | $this->assign('cdType', TRUE); |
| 130 | return CRM_Custom_Form_CustomData::preProcess($this); |
| 131 | } |
| 132 | |
| 133 | $this->_contactId = $this->get('contactId'); |
| 134 | |
| 135 | $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); |
| 136 | |
| 137 | $this->_relationshipId = $this->get('id'); |
| 138 | |
| 139 | $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); |
| 140 | |
| 141 | $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); |
| 142 | |
| 143 | $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); |
| 144 | |
| 145 | $this->assign('display_name_a', $this->_display_name_a); |
| 146 | |
| 147 | // Set page title based on action |
| 148 | switch ($this->_action) { |
| 149 | case CRM_Core_Action::VIEW: |
| 150 | CRM_Utils_System::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a))); |
| 151 | break; |
| 152 | case CRM_Core_Action::ADD: |
| 153 | CRM_Utils_System::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a))); |
| 154 | break; |
| 155 | case CRM_Core_Action::UPDATE: |
| 156 | CRM_Utils_System::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a))); |
| 157 | break; |
| 158 | case CRM_Core_Action::DELETE: |
| 159 | CRM_Utils_System::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a))); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); |
| 164 | |
| 165 | //get the relationship values. |
| 166 | $this->_values = array(); |
| 167 | if ($this->_relationshipId) { |
| 168 | $params = array('id' => $this->_relationshipId); |
| 169 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); |
| 170 | } |
| 171 | |
| 172 | if (!$this->_rtypeId) { |
| 173 | $params = $this->controller->exportValues($this->_name); |
| 174 | if (isset($params['relationship_type_id'])) { |
| 175 | $this->_rtypeId = $params['relationship_type_id']; |
| 176 | } |
| 177 | elseif (!empty($this->_values)) { |
| 178 | $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | //get the relationship type id |
| 183 | $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); |
| 184 | |
| 185 | //get the relationship type |
| 186 | if (!$this->_rtype) { |
| 187 | $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); |
| 188 | } |
| 189 | |
| 190 | //need to assign custom data type and subtype to the template - FIXME: explain why |
| 191 | $this->assign('customDataType', 'Relationship'); |
| 192 | $this->assign('customDataSubType', $this->_relationshipTypeId); |
| 193 | $this->assign('entityID', $this->_relationshipId); |
| 194 | |
| 195 | //use name as it remain constant, CRM-3336 |
| 196 | $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); |
| 197 | |
| 198 | // Current employer? |
| 199 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 200 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { |
| 201 | $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // when custom data is included in this page |
| 206 | if (!empty($_POST['hidden_custom'])) { |
| 207 | CRM_Custom_Form_CustomData::preProcess($this); |
| 208 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 209 | CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * This function sets the default values for the form. Relationship that in edit/view mode |
| 215 | * the default values are retrieved from the database |
| 216 | * |
| 217 | * @access public |
| 218 | * |
| 219 | * @return void |
| 220 | */ |
| 221 | function setDefaultValues() { |
| 222 | if ($this->_cdType) { |
| 223 | return CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 224 | } |
| 225 | |
| 226 | $defaults = array(); |
| 227 | |
| 228 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 229 | if (!empty($this->_values)) { |
| 230 | $defaults['relationship_type_id'] = $this->_rtypeId; |
| 231 | if (!empty($this->_values['start_date'])) { |
| 232 | list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults($this->_values['start_date']); |
| 233 | } |
| 234 | if (!empty($this->_values['end_date'])) { |
| 235 | list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_values['end_date']); |
| 236 | } |
| 237 | $defaults['description'] = CRM_Utils_Array::value('description', $this->_values); |
| 238 | $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values); |
| 239 | |
| 240 | // The javascript on the form will swap these fields if it is a b_a relationship, so we compensate here |
| 241 | $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_' . $this->_rtype, $this->_values); |
| 242 | $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_' . strrev($this->_rtype), $this->_values); |
| 243 | |
| 244 | $defaults['is_current_employer'] = $this->_isCurrentEmployer; |
| 245 | |
| 246 | // Load info about the related contact |
| 247 | $contact = new CRM_Contact_DAO_Contact(); |
| 248 | if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) { |
| 249 | $contact->id = $this->_values['contact_id_b']; |
| 250 | } |
| 251 | else { |
| 252 | $contact->id = $this->_values['contact_id_a']; |
| 253 | } |
| 254 | if ($contact->find(TRUE)) { |
| 255 | $defaults['related_contact_id'] = $contact->id; |
| 256 | $this->_display_name_b = $contact->display_name; |
| 257 | $this->assign('display_name_b', $this->_display_name_b); |
| 258 | } |
| 259 | |
| 260 | $noteParams = array( |
| 261 | 'entity_id' => $this->_relationshipId, |
| 262 | 'entity_table' => 'civicrm_relationship', |
| 263 | 'limit' => 1, |
| 264 | 'version' => 3 |
| 265 | ); |
| 266 | $note = civicrm_api('Note' ,'getsingle', $noteParams); |
| 267 | $defaults['note'] = CRM_Utils_Array::value('note', $note); |
| 268 | } |
| 269 | } |
| 270 | else { |
| 271 | $defaults['is_active'] = $defaults['is_current_employer'] = 1; |
| 272 | $defaults['relationship_type_id'] = $this->_rtypeId; |
| 273 | } |
| 274 | |
| 275 | $this->_enabled = $defaults['is_active']; |
| 276 | return $defaults; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * This function is used to add the rules for form. |
| 281 | * |
| 282 | * @return void |
| 283 | * @access public |
| 284 | */ |
| 285 | function addRules() { |
| 286 | if ($this->_cdType) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
| 291 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Function to build the form |
| 297 | * |
| 298 | * @return void |
| 299 | * @access public |
| 300 | */ |
| 301 | public function buildQuickForm() { |
| 302 | if ($this->_cdType) { |
| 303 | return CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 304 | } |
| 305 | |
| 306 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 307 | $this->addButtons(array( |
| 308 | array( |
| 309 | 'type' => 'next', |
| 310 | 'name' => ts('Delete'), |
| 311 | 'isDefault' => TRUE, |
| 312 | ), |
| 313 | array( |
| 314 | 'type' => 'cancel', |
| 315 | 'name' => ts('Cancel'), |
| 316 | ), |
| 317 | ) |
| 318 | ); |
| 319 | return; |
| 320 | } |
| 321 | // Just in case custom data includes a rich text field |
| 322 | $this->assign('includeWysiwygEditor', TRUE); |
| 323 | |
| 324 | // Select list |
| 325 | $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); |
| 326 | |
| 327 | // Metadata needed on clientside |
| 328 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); |
| 329 | $jsData = array(); |
| 330 | // Get just what we need to keep the dom small |
| 331 | $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b')); |
| 332 | foreach($this->_allRelationshipNames as $id => $vals) { |
| 333 | if ($vals['name_a_b'] === 'Employee of') { |
| 334 | $this->assign('employmentRelationship', $id); |
| 335 | } |
| 336 | if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { |
| 337 | $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames[$id], $whatWeWant)); |
| 338 | // Add user-friendly placeholder |
| 339 | foreach (array('a', 'b') as $x) { |
| 340 | $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); |
| 341 | $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | $this->assign('relationshipData', $jsData); |
| 346 | |
| 347 | $this->add( |
| 348 | 'select', |
| 349 | 'relationship_type_id', |
| 350 | ts('Relationship Type'), |
| 351 | array( '' => ts('- select -')) + $relationshipList, |
| 352 | TRUE, |
| 353 | array('class' => 'crm-select2 huge') |
| 354 | ); |
| 355 | |
| 356 | $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact'); |
| 357 | $contactField = $this->addEntityRef('related_contact_id', $label, array('multiple' => TRUE, 'create' => TRUE), TRUE); |
| 358 | // This field cannot be updated |
| 359 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 360 | $contactField->freeze(); |
| 361 | } |
| 362 | |
| 363 | $this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer')); |
| 364 | |
| 365 | $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate')); |
| 366 | $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate')); |
| 367 | |
| 368 | $this->add('advcheckbox', 'is_active', ts('Enabled?')); |
| 369 | |
| 370 | // CRM-14612 - Don't use adv-checkbox as it interferes with the form js |
| 371 | $this->add('checkbox', 'is_permission_a_b'); |
| 372 | $this->add('checkbox', 'is_permission_b_a'); |
| 373 | |
| 374 | $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Relationship', 'description')); |
| 375 | |
| 376 | CRM_Contact_Form_Edit_Notes::buildQuickForm($this); |
| 377 | |
| 378 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 379 | $this->addButtons(array( |
| 380 | array( |
| 381 | 'type' => 'cancel', |
| 382 | 'name' => ts('Done'), |
| 383 | ), |
| 384 | )); |
| 385 | } |
| 386 | else { |
| 387 | // make this form an upload since we don't know if the custom data injected dynamically is of type file etc. |
| 388 | $this->addButtons(array( |
| 389 | array( |
| 390 | 'type' => 'upload', |
| 391 | 'name' => ts('Save Relationship'), |
| 392 | 'isDefault' => TRUE, |
| 393 | ), |
| 394 | array( |
| 395 | 'type' => 'cancel', |
| 396 | 'name' => ts('Cancel'), |
| 397 | ), |
| 398 | )); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * This function is called when the form is submitted |
| 404 | * |
| 405 | * @access public |
| 406 | * |
| 407 | * @return void |
| 408 | */ |
| 409 | public function postProcess() { |
| 410 | // store the submitted values in an array |
| 411 | $params = $this->controller->exportValues($this->_name); |
| 412 | |
| 413 | // action is taken depending upon the mode |
| 414 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 415 | CRM_Contact_BAO_Relationship::del($this->_relationshipId); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | $ids = array('contact' => $this->_contactId); |
| 420 | |
| 421 | $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']); |
| 422 | |
| 423 | // CRM-14612 - Don't use adv-checkbox as it interferes with the form js |
| 424 | $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, 0); |
| 425 | $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, 0); |
| 426 | |
| 427 | // Update mode (always single) |
| 428 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 429 | $ids['relationship'] = $this->_relationshipId; |
| 430 | $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId); |
| 431 | $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a; |
| 432 | |
| 433 | if ($this->_isCurrentEmployer) { |
| 434 | // if relationship type changes, relationship is disabled, or "current employer" is unchecked, |
| 435 | // clear the current employer. CRM-3235. |
| 436 | $relChanged = $relationshipTypeId != $this->_values['relationship_type_id']; |
| 437 | if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) { |
| 438 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']); |
| 439 | // Refresh contact summary if in ajax mode |
| 440 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | // Create mode (could be 1 or more relationships) |
| 445 | else { |
| 446 | // Fill up this weird param with contact ids like the weird relationship bao expects |
| 447 | $params['contact_check'] = array_fill_keys(explode(',', $params['related_contact_id']), 1); |
| 448 | if (!$this->_rtype) { |
| 449 | list(, $this->_rtype) = explode('_', $params['relationship_type_id'], 2); |
| 450 | } |
| 451 | } |
| 452 | $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE); |
| 453 | $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE); |
| 454 | |
| 455 | // Process custom data |
| 456 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId); |
| 457 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess( |
| 458 | $params, |
| 459 | $customFields, |
| 460 | $this->_relationshipId, |
| 461 | 'Relationship' |
| 462 | ); |
| 463 | |
| 464 | // Save relationships |
| 465 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids); |
| 466 | |
| 467 | // if this is called from case view, |
| 468 | //create an activity for case role removal.CRM-4480 |
| 469 | if ($this->_caseId) { |
| 470 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); |
| 471 | } |
| 472 | |
| 473 | if ($valid) { |
| 474 | CRM_Core_Session::setStatus(ts('Relationship created.', array('count' => $valid, 'plural' => '%count relationships created.')), ts('Saved'), 'success'); |
| 475 | } |
| 476 | if ($invalid) { |
| 477 | CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records'))); |
| 478 | } |
| 479 | if ($duplicate) { |
| 480 | 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'))); |
| 481 | } |
| 482 | if ($saved) { |
| 483 | CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); |
| 484 | } |
| 485 | |
| 486 | // Save notes |
| 487 | if ($this->_action & CRM_Core_Action::UPDATE || $params['note']) { |
| 488 | foreach ($relationshipIds as $id) { |
| 489 | $noteParams = array( |
| 490 | 'entity_id' => $id, |
| 491 | 'entity_table' => 'civicrm_relationship', |
| 492 | ); |
| 493 | $existing = civicrm_api3('note' ,'get', $noteParams); |
| 494 | if (!empty($existing['id'])) { |
| 495 | $noteParams['id'] = $existing['id']; |
| 496 | } |
| 497 | $noteParams['note'] = $params['note']; |
| 498 | $noteParams['contact_id'] = $this->_contactId; |
| 499 | if (!empty($existing['id']) || $params['note']) { |
| 500 | $action = $params['note'] ? 'create' : 'delete'; |
| 501 | civicrm_api3('note', $action, $noteParams); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Membership for related contacts CRM-1657 |
| 507 | if (CRM_Core_Permission::access('CiviMember') && (!$duplicate)) { |
| 508 | $params['relationship_ids'] = $relationshipIds; |
| 509 | if ($this->_action & CRM_Core_Action::ADD) { |
| 510 | CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, |
| 511 | $params, $ids, |
| 512 | $this->_action |
| 513 | ); |
| 514 | } |
| 515 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
| 516 | //fixes for CRM-7985 |
| 517 | //only if the relationship has been toggled to enable /disable |
| 518 | if (CRM_Utils_Array::value('is_active', $params) != $this->_enabled) { |
| 519 | $active = !empty($params['is_active']) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; |
| 520 | CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_relationshipId, $active); |
| 521 | } |
| 522 | } |
| 523 | // Refresh contact tabs which might have been affected |
| 524 | $this->ajaxResponse['updateTabs'] = array( |
| 525 | '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId), |
| 526 | '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId), |
| 527 | ); |
| 528 | } |
| 529 | // Set current employee/employer relationship, CRM-3532 |
| 530 | if ($params['is_current_employer'] && $this->_allRelationshipNames[$relationshipTypeId]["name_a_b"] == 'Employee of') { |
| 531 | $employerParams = array(); |
| 532 | foreach ($relationshipIds as $id) { |
| 533 | // Fixme this is dumb why do we have to look this up again? |
| 534 | $rel = CRM_Contact_BAO_Relationship::getContactIds($id); |
| 535 | $employerParams[$rel->contact_id_a] = $rel->contact_id_b; |
| 536 | } |
| 537 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams); |
| 538 | // Refresh contact summary if in ajax mode |
| 539 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * function for date validation |
| 545 | * |
| 546 | * @param array $params (reference ) an assoc array of name/value pairs |
| 547 | * |
| 548 | * @return mixed true or array of errors |
| 549 | * @access public |
| 550 | * @static |
| 551 | */ |
| 552 | static function dateRule($params) { |
| 553 | $errors = array(); |
| 554 | |
| 555 | // check start and end date |
| 556 | if (!empty($params['start_date']) && !empty($params['end_date'])) { |
| 557 | $start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params)); |
| 558 | $end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params)); |
| 559 | if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) { |
| 560 | $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.'); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return empty($errors) ? TRUE : $errors; |
| 565 | } |
| 566 | |
| 567 | } |
| 568 | |