You are here

node_limit_interval.module in Node Limit 7

Same filename and directory in other branches
  1. 6 node_limit_interval/node_limit_interval.module

Module to restrict the number of nodes within a time interval.

File

node_limit_interval/node_limit_interval.module
View source
<?php

/**
 * @file
 * Module to restrict the number of nodes within a time interval.
 */
define("NODE_LIMIT_INTERVAL_SECONDS", "1");
define("NODE_LIMIT_INTERVAL_MINUTES", "60");
define("NODE_LIMIT_INTERVAL_HOURS", "3600");
define("NODE_LIMIT_INTERVAL_DAYS", "86400");
define("NODE_LIMIT_INTERVAL_WEEKS", "604800");
define("NODE_LIMIT_INTERVAL_MONTHS", "2419200");
define("NODE_LIMIT_INTERVAL_YEARS", "31536000");

/**
 * Implements hook_node_limit_applies_in_context().
 */
function node_limit_interval_node_limit_applies_in_context($lid, $node, $user) {
  $limit = node_limit_interval_node_limit_load($lid);
  $applies = NODE_LIMIT_LIMIT_DOES_APPLY;
  if (empty($limit)) {
    $applies = NODE_LIMIT_LIMIT_NEUTRAL;
  }
  return array(
    'node_limit_interval' => $applies,
  );
}

/**
 * Implements hook_node_limit_sql().
 */
function node_limit_interval_node_limit_sql($lid, SelectQuery $select) {
  $limit = node_limit_interval_node_limit_load($lid);
  if (empty($limit)) {
    return;
  }
  if ($limit['node_limit_interval'] != NODE_LIMIT_INTERVAL_MONTHS) {
    $interval = floatval($limit['node_limit_interval']['interval']) * intval($limit['node_limit_interval']['unit']);
  }
  else {
    $then = mktime(date('H'), date('i'), date('s'), date('n') - floatval($limit['node_limit_interval']['interval']));
    $now = mktime();
    $interval = $now - $then;
  }
  $select
    ->where('created + :interval >= :reference', array(
    ':interval' => $interval,
    ':reference' => REQUEST_TIME,
  ));
}

/**
 * Implements hook_node_limit_render_element().
 */
function node_limit_interval_node_limit_render_element(&$element) {
  $title = $element['interval']['#title'];
  unset($element['interval']['#title']);
  $interval = drupal_render($element['interval']);
  $unit = drupal_render($element['unit']);
  $html = str_replace('div', 'span', $title . $interval . $unit);
  return $html;
}

/**
 * Implements hook_node_limit_element().
 */
function node_limit_interval_node_limit_element($lid = 0) {
  $limit = node_limit_interval_node_limit_load($lid);
  $interval_options = _node_limit_interval_interval_options();
  $interval = !empty($limit['node_limit_interval']['interval']) ? $limit['node_limit_interval']['interval'] : '';
  $unit = !empty($limit['node_limit_interval']['unit']) ? $limit['node_limit_interval']['unit'] : '';
  return array(
    'node_limit_interval' => array(
      '#title' => t('Time Interval'),
      '#custom_render' => TRUE,
      'interval' => array(
        '#type' => 'textfield',
        '#title' => t('Within the past'),
        '#size' => 10,
        '#default_value' => $interval,
      ),
      'unit' => array(
        '#type' => 'select',
        '#options' => $interval_options,
        '#default_value' => $unit,
      ),
    ),
  );
}

/**
 * Prepare time frame select options.
 */
function _node_limit_interval_interval_options() {
  return array(
    NODE_LIMIT_INTERVAL_SECONDS => t('Seconds'),
    NODE_LIMIT_INTERVAL_MINUTES => t('Minutes'),
    NODE_LIMIT_INTERVAL_HOURS => t('Hours'),
    NODE_LIMIT_INTERVAL_DAYS => t('Days'),
    NODE_LIMIT_INTERVAL_WEEKS => t('Weeks'),
    NODE_LIMIT_INTERVAL_MONTHS => t('Months'),
    NODE_LIMIT_INTERVAL_YEARS => t('Years'),
  );
}

/**
 * Implements hook_node_limit_element_validate().
 */
function node_limit_interval_node_limit_element_validate($element) {

  /**
   * Validation:
   * interval must be an integer greater than 0
   * User must be in the {user} table
   */
  if (!is_numeric($element['interval']) || floatval($element['interval']) <= 0) {

    //less than 0
    return array(
      'error' => t('Time intervals must be an integer greater than 0'),
      'element' => 'interval',
    );
  }
  return TRUE;
}

/**
 * Implements hook_node_limit_save().
 */
function node_limit_interval_node_limit_save($lid, $applies, $element) {
  if ($applies) {
    $interval = intval($element['interval']);
    $unit = intval($element['unit']);
    db_insert('node_limit_interval')
      ->fields(array(
      'lid' => $lid,
      'ninterval' => $interval,
      'unit' => $unit,
    ))
      ->execute();
  }
}

/**
 * Implements hook_node_limit_delete().
 */
function node_limit_interval_node_limit_delete($lids) {
  db_delete('node_limit_interval')
    ->condition('lid', $lids, 'IN')
    ->execute();
}

/**
 * Implements hook_node_limit_load().
 */
function node_limit_interval_node_limit_load($lid) {
  $select = db_select('node_limit_interval', 'nti')
    ->fields('nti')
    ->condition('lid', $lid);
  $info = $select
    ->execute()
    ->fetchAssoc();
  if (empty($info['lid'])) {
    return array();
  }
  return array(
    'node_limit_interval' => array(
      'interval' => $info['ninterval'],
      'unit' => $info['unit'],
    ),
  );
}