You are here

public function RateWidgetBaseForm::ajaxSubmit in Rate 8.2

Ajax submit handler.

File

src/Form/RateWidgetBaseForm.php, line 409

Class

RateWidgetBaseForm
Form controller for rate vote forms.

Namespace

Drupal\rate\Form

Code

public function ajaxSubmit(array $form, FormStateInterface $form_state) {
  $entity = $this
    ->getEntity();
  $settings = $form_state
    ->get('settings');
  $options = $form_state
    ->get('options');
  $voting = $settings
    ->get('voting');
  $display = $settings
    ->get('display');
  $results = $settings
    ->get('results');
  $plugin = $form_state
    ->get('plugin');
  $result_function = $this
    ->getResultFunction($form_state);
  $template = $settings
    ->get('template');
  $rate_widget = $form_state
    ->get('rate_widget');
  $voted_entity_id = $entity
    ->getVotedEntityId();
  $voted_entity_type = $entity
    ->getVotedEntityType();
  $user_input = $form_state
    ->getUserInput()['value'];
  $value_type = $entity
    ->get('value_type')->value;
  $user_id = $entity
    ->get('user_id')->target_id;
  $disable_log = $this->config
    ->get('disable_log');
  $voted_entity = $this->entityTypeManager
    ->getStorage($voted_entity_type)
    ->load($voted_entity_id);
  $deadline_disabled = $this
    ->checkDeadlineDisabled($voted_entity, $voting);
  $display_readonly = isset($display['readonly']) && $display['readonly'] === 1 ? TRUE : FALSE;
  if (!$plugin
    ->canVote($entity) || $display_readonly === TRUE || $deadline_disabled === TRUE) {
    $form['value']['#disabled'] = TRUE;
    $form['value']['#prefix'] = '<div class="' . $template . '-rating-wrapper rate-disabled" can-edit="false">';
  }
  else {
    $form['value']['#disabled'] = FALSE;
    $form['value']['#prefix'] = '<div class="' . $template . '-rating-wrapper rate-enabled" can-edit="true">';
  }
  $this
    ->save($form, $form_state, $display_readonly, $deadline_disabled);

  // If the user clicked the same option - delete last vote.
  if ($form_state
    ->getUserInput()['value'] == $form['value']['#default_value']) {
    $entity
      ->delete();
    if ($disable_log == FALSE) {
      $message = 'Vote ' . $user_input . ' on ' . $voted_entity_id . ' was cancelled. Vote ' . $entity
        ->id() . ' was deleted.';
      $this->logger
        ->notice($message);
    }
    $entity = $plugin
      ->getEntityForVoting($form_state
      ->get('entity_type'), $form_state
      ->get('entity_bundle'), $form_state
      ->get('entity_id'), $entity
      ->bundle(), $value_type, $rate_widget, $settings, $user_id);
  }
  else {
    if ($disable_log == FALSE) {
      $message = 'Vote ' . $entity
        ->id() . ' saved. Voted ' . $user_input . ' on ' . $voted_entity_id . '.';
      $this->logger
        ->notice($message);
    }
  }
  if (isset($results['result_position']) && $results['result_position'] !== 'hidden') {
    $form['result']['#children']['result'] = $plugin
      ->getVoteSummary($entity);
    $form['#results'] = $plugin
      ->getVoteSummary($entity);
    $form['#results']['#disabled'] = $form['value']['#disabled'];
    $form['#results']['#deadline_disabled'] = $form['value']['#deadline_disabled'];
  }

  // Get the votes to populate the option results.
  $vote_type = $entity
    ->bundle();
  $votes = [];
  if (isset($results['result_type'])) {
    if ($results['result_type'] == 'user_vote_empty' || $results['result_type'] == 'user_vote_average') {
      $votes = $plugin
        ->getVotes($form_state
        ->get('entity_type'), $form_state
        ->get('entity_bundle'), $form_state
        ->get('entity_id'), $vote_type, $value_type, $rate_widget, $user_id);
      if ($results['result_type'] == 'user_vote_average' && count($votes) == 0) {
        $votes = $plugin
          ->getVotes($form_state
          ->get('entity_type'), $form_state
          ->get('entity_bundle'), $form_state
          ->get('entity_id'), $vote_type, $value_type, $rate_widget);
      }
    }
    else {
      $votes = $plugin
        ->getVotes($form_state
        ->get('entity_type'), $form_state
        ->get('entity_bundle'), $form_state
        ->get('entity_id'), $vote_type, $value_type, $rate_widget);
    }
  }

  // Sum/count of all options for numberupdown/fivestar.
  $all_votes = 0;
  $count_votes = 0;
  foreach ($votes as $vote) {
    $all_votes += array_sum($vote);
    $count_votes += count($vote);
  }

  // Handle the different templates/vote types.
  if (isset($template)) {
    if (isset($results['result_type']) && $results['result_type'] != 'vote_hidden') {
      foreach ($options as $key => $option) {
        if ($template == 'numberupdown') {
          if ($key > 0) {
            $form['value'][$key]['#option_result'] = isset($all_votes) ? $all_votes : 0;
          }
        }
        elseif ($template != 'fivestar') {
          if ($value_type == 'option') {
            $vote_sum[$key] = isset($votes[$key]) ? count($votes[$key]) : 0;
          }
          else {
            $vote_sum[$key] = isset($votes[$key]) ? array_sum($votes[$key]) : 0;
          }
          $form['value'][$key]['#option_result'] = $vote_sum[$key] < 0 ? $vote_sum[$key] * -1 : $vote_sum[$key];
        }
      }

      // Show the result after the last option (fivestar).
      if ($template == 'fivestar') {
        $entity_value = $entity
          ->getValue();
        if ($results['result_type'] == 'user_vote_empty') {
          $vote_avg = isset($entity_value) ? $entity_value : 0;
        }
        elseif ($results['result_type'] == 'user_vote_average') {
          $vote_avg = isset($entity_value) ? $entity_value : number_format($this
            ->getResults($result_function), 1);
        }
        elseif ($results['result_type'] == 'vote_average') {
          $vote_avg = number_format($this
            ->getResults($result_function), 1);
        }
        $form['value'][$key]['#option_result'] = $vote_avg;
        if ($entity
          ->isNew()) {
          foreach ($options as $option_id => $option) {
            if ($option_id <= $vote_avg) {
              $form['value'][$option_id]['#label_attributes']['class']['average'] = 'average';
            }
          }
        }
        else {
          if ($results['result_type'] == 'user_vote_average' || $results['result_type'] == 'vote_average') {
            foreach ($options as $option_id => $option) {
              if (isset($form['value'][$option_id]['#label_attributes']['class']['average'])) {
                unset($form['value'][$option_id]['#label_attributes']['class']['average']);
              }
            }
          }
        }
      }
    }
  }

  // Set 'rate-voted' html class to radio element.
  $form = $this
    ->setVotedClass($form, $entity, $options);
  $form_state
    ->setRebuild(TRUE);
  return $form;
}