From 410b1e02aca5180cbca1506d70d6360e9b5367ea Mon Sep 17 00:00:00 2001
From: scottrigby <scott@basekamp.com>
Date: Tue, 25 Oct 2011 21:06:51 -0400
Subject: [PATCH] Issue #991344: Check if input format exists before inserting

---
 input_formats.module |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/input_formats.module b/input_formats.module
index 36b7bbb..eecbf4c 100644
--- a/input_formats.module
+++ b/input_formats.module
@@ -511,8 +511,16 @@ function input_formats_delete_variables($input_format) {
  */
 function input_formats_save(&$input_format, $new = TRUE) {
   if ($new) {
-    db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $input_format->name);
-    $input_format->format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}"));
+    // Check if input format exists before inserting.
+    $query = db_query("SELECT format FROM {filter_formats} WHERE name = '%s'", $input_format->name);
+    $format = db_result($query);
+    if (!empty($format)) {
+      $input_format->format = $format;
+    }
+    else {
+      db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $input_format->name);
+      $input_format->format = db_result($query);
+    }
   }
   else {
     if (empty($input_format->format)) {
-- 
1.7.6.1

