You are here

private function WebformCivicrmPreProcess::loadMemberships in Webform CiviCRM Integration 8.5

Load existing membership information and display a message to members.

id

Parameters

int $c:

1 call to WebformCivicrmPreProcess::loadMemberships()
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 415
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function loadMemberships($c, $cid) {
  $today = date('Y-m-d');
  foreach ($this
    ->findMemberships($cid) as $num => $membership) {

    // Only show 1 expired membership, and only if there are no active ones
    if (!$membership['is_active'] && $num) {
      break;
    }
    $type = $membership['membership_type_id'];
    $msg = t('@type membership for @contact has a status of "@status".', [
      '@type' => $this
        ->getMembershipTypeField($type, 'name'),
      '@contact' => $this->info['contact'][$c]['contact'][1]['display_name'],
      '@status' => $membership['status'],
    ]);
    if (!empty($membership['end_date'])) {
      $end = [
        '@date' => \CRM_Utils_Date::customFormat($membership['end_date']),
      ];
      $msg .= ' ' . ($membership['end_date'] > $today ? t('Expires @date.', $end) : t('Expired @date.', $end));
    }
    $this
      ->setMessage($msg);
    for ($n = 1; $n <= $this->data['membership'][$c]['number_of_membership']; ++$n) {
      $fid = "civicrm_{$c}_membership_{$n}_membership_membership_type_id";
      if (empty($info['membership'][$c]['membership'][$n]) && ($this
        ->getData($fid) == $type || array_key_exists($type, $this
        ->getExposedOptions($fid)))) {
        $this->info['membership'][$c]['membership'][$n] = $membership;
        break;
      }
    }
  }
}