You are here

private function WebformCivicrmPostProcess::processCases in Webform CiviCRM Integration 8.5

Save case data

1 call to WebformCivicrmPostProcess::processCases()
WebformCivicrmPostProcess::postSave in src/WebformCivicrmPostProcess.php
Process webform submission after it is has been saved. Called by the following hooks:

File

src/WebformCivicrmPostProcess.php, line 1367
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function processCases() {
  $utils = \Drupal::service('webform_civicrm.utils');
  foreach (wf_crm_aval($this->data, 'case', []) as $n => $data) {
    if (is_array($data) && !empty($data['case'][1]['client_id'])) {
      $params = $data['case'][1];

      //Check if webform is set to update the existing case on contact.
      if (!empty($this->data['case'][$n]['existing_case_status']) && empty($this->ent['case'][$n]['id']) && !empty($this->ent['contact'][$n]['id'])) {
        $existingCase = $this
          ->findCaseForContact($this->ent['contact'][$n]['id'], [
          'case_type_id' => wf_crm_aval($this->data['case'][$n], 'case:1:case_type_id'),
          'status_id' => $this->data['case'][$n]['existing_case_status'],
        ]);
        if (!empty($existingCase['id'])) {
          $this->ent['case'][$n]['id'] = $existingCase['id'];
        }
      }

      // Set some defaults in create mode
      // Create duplicate case based on existing case if 'duplicate_case' checkbox is checked
      if (empty($this->ent['case'][$n]['id']) || !empty($this->data['case'][$n]['duplicate_case'])) {
        if (empty($params['case_type_id'])) {

          // Abort if no case type.
          continue;
        }

        // Automatic status... for lack of anything fancier just pick the first option ("Ongoing" on a standard install)
        if (empty($params['status_id'])) {
          $options = $utils
            ->wf_crm_apivalues('case', 'getoptions', [
            'field' => 'status_id',
          ]);
          $params['status_id'] = current(array_keys($options));
        }
        if (empty($params['subject'])) {
          $params['subject'] = Html::escape($this->node
            ->get('title'));
        }

        // Automatic creator_id - default to current user or contact 1
        if (empty($data['case'][1]['creator_id'])) {
          if (\Drupal::currentUser()
            ->isAuthenticated()) {
            $params['creator_id'] = $utils
              ->wf_crm_user_cid();
          }
          elseif (!empty($this->ent['contact'][1]['id'])) {
            $params['creator_id'] = $this->ent['contact'][1]['id'];
          }
          else {

            // Abort if no creator available
            continue;
          }
        }
      }
      else {
        $params['id'] = $this->ent['case'][$n]['id'];

        // These params aren't allowed in update mode
        unset($params['creator_id']);
      }

      // If orig_contact_id is missing and there's more than one Contact
      // on the Case, set orig_contact_id to the current ID. Otherwise
      // we get a Case.create error "Case is linked with more than one
      // contact id".
      if (empty($params['orig_contact_id']) && !empty($params['id'])) {
        $caseContactCount = $utils
          ->wf_civicrm_api('CaseContact', 'getcount', [
          'case_id' => $params['id'],
        ]);
        if ($caseContactCount > 1 && !empty($params['client_id'])) {
          $params['orig_contact_id'] = current((array) $params['client_id']);
        }
      }

      // Allow "automatic" status to pass-thru
      if (empty($params['status_id'])) {
        unset($params['status_id']);
      }
      $result = $utils
        ->wf_civicrm_api('case', 'create', $params);

      // Final processing if save was successful
      if (!empty($result['id'])) {

        // handle case tags
        $this
          ->handleEntityTags('case', $result['id'], $n, $params);

        // Store id
        $this->ent['case'][$n]['id'] = $result['id'];
        $relationshipStartDate = date('Ymd');

        // Save case roles
        foreach ($params as $param => $val) {
          if ($val && strpos($param, 'role_') === 0) {
            foreach ((array) $params['client_id'] as $client) {
              foreach ((array) $val as $contactB) {
                $relationshipParams = [
                  'relationship_type_id' => substr($param, 5),
                  'contact_id_a' => $client,
                  'contact_id_b' => $contactB,
                  'case_id' => $result['id'],
                  'is_active' => TRUE,
                ];

                // We can't create a duplicate relationship so check if active relationship exists first.
                $existingRelationships = $utils
                  ->wf_civicrm_api('relationship', 'get', $relationshipParams);
                if (!empty($existingRelationships['count'])) {

                  // Update the existing active relationship (case role)
                  $relationshipParams['id'] = reset($existingRelationships['values'])['id'];
                }
                else {

                  // We didn't used to set start_date - now we do when creating new relationships (case roles)
                  $relationshipParams['start_date'] = $relationshipStartDate;
                }
                $utils
                  ->wf_civicrm_api('relationship', 'create', $relationshipParams);
              }
            }
          }
        }
      }
    }
  }
}