You are here

function _ed_classified_form_submit in Classified Ads 5.2

Same name and namespace in other branches
  1. 5 ed_classified.module \_ed_classified_form_submit()
  2. 6.2 ed_classified.module \_ed_classified_form_submit()
  3. 7.2 ed_classified.module \_ed_classified_form_submit()

Implementation of form submission handler

1 call to _ed_classified_form_submit()
ed_classified_submit in ./ed_classified.module
Implementation of hook_submit() Crucial for 5.x, but removed in 6.x This hook will only trigger in Drupal 5.x
1 string reference to '_ed_classified_form_submit'
ed_classified_form in ./ed_classified.module
Implementation of hook_form(). This is how we piggyback off of the node type by tweaking the form with our elements. Drupal 5.x version is called $form_values rather than $form_state (both are an array).

File

./ed_classified.module, line 882
Simple text-based classified ads module. Michael Curry, Exodus Development, Inc. exodusdev@gmail.com for more information, please visit http://exodusdev.com/drupal/modules/classified.module Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights…

Code

function _ed_classified_form_submit($form, &$form_state) {
  module_load_include('inc', 'ed_classified', 'ed_classified_utils');
  $node = $form['#node'];
  $terms = $form['#post']['taxonomy'];
  $expiration_changed = FALSE;
  $expiration_old = $node->expires_on;
  $expiration = REQUEST_TIME + _ed_classified_days_to_seconds(_ed_classified_get_longest_duration($terms));
  $user_reset = $form_state['values']['reset_expiration'];
  if ($user_reset && !user_access('reset classified ad expiration')) {
    drupal_set_message(t('You do not have permission to reset ad expiration!'));
    $user_reset = FALSE;
  }

  // calc expiration date as appropriate
  // If new ad, or user wants to reset expiration, or taxonomy was changed... recalc expiration?
  if (empty($node->expires_on) || $user_reset) {

    // it's a new ad, or the user chose to reset the expiration
    $form_state['values']['expires_on'] = $expiration;

    // _ed_classified_get_default_ad_duration_in_seconds();
    // _edi_wd(sprintf('Ad expiration was %d (%s), now %d (%s)', $old_expires, _edi_safe_date_fmt($old_expires), $node->expires_on, _edi_safe_date_fmt($node->expires_on)));
    // we deal with republishing further down
    $expiration_changed = TRUE;
  }
  else {

    // This is not a new ad, and the user didn't choose to (or is not allowed to) reset the ad expiration
    // so, let's check the tentative new expiration, and if it's shorter than the current expiration - we need to use the new (shorter) one
    // Rationale: User may have changed the taxonomy terms for this ad (would be nice to be able to detect this accurately) and
    // we don't want someone to end up creating a long-living ad that they wouldn't be able to create by normal means
    if ($expiration < $node->expires_on) {
      $form_state['values']['expires_on'] = $expiration;

      // we deal with republishing further down
      $expiration_changed = TRUE;
    }
  }

  // log and notify if needed
  if ($expiration_changed) {

    // Sanity check - did expiration really change?
    if ($expiration_old != $expiration) {
      if (0 == $form_state['values']['status']) {
        $form_state['values']['status'] = 1;
        $publish_status = ' (setting to published)';
      }
      else {
        $publish_status = ' (already published)';
      }
      $msg = t('Ad expiration extended from %expires_old to %expires_new %publish_status.', array(
        '%nid' => $node->nid,
        '%expires_old' => _edi_safe_date_fmt($expiration_old),
        '%expires_new' => _edi_safe_date_fmt($expiration),
        '%publish_status' => $publish_status,
      ));
      _edi_wd($msg, WATCHDOG_NOTICE, _edi_l('view', drupal_get_path_alias('node/' . $node->nid)));
      drupal_set_message($msg);
    }
    else {
      drupal_set_message(t('No change to ad expiration date.'));
    }
  }

  //  print '<pre>'.print_r($form_state,1).'</pre>';
}