You are here

function classified_form in Classified Ads 7.3

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

Implements hook_form().

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

File

./classified.module, line 1039
A pure D7 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_type_get_type($node);
  $form = array();
  if (!empty($type->has_title)) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => empty($type->title_label) ? '' : check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#weight' => -5,
    );
  }

  // Can only happen when creating a new node.
  if (empty($node->expires)) {
    $lifetimes = _classified_get('lifetimes');
    $node->expires = 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');
  $form['expires_fs'] = array(
    '#type' => 'fieldset',
    '#title' => t('Classified Ad expiration'),
    '#collapsible' => TRUE,
    '#collapsed' => !$is_privileged,
  );

  // 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;
}