From 936e43d63437dec5d1f324f3ed1423b980f6bf3a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 7 Jan 2015 03:14:52 +1300 Subject: [PATCH] CRM-15786-master api now basic, BAO::create looks like a create --- CRM/Contact/BAO/Relationship.php | 6 +- api/v3/Relationship.php | 142 ++-------------------- tests/phpunit/api/v3/RelationshipTest.php | 3 + 3 files changed, 17 insertions(+), 134 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 4411555100..9ade55cc50 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -57,9 +57,13 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { throw new CRM_Core_Exception('Duplicate Relationship'); } if (self::checkValidRelationship($params, $params, 0)) { - throw new CRM_Core_Exception('Invalide Relationship'); + throw new CRM_Core_Exception('Invalid Relationship'); } $relationship = self::add($params); + if (!empty($params['contact_id_a'])) { + $ids = array('contactTarget' => $relationship->contact_id_b, 'contact' => $params['contact_id_a']); + CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, (empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE)); + } self::addRecent($params, $relationship); return $relationship; } diff --git a/api/v3/Relationship.php b/api/v3/Relationship.php index 85b7e6196d..5f363babd2 100644 --- a/api/v3/Relationship.php +++ b/api/v3/Relationship.php @@ -51,29 +51,8 @@ * @access public */ function civicrm_api3_relationship_create($params) { - - $values = array(); - _civicrm_api3_relationship_format_params($params, $values); - $ids = array(); - $action = CRM_Core_Action::ADD; - - if (!empty($params['id'])) { - $ids['contactTarget'] = $values['contact_id_b']; - $action = CRM_Core_Action::UPDATE; - } - - $values['relationship_type_id'] = $values['relationship_type_id'] . '_a_b'; - - $relationshipBAO = CRM_Contact_BAO_Relationship::create($values); - - // Handle related memberships CRM-13652 - if (!empty($params['contact_id_a'])) { - CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, $action); - } - $id = $relationshipBAO->id; - $values = array(); - _civicrm_api3_object_to_array($relationshipBAO, $values[$id]); - return civicrm_api3_create_success($values, $params, 'relationship', 'create'); + _civicrm_api3_handle_relationship_type($params); + return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship'); } /** @@ -162,121 +141,18 @@ function civicrm_api3_relationship_get($params) { } /** - * take the input parameter list as specified in the data model and - * convert it into the same format that we use in QF and BAO object + * legacy handling for relationship_type parameter * * @param array $params Associative array of property name/value * pairs to insert in new contact. - * @param array $values The reformatted properties that we can use internally - * ' - * - * @throws Exception - * @return array|CRM_Error - * @access public */ -function _civicrm_api3_relationship_format_params($params, &$values) { - // copy all the relationship fields as is - - $fields = CRM_Contact_DAO_Relationship::fields(); - _civicrm_api3_store_values($fields, $params, $values); - - $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); - if (!empty($params['id'])) { - $relation = new CRM_Contact_BAO_Relationship(); - $relation->id = $params['id']; - if (!$relation->find(TRUE)) { - throw new Exception('Relationship id is not valid'); - } - else { - if ((isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a) || - (isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) - ) { - throw new Exception('Cannot change the contacts once relationship has been created'); - } - else { - // since the BAO function is not std & won't accept just 'id' (aargh) let's - // at least return our BAO here - $values = array(); - _civicrm_api3_object_to_array($relation, $values); - $values = array_merge($values, $params); - // and we need to reformat our date fields.... - $dateFields = array('start_date', 'end_date'); - foreach ($dateFields as $dateField){ - $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]); - } +function _civicrm_api3_handle_relationship_type(&$params) { + if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) { + $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); + foreach ($relationTypes as $relationshipTypeId => $relationshipValue) { + if (CRM_Utils_Array::key(ucfirst($params['relationship_type']), $relationshipValue)) { + $params['relationship_type_id'] = $relationshipTypeId; } } } - - foreach ($params as $key => $value) { - // ignore empty values or empty arrays etc - if (CRM_Utils_System::isNull($value)) { - continue; - } - - switch ($key) { - case 'contact_id_a': - case 'contact_id_b': - if (!CRM_Utils_Rule::integer($value)) { - throw new Exception("contact_id not valid: $value"); - } - $dao = new CRM_Core_DAO(); - $qParams = array(); - $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", - $qParams - ); - if (!$svq) { - throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value."); - } - break; - - case 'relationship_type': - foreach ($relationTypes as $relTypId => $relValue) { - if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { - $relationshipTypeId = $relTypId; - break; - } - } - - if ($relationshipTypeId) { - if (!empty($values['relationship_type_id']) && - $relationshipTypeId != $values['relationship_type_id'] - ) { - throw new Exception('Mismatched Relationship Type and Relationship Type Id'); - } - $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; - } - else { - throw new Exception('Invalid Relationship Type'); - } - case 'relationship_type_id': - if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { - throw new Exception("$key not a valid: $value"); - } - - // execute for both relationship_type and relationship_type_id - $relation = $relationTypes[$params['relationship_type_id']]; - if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && - $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a']) - ) { - throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); - } - if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && - $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b']) - ) { - throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}"); - } - break; - - default: - break; - } - } - - if (array_key_exists('note', $params)) { - $values['note'] = $params['note']; - } - _civicrm_api3_custom_format_params($params, $values, 'Relationship'); - - return array(); } diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index 68b56fb5bd..2a8eeaf201 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -289,10 +289,13 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $relID = $result['id']; $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah')); $this->assertEquals($relID, $result['id']); + $this->assertEquals('blah', $result['values'][$result['id']]['description']); + $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1)); $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']); $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0)); + $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id'])); $this->assertEquals(0, $result['values'][$result['id']]['is_active']); $this->assertEquals('blah', $result['values'][$result['id']]['description']); $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']); -- 2.25.1