You are here

private function AdminForm::buildParticipantTab in Webform CiviCRM Integration 8.5

Event participant settings

1 call to AdminForm::buildParticipantTab()
AdminForm::buildForm in src/AdminForm.php
Build admin form for civicrm tab of a webform

File

src/AdminForm.php, line 772
Webform CiviCRM module's admin form.

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

private function buildParticipantTab() {
  $utils = \Drupal::service('webform_civicrm.utils');
  $this->form['participant'] = [
    '#type' => 'details',
    '#title' => t('Event Registration'),
    '#group' => 'webform_civicrm',
    '#attributes' => [
      'class' => [
        'civi-icon-participant',
      ],
    ],
  ];
  $reg_type = wf_crm_aval($this->data, 'participant_reg_type');
  $this->form['participant']['participant_reg_type'] = [
    '#type' => 'select',
    '#title' => t('Registration Method'),
    '#default_value' => $reg_type,
    '#options' => [
      t('- None -'),
      'all' => t('Register all contacts for the same event(s)'),
      'separate' => t('Register each contact separately'),
    ],
  ];
  $this
    ->help($this->form['participant']['participant_reg_type'], 'participant_reg_type');
  $this->form['participant']['event_type'] = [
    '#type' => 'select',
    '#title' => t('Show Events of Type(s)'),
    '#options' => [
      'any' => t('- Any Type -'),
    ] + $utils
      ->wf_crm_apivalues('event', 'getoptions', [
      'field' => 'event_type_id',
    ]),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:event_type', 'any'),
    '#prefix' => '<div id="event-reg-options-wrapper"><div class="web-civi-checkbox-set">',
    '#parents' => [
      'reg_options',
      'event_type',
    ],
    '#tree' => TRUE,
    '#multiple' => TRUE,
  ];
  $this->form['participant']['show_past_events'] = [
    '#type' => 'select',
    '#title' => t('Show Past Events'),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:show_past_events', 'now'),
    '#parents' => [
      'reg_options',
      'show_past_events',
    ],
    '#tree' => TRUE,
    '#options' => [
      'now' => t('- None -'),
      1 => t('All'),
      '-1 day' => t('Past Day'),
      '-1 week' => t('Past Week'),
      '-1 month' => t('Past Month'),
      '-2 month' => t('Past 2 Months'),
      '-3 month' => t('Past 3 Months'),
      '-6 month' => t('Past 6 Months'),
      '-1 year' => t('Past Year'),
      '-2 year' => t('Past 2 Years'),
    ],
  ];
  $this
    ->help($this->form['participant']['show_past_events'], 'reg_options_show_past_events');
  $this->form['participant']['show_future_events'] = [
    '#type' => 'select',
    '#title' => t('Show Future Events'),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:show_future_events', 1),
    '#parents' => [
      'reg_options',
      'show_future_events',
    ],
    '#tree' => TRUE,
    '#options' => [
      'now' => t('- None -'),
      1 => t('All'),
      '+1 day' => t('Next Day'),
      '+1 week' => t('Next Week'),
      '+1 month' => t('Next Month'),
      '+2 month' => t('Next 2 Months'),
      '+3 month' => t('Next 3 Months'),
      '+6 month' => t('Next 6 Months'),
      '+1 year' => t('Next Year'),
      '+2 year' => t('Next 2 Years'),
    ],
  ];
  $this
    ->help($this->form['participant']['show_future_events'], 'reg_options_show_future_events');
  $this->form['participant']['show_public_events'] = [
    '#type' => 'select',
    '#title' => t('Show Public Events'),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:show_public_events', 'title'),
    // This is breaking HTML in D8.
    // '#suffix' => '</div>',
    '#parents' => [
      'reg_options',
      'show_public_events',
    ],
    '#tree' => TRUE,
    '#options' => [
      'all' => t('Public and Private'),
      '1' => t('Public'),
      '0' => t('Private'),
    ],
  ];
  $this
    ->help($this->form['participant']['show_public_events'], 'reg_options_show_public_events');
  $this->form['participant']['title_display'] = [
    '#type' => 'select',
    '#title' => t('Title Display'),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:title_display', 'title'),
    '#suffix' => '</div>',
    '#parents' => [
      'reg_options',
      'title_display',
    ],
    '#tree' => TRUE,
    '#options' => [
      'title' => t('Title Only'),
      'title type' => t('Title + Event Type'),
      'title start dateformatYear' => t('Title + Year'),
      'title start dateformatPartial' => t('Title + Month + Year'),
      'title start dateformatFull' => t('Title + Start-Date'),
      'title start dateformatTime' => t('Title + Start-Time'),
      'title start dateformatDatetime' => t('Title + Start-Date-Time'),
      'title start end dateformatFull' => t('Title + Start-Date + End'),
      'title start end dateformatTime' => t('Title + Start-Time + End'),
      'title start end dateformatDatetime' => t('Title + Start-Date-Time + End'),
    ],
  ];
  $this
    ->help($this->form['participant']['title_display'], 'reg_options_title_display');
  $this->form['participant']['reg_options'] = [
    '#prefix' => '<div class="clearfix"> </div>',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#title' => t('Registration Options'),
    '#collapsible' => TRUE,
    '#collapsed' => isset($this->data['participant']),
    '#tree' => TRUE,
  ];
  $field = [
    '#type' => 'select',
    '#title' => t('Show Remaining Space in Events'),
    '#default_value' => wf_crm_aval($this->data, 'reg_options:show_remaining', 0),
    '#options' => [
      t('Never'),
      'always' => t('Always'),
      '0_full' => t('When full - 0 spaces left'),
    ],
  ];
  $this
    ->help($field, 'reg_options_show_remaining');
  foreach ([
    5,
    10,
    20,
    50,
    100,
    200,
    500,
    1000,
  ] as $num) {
    $field['#options'][$num] = t('When under :num spaces left', [
      ':num' => $num,
    ]);
  }
  $this->form['participant']['reg_options']['show_remaining'] = $field;
  $this->form['participant']['reg_options']['validate'] = [
    '#type' => 'checkbox',
    '#title' => t('Prevent Registration for Past/Full Events'),
    '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:validate'),
  ];
  $this
    ->help($this->form['participant']['reg_options']['validate'], 'reg_options_validate');
  $this->form['participant']['reg_options']['block_form'] = [
    '#type' => 'checkbox',
    '#title' => t('Block Form Access when Event(s) are Full/Ended'),
    '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:block_form'),
  ];
  $this->form['participant']['reg_options']['disable_unregister'] = [
    '#type' => 'checkbox',
    '#title' => t('Disable unregistering participants from unselected events.'),
    '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:disable_unregister'),
  ];
  $this->form['participant']['reg_options']['allow_url_load'] = [
    '#type' => 'checkbox',
    '#title' => t('Allow events to be autoloaded from URL'),
    '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:allow_url_load'),
  ];
  $this
    ->help($this->form['participant']['reg_options']['block_form'], 'reg_options_block_form');
  $this
    ->help($this->form['participant']['reg_options']['disable_unregister'], 'reg_options_disable_unregister');
  $this
    ->help($this->form['participant']['reg_options']['allow_url_load'], 'reg_options_allow_url_load');
  $this
    ->addAjaxItem('participant', 'participant_reg_type', 'participants');
  $this
    ->addAjaxItem('participant', 'event_type', 'participants');
  $this
    ->addAjaxItem('participant', 'show_past_events', 'participants');
  $this
    ->addAjaxItem('participant', 'show_future_events', 'participants');
  $this
    ->addAjaxItem('participant', 'show_public_events', 'participants');
  $this
    ->addAjaxItem('participant', 'title_display', 'participants');
  for ($n = 1; $reg_type && ($n <= count($this->data['contact']) && $reg_type != 'all' || $n == 1); ++$n) {
    $this->form['participant']['participants'][$n] = [
      '#type' => 'fieldset',
      '#title' => $reg_type == 'all' ? t('Registration') : $utils
        ->wf_crm_contact_label($n, $this->data, 'wrap'),
    ];
    $num = wf_crm_aval($this->data, "participant:{$n}:number_of_participant");
    if (!$num || $n > 1 && $reg_type == 'all') {
      $num = 0;
    }
    $this->form['participant']['participants'][$n]['participant_' . $n . '_number_of_participant'] = [
      '#type' => 'select',
      '#title' => $reg_type == 'all' ? t('Number of Event Sets') : t('Number of Event Sets for @contact', [
        '@contact' => $utils
          ->wf_crm_contact_label($n, $this->data, 'wrap'),
      ]),
      '#default_value' => $num,
      '#options' => range(0, $this->sets['participant']['max_instances']),
      '#prefix' => '<div class="number-of">',
      '#suffix' => '</div>',
    ];
    $this
      ->addAjaxItem("participant:participants:{$n}", 'participant_' . $n . '_number_of_participant', 'div');
    $particpant_extensions = [
      1 => 'role_id',
      2 => 'event_id',
      3 => 'event_type',
    ];
    for ($e = 1; $e <= $num; ++$e) {
      $fs = "participant_{$n}_event_{$e}_fieldset";
      $this->form['participant']['participants'][$n]['div'][$fs] = [
        '#type' => 'fieldset',
        '#title' => t('Event :num', [
          ':num' => $e,
        ]),
        '#attributes' => [
          'id' => $fs,
        ],
      ];
      foreach ($this->sets as $sid => $set) {
        if ($set['entity_type'] == 'participant') {
          $sid = 'civicrm_' . $n . '_participant_' . $e . '_' . $sid . '_fieldset';
          $class = 'web-civi-checkbox-set';
          if (!empty($set['sub_types'])) {
            $role_id = wf_crm_aval($this->data, "participant:{$n}:particpant:{$e}:role_id", '');
            $event_id = wf_crm_aval($this->data, "participant:{$n}:particpant:{$e}:event_id", '');
            $event_type = wf_crm_aval($this->data, 'reg_options:event_type', '');
            if ($event_id && $event_id !== 'create_civicrm_webform_element') {
              list($event_id, $event_type) = explode('-', $event_id);
            }
            $ext = $particpant_extensions[$set['extension_of']];
            if (!in_array(${$ext}, $set['sub_types'])) {
              $class .= ' hidden';
            }
            $class .= ' extends-condition ' . str_replace('_', '', $ext) . '-' . implode('-', $set['sub_types']);
          }
          $this->form['participant']['participants'][$n]['div'][$fs][$sid] = [
            '#type' => 'fieldset',
            '#title' => $set['label'],
            '#attributes' => [
              'id' => $sid,
              'class' => [
                $class,
              ],
            ],
            'js_select' => $this
              ->addToggle($sid),
          ];
          foreach ($set['fields'] as $fid => $field) {
            $id = 'civicrm_' . $n . '_participant_' . $e . '_' . $fid;
            $item = $this
              ->addItem($id, $field);
            if ($fid == 'participant_event_id') {
              $item['#prefix'] = '<div class="auto-width">';
              $item['#suffix'] = '</div>';
            }
            if ($fid == 'participant_event_id' || $fid == 'participant_role_id') {
              $item['#attributes']['onchange'] = "wfCiviAdmin.participantConditional('#{$fs}');";
              $item['#attributes']['class'][] = $fid;
              ${$fid} = wf_crm_aval($item, '#default_value');
            }
            $this->form['participant']['participants'][$n]['div'][$fs][$sid][$id] = $item;
          }
        }
      }
    }
  }
}