From a966cabd36940eccccac62f5ae911cabd19f6577 Mon Sep 17 00:00:00 2001
From: Julia Kulla-Mader <jkullama@gmail.com>
Date: Fri, 25 Feb 2011 10:43:02 -0500
Subject: [PATCH 1/2] Issue #998416 by JuliaKM: Adding support for actions, views, and themed menu links

---
 restricted_content.views.inc        |   48 +++++++++++++++++++++++++++++++++++
 restricted_content_handler_bool.inc |   10 +++++++
 2 files changed, 58 insertions(+), 0 deletions(-)
 create mode 100755 restricted_content.views.inc
 create mode 100755 restricted_content_handler_bool.inc

diff --git a/restricted_content.views.inc b/restricted_content.views.inc
new file mode 100755
index 0000000..322c665
--- /dev/null
+++ b/restricted_content.views.inc
@@ -0,0 +1,48 @@
+<?php
+function restricted_content_views_data() {
+  $data = array();
+
+  $data['restricted_content']['table']['group'] = t('Node');
+
+  $data['restricted_content']['table']['join'] = array(
+    'node' => array(
+      'left_field' => 'nid',
+      'field' => 'nid',
+    ),
+    'restricted_content' => array(
+      'left_field' => 'nid',
+      'field' => 'nid',
+    ),
+  );
+
+  $data['restricted_content']['nid'] = array(
+    'title' => t('Restricted content'),
+    'help' => t('Whether or not the node is restricted.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'restricted_content_handler_bool',
+      'label' => t('Restricted'),
+      'type' => 'yes-no',
+    ),
+  );
+
+  return $data;
+
+}
+
+function restricted_content_views_handlers() {
+  return array(
+    'info' => array(
+      'path' => drupal_get_path('module', 'restricted_content'),
+    ),
+    'handlers' => array(
+      'restricted_content_handler_bool' => array(
+        'parent' => 'views_handler_filter_boolean_operator',
+        'file' => 'restricted_content_handler_bool.inc',
+      ),
+    ),
+  );
+}
\ No newline at end of file
diff --git a/restricted_content_handler_bool.inc b/restricted_content_handler_bool.inc
new file mode 100755
index 0000000..7c32ec5
--- /dev/null
+++ b/restricted_content_handler_bool.inc
@@ -0,0 +1,10 @@
+<?php
+
+class restricted_content_handler_bool extends views_handler_filter_boolean_operator {
+  
+  function query() {
+    $this->ensure_my_table();
+    $qualified_name = "$this->table_alias.$this->real_field"; 
+    $this->query->add_where($this->options['group'], $qualified_name . (empty($this->value) ? " = 0 OR $qualified_name IS NULL" : ' IS NOT NULL'));
+  }
+}
\ No newline at end of file
-- 
1.7.3.2


From 67691410e261b748843c8bb0cce7be5fafcfdad2 Mon Sep 17 00:00:00 2001
From: Julia Kulla-Mader <jkullama@gmail.com>
Date: Fri, 25 Feb 2011 10:44:39 -0500
Subject: [PATCH 2/2] Issue #998416 by JuliaKM: Adding support for actions, views, and themed menu links

---
 restricted_content.admin.inc |    8 ++-
 restricted_content.info      |    8 ++
 restricted_content.install   |    1 +
 restricted_content.module    |  218 ++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 226 insertions(+), 9 deletions(-)
 mode change 100644 => 100755 restricted_content.admin.inc
 mode change 100644 => 100755 restricted_content.info
 mode change 100644 => 100755 restricted_content.install
 mode change 100644 => 100755 restricted_content.module

diff --git a/restricted_content.admin.inc b/restricted_content.admin.inc
old mode 100644
new mode 100755
index 2350284..0313aec
--- a/restricted_content.admin.inc
+++ b/restricted_content.admin.inc
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 function restricted_content_settings_form() {
   $form['restricted_content_message'] = array(
@@ -13,7 +14,12 @@ function restricted_content_settings_form() {
     '#default_value' => restricted_content_var('message_anon'),
     '#wysiyg' => FALSE,
   );
-
+  $form['restricted_content_message_accessible'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Message to display for users with access to restricted content'),
+    '#default_value' => restricted_content_var('message_accessible'),
+    '#wysiyg' => FALSE,
+  );
   if (module_exists('token')) {
     $form['tokens'] = array(
       '#value' => theme('token_help', 'node'),
diff --git a/restricted_content.info b/restricted_content.info
old mode 100644
new mode 100755
index 88e20a1..4fb0993
--- a/restricted_content.info
+++ b/restricted_content.info
@@ -1,5 +1,13 @@
+; $Id$
 name = Restricted content
 description = Restrict content access for only certain user roles, but show a nice message.
 core = 6.x
 package = Access control
 suggests[] = token
+
+; Information added by drupal.org packaging script on 2010-07-11
+version = "6.x-1.x-dev"
+core = "6.x"
+project = "restricted_content"
+datestamp = "1278835503"
+
diff --git a/restricted_content.install b/restricted_content.install
old mode 100644
new mode 100755
index 15b499e..71ccde1
--- a/restricted_content.install
+++ b/restricted_content.install
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 /**
  * Implementation of hook_install().
diff --git a/restricted_content.module b/restricted_content.module
old mode 100644
new mode 100755
index 6e46670..cce1610
--- a/restricted_content.module
+++ b/restricted_content.module
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 /**
  * Implementation of hook_perm().
@@ -26,10 +27,11 @@ function restricted_content_menu() {
  * Implementation of hook_form_alter().
  */
 function restricted_content_form_alter(&$form, $form_state, $form_id) {
+  $nid = $form['nid']['#value']; //modified http://drupal.org/node/826350
   if ($form_id == 'node_type_form') {
     //restricted_content_node_form($form);
   }
-  elseif ($form['#id'] == 'node-form') {
+elseif ($form['#id'] == 'node-form' && isset($nid)) {
     $default = unserialize(db_result(db_query("SELECT rids FROM {restricted_content} WHERE nid = %d", $form['nid']['#value'])));
     restricted_content_node_form($form, $default);
     $form['#submit'][] = 'restricted_content_node_form_submit';
@@ -94,29 +96,63 @@ function restricted_content_node_access($nid, $account = NULL) {
 
 function restricted_content_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   global $user;
-
+  
+  //delete when restricted content no longer set
   if ($op == 'delete') {
+     drupal_set_message("Time to Delete");
     db_query("DELETE FROM {restricted_content} WHERE nid = %d", $node->nid);
   }
-  elseif ($op == 'alter' && !restricted_content_form_access($node->uid) && !restricted_content_node_access($node->nid)) {
-    $message = restricted_content_var('message');
+  
+  //NOTE: Removed check for admin restricted menu for testing
+  //User has access to node because of role and the node is restricted
+  if ($op == 'alter' && restricted_content_node_access($node->nid) && restricted_check($node->nid)) {
+      
+    $message = restricted_content_var('message_accessible');
     $node->restricted = TRUE;
     $node->comment = COMMENT_NODE_DISABLED;
 
+    // Perform token replacement if token module is available.
+    if (module_exists('token')) {
+      $message = token_replace($message, 'node', $node);
+    }
+    
+//    $new_combo_teaser = theme('display_restriction', $message, $content, "accessible");    
+    
+    if ($node->teaser) {
+      $node->teaser = theme('display_restriction', $message, "accessible", $node->teaser);
+    }
+
+    if ($node->body) {
+        $node->body = theme('display_restriction', $message, "accessible", $node->body);
+      }
+     }    
+     
+   //user does not have access to page either because or role or because anonymous
+  if ($op == 'alter' && !restricted_content_form_access($node->uid) && !restricted_content_node_access($node->nid)) {
+
+    $message = restricted_content_var('message');
+    $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)) {
       $message .= ' '. restricted_content_var('message_anon');
     }
-
+  
     // Perform token replacement if token module is available.
     if (module_exists('token')) {
       $message = token_replace($message, 'node', $node);
     }
+  
+    if ($node->teaser) {
+      $node->teaser = theme('display_restriction', $message, "restricted");
+    }
 
-    $node->teaser = $message;
-    $node->body = $message;
-  }
+    if ($node->body) {
+        $node->body = theme('display_restriction', $message, "restricted");
+    }
+          }
   /*elseif ($op == 'alter' && !empty($node->restricted)) {
     // Send a 403 if this is an individual page view.
     if ($page) {
@@ -143,6 +179,10 @@ function restricted_content_preprocess_node(&$vars) {
  */
 function restricted_content_token_list($type = 'all') {
   $tokens['global']['site-register-url'] = t('The URL of the register user page');
+  $tokens['global']['restricted-content-message'] = t('Message to replace content for restricted users.');
+  $tokens['global']['restricted-content-message-anon'] = t('Additional message to display for restricted anonymous users.');
+  $tokens['global']['restricted-content-message-accessible'] = t('Message to display for users with access to restricted content.');
+
   if ($type == 'node' || $type == 'all') {
     $tokens['node']['type-name-lower'] = t('Node type (user-friendly version lowercased)');
   }
@@ -154,6 +194,10 @@ function restricted_content_token_list($type = 'all') {
  */
 function restricted_content_token_values($type, $object = NULL) {
   $tokens['site-register-url'] = url('user/register');
+  $values['restricted-content-message'] =variable_get('restricted_content_message', 1);
+  $values['restricted-content-message-anon'] =variable_get('restricted_content_message_anon', 1);
+  $values['restricted-content-message-accessible'] =variable_get('restricted_content_message_accessible', 1);
+
   if ($type == 'node') {
     $tokens['type-name-lower'] = drupal_strtolower(node_get_types('name', $object));
   }
@@ -167,6 +211,7 @@ function restricted_content_variables() {
   return array(
     'restricted_content_message' => t('This !token-type-name has been restricted to certain users.', array('!token-type-name' => module_exists('token') ? '[type-name-lower]' : t('content'))),
     'restricted_content_message_anon' => t('Please <a href="!token-register">register for a user account</a> to view this !token-type-name.', array('!token-type-name' => module_exists('token') ? '[type-name-lower]' : t('content'), '!token-register' => module_exists('token') ? '[site-register-url]' : url('user/register'))),
+  'restricted_content_message_accessible' => t('You have special privileges to access this !token-type-name.', array('!token-type-name' => module_exists('token') ? '[type-name-lower]' : t('content'))),
   );
 }
 
@@ -187,3 +232,160 @@ function restricted_content_var($name, $default = NULL) {
 
   return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
 }
+
+
+function restricted_check($nid) {
+    $query = "SELECT nid FROM {restricted_content} WHERE nid = '%d'";
+    $sql = db_query($query, $nid);
+      
+  if (db_affected_rows() > 0){
+     return TRUE;  
+   } 
+}
+
+
+/**
+ *  Implementation of hook_menu_link_alter().
+ *  Adds a restricted-content class to menu links
+ */
+
+function restricted_content_menu_link_alter(&$item, $menu) {
+  //if the node is marked as restricted add restricted content class
+   $path = explode('/', $item['link_path']);
+   $nid = $path[1];   
+  
+ if (restricted_check($nid)) {
+     $item['options']['attributes']['class'] = 'restricted-content';
+ }
+  else {
+     $item['options']['attributes']['class'] = '';      
+ }
+}
+
+/**
+ * Implementation of hook_action_info().
+ */
+function restricted_content_action_info() {
+  return array(
+    'restrict_content_action' => array(
+      'description' => t('Add restricted content conditions for a node'),
+      'configurable' => TRUE,
+      'type' => 'node',
+      'hooks' => array(
+        'nodeapi' => array('alter')
+      )
+    ),
+    'unrestrict_content_action' => array(
+      'description' => t('Remove restricted content conditions for a node'),
+      'type' => 'node',
+      'configurable' => FALSE,
+      'hooks' => array(
+        'nodeapi' => array('delete')
+      )
+    )
+  );
+}
+
+/**
+ * Action to restrict a node.
+ *
+ * @param $node
+ *   A node object.
+ * @param $context
+ *   An array providing more information about the context of the call to this action.
+ *
+ * @ingroup actions
+ * @see restrict_content_action_form()
+ * @see restrict_content_action_submit()
+ */
+function restrict_content_action($node, $context) {
+    // drupal_set_message(print_r($context['rids'], TRUE));
+  $rids = array_keys(array_filter($context['rids']));
+  db_query("DELETE FROM {restricted_content} WHERE nid = %d", $node->nid);
+  if ($rids) {
+    db_query("INSERT INTO {restricted_content} VALUES (%d, '%s')", $node->nid, serialize($rids));
+  }
+}
+
+/**
+ * Form builder; Prepare a form for selecting restricted roles.
+ *
+ * @ingroup forms
+ * @see restrict_content_action()
+ * @see restrict_content_action_submit()
+ */
+function restrict_content_action_form($context) {
+  $form = array();
+  $form['#node']->restricted_content = isset($context->restricted_content) ? $context->restricted_content : NULL;
+  $form['#node']->type = NULL;
+  restricted_content_node_form($form);
+  $form['restricted_content']['#weight'] = 0;
+  $form['restricted_content']['#collapsed'] = FALSE;
+  $form['restricted_content']['#description'] = t('The roles selected will be granted access to restricted content.');
+  return $form;
+}
+
+/**
+ * Process restrict_content_action_form form submissions.
+ *
+ * @see restrict_content_action()
+ */
+function restrict_content_action_submit($form, $form_state) {
+   return array('rids' => $form_state['values']['restricted_content']['rids']);
+}
+
+/**
+ * Action to unrestrict a node.
+ *
+ */
+function unrestrict_content_action($node, $context) {
+  if ($node->restricted = TRUE) {
+    $node->restricted_content['rids'] = $context->restricted_content['rids'];
+  }
+  module_invoke('restricted_content', 'nodeapi', $node, 'delete');
+}
+
+
+/**
+ * Implementation of hook_views_api().
+ */
+function restricted_content_views_api() {
+  return array(
+    'api' => 2,
+    'path' => drupal_get_path('module', 'restricted_content'),
+  );
+}
+
+
+/**
+ * Implementation of hook_theme().
+ */
+function restricted_content_theme() {
+    return array(
+        'display_restriction' => array(
+            'arguments' => array('message' => NULL, 'status' => NULL, 'content' => NULL))
+    );
+}
+
+/**
+* Theme function for theming quotes.
+*
+* @param $message
+* The message content as a string.
+* @param $status
+* The status of the restriction.
+* @param $content
+* The content to be appended to the message or removed
+* @return
+* An HTML themed string.
+*/
+function theme_display_restriction($message, $status, $content) {
+    $restriction_output = "<div id=\"restricted-text\" class=\"$status\">" . t($message) .'</div>';
+    
+    //appending $content when necessary
+    $restriction_output .= $content; 
+   
+  return $restriction_output;
+}
+
+
-- 
1.7.3.2

