You are here

node_limit_userofrole.module in Node Limit 7

Module to restrict the number of nodes by role.

File

node_limit_userofrole/node_limit_userofrole.module
View source
<?php

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

/**
 * Implements hook_user_role_delete().
 *
 * Delete all rules related to the role being deleted.
 */
function node_limit_userofrole_user_role_delete($role) {
  $limits = db_select('node_limit_userofrole', 'src')
    ->fields('src', array(
    'lid',
  ))
    ->condition('rid', $role->rid)
    ->execute();
  $lids = array();
  foreach ($limits as $limit) {
    $lids[] = $limit->lid;
  }
  node_limit_delete($lids);
}

/**
 * Implements hook_node_limit_applies_in_context().
 */
function node_limit_userofrole_node_limit_applies_in_context($lid, $node, $user) {
  $limit = node_limit_userofrole_node_limit_load($lid);
  $applies = NODE_LIMIT_LIMIT_DOESNT_APPLY;
  if (empty($limit)) {
    $applies = NODE_LIMIT_LIMIT_NEUTRAL;
  }
  elseif (!empty($user)) {
    $isRoleAnonymous = $limit['node_limit_userofrole']['rid'] == DRUPAL_ANONYMOUS_RID;
    $isRoleAuth = $limit['node_limit_userofrole']['rid'] == DRUPAL_AUTHENTICATED_RID;
    if (isset($user->roles[$limit['node_limit_userofrole']['rid']]) || $user->uid > 0 && $isRoleAnonymous || $user->uid == 0 && $isRoleAuth) {
      $applies = NODE_LIMIT_LIMIT_DOES_APPLY;
    }
  }
  return array(
    'node_limit_userofrole' => $applies,
  );
}

/**
 * Implements hook_node_limit_sql().
 */
function node_limit_userofrole_node_limit_sql($lid, SelectQuery $select) {
  global $user;
  $limit = node_limit_userofrole_node_limit_load($lid);
  if (empty($limit)) {
    return;
  }
  $select
    ->condition('uid', $user->uid);
}

/**
 * Implements hook_node_limit_element().
 */
function node_limit_userofrole_node_limit_element($lid = 0) {
  $limit = node_limit_userofrole_node_limit_load($lid);
  $rid = !empty($limit['node_limit_userofrole']['rid']) ? $limit['node_limit_userofrole']['rid'] : '';
  $roles = user_roles();
  if (!empty($roles)) {
    return array(
      'node_limit_userofrole' => array(
        '#type' => 'select',
        '#title' => t('Users of Role'),
        '#options' => $roles,
        '#default_value' => $rid,
      ),
    );
  }
}

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

  /**
   * Validation:
   * rid must be a role
   */
  $roles = user_roles();
  if (!isset($roles[$element])) {

    // Unknown role.
    return array(
      'error' => t('Unknown role "%role"', array(
        '%role' => $element,
      )),
    );
  }
  return TRUE;
}

/**
 * Implements hook_node_limit_save().
 */
function node_limit_userofrole_node_limit_save($lid, $applies, $element) {
  if ($applies) {

    // In the clone context, $element is an array containing
    // the rid and the role name
    if (is_array($element)) {
      $element = $element['rid'];
    }
    db_insert('node_limit_userofrole')
      ->fields(array(
      'lid' => $lid,
      'rid' => $element,
    ))
      ->execute();
  }
}

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

/**
 * Implements hook_node_limit_load().
 */
function node_limit_userofrole_node_limit_load($lid) {
  $select = db_select('node_limit_userofrole', 'nlr');
  $select
    ->join('role', 'r', 'r.rid = nlr.rid');
  $select
    ->fields('nlr')
    ->fields('r', array(
    'name',
  ))
    ->condition('lid', $lid);
  $info = $select
    ->execute()
    ->fetchAssoc();
  if (empty($info['rid'])) {
    return array();
  }
  return array(
    'node_limit_userofrole' => array(
      'rid' => $info['rid'],
      'name' => $info['name'],
    ),
  );
}