<?php
// $Id$

/**
 * Implementation of hook_perm().
 */
function restricted_content_perm() {
  return array('restrict content access', 'restrict own content access');
}

/**
 * Implementation of hook_menu().
 */
function restricted_content_menu() {
  /*$items['node/%node/access'] = array(
    'title' => 'Access',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('restricted_content_node_form'),
    'access callback' => 'restricted_content_tab_access',
    'access arguments' => array(1),
    //'weight' => 1,
    //'file' => 'node.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );*/
  $items['admin/content/restricted'] = array(
    'title' => 'Restricted content',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('restricted_content_settings_form'),
    'access arguments' => array('restrict content access'),
    'file' => 'restricted_content.admin.inc',
  );

  return $items;
}

/*function restricted_content_tab_access($node) {
  global $user;

  return user_access('grant content access') || (user_access('grant own content access') && $node->uid === $user->uid);
}*/

/**
 * Implementation of hook_form_alter().
 */
function restricted_content_form_alter(&$form, $form_state, $form_id) {
  if ('node_type_form' == $form_id) {
    //node_privacy_byrole_node_type_form($form);
  }
  elseif ($form['#id'] == 'node-form') {
    $form['restricted_content'] = array(
      '#type' => 'fieldset',
      '#title' => t('Restricted Access'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#access' => restricted_content_form_access($form['#node']),
    );
    $form['restricted_content']['rids'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Restrict viewing from users with any of the following user roles'),
      '#description' => t('If no roles are selected, the node will be viewable by all users.'),
      '#options' => user_roles(),
      '#default_value' => unserialize(db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $form['#node']->nid))),
    );
    $form['#submit'][] = 'restricted_content_node_form_submit';
    //restricted_content_form_node_form_alter($form);
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 */
/*function restricted_content_form_node_form_alter(&$form, $form_state = array()) {
  $form['restricted_content'] = array(
    '#type' => 'fieldset',
    '#title' => t('Restricted Access'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#access' => restricted_content_form_access($form['#node']),
  );
  $form['restricted_content']['rids'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Restrict this content from users with any of the following user roles'),
    '#description' => t('If no roles are selected, the node will be viewable by all users.'),
    '#options' => user_roles(),
  );
  $form['#submit'][] = 'restricted_content_node_form_submit';
}*/

function restricted_content_node_form_submit($form, $form_state) {
  $nid = $form_state['values']['nid'];
  $rids = array_keys(array_filter($form_state['values']['restricted_content']['rids']));
  db_query("DELETE FROM {restricted_content} WHERE nid = %d", $nid);
  if ($rids) {
    db_query("INSERT INTO {restricted_content} VALUES (%d, '%s')", $nid, serialize($rids));
  }
}

function restricted_content_form_access($node) {
  global $user;

  return user_access('restrict content access') || ($node->nid == $user->uid && user_access('restrict own content access'));
}

function restricted_content_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  global $user;

  if ($op == 'delete') {
    db_query("DELETE FROM {restricted_content} WHERE nid = %d", $node->nid);
  }
  elseif ($op == 'load' && !restricted_content_form_access($node)) {
    $rids = db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $node->nid));
    if ($rids && array_intersect(unserialize($rids), array_keys($user->roles))) {

      $node_type = drupal_strtolower(node_get_types('name', $node));
      $node->body = t('This @node-type has been restricted to certain users.', array('@node-type' => $node_type));
      $node->restricted = TRUE;
      $node->comment = COMMENT_NODE_DISABLED;

      // Add a register link of the user is anonymous and can register for an
      // account.
      if (!$user->uid && variable_get('user_register', 1)) {
        $node->body .= ' '. t('Please <a href="@link-register">register for a user account</a> to view this @node-type.', array('@link-register' => url('user/register'), '@node-type' => $node_type));
      }

      $node->teaser = $node->body;
    }
  }
  elseif ($op == 'alter' && !empty($node->restricted)) {
    // Send a 403 if this is an individual page view.
    if ($page) {
      //drupal_access_denied();
      drupal_set_header('HTTP/1.1 403 Forbidden');
    }

    // Unset the links.
    //unset($node->links);
  }
}

function restricted_content_preprocess_node(&$vars) {
  if (!empty($vars['node']->restricted)) {
  //echo "<pre>"; var_export($vars); echo "</pre>";// die();
    $vars['submitted'] = FALSE;
    $vars['picture'] = FALSE;
    $vars['taxonomy'] = FALSE;
    $vars['terms'] = FALSE;
    $vars['links'] = FALSE;
    $vars['node_url'] = '';
  }
}
