You are here

function shurly_edit_form_submit in ShURLy 7

Submit handler for above form

File

./shurly.module, line 331
description http://www.youtube.com/watch?v=Qo7qoonzTCE

Code

function shurly_edit_form_submit($form, &$form_state) {

  //check if we have gone through a confirm process. If not, store the form values and tell the form api to rebuild this form

  //using the confirm_form function
  if (!isset($form_state['storage']['confirm'])) {
    $form_state['storage']['confirm'] = TRUE;

    // this will cause the form to be rebuilt, entering the confirm part of the form
    $form_state['rebuild'] = TRUE;

    // along with this
    $form_state['storage']['original_values'] = $form_state['values'];

    //stores the original form values so we can access them later
    $form_state['storage']['new_values'] = $form_state['input'];

    //stores the original form values so we can access them later
  }
  else {
    drupal_set_message(t('URL has been edited'));
    $rid = $form_state['storage']['original_values']['rid'];
    $dest = $form_state['storage']['new_values']['destination'];

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

    // Still to add: vid, count
    // First save the current data into the history table for future reference
    db_query('INSERT INTO {shurly_history} (rid, vid, source, destination, last_date, count) VALUES (:rid, :vid, :source, :destination, :last_date, :count)', array(
      ':rid' => $form_state['storage']['original_values']['rid'],
      ':vid' => isset($previous_history['vid']) ? $previous_history['vid'] + 1 : 0,
      ':source' => $form_state['storage']['original_values']['source'],
      ':destination' => $form_state['storage']['original_values']['destination'],
      ':last_date' => time(),
      ':count' => $form_state['storage']['original_values']['count'],
    ));

    // update access information on this row
    db_query('UPDATE {shurly} SET destination = :new_destination, hash = :new_hash, count = :reset_count WHERE rid = :rid', array(
      ':new_destination' => $dest,
      ':new_hash' => md5($dest),
      ':reset_count' => 0,
      'rid' => $rid,
    ));
  }
}