$values = array();
_civicrm_api3_relationship_format_params($params, $values);
$ids = array();
- $action = CRM_Core_Action::ADD;
if (CRM_Utils_Array::value('id', $params)) {
- $ids['relationship'] = $params['id'];
$ids['contactTarget'] = $values['contact_id_b'];
- $action = CRM_Core_Action::UPDATE;
}
$values['relationship_type_id'] = $values['relationship_type_id'] . '_a_b';
- $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']);
- $ids['contact'] = $values['contact_id_a'];
+ if(!empty($params['contact_id_b'])){
+ $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']);
+ }
+ if(!empty($values['contact_id_a'])){
+ $ids['contact'] = $values['contact_id_a'];
+ }
$relationshipBAO = CRM_Contact_BAO_Relationship::create($values, $ids);
if ($relationshipBAO[1]) {
- return civicrm_api3_create_error('Relationship is not valid');
+ throw new API_Exception('Relationship is not valid');
}
elseif ($relationshipBAO[2]) {
- return civicrm_api3_create_error('Relationship already exists');
+ throw new API_Exception('Relationship already exists');
}
- CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, $action);
- $relationID = $relationshipBAO[4][0];
- return civicrm_api3_create_success(array(
- $relationID => array('id' => $relationID,
- 'moreIDs' => implode(',', $relationshipBAO[4]),
- )));
+ $id = $relationshipBAO[4][0];
+ $values = array();
+ _civicrm_api3_object_to_array($relationshipBAO[5][$id], $values[$id]);
+ return civicrm_api3_create_success($values, $params, 'relationship', 'create');
}
/**
* @access public
*/
function civicrm_api3_relationship_get($params) {
-
+ $options = _civicrm_api3_get_options_from_params($params);
if (!CRM_Utils_Array::value('contact_id', $params)) {
$relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
}
$relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
CRM_Utils_Array::value('status_id', $params),
0,
- 0,
- CRM_Utils_Array::value('id', $params), NULL
+ CRM_Utils_Array::value('is_count', $options),
+ CRM_Utils_Array::value('id', $params),
+ NULL,
+ NULL,
+ FALSE,
+ $params
);
}
+ //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
+ if($options['is_count']) {
+ return array('count' => $relationships);
+ }
foreach ($relationships as $relationshipId => $values) {
_civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id',$values));
}
-
-
return civicrm_api3_create_success($relationships, $params);
}