--- C:/Documents and Settings/Patrick/Local Settings/Temp/path.module-revBASE.svn000.tmp.module	Tue Jun 08 22:12:55 2010
+++ C:/Documents and Settings/Patrick/My Documents/_WhiskyEchoBravo/src/OACIQ/trunk/www/modules/path/path.module	Tue Jun 08 22:34:14 2010
@@ -84,10 +84,20 @@
 
 /**
  * Set an aliased path for a given Drupal path, preventing duplicates.
+ * 
+ * @param $path
+ *   Path URL. Set to NULL to delete alias.
+ * @param $alias
+ *   Alias URL. Set to NULL to delete alias.
+ * @param $pid
+ *   Path id to update. Set to NULL to create a new alias or to delete a group of aliases.
+ * @param $language
+ *   The language this alias is valid for.
  */
 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
   $path = urldecode($path);
   $alias = urldecode($alias);
+  $result = TRUE;
   // First we check if we deal with an existing alias and delete or modify it based on pid.
   if ($pid) {
     // An existing alias.
@@ -97,19 +107,23 @@
     }
     else {
       // Update the existing alias.
-      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
+      $count = db_result(db_query("SELECT COUNT(pid) FROM {url_alias} WHERE pid != %d AND dst = '%s' AND language = '%s'", $pid, $alias, $language));
+      if ($count == 0) {
+        db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
+      }
+      else {
+        $result = FALSE;
+      }
     }
   }
   else if ($path && $alias) {
-    // Check for existing aliases.
-    if ($alias == drupal_get_path_alias($path, $language)) {
-      // There is already such an alias, neutral or in this language.
-      // Update the alias based on alias; setting the language if not yet done.
-      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
+    // Create a new URL alias.
+    $count = db_result(db_query("SELECT COUNT(pid) FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language));
+    if ($count == 0) {
+      db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
     }
     else {
-      // A new alias. Add it to the database.
-      db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
+      $result = FALSE;
     }
   }
   else {
@@ -122,6 +136,8 @@
     }
   }
   drupal_clear_path_cache();
+  
+  return $result;
 }
 
 
@@ -162,6 +178,7 @@
         break;
 
       case 'update':
+        drupal_set_message($node->pid);
         path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
         break;
 
