You are here

private function WebformCivicrmPostProcess::processGrants in Webform CiviCRM Integration 8.5

Save grants

1 call to WebformCivicrmPostProcess::processGrants()
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 1641
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

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

      // Set some defaults in create mode
      if (empty($this->ent['grant'][$n]['id'])) {

        // Automatic status... for lack of anything fancier just pick the first option ("Submitted" on a standard install)
        if (empty($params['status_id'])) {
          $options = $utils
            ->wf_crm_apivalues('grant', 'getoptions', [
            'field' => 'status_id',
          ]);
          $params['status_id'] = current(array_keys($options));
        }
        if (empty($params['application_received_date'])) {
          $params['application_received_date'] = 'now';
        }
        if (empty($params['grant_report_received'])) {
          $params['grant_report_received'] = '0';
        }
      }
      else {
        $params['id'] = $this->ent['grant'][$n]['id'];
      }

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

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

        // Store id
        $this->ent['grant'][$n]['id'] = $result['id'];

        // Save attachments
        if (isset($data['grantupload'])) {
          $this
            ->processAttachments('grant', $n, $result['id'], empty($params['id']));
        }
      }
    }
  }
}