From f33f9a5fcdb381c073501ba4c81aba3a75ce56e3 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Tue, 12 Apr 2005 18:52:47 +0000
Subject: [PATCH] - Patch #19965 by Robin Monks: fixed problem with duplicate
 block titles.

---
 database/database.mysql    |  1 -
 database/database.pgsql    |  3 +--
 database/updates.inc       | 17 ++++++++++++++++-
 modules/block.module       | 16 +++++++++++-----
 modules/block/block.module | 16 +++++++++++-----
 5 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/database/database.mysql b/database/database.mysql
index fbb2e76b5592..d65c27c6937e 100644
--- a/database/database.mysql
+++ b/database/database.mysql
@@ -154,7 +154,6 @@ CREATE TABLE boxes (
   info varchar(128) NOT NULL default '',
   format int(4) NOT NULL default '0',
   PRIMARY KEY (bid),
-  UNIQUE KEY title (title),
   UNIQUE KEY info (info)
 ) TYPE=MyISAM;
 
diff --git a/database/database.pgsql b/database/database.pgsql
index a911049f0b03..92f5dc0b72d6 100644
--- a/database/database.pgsql
+++ b/database/database.pgsql
@@ -153,8 +153,7 @@ CREATE TABLE boxes (
   info varchar(128) NOT NULL default '',
   format smallint NOT NULL default '0',
   PRIMARY KEY  (bid),
-  UNIQUE (info),
-  UNIQUE (title)
+  UNIQUE (info)
 );
 
 --
diff --git a/database/updates.inc b/database/updates.inc
index 19eac2c26af7..bc77859055bb 100644
--- a/database/updates.inc
+++ b/database/updates.inc
@@ -106,7 +106,8 @@
   "2005-03-18" => "update_127",
   "2005-03-21" => "update_128",
   "2005-04-08: first update since Drupal 4.6.0 release" => "update_129",
-  "2005-04-10" => "update_130"
+  "2005-04-10" => "update_130",
+  "2005-04-11" => "update_131"
 );
 
 function update_32() {
@@ -2376,4 +2377,18 @@ function update_130() {
   return $ret;
 }
 
+function update_131() {
+  $ret = array();
+
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {boxes} DROP INDEX title");
+    $ret[] = update_sql("ALTER TABLE {boxes} ADD INDEX title (title)");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    $ret[] = update_sql("DROP INDEX boxes_title_idx");;
+    $ret[] = update_sql("CREATE INDEX title ON {boxes} (title)");
+  }
+
+  return $ret;
+}
 ?>
diff --git a/modules/block.module b/modules/block.module
index 4248f9bb4cc9..5990d75225ab 100644
--- a/modules/block.module
+++ b/modules/block.module
@@ -312,12 +312,13 @@ function block_box_add() {
 
   switch ($op) {
     case t('Save block'):
-      block_box_save($edit);
-      drupal_set_message(t('The new block has been added.'));
-      drupal_goto('admin/block');
-
+      if (block_box_save($edit)) {
+        drupal_set_message(t('The new block has been added.'));
+        drupal_goto('admin/block');
+      }
+      // deliberate no break
     default:
-      $form = block_box_form();
+      $form = block_box_form($edit);
       $form .= form_submit(t('Save block'));
       $output .= form($form);
   }
@@ -368,8 +369,13 @@ function block_box_save($edit, $delta = NULL) {
     db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
   }
   else {
+    if (empty($edit['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $edit['info']))) {
+      form_set_error('title', t('Please ensure each block description is unique.'));
+      return false;
+    }
     db_query("INSERT INTO {boxes} (title, body, info, format) VALUES  ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
   }
+  return true;
 }
 
 /**
diff --git a/modules/block/block.module b/modules/block/block.module
index 4248f9bb4cc9..5990d75225ab 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -312,12 +312,13 @@ function block_box_add() {
 
   switch ($op) {
     case t('Save block'):
-      block_box_save($edit);
-      drupal_set_message(t('The new block has been added.'));
-      drupal_goto('admin/block');
-
+      if (block_box_save($edit)) {
+        drupal_set_message(t('The new block has been added.'));
+        drupal_goto('admin/block');
+      }
+      // deliberate no break
     default:
-      $form = block_box_form();
+      $form = block_box_form($edit);
       $form .= form_submit(t('Save block'));
       $output .= form($form);
   }
@@ -368,8 +369,13 @@ function block_box_save($edit, $delta = NULL) {
     db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
   }
   else {
+    if (empty($edit['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $edit['info']))) {
+      form_set_error('title', t('Please ensure each block description is unique.'));
+      return false;
+    }
     db_query("INSERT INTO {boxes} (title, body, info, format) VALUES  ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
   }
+  return true;
 }
 
 /**
-- 
GitLab