You are here

public function ShurlyEditForm::submitForm in ShURLy 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/ShurlyEditForm.php, line 143

Class

ShurlyEditForm
ShurlyActionsForm.

Namespace

Drupal\shurly\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $storage =& $form_state
    ->getStorage();
  $rid = $storage['shurly']['rid'];
  $new_destination = $form_state
    ->getValue('destination');
  $request_time = \Drupal::time()
    ->getRequestTime();

  // Get the most recent history for this redirect (if exists)
  $previous_history = \Drupal::database()
    ->query('SELECT * FROM {shurly_history} WHERE rid = :rid ORDER BY vid DESC LIMIT 1', [
    'rid' => $rid,
  ])
    ->fetchAssoc();

  // Still to add: vid, count
  // First save the current data into the history table for future reference.
  \Drupal::database()
    ->query('INSERT INTO {shurly_history} (rid, vid, source, destination, last_date, count) VALUES (:rid, :vid, :source, :destination, :last_date, :count)', [
    ':rid' => $rid,
    ':vid' => isset($previous_history['vid']) ? $previous_history['vid'] + 1 : 0,
    ':source' => $storage['shurly']['source'],
    ':destination' => $storage['shurly']['destination'],
    ':last_date' => $request_time,
    ':count' => $storage['shurly']['count'],
  ]);

  // Update access information on this row.
  \Drupal::database()
    ->query('UPDATE {shurly} SET destination = :new_destination, count = :reset_count WHERE rid = :rid', [
    ':new_destination' => $new_destination,
    ':reset_count' => 0,
    'rid' => $rid,
  ]);
  unset($storage['shurly']);
  $this
    ->messenger()
    ->addStatus(t('URL has been edited.'));
}