You are here

node_limit_role.module in Node Limit 8

Module to restrict the number of nodes by role.

File

old/node_limit_role/node_limit_role.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_role_user_role_delete($role) {
  $limits = \Drupal::database()
    ->select('node_limit_role', '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_role_node_limit_applies_in_context($lid, $node, $user) {
  $limit = node_limit_role_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_role']['rid'] == DRUPAL_ANONYMOUS_RID;
    $isRoleAuth = $limit['node_limit_role']['rid'] == DRUPAL_AUTHENTICATED_RID;
    if (isset($user->roles[$limit['node_limit_role']['rid']]) || $user->uid > 0 && $isRoleAnonymous || $user->uid == 0 && $isRoleAuth) {
      $applies = NODE_LIMIT_LIMIT_DOES_APPLY;
    }
  }
  return array(
    'node_limit_role' => $applies,
  );
}

/**
 * Implements hook_node_limit_sql().
 */
function node_limit_role_node_limit_sql($lid, SelectQuery $select) {
  $limit = node_limit_role_node_limit_load($lid);
  if (empty($limit)) {
    return;
  }
  if ($limit['node_limit_role']['rid'] == DRUPAL_ANONYMOUS_RID) {
    $select
      ->condition('uid', 0);
  }
  elseif ($limit['node_limit_role']['rid'] == DRUPAL_AUTHENTICATED_RID) {
    $select
      ->condition('uid', 0, '!=');
  }
  else {
    $subselect = \Drupal::database()
      ->select('users_roles', 'ur')
      ->fields('ur', array(
      'uid',
    ))
      ->condition('rid', $limit['node_limit_role']['rid']);
    $select
      ->condition('uid', $subselect, 'IN');
  }
}

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

/**
 * Implements hook_node_limit_element_validate().
 */
function node_limit_role_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_role_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'];
    }
    \Drupal::database()
      ->insert('node_limit_role')
      ->fields(array(
      'lid' => $lid,
      'rid' => $element,
    ))
      ->execute();
  }
}

/**
 * Implements hook_node_limit_delete().
 */
function node_limit_role_node_limit_delete($lids) {
  \Drupal::database()
    ->delete('node_limit_role')
    ->condition('lid', $lids, 'IN')
    ->execute();
}

/**
 * Implements hook_node_limit_load().
 */
function node_limit_role_node_limit_load($lid) {
  $select = \Drupal::database()
    ->select('node_limit_role', '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_role' => array(
      'rid' => $info['rid'],
      'name' => $info['name'],
    ),
  );
}