Author: sevein
Date: Wed Dec 15 07:08:48 2010
New Revision: 8888

Log:
Compute unique slug adding contiguous numeric suffix. Fixes issue 1906.

Modified:
   trunk/lib/model/QubitObject.php

Modified: trunk/lib/model/QubitObject.php
==============================================================================
--- trunk/lib/model/QubitObject.php     Wed Dec 15 01:52:42 2010        (r8887)
+++ trunk/lib/model/QubitObject.php     Wed Dec 15 07:08:48 2010        (r8888)
@@ -207,6 +207,7 @@
         INSERT INTO '.QubitSlug::TABLE_NAME.' ('.QubitSlug::OBJECT_ID.', 
'.QubitSlug::SLUG.')
         VALUES (?, ?)');
 
+      // Unless it is set, get random, digit and letter slug
       if (1 > strlen($this->slug))
       {
         $statement->execute(array($this->id, 
QubitSlug::getUnique($connection)));
@@ -214,16 +215,31 @@
         return $this;
       }
 
-      try
+      // Compute unique slug adding contiguous numeric suffix
+      $suffix = 2;
+      do
       {
-        $statement->execute(array($this->id, $this->slug));
-      }
+        try
+        {
+          $statement->execute(array($this->id, $this->slug));
+          unset($suffix);
+        }
+        // Collision? Try next suffix
+        catch (PDOException $e)
+        {
+          if (2 == $suffix)
+          {
+            $this->slug .= "-$suffix";
+          }
+          else
+          {
+            $this->slug = preg_replace('/-\d+$/', '', $this->slug, 
1)."-$suffix";
+          }
 
-      // Collision?  Try random, digit and letter slug
-      catch (PDOException $e)
-      {
-        $statement->execute(array($this->id, 
QubitSlug::getUnique($connection)));
+          $suffix++;
+        }
       }
+      while (isset($suffix));
     }
 
     return $this;

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to