You are here

function wf_crm_case_find in Webform CiviCRM Integration 7.3

Fetch an existing case for client(s) Based on url param or status_id and case_type_id

Parameters

$params: Array of case info in standard webform_civicrm format

$ids: Passed if client id refers to a contact number and not a contact id

2 calls to wf_crm_case_find()
wf_crm_process_submission in ./webform_civicrm_forms.inc
Webform submission handler Create/update CiviCRM contacts and related data Called by presave, insert and update webform hooks
_wf_crm_frontend_form_alter in ./webform_civicrm_forms.inc
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

./webform_civicrm_utils.inc, line 1396
Webform CiviCRM module's common utility functions.

Code

function wf_crm_case_find($params, $ids = NULL) {

  // Look for case id passed in url
  if (!empty($_GET['caseid']) && is_numeric($_GET['caseid'])) {
    $result = wf_civicrm_api('case', 'get', array(
      'case_id' => $_GET['caseid'],
    ));
    if (isset($result['values'][$_GET['caseid']])) {
      $case = $result['values'][$_GET['caseid']];

      // Verify that case is correct type and user has access to this case
      if ($case['case_type_id'] == $params['case'][1]['case_type_id']) {

        // Respect admin privledges
        if (user_access('access all cases and activities')) {
          return $case;
        }
        $case_cids = array();
        foreach ($case['contacts'] as $contact) {
          $case_cids[] = $contact['contact_id'];
        }
        foreach ((array) wf_crm_aval($ids, 'cid', $params['case'][1]['client_id']) as $cid) {
          if (in_array($cid, $case_cids)) {

            // User is involved in the case therefore the found case is correct
            return $case;
          }
        }
      }
    }
  }

  // Match existing case based on client and status
  if (!empty($params['existing_case_status']) && !empty($params['case'][1]['client_id'])) {
    foreach ((array) $params['case'][1]['client_id'] as $client_id) {
      if ($ids !== NULL) {
        $client_id = wf_crm_aval($ids, "cid:{$client_id}");
      }
      if ($client_id) {
        $result = wf_civicrm_api('case', 'get', array(
          'client_id' => $client_id,
        ));
        foreach (wf_crm_aval($result, 'values', array()) as $case) {
          if (empty($case['is_deleted']) && in_array($case['status_id'], $params['existing_case_status'])) {
            $case_types = $case['case_type_id'];
            if (is_string($case_types)) {
              $case_types = explode(',', $case_types);
            }
            if (in_array($params['case'][1]['case_type_id'], $case_types)) {
              return $case;
            }
          }
        }
      }
    }
  }
}