]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/BookRepo.php
Fixed password re-writing and improved book slug creation
[bookstack] / app / Repos / BookRepo.php
index 5a772539f113bf11dcfe774e307448cb82de2949..488478aac5daf58dbae57df69a22dda29b1033c0 100644 (file)
@@ -1,5 +1,6 @@
 <?php namespace Oxbow\Repos;
 
+use Illuminate\Support\Str;
 use Oxbow\Book;
 
 class BookRepo
@@ -62,4 +63,22 @@ class BookRepo
         return $lastElem ? $lastElem->priority + 1 : 0;
     }
 
+    public function doesSlugExist($slug, $currentId = false)
+    {
+        $query = $this->book->where('slug', '=', $slug);
+        if($currentId) {
+            $query = $query->where('id', '!=', $currentId);
+        }
+        return $query->count() > 0;
+    }
+
+    public function findSuitableSlug($name, $currentId = false)
+    {
+        $slug = Str::slug($name);
+        while($this->doesSlugExist($slug, $currentId)) {
+            $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+        }
+        return $slug;
+    }
+
 }
\ No newline at end of file