From 239dac318c0fa8bb2691da49bead5fcfa0e11ff0 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 28 Feb 2014 22:01:55 -0500 Subject: [PATCH] CRM-14288 - Refactor and simplify relationship form --- CRM/Contact/Form/Relationship.php | 620 +++++----------- templates/CRM/Contact/Form/Relationship.tpl | 756 +++++--------------- 2 files changed, 349 insertions(+), 1027 deletions(-) diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index f4d7bd44ef..f7bdb463c3 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -39,11 +39,6 @@ */ class CRM_Contact_Form_Relationship extends CRM_Core_Form { - /** - * max number of contacts we will display for a relationship - */ - CONST MAX_RELATIONSHIPS = 50; - /** * The relationship id, used when editing the relationship * @@ -96,6 +91,21 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { */ protected $_allRelationshipNames; + /** + * @var bool + */ + protected $_enabled; + + /** + * @var bool + */ + protected $_isCurrentEmployer; + + /** + * @var string + */ + protected $_contactType; + /** * The relationship values if Updating relationship */ @@ -106,6 +116,11 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { */ protected $_caseId; + /** + * @var mixed + */ + public $_cdType; + function preProcess() { //custom data related code $this->_cdType = CRM_Utils_Array::value('type', $_GET); @@ -117,6 +132,8 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $this->_contactId = $this->get('contactId'); + $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); + $this->_relationshipId = $this->get('id'); $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); @@ -125,8 +142,23 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); - $this->assign('sort_name_a', $this->_display_name_a); - CRM_Utils_System::setTitle(ts('Relationships for') . ' ' . $this->_display_name_a); + $this->assign('display_name_a', $this->_display_name_a); + + // Set page title based on action + switch ($this->_action) { + case CRM_Core_Action::VIEW: + CRM_Utils_System::setTitle(ts('View Relationship for %1', $this->_display_name_a)); + break; + case CRM_Core_Action::ADD: + CRM_Utils_System::setTitle(ts('Add Relationship for %1', $this->_display_name_a)); + break; + case CRM_Core_Action::UPDATE: + CRM_Utils_System::setTitle(ts('Edit Relationship for %1', $this->_display_name_a)); + break; + case CRM_Core_Action::DELETE: + CRM_Utils_System::setTitle(ts('Delete Relationship for %1', $this->_display_name_a)); + break; + } $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); @@ -154,12 +186,22 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { if (!$this->_rtype) { $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); } - $this->assign('rtype', $this->_rtype); + //need to assign custom data type and subtype to the template - FIXME: explain why + $this->assign('customDataType', 'Relationship'); + $this->assign('customDataSubType', $this->_relationshipTypeId); + $this->assign('entityID', $this->_relationshipId); //use name as it remain constant, CRM-3336 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); + // Current employer? + if ($this->_action & CRM_Core_Action::UPDATE) { + if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { + $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); + } + } + // when custom data is included in this page if (!empty($_POST['hidden_custom'])) { CRM_Custom_Form_CustomData::preProcess($this); @@ -194,8 +236,14 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { } $defaults['description'] = CRM_Utils_Array::value('description', $this->_values); $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values); - $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $this->_values); - $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $this->_values); + + // The javascript on the form will swap these fields if it is a b_a relationship, so we compensate here + $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_' . $this->_rtype, $this->_values); + $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_' . strrev($this->_rtype), $this->_values); + + $defaults['is_current_employer'] = $this->_isCurrentEmployer; + + // Load info about the related contact $contact = new CRM_Contact_DAO_Contact(); if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) { $contact->id = $this->_values['contact_id_b']; @@ -204,37 +252,22 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $contact->id = $this->_values['contact_id_a']; } if ($contact->find(TRUE)) { + $defaults['related_contact_id'] = $contact->id; $this->_display_name_b = $contact->display_name; - $this->assign('sort_name_b', $this->_display_name_b); - - //is current employee/employer. - if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of' && - $contact->id == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id') - ) { - $defaults['is_current_employer'] = 1; - $this->_values['current_employee_id'] = $this->_contactId; - $this->_values['current_employer_id'] = $contact->id; - } - elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of' && - $this->_contactId == $contact->employer_id - ) { - $defaults['is_current_employer'] = 1; - $this->_values['current_employee_id'] = $contact->id; - $this->_values['current_employer_id'] = $this->_contactId; - } + $this->assign('display_name_b', $this->_display_name_b); } - $relationshipID = $this->_values['id']; - $query = "SELECT id, note FROM civicrm_note where entity_table = 'civicrm_relationship' and entity_id = $relationshipID order by modified_date desc"; - $dao = new CRM_Core_DAO(); - $dao->query($query); - if ($dao->fetch($query)) { - $defaults['note'] = $dao->note; - } + $noteParams = array( + 'entity_id' => $this->_relationshipId, + 'entity_table' => 'civicrm_relationship', + 'limit' => 1, + ); + $note = civicrm_api3('note' ,'getsingle', $noteParams); + $defaults['note'] = CRM_Utils_Array::value('note', $note); } } else { - $defaults['is_active'] = 1; + $defaults['is_active'] = $defaults['is_current_employer'] = 1; $defaults['relationship_type_id'] = $this->_rtypeId; } @@ -254,17 +287,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { } if (!($this->_action & CRM_Core_Action::DELETE)) { - $this->addRule('relationship_type_id', ts('Please select a relationship type.'), 'required'); - - // add a form rule only when creating a new relationship - // edit is severely limited, so add a simpleer form rule - if ($this->_action & CRM_Core_Action::ADD) { - $this->addFormRule(array('CRM_Contact_Form_Relationship', 'formRule'), $this); - $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); - } - elseif ($this->_action & CRM_Core_Action::UPDATE) { - $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); - } + $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); } } @@ -279,10 +302,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { return CRM_Custom_Form_CustomData::buildQuickForm($this); } - $relTypeID = explode('_', $this->_rtypeId, 3); - if ($this->_action & CRM_Core_Action::DELETE) { - $this->assign('id', $this->_relationshipId); $this->addButtons(array( array( 'type' => 'next', @@ -297,165 +317,84 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { ); return; } - - $callAjax = $this->get('callAjax'); - - $searchRows = NULL; - if (!$callAjax) { - $searchRows = $this->get('searchRows'); - } - else { - - $this->addElement('hidden', 'store_contacts', '', array('id' => 'store_contacts')); - $sourceUrl = 'snippet=4&relType=' . $this->get('relType'); - $sourceUrl .= '&relContact=' . $this->get('relContact'); - $sourceUrl .= '&cid=' . $this->_contactId; - - $this->assign('searchCount', TRUE); - - // To handle employee of and employer of - if (!empty($this->_relationshipTypeId) && - !empty($this->_rtype) - ) { - $sourceUrl .= '&typeName=' . $this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"]; + // Just in case custom data includes a rich text field + $this->assign('includeWysiwygEditor', TRUE); + + // Select list + $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); + + // Metadata needed on clientside + $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); + $jsData = array(); + // Filter it down to just what we need to keep the dom small + $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b')); + foreach($this->_allRelationshipNames as $id => $vals) { + if ($vals['name_a_b'] === 'Employee of') { + $this->assign('employmentRelationship', $id); + } + if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { + $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames[$id], $whatWeWant)); + // Add user-friendly placeholder + foreach (array('a', 'b') as $x) { + $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); + $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); + } } - $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/relationshipcontacts', $sourceUrl, FALSE, NULL, FALSE)); } + $this->assign('relationshipData', $jsData); - $this->assign('callAjax', $callAjax); - $this->_callAjax = $callAjax; - - $this->addElement('select', + $this->add( + 'select', 'relationship_type_id', ts('Relationship Type'), - array( - '' => ts('- select -')) + - CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, - $this->_rtype, - $this->_relationshipId, - NULL, FALSE, 'label' - ) + array( '' => ts('- select -')) + $relationshipList, + TRUE, + array('class' => 'crm-select2 huge') ); - // add a ajax facility for searching contacts - $dataUrl = CRM_Utils_System::url('civicrm/ajax/search', 'reset=1', TRUE, NULL, FALSE); - $this->assign('dataUrl', $dataUrl); - CRM_Contact_Form_NewContact::buildQuickForm($this); + $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact'); + $contactField = $this->addEntityRef('related_contact_id', $label, array('multiple' => TRUE, 'create' => TRUE), TRUE); + // This field cannot be updated + if ($this->_action & CRM_Core_Action::UPDATE) { + $contactField->freeze(); + } + + $this->add('checkbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer')); $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate')); $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate')); - $this->addElement('checkbox', 'is_active', ts('Enabled?'), NULL, NULL); - $this->addElement('checkbox', 'is_permission_a_b', ts('Permission for contact a to view and update information for contact b'), NULL); - $this->addElement('checkbox', 'is_permission_b_a', ts('permission for contact b to view and update information for contact a'), NULL); + $this->add('checkbox', 'is_active', ts('Enabled?')); + + $this->add('checkbox', 'is_permission_a_b'); + $this->add('checkbox', 'is_permission_b_a'); $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Relationship', 'description')); CRM_Contact_Form_Edit_Notes::buildQuickForm($this); - $searchCount = $this->get('searchCount'); - $duplicateRelationship = $this->get('duplicateRelationship'); - $searchDone = $this->get('searchDone'); - - $isEmployeeOf = $isEmployerOf = FALSE; - if (!empty($this->_relationshipTypeId) && - !empty($this->_rtype) - ) { - if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { - $isEmployeeOf = TRUE; - } - elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') { - $isEmployerOf = TRUE; - } - } - - $employers = $checkBoxes = $employees = array(); - if ($searchRows) { - foreach ($searchRows as $id => $row) { - $checkBoxes[$id] = $this->createElement('checkbox', $id, NULL, ''); - if ($isEmployeeOf) { - $employers[$id] = $this->createElement('radio', NULL, $id, NULL, $id); - } - elseif ($isEmployerOf) { - $employees[$id] = $this->createElement('checkbox', $id, NULL, ''); - } - } - - $this->addGroup($checkBoxes, 'contact_check'); - $this->assign('searchRows', $searchRows); - } - - if ($isEmployeeOf) { - $this->assign('isEmployeeOf', $isEmployeeOf); - if (!$callAjax) { - $this->addGroup($employers, 'employee_of'); - } - } - elseif ($isEmployerOf) { - $this->assign('isEmployerOf', $isEmployerOf); - if (!$callAjax) { - $this->addGroup($employees, 'employer_of'); - } - } - - if ($callAjax && ($isEmployeeOf || $isEmployerOf)) { - $this->addElement('hidden', 'store_employers', '', array('id' => 'store_employers')); - } - - if ($this->_action & CRM_Core_Action::UPDATE) { - $this->addElement('checkbox', 'is_current_employer'); - } - - $this->assign('duplicateRelationship', $duplicateRelationship); - $this->assign('searchCount', $searchCount); - $this->assign('searchDone', $searchDone); - - if ($this->get('contact_type')) { - $typeLabel = CRM_Contact_BAO_ContactType::getLabel($this->get('contact_type')); - $this->assign('contact_type', $this->get('contact_type')); - $this->assign('contact_type_display', $typeLabel); - } - - if ($searchDone) { - $searchBtn = ts('Search Again'); + if ($this->_action & CRM_Core_Action::VIEW) { + $this->addButtons(array( + array( + 'type' => 'cancel', + 'name' => ts('Done'), + ), + )); } else { - $searchBtn = ts('Search'); - } - $this->addElement('submit', $this->getButtonName('refresh'), $searchBtn, array('class' => 'form-submit', 'id' => 'search-button')); - $this->addElement('submit', $this->getButtonName('refresh', 'save'), 'Quick Save', array('class' => 'form-submit', 'id' => 'quick-save')); - $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'form-submit cancel')); - - $this->addElement('submit', $this->getButtonName('refresh', 'savedetails'), 'Save Relationship', array('class' => 'form-submit hiddenElement', 'id' => 'details-save')); - $this->addElement('checkbox', 'add_current_employer', ts('Current Employer'), NULL); - $this->addElement('checkbox', 'add_current_employee', ts('Current Employee'), NULL); - - //need to assign custom data type and subtype to the template - $this->assign('customDataType', 'Relationship'); - $this->assign('customDataSubType', $this->_relationshipTypeId); - $this->assign('entityID', $this->_relationshipId); - - // make this form an upload since we dont know if the custom data injected dynamically - // is of type file etc $uploadNames = $this->get( - // 'uploadNames' ); - $buttonParams = array( - 'type' => 'upload', - 'name' => ts('Save Relationship'), - 'isDefault' => TRUE, - ); - if ($callAjax) { - $buttonParams['js'] = array('onclick' => ' submitAjaxData();'); - } - - $this->addButtons(array( - $buttonParams - , + // make this form an upload since we don't know if the custom data injected dynamically is of type file etc. + $this->addButtons(array( + array( + 'type' => 'upload', + 'name' => ts('Save Relationship'), + 'isDefault' => TRUE, + ), array( 'type' => 'cancel', 'name' => ts('Cancel'), ), - ) - ); + )); + } } /** @@ -468,96 +407,49 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { public function postProcess() { // store the submitted values in an array $params = $this->controller->exportValues($this->_name); - $quickSave = FALSE; - if (!empty($_POST['_qf_Relationship_refresh_save']) || !empty($_POST['_qf_Relationship_refresh_savedetails'])) { - $quickSave = TRUE; - } - - $this->set('searchDone', 0); - $this->set('callAjax', FALSE); - if (!empty($_POST['_qf_Relationship_refresh']) || $quickSave) { - if (is_numeric($params['contact_select_id'][1])) { - if ($quickSave) { - $params['contact_check'] = array($params['contact_select_id'][1] => 1); - } - } - else { - $this->set('callAjax', TRUE); - $this->set('relType', $params['relationship_type_id']); - $this->set('relContact', $params['contact'][1]); - $quickSave = FALSE; - } - $this->set('searchDone', 1); - if (!$quickSave) { - return; - } - } // action is taken depending upon the mode - $ids = array(); - $ids['contact'] = $this->_contactId; - - // modify params for ajax call - $this->modifyParams($params); - if ($this->_action & CRM_Core_Action::DELETE) { CRM_Contact_BAO_Relationship::del($this->_relationshipId); return; } - $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $params['relationship_type_id']); + $ids = array('contact' => $this->_contactId); + + // Boolean fields (checkboxes) are nonexistant when submitted if unchecked - cast them to boolean + // FIXME: Dear Quickform, this sucks. + $params['is_active'] = !empty($params['is_active']); + $params['is_permission_a_b'] = !empty($params['is_permission_a_b']); + $params['is_permission_b_a'] = !empty($params['is_permission_b_a']); + + + $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']); if ($this->_action & CRM_Core_Action::UPDATE) { $ids['relationship'] = $this->_relationshipId; $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId); $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a; - //if relationship type change and previously it was - //employer / emplyee relationship with current employer - //than clear the current employer. CRM-3235. - - //make sure we has to have employer id before firing queries, CRM-7306 - $employerId = CRM_Utils_Array::value('current_employee_id', $this->_values); - $isDisabled = TRUE; - if (!empty($params['is_active'])) { - $isDisabled = FALSE; - } - $relChanged = TRUE; - if ($relationshipTypeId == $this->_values['relationship_type_id']) { - $relChanged = FALSE; - } - if ($employerId && ($isDisabled || $relChanged)) { - CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['current_employee_id']); + // if relationship type changes, relationship is disabled, or "current employer" is unchecked, + // clear the current employer. CRM-3235. + $isDisabled = !$params['is_active'] || !$params['is_current_employer']; + $relChanged = $relationshipTypeId != $this->_values['relationship_type_id']; + if ($this->_isCurrentEmployer && ($isDisabled || $relChanged)) { + CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']); } - //if field key doesn't exists in params that means the user has unchecked checkbox, - //hence fill FALSE to params - $params['is_active'] = $isDisabled ? FALSE : TRUE; - $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, FALSE); - $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, FALSE); } - elseif ($quickSave) { - if (!empty($params['add_current_employee']) && - $this->_allRelationshipNames[$relationshipTypeId]['name_a_b'] == 'Employee of' - ) { - $params['employee_of'] = $params['contact_select_id'][1]; - } - elseif (!empty($params['add_current_employer']) && - $this->_allRelationshipNames[$relationshipTypeId]['name_b_a'] == 'Employer of' - ) { - $params['employer_of'] = array($params['contact_select_id'][1] => 1); - } + // Create mode - save 1 or more new relationships + else { + // Fill up this weird param with contact ids like the weird relationship bao expects + $params['contact_check'] = array_fill_keys(explode(',', $params['related_contact_id']), 1); if (!$this->_rtype) { - $this->_rtype = str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']); + list(, $this->_rtype) = explode('_', $relationshipTypeId, 2); } } - - if (!$params['note']) { - $params['note'] = 'null'; - } $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE); $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE); - //special case to handle if all checkboxes are unchecked + // Process custom data $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId); $params['custom'] = CRM_Core_BAO_CustomField::postProcess( $params, @@ -566,6 +458,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { 'Relationship' ); + // Save relationships list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids); // if this is called from case view, @@ -574,12 +467,11 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); } - $status = ''; if ($valid) { - CRM_Core_Session::setStatus(ts('New relationship created.', array('count' => $valid, 'plural' => '%count new relationships created.')), ts('Saved'), 'success'); + CRM_Core_Session::setStatus(ts('Relationship created.', array('count' => $valid, 'plural' => '%count relationships created.')), ts('Saved'), 'success'); } if ($invalid) { - 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'))); + 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'))); } if ($duplicate) { 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'))); @@ -588,29 +480,29 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); } - if (!empty($relationshipIds)) { - $note = new CRM_Core_DAO_Note(); - $note->entity_id = $relationshipIds[0]; - $note->entity_table = 'civicrm_relationship'; - $noteIds = array(); - if ($note->find(TRUE)) { - $id = $note->id; - $noteIds['id'] = $id; + // Save notes + if ($this->action & CRM_Core_Action::UPDATE || $params['note']) { + foreach ($relationshipIds as $id) { + $noteParams = array( + 'entity_id' => $id, + 'entity_table' => 'civicrm_relationship', + ); + $existing = civicrm_api3('note' ,'get', $noteParams); + if (!empty($existing['id'])) { + $noteParams['id'] = $existing['id']; + } + $noteParams['note'] = $params['note']; + $noteParams['contact_id'] = $this->_contactId; + if (!empty($existing['id']) || $params['note']) { + $action = $params['note'] ? 'create' : 'delete'; + civicrm_api3('note', $action, $noteParams); + } } - - $noteParams = array( - 'entity_id' => $relationshipIds[0], - 'entity_table' => 'civicrm_relationship', - 'note' => $params['note'], - 'contact_id' => $this->_contactId, - ); - CRM_Core_BAO_Note::add($noteParams, $noteIds); - - $params['relationship_ids'] = $relationshipIds; } // Membership for related contacts CRM-1657 if (CRM_Core_Permission::access('CiviMember') && (!$duplicate)) { + $params['relationship_ids'] = $relationshipIds; if ($this->_action & CRM_Core_Action::ADD) { CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, $params, $ids, @@ -626,146 +518,16 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { } } } - //handle current employee/employer relationship, CRM-3532 - if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { - $orgId = NULL; - if (!empty($params['employee_of'])) { - $orgId = $params['employee_of']; - } - elseif ($this->_action & CRM_Core_Action::UPDATE) { - if (!empty($params['is_current_employer']) && !empty($params['is_active'])) { - if (CRM_Utils_Array::value('contactTarget', $ids) != - CRM_Utils_Array::value('current_employer_id', $this->_values) - ) { - $orgId = CRM_Utils_Array::value('contactTarget', $ids); - } - } - elseif (CRM_Utils_Array::value('contactTarget', $ids) == - CRM_Utils_Array::value('current_employer_id', $this->_values) - ) { - //clear current employer. - CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_contactId); - } - } - - //set current employer - if ($orgId) { - $currentEmpParams[$this->_contactId] = $orgId; - CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); - } - } - elseif ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') { - $individualIds = array(); - if (!empty($params['employer_of'])) { - $individualIds = array_keys($params['employer_of']); - } - elseif ($this->_action & CRM_Core_Action::UPDATE) { - if (!empty($params['is_current_employer'])) { - if (CRM_Utils_Array::value('contactTarget', $ids) != - CRM_Utils_Array::value('current_employee_id', $this->_values) - ) { - $individualIds[] = CRM_Utils_Array::value('contactTarget', $ids); - } - } - elseif (CRM_Utils_Array::value('contactTarget', $ids) == - CRM_Utils_Array::value('current_employee_id', $this->_values) - ) { - // clear current employee - CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($ids['contactTarget']); - } - } - - //set current employee - if (!empty($individualIds)) { - - //build the employee params. - foreach ($individualIds as $key => $Id) { - $currentEmpParams[$Id] = $this->_contactId; - } - - CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); - } - } - // Don't redirect in ajaxForm context - // FIXME: Perhaps changing the button type would avoid needing the redirect at allq - if ($quickSave && empty($_REQUEST['snippet'])) { - $session = CRM_Core_Session::singleton(); - CRM_Utils_System::redirect($session->popUserContext()); - } - } - //end of function - - /** - * function for validation - * - * @param array $params (reference ) an assoc array of name/value pairs - * - * @return mixed true or array of errors - * @access public - * @static - */ - static function formRule($params, $files, $form) { - // hack, no error check for refresh - if (!empty($_POST['_qf_Relationship_refresh']) || !empty($_POST['_qf_Relationship_refresh_save']) || - CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST) - ) { - return TRUE; - } - - $form->modifyParams($params); - - $ids = array(); - $session = CRM_Core_Session::singleton(); - $ids['contact'] = $form->get('contactId'); - $ids['relationship'] = $form->get('relationshipId'); - - $errors = array(); - $employerId = NULL; - if (!empty($params['contact_check']) && is_array($params['contact_check'])) { - foreach ($params['contact_check'] as $cid => $dontCare) { - $message = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $cid); - if ($message) { - $errors['relationship_type_id'] = $message; - break; - } - - if ($cid == CRM_Utils_Array::value('employee_of', $params)) { - $employerId = $cid; - } - } - } - else { - if ($form->_callAjax) { - $errors['store_contacts'] = ts('Select select at least one contact from Target Contact(s).'); - } - else { - $errors['contact_check'] = ts('Please select at least one contact.'); - } - } - - if (!empty($params['employee_of']) && - !$employerId - ) { - if ($form->_callAjax) { - $errors['store_employer'] = ts('Current employer should be one of the selected contacts.'); - } - else { - $errors['employee_of'] = ts('Current employer should be one of the selected contacts.'); - } - } - - if (!empty($params['employer_of']) && !empty($params['contact_check']) && - array_diff(array_keys($params['employer_of']), array_keys($params['contact_check'])) - ) { - if ($form->_callAjax) { - $errors['store_employer'] = ts('Current employee should be among the selected contacts.'); - } - else { - $errors['employer_of'] = ts('Current employee should be among the selected contacts.'); + // Set current employee/employer relationship, CRM-3532 + if ($params['is_current_employer'] && $this->_allRelationshipNames[$relationshipTypeId]["name_a_b"] == 'Employee of') { + $employerParams = array(); + foreach ($relationshipIds as $id) { + // Fixme this is dumb why do we have to look this up again? + $rel = CRM_Contact_BAO_Relationship::getContactIds($id); + $employerParams[$rel->contact_id_a] = $rel->contact_id_b; } + CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams); } - - return empty($errors) ? TRUE : $errors; } /** @@ -792,35 +554,5 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { return empty($errors) ? TRUE : $errors; } - function modifyParams(&$params) { - if (!$this->_callAjax) { - return; - } - - if (!empty($params['store_contacts'])) { - $storedContacts = array(); - foreach (explode(',', $params['store_contacts']) as $value) { - if ($value) { - $storedContacts[$value] = 1; - } - } - $params['contact_check'] = $storedContacts; - } - - if (!empty($params['store_employers'])) { - $employeeContacts = array(); - foreach (explode(',', $params['store_employers']) as $value) { - if ($value) { - $employeeContacts[$value] = $value; - } - } - if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') { - $params['employee_of'] = current($employeeContacts); - } - else { - $params['employer_of'] = $employeeContacts; - } - } - } } diff --git a/templates/CRM/Contact/Form/Relationship.tpl b/templates/CRM/Contact/Form/Relationship.tpl index 05fb113600..acaeeeb5a8 100644 --- a/templates/CRM/Contact/Form/Relationship.tpl +++ b/templates/CRM/Contact/Form/Relationship.tpl @@ -28,601 +28,191 @@ {include file="CRM/Custom/Form/CustomData.tpl"} {else} {if $action eq 4 } {* action = view *} -

{ts}View Relationship{/ts}

-
- - {foreach from=$viewRelationship item="row"} - - - - - {if $isCurrentEmployer} - - {/if} - {if $row.start_date} - - {/if} - {if $row.end_date} - - {/if} - {if $row.description} - - {/if} +
+
{$row.relation}{$row.name}
{ts}Current Employee?{/ts}{ts}Yes{/ts}
{ts}Start Date{/ts}{$row.start_date|crmDate}
{ts}End Date{/ts}{$row.end_date|crmDate}
{ts}Description{/ts}{$row.description}
+ {foreach from=$viewRelationship item="row"} + + + + + {if $isCurrentEmployer} + + {/if} + {if $row.start_date} + + {/if} + {if $row.end_date} + + {/if} + {if $row.description} + + {/if} {foreach from=$viewNote item="rec"} - {if $rec } - - {/if} - {/foreach} - {if $row.is_permission_a_b} - {if $row.rtype EQ 'a_b' AND $is_contact_id_a} - - {else} - - {/if} - {/if} - {if $row.is_permission_b_a} - {if $row.rtype EQ 'a_b' AND $is_contact_id_a} - - {else} - - {/if} + {if $rec } + {/if} - - + {/foreach} + + + + + {/foreach} -
{$row.relation}{$row.name}
{ts}Current Employee?{/ts}{ts}Yes{/ts}
{ts}Start Date{/ts}{$row.start_date|crmDate}
{ts}End Date{/ts}{$row.end_date|crmDate}
{ts}Description{/ts}{$row.description}
{ts}Note{/ts}{$rec}
 '{$displayName}' can view and update information for '{$row.display_name}'
 '{$row.display_name}' can view and update information for '{$displayName}'
 '{$row.display_name}' can view and update information for '{$displayName}'
 '{$displayName}' can view and update information for '{$row.display_name}'
{ts}Note{/ts}{$rec}
{ts}Status{/ts}{if $row.is_active}{ts}Enabled{/ts} {else} {ts}Disabled{/ts}{/if}
+ {if $row.is_permission_a_b or $row.is_permission_b_a} + {if $row.is_permission_a_b} +
+ {if $row.rtype EQ 'a_b' AND $is_contact_id_a} + {ts 1=$displayName 2=$row.display_name}%1 can view and update information about %2.{/ts} + {else} + {ts 1=$row.display_name 2=$displayName}%1 can view and update information about %2.{/ts} + {/if} +
+ {/if} + {if $row.is_permission_b_a} +
+ {if $row.rtype EQ 'a_b' AND $is_contact_id_a} + {ts 1=$row.display_name 2=$displayName}%1 can view and update information about %2.{/ts} + {else} + {ts 1=$displayName 2=$row.display_name}%1 can view and update information about %2.{/ts} + {/if} +
+ {/if} + {else} + {ts}None{/ts} + {/if} +
{ts}Status{/ts}{if $row.is_active}{ts}Enabled{/ts}{else}{ts}Disabled{/ts}{/if}
- {include file="CRM/Custom/Page/CustomDataView.tpl"} - -
+ + {include file="CRM/Custom/Page/CustomDataView.tpl"} + {/if} - {if $action eq 2 | $action eq 1} {* add and update actions *} - {* Retrieve the ID for the Employee / Employer relationship type *} - {crmAPI var="employmentRelationship" entity="RelationshipType" action="getvalue" version="3" name_a_b="Employee of" return="id"} -

{if $action eq 1}{ts}New Relationship{/ts}{else}{ts}Edit Relationship{/ts}{/if}

- {literal} - - {/literal} + {if $action eq 2 or $action eq 1} {* add and update actions *}
- {if $action eq 1} -
- {ts}Select the relationship type. Then locate target contact(s) for this relationship by entering a partial name and selecting from the dropdown, or clicking 'Search'. If the target contact does not exist, you can create a new contact.{/ts} -
- {/if} - - - - {if $action EQ 2} {* action = update *} - {literal} - - {/literal} - - - - - -
{$form.relationship_type_id.label}{$form.relationship_type_id.html}
- - - {$form.is_current_employer.html}
- {else} {* action = add *} - - {include file="CRM/Contact/Form/NewContact.tpl" newContactCallback="afterCreateNew()"} - - - {literal} - - {/literal} - - - -
- {$form._qf_Relationship_refresh.html} - - {$form._qf_Relationship_cancel.html} -
-
- - {if $searchDone } {* Search button clicked *} - {if $searchCount || $callAjax} - {if $searchRows || $callAjax} {* we got rows to display *} -
{ts}Mark Target Contact(s) for this Relationship{/ts} -
- {ts}Mark the target contact(s) for this relationship if it appears below. Otherwise you may modify the search name above and click Search again.{/ts} -
- {strip} - {if $callAjax} -

- {$form.store_contacts.html} + + + + + + + + {* remove crm-select2 class - hack to prevent widget from being initialized so we can mess with it first *} + + + + + + + + + + + + + + + + + + + + {capture assign="contact_b"}{if $action eq 1}{ts}selected contact(s){/ts}{else}{$display_name_b}{/if}{/capture} + + + + + + + + + + + +
{$form.relationship_type_id.label}{$form.relationship_type_id.html}
{$form.start_date.label}{include file="CRM/common/jcalendar.tpl" elementName=start_date}{$form.end_date.label} {include file="CRM/common/jcalendar.tpl" elementName=end_date}
+ {ts}If this relationship has start and/or end dates, specify them here.{/ts}
{$form.description.label}{$form.description.html}
{$form.note.label}{$form.note.html}
+ {$form.is_permission_a_b.html} + {ts 1=$display_name_a 2=$contact_b}%1 can view and update information about %2.{/ts} +
+ {$form.is_permission_b_a.html} + {ts 1=$contact_b|ucfirst 2=$display_name_a}%1 can view and update information about %2.{/ts} +
{$form.is_active.label}{$form.is_active.html}
+
+
+
+ {/if} + {if ($action EQ 1) OR ($action EQ 2) } + {*include custom data js file - setting includeWysiwygEditor=TRUE because it's already been loaded*} + {include file="CRM/common/customData.tpl" includeWysiwygEditor=TRUE} + {/if} {if $action eq 8} -
{ts}Delete Relationship{/ts} -
- {capture assign=relationshipsString}{$currentRelationships.$id.relation}{ $disableRelationships.$id.relation} {$currentRelationships.$id.name}{ $disableRelationships.$id.name }{/capture} - {ts 1=$relationshipsString}Are you sure you want to delete the Relationship '%1'?{/ts} -
-
{include file="CRM/common/formButtons.tpl" location="bottom"}
-
+
+ {ts}Are you sure you want to delete this Relationship?{/ts} +
{/if} +
{include file="CRM/common/formButtons.tpl" location="bottom"}
{/if} {* close of custom data else*} -{if $callAjax} -{literal} - -{/literal} -{/if} - -{if ($action EQ 1) OR ($action EQ 2) } -{*include custom data js file*} -{include file="CRM/common/customData.tpl"} -{literal} - -{/literal} -{/if} -{if $action EQ 2} -{literal} - -{/literal} -{/if} -- 2.25.1