示例#1
0
/**
 * Get details of a particular case, or search for cases, depending on params.
 *
 * Please provide one (and only one) of the four get/search parameters:
 *
 * @param array $params
 *   'id' => if set, will get all available info about a case, including contacts and activities
 *
 *   // if no case_id provided, this function will use one of the following search parameters:
 *   'client_id' => finds all cases with a specific client
 *   'activity_id' => returns the case containing a specific activity
 *   'contact_id' => finds all cases associated with a contact (in any role, not just client)
 *
 * @throws API_Exception
 * @return array
 *   (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
 */
function civicrm_api3_case_get($params)
{
    $options = _civicrm_api3_get_options_from_params($params);
    $sql = CRM_Utils_SQL_Select::fragment();
    // Add clause to search by client
    if (!empty($params['contact_id'])) {
        $contacts = array();
        foreach ((array) $params['contact_id'] as $c) {
            if (!CRM_Utils_Rule::positiveInteger($c)) {
                throw new API_Exception('Invalid parameter: contact_id. Must provide numeric value(s).');
            }
            $contacts[] = $c;
        }
        $sql->join('civicrm_case_contact', 'INNER JOIN civicrm_case_contact ON civicrm_case_contact.case_id = a.id')->where('civicrm_case_contact.contact_id IN (' . implode(',', $contacts) . ')');
    }
    // Add clause to search by activity
    if (!empty($params['activity_id'])) {
        if (!CRM_Utils_Rule::positiveInteger($params['activity_id'])) {
            throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.');
        }
        $activityId = $params['activity_id'];
        $originalId = CRM_Core_DAO::getFieldValue('CRM_Activity_BAO_Activity', $activityId, 'original_id');
        if ($originalId) {
            $activityId .= ',' . $originalId;
        }
        $sql->join('civicrm_case_activity', 'INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = a.id')->where("civicrm_case_activity.activity_id IN ({$activityId})");
    }
    $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case', $sql);
    if (empty($options['is_count'])) {
        // For historic reasons we return these by default only when fetching a case by id
        if (!empty($params['id']) && empty($options['return'])) {
            $options['return'] = array('contacts' => 1, 'activities' => 1, 'contact_id' => 1);
        }
        foreach ($foundcases['values'] as &$case) {
            _civicrm_api3_case_read($case, $options);
        }
    }
    return $foundcases;
}
示例#2
0
/**
* Get details of a particular case, or search for cases, depending on params
*
* Please provide one (and only one) of the four get/search parameters:
*
* @param array(
   'id' => if set, will get all available info about a case, including contacts and activities
*
* // if no case_id provided, this function will use one of the following search parameters:
* 'client_id' => finds all cases with a specific client
* 'activity_id' => returns the case containing a specific activity
* 'contact_id' => finds all cases associated with a contact (in any role, not just client)
*
* {@getfields case_get}
*
* @return (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
* @access public
* @todo Erik Hommel 16 dec 2010 check if all DB fields are returned
*/
function civicrm_api3_case_get($params)
{
    $options = _civicrm_api3_get_options_from_params($params);
    //search by client
    if (!empty($params['contact_id'])) {
        $ids = array();
        foreach ((array) $params['contact_id'] as $cid) {
            if (is_numeric($cid)) {
                $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE));
            }
        }
        $cases = array();
        foreach ($ids as $id) {
            if ($case = _civicrm_api3_case_read($id, $options)) {
                $cases[$id] = $case;
            }
        }
        return civicrm_api3_create_success($cases, $params, 'case', 'get');
    }
    //search by activity
    if (!empty($params['activity_id'])) {
        if (!is_numeric($params['activity_id'])) {
            return civicrm_api3_create_error('Invalid parameter: activity_id. Must provide a numeric value.');
        }
        $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']);
        if (!$caseId) {
            return civicrm_api3_create_success(array(), $params, 'case', 'get');
        }
        $case = array($caseId => _civicrm_api3_case_read($caseId, $options));
        return civicrm_api3_create_success($case, $params, 'case', 'get');
    }
    //search by contacts
    if ($contact = CRM_Utils_Array::value('contact_id', $params)) {
        if (!is_numeric($contact)) {
            return civicrm_api3_create_error('Invalid parameter: contact_id.  Must provide a numeric value.');
        }
        $sql = "\nSELECT DISTINCT case_id\n  FROM civicrm_relationship\n WHERE (contact_id_a = {$contact}\n    OR contact_id_b = {$contact})\n   AND case_id IS NOT NULL";
        $dao =& CRM_Core_DAO::executeQuery($sql);
        $cases = array();
        while ($dao->fetch()) {
            $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options);
        }
        return civicrm_api3_create_success($cases, $params, 'case', 'get');
    }
    // For historic reasons we always return these when an id is provided
    $caseId = CRM_Utils_Array::value('id', $params);
    if ($caseId) {
        $options['return'] = array('contacts' => 1, 'activities' => 1);
    }
    $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case');
    $cases = array();
    foreach ($foundcases['values'] as $foundcase) {
        if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) {
            $cases[$foundcase['id']] = $case;
        }
    }
    return civicrm_api3_create_success($cases, $params, 'case', 'get');
}
/**
* Get details of a particular case, or search for cases, depending on params
*
* Please provide one (and only one) of the four get/search parameters:
*
* @param array(
   'case_id'    => if set, will get all available info about a case, including contacts and activities
*
* // if no case_id provided, this function will use one of the following search parameters:
* 'client_id'   => finds all cases with a specific client
* 'activity_id' => returns the case containing a specific activity
* 'contact_id'  => finds all cases associated with a contact (in any role, not just client)
*
* {@getfields case_get}
*
* @return (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
* @access public
* @todo Eileen McNaughton 13 Oct 2011 No unit test
* @todo Erik Hommel 16 dec 2010 check if all DB fields are returned
*/
function civicrm_api3_case_get($params)
{
    // Get mode
    if (!($caseId = CRM_Utils_Array::value('id', $params))) {
        $caseId = CRM_Utils_Array::value('case_id', $params);
    }
    if ($caseId) {
        // Validate param
        if (!is_numeric($caseId)) {
            return civicrm_api3_create_error('Invalid parameter: case_id. Must provide a numeric value.');
        }
        $case = _civicrm_api3_case_read($caseId);
        if ($case) {
            //get case contacts
            $contacts = CRM_Case_BAO_Case::getcontactNames($caseId);
            $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId);
            $case['contacts'] = array_merge($contacts, $relations);
            //get case activities
            $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = {$caseId}";
            $dao = CRM_Core_DAO::executeQuery($query);
            $case['activities'] = array();
            while ($dao->fetch()) {
                $case['activities'][] = $dao->activity_id;
            }
            $cases = array($caseId => $case);
            return civicrm_api3_create_success($cases);
        } else {
            return civicrm_api3_create_success(array());
        }
    }
    //search by client
    if ($client = CRM_Utils_Array::value('client_id', $params)) {
        $ids = array();
        foreach ((array) $client as $cid) {
            if (is_numeric($cid)) {
                $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE));
            }
        }
        if (empty($ids)) {
            return civicrm_api3_create_success(array());
        }
        $cases = array();
        foreach ($ids as $id) {
            $cases[$id] = _civicrm_api3_case_read($id);
        }
        return civicrm_api3_create_success($cases);
    }
    //search by activity
    if ($act = CRM_Utils_Array::value('activity_id', $params)) {
        if (!is_numeric($act)) {
            return civicrm_api3_create_error('Invalid parameter: activity_id. Must provide a numeric value.');
        }
        $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($act);
        if (!$caseId) {
            return civicrm_api3_create_success(array());
        }
        $case = array($caseId => _civicrm_api3_case_read($caseId));
        return civicrm_api3_create_success($case);
    }
    //search by contacts
    if ($contact = CRM_Utils_Array::value('contact_id', $params)) {
        if (!is_numeric($contact)) {
            return civicrm_api3_create_error('Invalid parameter: contact_id.  Must provide a numeric value.');
        }
        $sql = "\nSELECT DISTINCT case_id\n  FROM civicrm_relationship\n WHERE (contact_id_a = {$contact}\n    OR contact_id_b = {$contact})\n   AND case_id IS NOT NULL";
        $dao =& CRM_Core_DAO::executeQuery($sql);
        $cases = array();
        while ($dao->fetch()) {
            $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id);
        }
        return civicrm_api3_create_success($cases);
    }
    return civicrm_api3_create_error('Missing required parameter. Must provide case_id, client_id, activity_id, or contact_id.');
}