You are here

function classified_form in Classified Ads 6.3

Same name and namespace in other branches
  1. 7.3 classified.module \classified_form()

Implements hook_form().

  • Insert title, body
  • Insert expiration date depending on admin permission

File

./classified.module, line 879
A pure D6 classified ads module inspired by the ed_classified module.

Code

function classified_form($node, &$form_state) {

  // Needed for body size limit and date display.
  drupal_add_js(drupal_get_path('module', 'classified') . '/classified.js');
  drupal_add_js(array(
    'classified' => array(
      'max_length' => _classified_get('max-length'),
    ),
  ), 'setting');
  $type = node_get_types('type', $node);
  $form = array();
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#weight' => -5,
    );
  }
  if ($type->has_body) {

    // In Drupal 6, we can use node_body_field() to get the body and filter
    // elements. This replaces the old textarea + filter_form() method of
    // setting this up. It will also ensure the teaser splitter gets set up
    // properly.
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }

  // Can only happen when creating a new node
  if (empty($node->expires)) {
    $lifetimes = _classified_get('lifetimes');
    $node->expires = $_SERVER['REQUEST_TIME'] + reset($lifetimes) * 24 * 60 * 60;
    unset($lifetimes);
  }

  // The form can be submitted for a field (image) instead of the whole form,
  // in which case the expire_date|mode keys will not be set, so testing for
  // "submitted" is useless.
  $date = isset($form_state['values']['expire_date']) ? $form_state['values']['expire_date'] : _classified_get_date_from_timestamp($node->expires);
  $mode = isset($form_state['values']['expire_mode']) ? $form_state['values']['expire_mode'] : NULL;
  $form['expires'] = array(
    '#type' => 'value',
    '#value' => $node->expires,
  );

  // Placeholder for submit-type data from expire_date.
  $form['expire_date_ts'] = $form['expires'];

  // Expiration fieldset: choices depend on permissions.
  $is_update = isset($node->nid);
  $is_privileged = user_access('administer classified ads') || user_access('reset classified ads expiration');
  $weight = module_exists('content') ? content_extra_field_weight('classified', 'expires') : 0;
  $form['expires_fs'] = array(
    '#type' => 'fieldset',
    '#title' => t('Classified Ad expiration'),
    '#collapsible' => TRUE,
    '#collapsed' => !$is_privileged,
    '#weight' => $weight,
  );

  // Available expiration modes depend on node existence and user permissions.
  $existing_modes = array(
    'node' => t('Keep current expiration date'),
    'reset' => t('Define expiration date automatically, based on the ad category.'),
    'force' => t('Define expiration date manually'),
  );
  $modes = array();

  // Only display expire info for existing nodes.
  if ($is_update) {
    $form['expires_fs']['#description'] = '<p>' . t('This ad is currently due to expire on @expire.', array(
      '@expire' => strftime(_classified_get('date-format'), _classified_get_timestamp_from_date($date)),
    )) . '</p>';

    // Can only keep info if it already exists.
    $modes['node'] = $existing_modes['node'];
  }

  // Default mode is always available.
  $modes['reset'] = $existing_modes['reset'];
  if ($is_privileged) {
    $modes['force'] = $existing_modes['force'];
  }
  if (is_null($mode)) {
    $keys = array_keys($modes);
    $mode = reset($keys);
    unset($keys);
  }
  $form['expires_fs']['expire_mode'] = array(
    '#type' => 'radios',
    '#options' => $modes,
    '#default_value' => $mode,
    '#weight' => 0,
  );
  $form['expires_fs']['expire_date'] = array(
    '#type' => 'date',
    '#title' => t('Ad expiration date'),
    '#default_value' => $date,
    '#description' => t('This field will only be applied if the date is defined manually.'),
    '#weight' => 1,
    '#access' => $is_privileged,
  );

  // Normalize date to timestamp.
  $form['#submit'][] = 'classified_form_submit';
  return $form;
}