/**
  * This is the function that check/add if the relationship created is valid.
  *
  * @param array $params
  *   (reference ) an assoc array of name/value pairs.
  * @param array $ids
  *   The array that holds all the db ids.
  * @param int $contactId
  *   This is contact id for adding relationship.
  *
  * @return CRM_Contact_BAO_Relationship
  */
 public static function add(&$params, $ids = array(), $contactId = NULL)
 {
     $relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));
     $hook = 'create';
     if ($relationshipId) {
         $hook = 'edit';
     }
     //@todo hook are called from create and add - remove one
     CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params);
     $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params);
     // explode the string with _ to get the relationship type id
     // and to know which contact has to be inserted in
     // contact_id_a and which one in contact_id_b
     list($type) = explode('_', $relationshipTypes);
     // check if the relationship type is Head of Household then update the
     // household's primary contact with this contact.
     if ($type == 6) {
         CRM_Contact_BAO_Household::updatePrimaryContact($params['contact_id_b'], $params['contact_id_a']);
     }
     $relationship = new CRM_Contact_BAO_Relationship();
     //@todo this code needs to be updated for the possibility that not all fields are set
     // by using $relationship->copyValues($params);
     // (update)
     $relationship->contact_id_b = $params['contact_id_b'];
     $relationship->contact_id_a = $params['contact_id_a'];
     $relationship->relationship_type_id = $type;
     $relationship->id = $relationshipId;
     $dateFields = array('end_date', 'start_date');
     foreach (self::getdefaults() as $defaultField => $defaultValue) {
         if (isset($params[$defaultField])) {
             if (in_array($defaultField, $dateFields)) {
                 $relationship->{$defaultField} = CRM_Utils_Date::format(CRM_Utils_Array::value($defaultField, $params));
                 if (!$relationship->{$defaultField}) {
                     $relationship->{$defaultField} = 'NULL';
                 }
             } else {
                 $relationship->{$defaultField} = $params[$defaultField];
             }
         } elseif (!$relationshipId) {
             $relationship->{$defaultField} = $defaultValue;
         }
     }
     $relationship->save();
     // add custom field values
     if (!empty($params['custom'])) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_relationship', $relationship->id);
     }
     $relationship->free();
     CRM_Utils_Hook::post($hook, 'Relationship', $relationship->id, $relationship);
     return $relationship;
 }
 /**
  * This is the function that check/add if the relationship created is valid
  *
  * @param array  $params      (reference ) an assoc array of name/value pairs
  * @param integer $contactId  this is contact id for adding relationship
  * @param array $ids          the array that holds all the db ids
  *
  * @return object CRM_Contact_BAO_Relationship
  * @access public
  * @static
  */
 static function add(&$params, &$ids, $contactId)
 {
     if (CRM_Utils_Array::value('relationship', $ids)) {
         CRM_Utils_Hook::pre('edit', 'Relationship', $ids['relationship'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Relationship', NULL, $params);
     }
     $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params);
     // expolode the string with _ to get the relationship type id and to know which contact has to be inserted in
     // contact_id_a and which one in contact_id_b
     list($type, $first, $second) = explode('_', $relationshipTypes);
     ${'contact_' . $first} = CRM_Utils_Array::value('contact', $ids);
     ${'contact_' . $second} = $contactId;
     //check if the relationship type is Head of Household then update the household's primary contact with this contact.
     if ($type == 6) {
         CRM_Contact_BAO_Household::updatePrimaryContact($contact_b, $contact_a);
     }
     $relationship = new CRM_Contact_BAO_Relationship();
     $relationship->contact_id_b = $contact_b;
     $relationship->contact_id_a = $contact_a;
     $relationship->relationship_type_id = $type;
     $relationship->is_active = CRM_Utils_Array::value('is_active', $params) ? 1 : 0;
     $relationship->is_permission_a_b = CRM_Utils_Array::value('is_permission_a_b', $params, 0);
     $relationship->is_permission_b_a = CRM_Utils_Array::value('is_permission_b_a', $params, 0);
     $relationship->description = CRM_Utils_Array::value('description', $params);
     $relationship->start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params));
     $relationship->case_id = CRM_Utils_Array::value('case_id', $params);
     if (!$relationship->start_date) {
         $relationship->start_date = 'NULL';
     }
     $relationship->end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params));
     if (!$relationship->end_date) {
         $relationship->end_date = 'NULL';
     }
     $relationship->id = CRM_Utils_Array::value('relationship', $ids);
     $relationship->save();
     // add custom field values
     if (CRM_Utils_Array::value('custom', $params)) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_relationship', $relationship->id);
     }
     $relationship->free();
     if (CRM_Utils_Array::value('relationship', $ids)) {
         CRM_Utils_Hook::post('edit', 'Relationship', $relationship->id, $relationship);
     } else {
         CRM_Utils_Hook::post('create', 'Relationship', $relationship->id, $relationship);
     }
     return $relationship;
 }