Index: database/database.mysql
===================================================================
RCS file: /cvs/drupal/drupal/database/database.mysql,v
retrieving revision 1.224
diff -u -r1.224 database.mysql
--- database/database.mysql	22 Feb 2006 10:06:46 -0000	1.224
+++ database/database.mysql	22 Feb 2006 20:24:38 -0000
@@ -604,6 +604,18 @@
 /*!40100 DEFAULT CHARACTER SET utf8 */ ;
 
 --
+-- Table structure for table 'blocks_roles'
+--
+
+CREATE TABLE blocks_roles (
+  module varchar(64) NOT NULL,  
+  delta varchar(32) NOT NULL,  
+  rid int(10) unsigned NOT NULL,
+  PRIMARY KEY (module, delta, rid)
+) TYPE=MyISAM
+/*!40100 DEFAULT CHARACTER SET utf8 */ ;
+
+--
 -- Table structure for table 'search_dataset'
 --
 CREATE TABLE search_dataset (
Index: database/database.pgsql
===================================================================
RCS file: /cvs/drupal/drupal/database/database.pgsql,v
retrieving revision 1.169
diff -u -r1.169 database.pgsql
--- database/database.pgsql	22 Feb 2006 10:06:46 -0000	1.169
+++ database/database.pgsql	22 Feb 2006 20:24:51 -0000
@@ -567,6 +567,17 @@
 );
 
 --
+-- Table structure for table 'blocks_roles'
+--
+
+CREATE TABLE blocks_roles (
+  module varchar(64) NOT NULL,  
+  delta varchar(32) NOT NULL,  
+  rid integer NOT NULL,
+  PRIMARY KEY (module, delta, rid)
+);
+
+--
 -- Table structure for table 'search_dataset'
 --
 CREATE TABLE search_dataset (
Index: database/updates.inc
===================================================================
RCS file: /cvs/drupal/drupal/database/updates.inc,v
retrieving revision 1.197
diff -u -r1.197 updates.inc
--- database/updates.inc	22 Feb 2006 10:06:46 -0000	1.197
+++ database/updates.inc	22 Feb 2006 20:24:59 -0000
@@ -1685,3 +1685,25 @@
 
   return $ret;
 }
+
+function system_update_174() {
+  $ret = array();
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("CREATE TABLE blocks_roles (
+      module varchar(64) NOT NULL,  
+      delta varchar(32) NOT NULL,  
+      rid int(10) unsigned NOT NULL,
+      PRIMARY KEY (module, delta, rid)
+      ) TYPE=MyISAM");
+  }
+  else if ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("CREATE TABLE blocks_roles (
+      module varchar(64) NOT NULL,  
+      delta varchar(32) NOT NULL,  
+      rid integer NOT NULL,
+      PRIMARY KEY (module, delta, rid)
+      );");
+  }
+
+  return $ret;
+}
Index: modules/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block.module,v
retrieving revision 1.202
diff -u -r1.202 block.module
--- modules/block.module	21 Feb 2006 18:46:54 -0000	1.202
+++ modules/block.module	22 Feb 2006 20:25:23 -0000
@@ -12,26 +12,28 @@
 function block_help($section) {
   switch ($section) {
     case 'admin/help#block':
-      $output = '<p>'. t('Blocks are the boxes of related/grouped data that are visible in the sidebar(s) of your web site. These are usually generated automatically by modules (e.g. recent forum topics), but administrators can also create their own defined blocks.') .'</p>';
-      $output .= '<p>'. t('The sidebar each block appears in depends on both which theme you are using (some are left-only, some right, some both), and on the settings in block management.') .'</p>';
-      $output .= '<p>'. t('The block management screen lets you specify the vertical sort-order of the blocks within a sidebar. You do this by assigning a weight to each block. Lighter blocks (smaller weight) "float up" towards the top of the sidebar. Heavier ones "sink down" towards the bottom of it.') .'</p>';
+      $output = '<p>'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'</p>';
+      $output .= '<p>'. t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') .'</p>';
+      $output .= '<p>'. t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region;  heavier ones "sink".') .'</p>';
       $output .= t('<p>A block\'s visibility depends on:</p>
 <ul>
 <li>Its enabled checkbox. Disabled blocks are never shown.</li>
 <li>Its throttle checkbox. Throttled blocks are hidden during high server loads.</li>
-<li>Its path options. Blocks can be configured to only show/hide on certain pages.</li>
-<li>User settings. Administrators can choose to let your users decide whether to show/hide certain blocks.</li>
-<li>Its function. Dynamic blocks (such as those defined by modules) may be empty on certain pages and will not be shown.</li>
+<li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
+<li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
+<li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
+<li>Its user-role visibility settings. Administrators can choose to let blocks be visible only for certain user roles.</li>
+<li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
 </ul>
 ');
       $output .= '<h3>'. t('Module blocks') .'</h3>';
-      $output .= '<p>'. t('Module blocks are available when modules are enabled.  These blocks can be administered in block administration.') .'</p>';
+      $output .= '<p>'. t('Some modules generate blocks that become available when the modules are enabled.  These blocks can be administered from the block administration screen (<a href="%admin-block">administer &gt;&gt; blocks</a>)', array('%admin-block' => url('admin/block'))) .'</p>';
       $output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
-      $output .= '<p>'. t('An administrator defined block contains content supplied by the administrator. Each admin-defined block consists of a title, a description, and a body which can be as long as you wish. The Drupal engine will render the content of the block.') .'</p>';
+      $output .= '<p>'. t('An administrator-defined block contains content defined by the administrator. Each such block consists of a title, a description, and a body, which can be as long as you wish. Blocks content can be in any of the input formats supported for other content.') .'</p>';
       $output .= t('<p>You can</p>
 <ul>
-<li>enable throttle and configure blocks at <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
-<li>add a block at <a href="%admin-block-add">administer &gt;&gt; blocks &gt;&gt; add block</a>.</li>
+<li>enable, throttle, and configure blocks at <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
+<li>add an administrator-defined block at <a href="%admin-block-add">administer &gt;&gt; blocks &gt;&gt; add block</a>.</li>
 </ul>
 ', array('%admin-block' => url('admin/block'), '%admin-block-add' => url('admin/block/add')));
       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%block">Block page</a>.', array('%block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
@@ -40,9 +42,10 @@
       return t('Controls the boxes that are displayed around the main content.');
     case 'admin/block':
       return t("
-<p>Blocks are content rendered into regions, often boxes in the left and right side bars of the web site. They are made available by modules or created manually.</p>
-<p>Only enabled blocks are shown. You can position the blocks by deciding which area of the page they will show up on (e.g., a sidebar) and in which order they appear (weight).  Highlighting on this page shows the regions where content will be rendered.</p>
+<p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
+<p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar).   Highlighted labels on this page show the regions into which blocks can be rendered.  You can specify where within a region a block will appear by adjusting its weight.</p>
 <p>If you want certain blocks to disable themselves temporarily during high server loads, check the 'Throttle' box. You can configure the auto-throttle on the <a href=\"%throttle\">throttle configuration page</a> after having enabled the throttle module.</p>
+<p>You can configure the behaviour of each block (e.g., specifying on which pages and for what users it will appear) by clicking the 'configure' link for each block. </p>
 ", array('%throttle' => url('admin/settings/throttle')));
     case 'admin/block/add':
       return t('<p>Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="%overview">blocks</a>. The title is used when displaying the block. The description is used in the "block" column on the <a href="%overview">blocks</a> page.</p>', array('%overview' => url('admin/block')));
@@ -181,7 +184,7 @@
         }
         // Otherwise, use any set values, or else substitute defaults.
         else {
-          $properties = array ('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
+          $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
           foreach ($properties as $property => $default) {
             if (!isset ($block[$property])) {
               $block[$property] = $default;
@@ -191,7 +194,16 @@
 
         // reinsert blocks into table
         db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)",
-          $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
+                 $block['module'], 
+                 $block['delta'],
+                 $theme_key,
+                 $block['status'],
+                 $block['weight'],
+                 $block['region'],
+                 $block['visibility'],
+                 $block['pages'],
+                 $block['custom'],
+                 $block['throttle']);
         $blocks[] = $block;
 
         // build array to sort on
@@ -286,7 +298,7 @@
   $rows = array();
   if (count($regions)) {
     foreach ($regions as $region => $row) {
-      $region_title = t('%region', array ('%region' => ucfirst($block_regions[$region])));
+      $region_title = t('%region', array('%region' => ucfirst($block_regions[$region])));
       $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
       $rows = array_merge($rows, $row);
     }
@@ -319,13 +331,26 @@
 
   $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
 
+  $default_role_options = array();
+  $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
+  while ($role = db_fetch_object($result)) {
+    $default_role_options[] = $role->rid;
+  }
+
+  // get array of permitted roles
+  if (!count($default_role_options)) {
+    // nothing is set; make default 'visible to anonymous and authenticated users'
+    $default_role_options = array(1, 2);
+  }
+ 
   // Module-specific block configurations.
   if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
-    $form['block_settings'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Block specific settings'),
-      '#collapsible' => true,
-    );
+    $form['block_settings'] = 
+      array(
+        '#type' => 'fieldset',
+        '#title' => t('Block specific settings'),
+        '#collapsible' => true,
+      );
 
     foreach ($settings as $k => $v) {
       $form['block_settings'][$k] = $v;
@@ -346,7 +371,10 @@
   $form['user_vis_settings']['custom'] = array(
     '#type' => 'radios',
     '#title' => t('Custom visibility settings'),
-    '#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.')),
+    '#options' => 
+      array(t('Users cannot control whether or not they see this block.'), 
+            t('Show this block by default, but let individual users hide it.'), 
+            t('Hide this block by default but let individual users show it.')),
     '#description' =>  t('Allow individual users to customize the visibility of this block in their account settings.'),
     '#default_value' => $edit['custom'],
   );
@@ -355,6 +383,27 @@
     '#title' => t('Page specific visibility settings'),
     '#collapsible' => true,
   );
+
+  $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
+  $role_options = array();
+  while ($role = db_fetch_object($result)) {
+    $role_options[$role->rid] = $role->name;
+  }
+
+  $form['role_vis_settings'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Role specific visibility settings'),
+      '#collapsible' => true,
+    );
+  $form['role_vis_settings']['roles'] = array(
+      '#type' => 'checkboxes',
+      '#title' => t('Block visibility by role'),
+      '#default_value' => $default_role_options,
+      '#options' => $role_options,
+      '#weight' => -15, 
+      '#description' =>  t('Show this block only for the selected role(s).  You may select multiple roles.  If you select no roles, the block will be visible to all users.'),
+    );
+  
   $access = user_access('use PHP for block visibility');
 
   if ($edit['visibility'] == 2 && !$access) {
@@ -402,7 +451,19 @@
 
 function block_admin_configure_submit($form_id, $form_values) {
   if (!form_get_errors()) {
-    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']);
+    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", 
+             $form_values['visibility'],
+             $form_values['pages'],
+             $form_values['custom'],
+             $form_values['module'],
+             $form_values['delta']);
+    db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
+    foreach (array_filter($form_values['roles']) as $selection => $rid ) {
+      db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", 
+               $rid,
+               $form_values['module'],
+               $form_values['delta']);
+    }
     module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
     drupal_set_message(t('The block configuration has been saved.'));
     cache_clear_all();
@@ -573,9 +634,15 @@
   global $user, $theme_key;
 
   static $blocks = array();
+  static $user_roles = array();
+
+  if (!count($user_roles)) {
+    // get the user's role (as rids)
+    $user_roles = array_keys($user->roles);
+  }
 
   if (!count($blocks)) {
-    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
+    $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT OUTER JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", $theme_key, implode(',', $user_roles));
     while ($block = db_fetch_object($result)) {
       if (!isset($blocks[$block->region])) {
         $blocks[$block->region] = array();
@@ -631,5 +698,3 @@
   }
   return $blocks[$region];
 }
-
-
