You are here

private function WebformCivicrmPreProcess::findExistingActivities in Webform CiviCRM Integration 8.5

Find activity ids based on url input or "existing activity" settings

1 call to WebformCivicrmPreProcess::findExistingActivities()
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
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

src/WebformCivicrmPreProcess.php, line 700
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function findExistingActivities() {
  $request = \Drupal::request();
  $utils = \Drupal::service('webform_civicrm.utils');

  // Support legacy url param
  if (empty($request->query
    ->get('activity1')) && !empty($request->query
    ->get('aid'))) {
    $request->query
      ->set('activity1', $request->query
      ->get('aid'));
  }
  for ($n = 1; $n <= $this->data['activity']['number_of_activity']; ++$n) {
    if (!empty($this->data['activity'][$n]['activity'][1]['target_contact_id'])) {
      $targets = [];
      foreach ($this->data['activity'][$n]['activity'][1]['target_contact_id'] as $c) {
        if (!empty($this->ent['contact'][$c]['id'])) {
          $targets[] = $this->ent['contact'][$c]['id'];
        }
      }
      if ($targets) {
        if ($utils
          ->wf_crm_is_positive($request->query
          ->get("activity{$n}"))) {
          $id = $request->query
            ->get("activity{$n}");
          $item = $utils
            ->wf_civicrm_api('activity', 'getsingle', [
            'id' => $id,
            'return' => [
              'target_contact_id',
            ],
          ]);
          if (array_intersect($targets, $item['target_contact_id'])) {
            $this->ent['activity'][$n] = [
              'id' => $id,
            ];
          }
        }
        elseif (!empty($this->data['activity'][$n]['existing_activity_status'])) {

          // If the activity type hasn't been set, bail.
          if (empty($this->data['activity'][$n]['activity'][1]['activity_type_id'])) {
            $logger = \Drupal::logger('webform_civicrm');
            $logger
              ->error("Activity type to update hasn't been set, so won't try to update activity. location = %1, webform activity number : %2", [
              '%1' => $request
                ->getRequestUri(),
              '%2' => $n,
            ]);
            continue;
          }

          // The api doesn't accept an array of target contacts so we'll do it as a loop
          // If targets has more than one entry, the below could result in the wrong activity getting updated.
          foreach ($targets as $cid) {
            $params = [
              'sequential' => 1,
              'target_contact_id' => $cid,
              'status_id' => [
                'IN' => (array) $this->data['activity'][$n]['existing_activity_status'],
              ],
              'is_deleted' => '0',
              'is_current_revision' => '1',
              'options' => [
                'limit' => 1,
              ],
            ];
            $params['activity_type_id'] = $this->data['activity'][$n]['activity'][1]['activity_type_id'];
            $items = $utils
              ->wf_crm_apivalues('activity', 'get', $params);
            if (isset($items[0]['id'])) {
              $this->ent['activity'][$n] = [
                'id' => $items[0]['id'],
              ];
              break;
            }
          }
        }
      }
    }
  }
}