You are here

function book_access_node_update in Book access 7.2

Same name and namespace in other branches
  1. 1.x book_access.module \book_access_node_update()

Implements hook_node_update().

Makes sure defaults propagate to pages that have been 'upgraded' to a book.

File

./book_access.module, line 877
Allows to set the access control for book nodes on a per book basis. Based on forum_access.module and tac_lite.module.

Code

function book_access_node_update($node) {
  if (isset($node->book['bid']) && $node->book['bid']) {

    // check for existing permissions
    $rowCount = db_select('book_access_author', 'book_access_author')
      ->condition('nid', $node->book['bid'], '=')
      ->condition('uid', $node->uid)
      ->countQuery()
      ->execute()
      ->fetchField();
    if ($rowCount == 0) {
      BookAccess::setAuthorGrants($node->book['bid'], $node->uid, drupal_map_assoc($node->uid == 0 ? BookAccess::defaultGrants() : variable_get("book_access_default_author_access")));
    }
    $roles = user_roles();
    $rids = array_keys($roles);
    $roleResultSet = db_select('book_access_role', 'book_access_role')
      ->condition('nid', $node->book['bid'], '=')
      ->condition('rid', $rids, 'IN')
      ->fields('book_access_role', array(
      'rid',
    ))
      ->distinct()
      ->execute();
    if ($roleResultSet
      ->rowCount() < count($rids)) {

      // fill in the missing ones
      $existingRids = array();
      foreach ($roleResultSet as $row) {
        $existingRids[] = $row->rid;
      }
      $missingRids = array_diff($rids, $existingRids);
      foreach ($missingRids as $rid) {
        $defaultToUse = variable_get("book_access_role_{$rid}_default");
        if ($defaultToUse !== NULL) {
          $roleDefaults = array();
          foreach ($defaultToUse as $grant) {
            $roleDefaults[$grant] = $rid;
          }
          BookAccess::addRoleGrants($node->book['bid'], array(
            $rid,
          ), $roleDefaults);
        }
      }
    }
  }
}