You are here

function rate_expiration_form_alter in Rate 6.2

Same name and namespace in other branches
  1. 7 expiration/rate_expiration.module \rate_expiration_form_alter()

Implements hook_form_alter().

Add the date selection to the node form.

File

expiration/rate_expiration.module, line 111

Code

function rate_expiration_form_alter(&$form, &$form_state, $form_id) {
  if (!preg_match('/_node_form$/', $form_id)) {
    return;
  }
  $node = $form['#node'];

  // Get active rate widgets and filter widgets without the "allow override" option..
  $widgets = rate_get_active_widgets('node', $node->type);
  $widgets = array_filter($widgets, '_rate_expiration_filter_widgets');
  if (!$widgets) {

    // There no widgets for this nodetype with the "allow override" option.
    return;
  }

  // Get default values for all widgets.
  $defaults = empty($node->nid) ? array() : _rate_expiration_get_node_values($node->nid);
  foreach ($widgets as $widget) {
    if (isset($defaults[$widget->name])) {
      $startdate = $defaults[$widget->name]['start'];
      $enddate = $defaults[$widget->name]['end'];
    }
    else {
      $startdate = empty($node->created) ? time() : $node->created;
      $enddate = NULL;

      // Decrement startdate to prevent voting from being disabled a few minutes after node
      // creation (may happen cause the date increment of 5 minutes).
      $startdate -= 180;
    }
    $container = 'rate_expiration_' . $widget->name;
    $form[$container] = array(
      '#type' => 'fieldset',
      '#title' => t('Expiration for %name widget', array(
        '%name' => $widget->title,
      )),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form[$container]["{$container}_begin"] = array(
      '#type' => 'date_select',
      '#title' => t('Enable voting on'),
      '#description' => t('Leave empty to disable startdate.'),
      '#default_value' => $startdate ? format_date($startdate, 'custom', 'Y-m-d H:i:s') : NULL,
      '#date_increment' => 5,
    );
    $form[$container]["{$container}_end"] = array(
      '#type' => 'date_select',
      '#title' => t('Close voting on'),
      '#description' => $widget->expiration > 0 ? t('Leave empty to allow for @interval after node creation', array(
        '@interval' => format_interval($widget->expiration),
      )) : t('Leave empty to allow forever.'),
      '#default_value' => $enddate ? format_date($enddate, 'custom', 'Y-m-d H:i:s') : NULL,
      '#date_increment' => 5,
    );
  }
}