You are here

rate_expiration.module in Rate 6.2

Same filename and directory in other branches
  1. 7 expiration/rate_expiration.module

File

expiration/rate_expiration.module
View source
<?php

/**\
 * Implements hook_form_FORM_ID_alter().
 *
 * Add configuration options to the widget form.
 */
function rate_expiration_form_rate_widget_form_alter(&$form, &$form_state) {
  if (isset($form['error'])) {

    // Do not add our fields on forms with an error.
    return;
  }
  if ($form['#widget_id']) {
    $widgets = variable_get(RATE_VAR_WIDGETS, array());
    $widget = $widgets[$form['#widget_id']];
  }
  else {
    $widget = new stdClass();
  }
  $form['expiration'] = array(
    '#type' => 'fieldset',
    '#title' => t('Expiration settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options = array(
    -1 => t('Never'),
  ) + drupal_map_assoc(array(
    300,
    900,
    1800,
    3600,
    10800,
    21600,
    32400,
    43200,
    86400,
    172800,
    345600,
    604800,
    1209600,
    1814400,
    2419200,
  ), 'format_interval');
  $form['expiration']['expiration'] = array(
    '#type' => 'select',
    '#title' => t('Disable voting after this period'),
    '#options' => $options,
    '#default_value' => isset($widget->expiration) ? $widget->expiration : -1,
    '#description' => t('Time that has to elapse after creating a node before voting is disabled.'),
  );
  $form['expiration']['expiration_allow_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow override'),
    '#default_value' => isset($widget->expiration_allow_override) ? $widget->expiration_allow_override : FALSE,
    '#description' => t('When enabled, the expiration date can be customized in the node edit screen.'),
  );
}

/**
 * Implements hook_rate_widget().
 */
function rate_expiration_rate_widget($op, &$widget, $values = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      $widget->expiration = $values['expiration'];
      $widget->expiration_allow_override = !empty($values['expiration_allow_override']);
      break;
    case 'delete':
      $sql = 'DELETE FROM {rate_expiration} WHERE widget_name = \'%s\'';
      db_query($sql, $widget->name);
      break;
    case 'view':
      if ($values['content_type'] == 'node') {
        if ($node = node_load($values['content_id'])) {
          $configuration = _rate_expiration_get_node_values($node->nid);
          if (isset($configuration[$widget->name])) {
            $start = $configuration[$widget->name]['start'];
            $end = $configuration[$widget->name]['end'];
          }
          else {
            $start = 0;
            $end = 0;
          }
          if ($widget->expiration > 0) {
            !empty($end) or $end = $node->created + $widget->expiration;
          }
          if ($end > 0 && $end <= time() || $start > 0 && $start > time()) {
            $widget->mode = RATE_CLOSED;
          }
        }
      }
      break;
  }
}

/**
 * Get configuration for a widgets on a given node.
 */
function _rate_expiration_get_node_values($nid) {
  static $cache = array();
  if (isset($cache[$nid])) {
    return $cache[$nid];
  }
  $output = array();
  $sql = 'SELECT re.widget_name, re.start, re.end
  FROM {rate_expiration} re
  WHERE nid = %d';
  $res = db_query($sql, $nid);
  while ($rec = db_fetch_array($res)) {
    $output[$rec['widget_name']] = $rec;
  }
  $cache[$nid] = $output;
  return $output;
}

/**
 * Implements hook_form_alter().
 *
 * Add the date selection to the node form.
 */
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,
    );
  }
}

/**
 * Array_filter() callback. Filter widgets without the allow_override option.
 */
function _rate_expiration_filter_widgets($item) {
  return !empty($item->expiration_allow_override);
}

/**
 * Implements hook_nodeapi().
 */
function rate_expiration_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    case 'insert':
    case 'update':

      // 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');
      foreach ($widgets as $widget) {
        $timezone = is_null($user->timezone) ? variable_get('date_default_timezone', 0) : $user->timezone;
        $container = 'rate_expiration_' . $widget->name;
        $begin = "{$container}_begin";
        $end = "{$container}_end";
        $begin = $node->{$begin} ? strtotime($node->{$begin} . 'Z') - $timezone : NULL;
        $end = $node->{$end} ? strtotime($node->{$end} . 'Z') - $timezone : NULL;
        if (empty($begin) && empty($end)) {
          $sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
          db_query($sql, $node->nid);
        }
        else {
          $sql = 'UPDATE {rate_expiration}
          SET start = %d, end = %d
          WHERE nid = %d';
          db_query($sql, $begin, $end, $node->nid);
          if (!db_affected_rows()) {
            $sql = 'INSERT INTO {rate_expiration}
            (nid, widget_name, start, end)
            VALUES
            (%d, \'%s\', %d, %d)';
            db_query($sql, $node->nid, $widget->name, $begin, $end);
          }
        }
      }
      break;
    case 'delete':
      $sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
      db_query($sql, $node->nid);
      break;
  }
}

Functions

Namesort descending Description
rate_expiration_form_alter Implements hook_form_alter().
rate_expiration_form_rate_widget_form_alter
rate_expiration_nodeapi Implements hook_nodeapi().
rate_expiration_rate_widget Implements hook_rate_widget().
_rate_expiration_filter_widgets Array_filter() callback. Filter widgets without the allow_override option.
_rate_expiration_get_node_values Get configuration for a widgets on a given node.