<?php

/**
 * @file
 * Restrict access to nodes that belong to certain partners.
 */

/**
 * Implements hook_permission().
 */
function archibald_partner_access_permission() {
  $permissions = array();

  foreach (archibald_partner_load_all() as $id => $partner) {
    $permissions["access partner {$id} content"] = array(
      'title' => t("Access %partner content", array(
        '%partner' => $partner->name,
      )),
      'description' => t("Access content assigned to partner %partner. Users will still need specific edit or delete node permissions in order to update or remove content.", array(
        '%partner' => $partner->name,
      )),
    );
  }

  return $permissions;
}

/**
 * Implements hook_node_grants().
 */
function archibald_partner_access_node_grants($account, $op) {
  $grants = array();

  // Load all partners, and check for user access on each one.
  foreach (archibald_partner_load_all() as $id => $partner) {
    if (user_access("access partner {$id} content", $account)) {
      // Check if the specific operation is permitted. If view, it is always
      // permitted.
      if ($op == 'view') {
        $grants['archibald_partner_access_view'][] = $id;
      }
      elseif (
        $op == 'update' &&
        (
          user_access('edit own archibald_lomch_description content', $account) ||
          user_access('edit any archibald_lomch_description content', $account)
        )
      ) {
        $grants['archibald_partner_access_update'][] = $id;
      }
      elseif (
        $op == 'delete' &&
        (
          user_access('delete own archibald_lomch_description content', $account) ||
          user_access('delete any archibald_lomch_description content', $account)
        )
      ) {
        $grants['archibald_partner_access_delete'][] = $id;
      }
    }
  }

  return $grants;
}

/**
 * Implements hook_node_access_records().
 */
function archibald_partner_access_node_access_records($node) {
  // We only care about archibald_lomch_description nodes.
  if (
    $node->type == 'archibald_lomch_description' &&
    !empty($node->archibald_publication_partner[LANGUAGE_NONE][0]['target_id'])
  ) {
    $grants = array();
    $gid = $node->archibald_publication_partner[LANGUAGE_NONE][0]['target_id'];

    $grants[] = array(
      'realm' => 'archibald_partner_access_view',
      'gid' => $gid,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );

    $grants[] = array(
      'realm' => 'archibald_partner_access_edit',
      'gid' => $gid,
      'grant_view' => 1,
      'grant_update' => 1,
      'grant_delete' => 0,
      'priority' => 0,
    );

    $grants[] = array(
      'realm' => 'archibald_partner_access_delete',
      'gid' => $gid,
      'grant_view' => 1,
      'grant_update' => 1,
      'grant_delete' => 1,
      'priority' => 0,
    );

    return $grants;
  }
}

/**
 * Implements hook_archibald_partner_access().
 */
function archibald_partner_access_archibald_partner_access($op, $partner = NULL, $account = NULL) {
  if ($partner && $account) {
    // True won't have any effect, but we can restrict if this equals false.
    return user_access("access partner {$partner->id} content", $account);
  }
}
