Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.218
diff -u -r1.218 block.module
--- modules/block/block.module	20 Aug 2006 06:49:15 -0000	1.218
+++ modules/block/block.module	20 Aug 2006 19:58:53 -0000
@@ -80,6 +80,11 @@
       'callback' => 'drupal_get_form',
       'callback arguments' => array('block_admin_configure'),
       'type' => MENU_CALLBACK);
+    $items[] = array('path' => 'admin/build/block/visibility/configure', 'title' => t('configure block'),
+      'access' => user_access('administer blocks'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('block_admin_visibility'),
+      'type' => MENU_CALLBACK);
     $items[] = array('path' => 'admin/build/block/delete', 'title' => t('delete block'),
       'access' => user_access('administer blocks'),
       'callback' => 'drupal_get_form',
@@ -90,14 +95,24 @@
       'callback' => 'drupal_get_form',
       'callback arguments' => array('block_box_form'),
       'type' => MENU_LOCAL_TASK);
+    $items[] = array('path' => 'admin/build/block/placement', 'title' => t('block placement'),
+      'access' => user_access('administer blocks'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('block_admin_placement_configure'),
+      'type' => MENU_LOCAL_TASK);
+    $items[] = array('path' => 'admin/build/block/placement/remove', 'title' => t('remove block'),
+      'access' => user_access('administer blocks'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('block_admin_placement_remove'),
+      'type' => MENU_CALLBACK);
     foreach (list_themes() as $key => $theme) {
       if ($theme->status) {
         if ($key == variable_get('theme_default', 'bluemarine')) {
-          $items[] = array('path' => 'admin/build/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
+          $items[] = array('path' => 'admin/build/block/placement/' . $key, 'title' => t('@key settings', array('@key' => $key)),
             'access' => user_access('administer blocks'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
         }
         else {
-          $items[] = array('path' => 'admin/build/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
+          $items[] = array('path' => 'admin/build/block/placement/' . $key, 'title' => t('@key settings', array('@key' => $key)),
             'access' => user_access('administer blocks'), 'type' => MENU_LOCAL_TASK);
         }
       }
@@ -154,11 +169,8 @@
  *   Blocks currently exported by modules.
  */
 function _block_rehash() {
-  global $theme_key;
-
-  init_theme();
 
-  $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
+  $result = db_query("SELECT * FROM {blocks}");
   while ($old_block = db_fetch_object($result)) {
     $old_blocks[$old_block->module][$old_block->delta] = $old_block;
   }
@@ -173,22 +185,20 @@
         $block['delta']  = $delta;
         // If previously written to database, load values.
         if ($old_blocks[$module][$delta]) {
+          $block['block_id'] = $old_blocks[$module][$delta]->block_id;
           $block['status'] = $old_blocks[$module][$delta]->status;
-          $block['weight'] = $old_blocks[$module][$delta]->weight;
-          $block['region'] = $old_blocks[$module][$delta]->region;
-          $block['visibility'] = $old_blocks[$module][$delta]->visibility;
-          $block['pages'] = $old_blocks[$module][$delta]->pages;
-          $block['custom'] = $old_blocks[$module][$delta]->custom;
           $block['throttle'] = $old_blocks[$module][$delta]->throttle;
         }
         // 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, 'pages' => '', 'custom' => 0);
           foreach ($properties as $property => $default) {
-            if (!isset($block[$property])) {
-              $block[$property] = $default;
-            }
+            $block[$property] = $default;
           }
+          // Reinsert blocks into table
+          db_query("INSERT INTO {blocks} (module, delta, status, throttle) VALUES ('%s', '%s', %d, %d)",
+                   $block['module'], $block['delta'], $block['status'], $block['throttle']);
+          $block['block_id'] = db_next_id('{blocks}_block_id');
         }
 
         $blocks[] = $block;
@@ -196,60 +206,39 @@
     }
   }
 
-  db_lock_table('blocks');
-  // Remove all blocks from table.
-  db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
-
-  // Reinsert new set of blocks into table.
-  foreach ($blocks as $block) {
-    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']);
-  }
-  db_unlock_tables();
-
   return $blocks;
 }
 
+
+
 /**
  * Generate main block administration form.
  */
-function block_admin_display($theme = NULL) {
-  global $theme_key, $custom_theme;
-
-  // If non-default theme configuration has been selected, set the custom theme.
-  if ($theme) {
-    $custom_theme = $theme;
-  }
-  else {
-    $custom_theme = variable_get('theme_default', 'bluemarine');
-  }
-  init_theme();
+function block_admin_display() {
 
   // Fetch and sort blocks
   $blocks = _block_rehash();
   usort($blocks, '_block_compare');
 
   $throttle = module_exists('throttle');
-  $block_regions = system_region_list($theme_key);
 
   // Build form tree
-  $form['#action'] = arg(3) ? url('admin/build/block/list/' . $theme_key) : url('admin/build/block');
+  $form['#action'] = url('admin/build/block');
   $form['#tree'] = TRUE;
   foreach ($blocks as $i => $block) {
+    $form[$i]['block_id'] = array('#type' => 'value', '#value' => $block['block_id']);
     $form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
     $form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
     $form[$i]['info'] = array('#value' => $block['info']);
     $form[$i]['status'] = array('#type' => 'checkbox', '#default_value' => $block['status']);
-    $form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
-    $form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
-    $form[$i]['region'] = array('#type' => 'select',
-      '#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
-      '#options' => $block_regions,
-    );
 
     if ($throttle) {
       $form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
     }
-    $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
+
+    if ($settings = module_invoke($block['module'], 'block', 'configure', $block['delta']))
+      $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['block_id']));
+
     if ($block['module'] == 'block') {
       $form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
     }
@@ -262,7 +251,7 @@
 /**
  * Helper function for sorting blocks on admin/build/block.
  *
- * Active blocks are sorted by region, then by weight.
+ * Active blocks are sorted name.
  * Disabled blocks are sorted by name.
  */
 function _block_compare($a, $b) {
@@ -271,15 +260,8 @@
   if ($status) {
     return $status;
   }
-  // Enabled blocks
-  if ($a['status']) {
-    $place = strcmp($a['region'], $b['region']);
-    return $place ? $place : ($a['weight'] - $b['weight']);
-  }
-  // Disabled blocks
-  else {
-    return strcmp($a['info'], $b['info']);
-  }
+  // sort alphabetically
+  return strcmp($a['info'], $b['info']);
 }
 
 /**
@@ -287,71 +269,56 @@
  */
 function block_admin_display_submit($form_id, $form_values) {
   foreach ($form_values as $block) {
-    db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], $block['throttle'], $block['module'], $block['delta'], $block['theme']);
+    db_query("UPDATE {blocks} SET status = %d, throttle = %d WHERE block_id = %d", $block['status'], $block['throttle'], $block['block_id']);
   }
   drupal_set_message(t('The block settings have been updated.'));
   cache_clear_all();
+  return 'admin/build/block/';
 }
 
 /**
  * Theme main block administration form submission.
- *
- * Note: the blocks are already sorted in the right order,
- * grouped by status, region and weight.
+ * - here the user controls global block configuration: status, throttle and block-specific configuration
  */
 function theme_block_admin_display($form) {
   global $theme_key;
 
   $throttle = module_exists('throttle');
-  $block_regions = system_region_list($theme_key);
-
-  // Highlight regions on page to provide visual reference.
-  foreach ($block_regions as $key => $value) {
-    drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
-  }
 
   // Build rows
   $rows = array();
-  $last_region = '';
   $last_status = 1;
+  $rows[] = array(array('data' => t('Enabled'), 'class' => 'region', 'colspan' => ($throttle ? 5 : 4)));
   foreach (element_children($form) as $i) {
     $block = $form[$i];
     // Only take form elements that are blocks.
     if (is_array($block['info'])) {
-      // Fetch values
-      $region = $block['region']['#default_value'];
       $status = $block['status']['#default_value'];
 
-      // Output region header
-      if ($status && $region != $last_region) {
-        $region_title = t('%region', array('%region' => drupal_ucfirst($block_regions[$region])));
-        $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
-        $last_region = $region;
-      }
       // Output disabled header
-      elseif ($status != $last_status) {
-        $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
+      if ($status != $last_status) {
+        $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 5 : 4)));
         $last_status = $status;
       }
 
       // Generate block row
       $row = array(
         array('data' => drupal_render($block['info']), 'class' => 'block'),
-        drupal_render($block['status']) . drupal_render($block['theme']),
-        drupal_render($block['weight']),
-        drupal_render($block['region'])
+        drupal_render($block['status'])
       );
       if ($throttle) {
         $row[] = drupal_render($block['throttle']);
       }
-      $row[] = drupal_render($block['configure']);
+
+      $row[] = $block['configure'] ? drupal_render($block['configure']) : '';
+
       $row[] = $block['delete'] ? drupal_render($block['delete']) : '';
       $rows[] = $row;
     }
   }
 
   // Finish table
-  $header = array(t('Block'), t('Enabled'), t('Weight'), t('Placement'));
+  $header = array(t('Block'), t('Enabled'));
   if ($throttle) {
     $header[] = t('Throttle');
   }
@@ -371,31 +338,263 @@
 }
 
 /**
- * Menu callback; displays the block configuration form.
+ * Helper function for sorting blocks on admin/build/block/placement.
+ *
+ * Active blocks are sorted by region, then by weight, then by name.
  */
-function block_admin_configure($module = NULL, $delta = 0) {
+function _block_compare_placement($a, $b) {
+  $place = strcmp($a['region'], $b['region']);
+  if ($place)
+    return $place;
 
-  $form['module'] = array('#type' => 'value', '#value' => $module);
-  $form['delta'] = array('#type' => 'value', '#value' => $delta);
+   $diff = $a['weight'] - $b['weight'];
+   return $diff ? $diff : $place = strcmp($a['info'], $b['info']);
+}
 
-  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
+/**
+ * Update the 'blocks' DB table with the blocks currently exported by modules.
+ *
+ * @return
+ *   Blocks currently exported by modules.
+ */
+function _block_rehash_placement() {
+  global $theme_key;
 
-  // 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,
-    );
+  init_theme();
 
-    foreach ($settings as $k => $v) {
-      $form['block_settings'][$k] = $v;
+  $result = db_query("SELECT bt.*, b.status, b.module, b.delta FROM {block_theme} bt LEFT JOIN {blocks} b ON (bt.block_id = b.block_id) WHERE bt.theme = '%s' ORDER BY bt.instance", $theme_key);
+  while ($old_block = db_fetch_object($result)) {
+    $old_blocks[$old_block->instance] = $old_block;
+  }
+
+  if ($old_blocks) {
+    foreach ($old_blocks as $instance => $old_block) {
+      $block = array();
+
+      // If previously written to database, load values.
+      if ($old_block->status) {
+        $block['block_id'] = $old_block->block_id;
+        if ($instance == -1)
+          $block['instance'] = db_next_id('{block_theme}_instance');
+        else
+          $block['instance'] = $instance;
+
+        $module_blocks = module_invoke($old_block->module, 'block', 'list');
+        $block['info'] = $module_blocks[$old_block->delta]['info'];
+
+        $block['visibility'] = $old_block->visibility;
+        $block['pages'] = $old_block->pages;
+        $block['custom'] = $old_block->custom;
+
+        $block['weight'] = $old_block->weight;
+        $block['region'] = $old_block->region;
+        // Reinsert blocks into table
+        $blocks[] = $block;
+      }
+
+      if ($blocks) {
+        db_lock_table('{block_theme}');
+        db_query("DELETE FROM {block_theme} WHERE theme = '%s'", $theme_key);
+        foreach ($blocks as $i => $block) {
+          db_query("INSERT INTO {block_theme} (block_id, instance, theme, weight, region, visibility, pages, custom) VALUES (%d, %d, '%s', %d, '%s', %d, '%s', %d)",
+                  $block['block_id'], $block['instance'], $theme_key, $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom']);
+        }
+        db_unlock_tables();
+      }
+    }
+  }
+
+  return $blocks;
+}
+
+function block_admin_placement_configure($theme = NULL) {
+  global $theme_key, $custom_theme;
+
+  // If non-default theme configuration has been selected, set the custom theme.
+  if ($theme) {
+    $custom_theme = $theme;
+  }
+  else {
+    $custom_theme = variable_get('theme_default', 'bluemarine');
+  }
+  init_theme();
+
+  $block_regions = system_region_list($theme_key);
+
+  // Fetch and sort blocks
+  $blocks = _block_rehash();
+  $placed_blocks = _block_rehash_placement();
+  if (isset($placed_blocks))
+    usort($placed_blocks, '_block_compare_placement');
+
+    // Build form tree
+  $form['#action'] = arg(3) ? url('admin/build/block/placement/' . $theme_key) : url('admin/build/block/placement');
+  $form['#tree'] = TRUE;
+  $form['theme'] = array('#type' => 'value', '#value' => $theme_key);
+
+  // form for placing new instances of a block
+  $options = array();
+  foreach ($blocks as $i => $block) {
+    if ($block['status'])
+      $options[] = array($block['block_id'] => $block['info']);
+  }
+  $form['new_block']['block_id'] = array(
+    '#type' => 'select',
+    '#options' => $options,
+    '#description' => t('Place a new block in the selected region.'),
+  );
+  $form['new_block']['weight'] = array('#type' => 'weight', '#default_value' => 0);
+  $form['new_block']['region'] = array('#type' => 'select',
+                                       '#default_value' => system_default_region($theme_key),
+                                       '#options' => $block_regions,
+                                      );
+  $form['new_block']['submit'] = array('#type' => 'submit', '#value' => t('Place block'));
+
+  // form for configuring currently placed blocks
+  if (isset($placed_blocks)) {
+    foreach ($placed_blocks as $i => $block) {
+      $form['placement'][$i]['block_id'] = array('#type' => 'value', '#value' => $block['block_id']);
+      $form['placement'][$i]['instance'] = array('#type' => 'value', '#value' => $block['instance']);
+      $form['placement'][$i]['info'] = array('#value' => $block['info']);
+      $form['placement'][$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
+      $form['placement'][$i]['region'] = array('#type' => 'select',
+                                               '#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
+                                               '#options' => $block_regions,
+                                              );
+      $form['placement'][$i]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/visibility/configure/'. $block['instance']));
+      $form['placement'][$i]['remove'] = array('#value' => l(t('remove'), 'admin/build/block/placement/remove/'. $block['instance'] . '/' . $theme_key));
+    }
+    $form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
+  }
+  return $form;
+}
+
+/**
+ * Process main block placement form submission.
+ */
+function block_admin_placement_configure_submit($form_id, $form_values) {
+  $op = $_POST['op'];
+  switch ($op) {
+    case t('Place block'):
+      $block = $form_values['new_block'];
+      db_query("INSERT INTO {block_theme} (block_id, theme, weight, region, instance) VALUES (%d, '%s', %d, '%s', %d)", $block['block_id'], $form_values['theme'], $block['weight'], $block['region'], -1);
+      drupal_set_message(t('A new block has been placed.'));
+      break;
+    default:
+      foreach ($form_values['placement'] as $block) {
+      db_query("UPDATE {block_theme} SET weight = %d, region = '%s' WHERE block_id = %d AND instance = %d AND theme = '%s'", $block['weight'], $block['region'], $block['block_id'], $block['instance'], $form_values['theme']);
     }
+    drupal_set_message(t('The block placement settings have been updated.'));
   }
 
+  cache_clear_all();
+  return 'admin/build/block/placement/'.$form_values['theme'];
+}
+
+/**
+ * Menu callback; confirm block removal
+ */
+function block_admin_placement_remove($instance, $theme) {
+  $form['instance'] = array('#type' => 'hidden', '#value' => $instance);
+  $form['theme'] = array('#type' => 'hidden', '#value' => $theme);
+  return confirm_form($form, t('Are you sure you want to remove this block ?'), 'admin/build/block/placement/'.$theme, '', t('Remove'), t('Cancel'));
+}
+
+/**
+ * Remov of placed blocks
+ */
+function block_admin_placement_remove_submit($form_id, $form_values) {
+  db_query("DELETE FROM {block_theme} WHERE instance = %d", $form_values['instance']);
+  drupal_set_message(t('The block has been removed.'));
+  cache_clear_all();
+  return 'admin/build/block/placement/'. $form_values['theme'];
+}
+
+/**
+ * Block placement form.
+ *
+ * Note: the blocks are already sorted in the right order,
+ * grouped by status, region and weight.
+ */
+function theme_block_admin_placement_configure($form) {
+  global $theme_key;
+
+  $block_regions = system_region_list($theme_key);
+
+  // Highlight regions on page to provide visual reference.
+  foreach ($block_regions as $key => $value) {
+    drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
+  }
+
+  // Build rows
+  $rows = array();
+  $last_region = '';
+  foreach (element_children($form['placement']) as $i) {
+    $block = $form['placement'][$i];
+    // Only take form elements that are blocks.
+    if (is_array($block['info'])) {
+      // Fetch values
+      $region = $block['region']['#default_value'];
+
+      // Output region header
+      if ($region != $last_region) {
+        $region_title = t('%region', array('%region' => drupal_ucfirst($block_regions[$region])));
+        $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => 5));
+        $last_region = $region;
+      }
+
+      // Generate block row
+      $row = array(
+        array('data' => drupal_render($block['info']), 'class' => 'block'),
+        /*drupal_render($block['theme']),*/
+        drupal_render($block['weight']),
+        drupal_render($block['region'])
+      );
+      $row[] = drupal_render($block['configure']);
+      $row[] = $block['remove'] ? drupal_render($block['remove']) : '';
+      $rows[] = $row;
+    }
+  }
+
+  // Finish table
+  $header = array(t('Block'), t('Weight'), t('Placement'));
+
+  $new_block_row[] = array(array('data' => drupal_render($form['new_block']['block_id']),
+                                 'class' => 'block'),
+                           drupal_render($form['new_block']['weight']),
+                           drupal_render($form['new_block']['region']));
+
+  $output = theme('table', $header, $new_block_row, array('id' => 'blocks'));
+  $output .= drupal_render($form['new_block']['submit']);
+
+  $header[] = array('data' => t('Operations'), 'colspan' => 2);
+
+  // display the block add selection and button
+  $output .= theme('table', $header, $rows, array('id' => 'blocks'));
+  $output .= drupal_render($form['submit']);
+
+  // Also render the form_id as there is no drupal_render($form) call (as drupal_render does not appear to handle the
+  // multi-dimensional block form array very well).
+  $output .= drupal_render($form['form_id']);
+
+  return $output;
+}
+
+
+/**
+ * Menu callback; displays the block visibility configuration form.
+ */
+function block_admin_visibility($instance = 0) {
+  global $theme_key;
+
+  $form['instance'] = array('#type' => 'value', '#value' => $instance);
+  $edit = db_fetch_array(db_query("SELECT bt.block_id, bt.pages, bt.visibility, bt.custom, bt.region, b.module, b.delta
+                                   FROM {block_theme} bt, {blocks} b WHERE bt.block_id = b.block_id AND bt.instance = %d", $instance));
+
+  $form['block_id'] = array('#type' => 'value', '#value' => $edit['block_id']);
   // Get the block subject for the page title.
-  $info = module_invoke($module, 'block', 'list');
-  drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
+  $info = module_invoke($edit['module'], 'block', 'list');
+  drupal_set_title(t("'%name' block in region: %region", array('%name' => $info[$edit['delta']]['info'], '%region' => $edit['region'])));
 
   // Standard block configurations.
 
@@ -418,7 +617,7 @@
 
   // Role-based visibility settings
   $default_role_options = array();
-  $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
+  $result = db_query("SELECT rid FROM {blocks_roles} WHERE block_id = %d", $edit['block_id']);
   while ($role = db_fetch_object($result)) {
     $default_role_options[] = $role->rid;
   }
@@ -454,11 +653,11 @@
   }
   else {
     $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
-    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
+    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' =>  theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>')));
 
     if ($access) {
       $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
-      $description .= t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
+      $description .= t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => theme('placeholder', '<?php ?>')));
     }
     $form['page_vis_settings']['visibility'] = array(
       '#type' => 'radios',
@@ -474,6 +673,50 @@
     );
   }
 
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
+
+  return $form;
+}
+
+
+function block_admin_visibility_submit($form_id, $form_values) {
+  db_query("UPDATE {block_theme} SET visibility = %d, pages = '%s', custom = %d WHERE block_id = %d AND instance = %d", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['block_id'], $form_values['instance']);
+  db_query("DELETE FROM {blocks_roles} WHERE block_id = %d", $form_values['block_id']);
+  foreach (array_filter($form_values['roles']) as $rid) {
+    db_query("INSERT INTO {blocks_roles} (rid, block_id) VALUES (%d, %d)", $rid, $form_values['block_id']);
+  }
+  drupal_set_message(t('The block configuration has been saved.'));
+  cache_clear_all();
+  return 'admin/build/block/placement';
+}
+
+
+/**
+ * Menu callback; displays the block configuration form.
+ */
+function block_admin_configure($block_id) {
+
+  $edit = db_fetch_array(db_query("SELECT * FROM {blocks} WHERE block_id = %d", $block_id));
+
+  $module = $edit['module'];
+  $delta = $edit['delta'];
+
+  $form['module'] = array('#type' => 'value', '#value' => $module);
+  $form['delta'] = array('#type' => 'value', '#value' => $delta);
+
+  // 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,
+    );
+
+    foreach ($settings as $k => $v) {
+      $form['block_settings'][$k] = $v;
+    }
+  }
+
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save block'),
@@ -492,11 +735,6 @@
 
 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("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
-    foreach (array_filter($form_values['roles']) as $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();
@@ -504,6 +742,7 @@
   }
 }
 
+
 function block_box_form_validate($form_id, $form_values) {
   if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
     form_set_error('info', t('Please ensure that each block description is unique.'));
@@ -598,7 +837,7 @@
   switch ($type) {
     case 'form':
       if ($category == 'account') {
-        $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY b.weight, b.module", implode(',', array_keys($user->roles)));
+        $result = db_query("SELECT DISTINCT b.*, bt.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON (b.block_id = r.block_id) LEFT JOIN {block_theme} bt ON (b.block_id = bt.block_id) WHERE b.status = 1 AND custom != 0 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY bt.region, bt.weight, b.module", implode(',', array_keys($user->roles)));
         $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
         while ($block = db_fetch_object($result)) {
           $data = module_invoke($block->module, 'block', 'list');
@@ -645,7 +884,7 @@
   static $blocks = array();
 
   if (!count($blocks)) {
-    $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT 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(',', array_keys($user->roles)));
+    $result = db_query("SELECT DISTINCT b.*, bt.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON (b.block_id = r.block_id) LEFT JOIN {block_theme} bt ON (b.block_id = bt.block_id) WHERE bt.theme = '%s' AND b.status = 1 AND (r.rid IN (%s) OR r.rid IS NULL) ORDER BY bt.region, bt.weight, b.module", $theme_key, implode(',', array_keys($user->roles)));
     while ($block = db_fetch_object($result)) {
       if (!isset($blocks[$block->region])) {
         $blocks[$block->region] = array();
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.9
diff -u -r1.9 system.install
--- modules/system/system.install	20 Aug 2006 06:38:50 -0000	1.9
+++ modules/system/system.install	20 Aug 2006 19:52:07 -0000
@@ -23,16 +23,22 @@
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
       db_query("CREATE TABLE {blocks} (
+        block_id INT(10) NOT NULL auto_increment PRIMARY KEY,
         module varchar(64) DEFAULT '' NOT NULL,
         delta varchar(32) NOT NULL default '0',
-        theme varchar(255) NOT NULL default '',
         status tinyint DEFAULT '0' NOT NULL,
-        weight tinyint DEFAULT '0' NOT NULL,
+        throttle tinyint DEFAULT '0' NOT NULL
+      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
+      
+      db_query("CREATE TABLE {block_theme} (
+        block_id INT(10) NOT NULL,
+        instance INT(10) DEFAULT '-1' NOT NULL,
+        theme varchar(255) NOT NULL default '',
+        weight smallint DEFAULT '0' NOT NULL,
         region varchar(64) DEFAULT 'left' NOT NULL,
-        custom tinyint DEFAULT '0' NOT NULL,
-        throttle tinyint DEFAULT '0' NOT NULL,
-        visibility tinyint DEFAULT '0' NOT NULL,
-        pages text DEFAULT '' NOT NULL
+        custom smallint DEFAULT '0' NOT NULL,        
+        visibility smallint DEFAULT '0' NOT NULL,
+        pages text DEFAULT ''
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
       db_query("CREATE TABLE {boxes} (
@@ -240,10 +246,9 @@
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
       db_query("CREATE TABLE {blocks_roles} (
-        module varchar(64) NOT NULL,
-        delta varchar(32) NOT NULL,
+        block_id INT(10) NOT NULL DEFAULT 0,
         rid int unsigned NOT NULL,
-        PRIMARY KEY (module, delta, rid)
+        PRIMARY KEY (block_id, rid)
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
       db_query("CREATE TABLE {sessions} (
@@ -818,8 +823,10 @@
 
   db_query("INSERT INTO {variable} (name,value) VALUES('theme_default', 's:10:\"bluemarine\";')");
 
-  db_query("INSERT INTO {blocks} (module,delta,theme,status) VALUES('user', 0, 'bluemarine', 1)");
-  db_query("INSERT INTO {blocks} (module,delta,theme,status) VALUES('user', 1, 'bluemarine', 1)");
+  db_query("INSERT INTO {blocks} (module,delta,status) VALUES('user', 0, 1)");
+  db_query("INSERT INTO {blocks} (module,delta,status) VALUES('user', 1, 1)");
+  db_query("INSERT INTO {block_theme} (block_id,theme,region) VALUES(1, 'bluemarine', 'left')");
+  db_query("INSERT INTO {block_theme} (block_id,theme,region) VALUES(2, 'bluemarine', 'left')");
 
   db_query("INSERT INTO {node_access} VALUES (0, 0, 'all', 1, 0, 0)");
 
@@ -3047,6 +3054,74 @@
   return $ret;
 }
 
+function system_update_1006() {
+  $ret = array();
+  
+  switch ($GLOBALS['db_type']) {
+    case 'mysqli':
+    case 'mysql':
+      $ret[] = update_sql("CREATE TABLE {block_theme} (
+        block_id INT(10) NOT NULL,
+        instance INT(10) DEFAULT '-1' NOT NULL,
+        theme varchar(255) NOT NULL default '',
+        weight smallint DEFAULT '0' NOT NULL,
+        region varchar(64) DEFAULT 'left' NOT NULL,
+        custom smallint DEFAULT '0' NOT NULL,        
+        visibility smallint DEFAULT '0' NOT NULL,
+        pages text DEFAULT ''
+      )");
+      break;
+    case 'pgsql':      
+      break;
+  }
+  
+  $blocks = array();
+  $query = db_query('SELECT * FROM {blocks}');  
+  while ($block = db_fetch_array($query)) {
+    $query2 = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = %d", $block['module'], $block['delta']);
+    $block['roles'] = array();
+    while ($role = db_fetch_object($query2)) {
+      $block['roles'][] = $role->rid;
+    }
+    $blocks[] = $block;
+  }
+  
+  switch ($GLOBALS['db_type']) {
+    case 'mysqli':
+    case 'mysql':
+      $ret[] = update_sql("DELETE FROM {blocks}");
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `pages`"); 
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `visibility`"); 
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `custom`");
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `region`");
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `weight`");
+      $ret[] = update_sql("ALTER TABLE {blocks} DROP `theme`");
+      $ret[] = update_sql("ALTER TABLE {blocks} ADD COLUMN block_id int(10) NOT NULL auto_increment PRIMARY KEY FIRST");
+      $ret[] = update_sql("DELETE FROM {blocks_roles}");
+      $ret[] = update_sql("ALTER TABLE {blocks_roles} ADD COLUMN block_id int(10) NOT NULL DEFAULT 0 FIRST");
+      $ret[] = update_sql("ALTER TABLE {blocks_roles} DROP `module`");
+      $ret[] = update_sql("ALTER TABLE {blocks_roles} DROP `delta`");
+      break;
+    case 'pgsql':
+      break;
+  }
+  foreach ($blocks as $block_id => $block) {
+    db_query("INSERT INTO {blocks} (module, delta, status, throttle) VALUES ('%s', %d, %d, %d)", $block['module'], $block['delta'], $block['status'], $block['throttle']);
+    if (count($block['roles']) > 0) {
+      foreach ($block['roles'] as $i => $rid) {
+        db_query("INSERT INTO {blocks_roles} (block_id, rid) VALUES (%d, %d)", $block_id, $rid);
+      }
+    }
+    if ($block['status']) {
+      $instance = db_next_id("{block_theme}_instance");
+      db_query("INSERT INTO {block_theme} (block_id, instance, theme, weight, region, visibility, pages, custom) VALUES (%d, %d, '%s', %d, '%s', %d, '%s', %d)",
+                           $block_id+1, $instance, $block['theme'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom']);
+    }
+  }
+  
+  return $ret;
+}
+
 /**
  * @} End of "defgroup updates-4.7-to-x.x"
  * The next series of updates should start at 2000.
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.348
diff -u -r1.348 system.module
--- modules/system/system.module	20 Aug 2006 05:57:40 -0000	1.348
+++ modules/system/system.module	20 Aug 2006 19:38:58 -0000
@@ -392,7 +392,7 @@
 function system_admin_theme_submit($form_id, $form_values) {
   // If we're changing themes, make sure the theme has its blocks initialized.
   if ($form_values['admin_theme'] != variable_get('admin_theme', 'bluemarine')) {
-    $result = db_query("SELECT status FROM {blocks} WHERE theme = '%s'", $form_values['admin_theme']);
+    $result = db_query("SELECT block_id FROM {block_theme} WHERE theme = '%s'", $form_values['admin_theme']);
     if (!db_num_rows($result)) {
       system_initialize_theme_blocks($form_values['admin_theme']);
     }
@@ -1034,17 +1034,17 @@
  */
 function system_initialize_theme_blocks($theme) {
   // Initialize theme's blocks if none already registered.
-  if (!(db_num_rows(db_query("SELECT module FROM {blocks} WHERE theme = '%s'", $theme)))) {
+  if (!(db_num_rows(db_query("SELECT block_id FROM {block_theme} WHERE theme = '%s'", $theme)))) {
     $default_theme = variable_get('theme_default', 'bluemarine');
     $regions = system_region_list($theme);
-    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $default_theme);
+    $result = db_query("SELECT * FROM {block_theme} WHERE theme = '%s'", $default_theme);
     while($block = db_fetch_array($result)) {
       // If the region isn't supported by the theme, assign the block to the theme's default region.
       if (!array_key_exists($block['region'], $regions)) {
         $block['region'] = system_default_region($theme);
       }
-      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, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
+      db_query("INSERT INTO {block_theme} (block_id, theme, weight, region, visibility, pages, custom) VALUES (%d, '%s', %d, '%s', %d, '%s', %d)",
+          $block['block_id'], $theme, $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom']);
     }
   }
 }
