/**
 * ContactSegment.Delete API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contact_segment_delete($params)
{
    if (array_key_exists('id', $params)) {
        return civicrm_api3_create_success(CRM_Contactsegment_BAO_ContactSegment::deleteWithId($params['id']), $params, 'ContactSegment', 'Delete');
    } else {
        throw new API_Exception('Id is a mandatory param when deleting a contact segment', 'mandatory_id_missing', 020);
    }
}
 /**
  * Function to add or update contact segment
  * 
  * @param array $params
  * @return array $result
  * @throws Exception when params empty
  * @access public
  * @static
  */
 public static function add($params)
 {
     $result = array();
     $preContactSegment = array();
     if (empty($params)) {
         throw new Exception('Params can not be empty when adding or updating a contact segment', 9003);
     }
     $contactSegment = new CRM_Contactsegment_BAO_ContactSegment();
     $op = "create";
     // check if there is already a record for combination of role_value, contact_id and segment. If so, use that id to edit
     $contactSegment->checkAlreadyExists($params);
     if (isset($params['id'])) {
         $contactSegment->id = $params['id'];
         // pre hook if edit
         $op = "edit";
         $contactSegment->find(true);
         self::storeValues($contactSegment, $preContactSegment);
         CRM_Utils_Hook::pre($op, 'ContactSegment', $contactSegment->id, $preContactSegment);
     } else {
         $params['is_active'] = 1;
     }
     $fields = self::fields();
     foreach ($params as $paramKey => $paramValue) {
         if (isset($fields[$paramKey])) {
             $contactSegment->{$paramKey} = $paramValue;
         }
     }
     $contactSegment->processStartEndDate($params);
     $contactSegment->save();
     // post hook
     CRM_Utils_Hook::post($op, 'ContactSegment', $contactSegment->id, $contactSegment);
     self::storeValues($contactSegment, $result);
     // function to add or update parent if child
     self::processParentChild($contactSegment);
     return $result;
 }
/**
 * ContactSegment.Get API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contact_segment_get($params)
{
    return civicrm_api3_create_success(CRM_Contactsegment_BAO_ContactSegment::getValues($params), $params, 'ContactSegment', 'Get');
}
/**
 * ContactSegment.Create API
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_contact_segment_create($params)
{
    return civicrm_api3_create_success(CRM_Contactsegment_BAO_ContactSegment::add($params), $params, 'ContactSegment', 'Create');
}
/**
 * Implementation of hook civicrm_tabs
 * to add a contact segment tab to the contact summary
 *
 * @param array $tabs
 * @param int $contactID
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tabs
 */
function contactsegment_civicrm_tabs(&$tabs, $contactID)
{
    $tabs[] = CRM_Contactsegment_BAO_ContactSegment::tabs($tabs, $contactID);
}