You are here

node_limit_type.module in Node Limit 6

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

Module to restrict the number of nodes by content type.

File

node_limit_type/node_limit_type.module
View source
<?php

/**
 * @file
 * Module to restrict the number of nodes by content type.
 */

/**
 * Implementation of hook_user().
 *
 * Delete all rules related to the node type being deleted.
 */
function node_limit_type_node_type($op, $info) {
  switch ($op) {
    case 'delete':
      $sql = "DELETE FROM {node_limit_type} WHERE type = '%s'";
      db_query($sql, $info->type);
      $num = db_affected_rows();
      if ($num > 0) {
        drupal_set_message(t('Deleted !num.', array(
          '!num' => format_plural((int) $num, '1 limit rule', '@count limit rules'),
        )));
      }
      break;
  }
}

/**
 * Implementation of hook_node_limit_applies_in_context().
 */
function node_limit_type_node_limit_applies_in_context($lid, $node, $user) {
  $limit = node_limit_type_node_limit_load($lid);
  if (empty($limit)) {
    return NODE_LIMIT_LIMIT_NEUTRAL;
  }
  if ($limit['node_limit_type'] != $node->type) {
    return array(
      'node_limit_type' => NODE_LIMIT_LIMIT_DOESNT_APPLY,
    );
  }
  return array(
    'node_limit_type' => NODE_LIMIT_LIMIT_DOES_APPLY,
  );
}

/**
 * Implementation of hook_node_limit_sql().
 */
function node_limit_type_node_limit_sql($lid) {
  $limit = node_limit_type_node_limit_load($lid);
  if (empty($limit)) {
    return array();
  }
  return array(
    'where' => array(
      sprintf("n.type = '%s'", $limit['node_limit_type']),
    ),
  );
}

/**
 * Implementation of hook_node_limit_element().
 */
function node_limit_type_node_limit_element($lid = 0) {
  $limit = node_limit_type_node_limit_load($lid);
  $types = _node_limit_type_content_types();
  return array(
    'node_limit_type' => array(
      '#type' => 'select',
      '#title' => t('Content Type'),
      '#options' => $types,
      '#default_value' => $limit['node_limit_type'],
    ),
  );
}

/**
 * Implementation of hook_node_limit_element_validate().
 *
 * No validation is necessary for node types.
 */
function node_limit_type_node_limit_element_validate($element) {
  return TRUE;
}

/**
 * Implementation of hook_node_limit_save().
 */
function node_limit_type_node_limit_save($lid, $applies, $element) {
  drupal_set_message(print_r($element, true));
  if ($applies) {
    db_query("INSERT INTO {node_limit_type} VALUES('%d', '%s')", $lid, $element);
  }
}

/**
 * Implementation of hook_node_limit_delete().
 */
function node_limit_type_node_limit_delete($lid) {
  db_query("DELETE FROM {node_limit_type} WHERE `lid`='%d'", $lid);
}

/**
 * Implementation of hook_node_limit_load().
 */
function node_limit_type_node_limit_load($lid) {
  $sql = "SELECT * FROM {node_limit_type} WHERE `lid` = '%d'";
  $info = db_fetch_array(db_query($sql, $lid));
  if (intval($info['lid']) == 0) {
    return array();
  }
  return array(
    'node_limit_type' => $info['type'],
  );
}

/**
 * Returns an array of available content types.
 */
function _node_limit_type_content_types() {
  $types = array();
  $content_types = node_get_types('types');
  foreach ($content_types as $type) {
    $types[$type->type] = $type->name;
  }
  return $types;
}

Functions

Namesort descending Description
node_limit_type_node_limit_applies_in_context Implementation of hook_node_limit_applies_in_context().
node_limit_type_node_limit_delete Implementation of hook_node_limit_delete().
node_limit_type_node_limit_element Implementation of hook_node_limit_element().
node_limit_type_node_limit_element_validate Implementation of hook_node_limit_element_validate().
node_limit_type_node_limit_load Implementation of hook_node_limit_load().
node_limit_type_node_limit_save Implementation of hook_node_limit_save().
node_limit_type_node_limit_sql Implementation of hook_node_limit_sql().
node_limit_type_node_type Implementation of hook_user().
_node_limit_type_content_types Returns an array of available content types.