Fixes #156.
*/
public function findSuitableSlug($name, $currentId = false)
{
- $originalSlug = Str::slug($name);
- $slug = $originalSlug;
- $count = 2;
+ $slug = Str::slug($name);
+ if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $currentId)) {
- $slug = $originalSlug . '-' . $count;
- $count++;
+ $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
return $slug;
}
/**
* Get all child objects of a book.
* Returns a sorted collection of Pages and Chapters.
- * Loads the bookslug onto child elements to prevent access database access for getting the slug.
+ * Loads the book slug onto child elements to prevent access database access for getting the slug.
* @param Book $book
* @param bool $filterDrafts
* @return mixed
$pages = $pageQuery->get();
- $chapterQuery = $book->chapters()->with(['pages' => function($query) use ($filterDrafts) {
+ $chapterQuery = $book->chapters()->with(['pages' => function ($query) use ($filterDrafts) {
$this->permissionService->enforcePageRestrictions($query, 'view');
if ($filterDrafts) $query->where('draft', '=', false);
}]);
$child->pages->each(function ($page) use ($bookSlug) {
$page->setAttribute('bookSlug', $bookSlug);
});
- $child->pages = $child->pages->sortBy(function($child, $key) {
+ $child->pages = $child->pages->sortBy(function ($child, $key) {
$score = $child->priority;
if ($child->draft) $score -= 100;
return $score;
});
// Sort items with drafts first then by priority.
- return $children->sortBy(function($child, $key) {
+ return $children->sortBy(function ($child, $key) {
$score = $child->priority;
if ($child->isA('page') && $child->draft) $score -= 100;
return $score;
{
$pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
// Sort items with drafts first then by priority.
- return $pages->sortBy(function($child, $key) {
+ return $pages->sortBy(function ($child, $key) {
$score = $child->priority;
if ($child->draft) $score -= 100;
return $score;
public function findSuitableSlug($name, $bookId, $currentId = false)
{
$slug = Str::slug($name);
+ if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
$draftPage->fill($input);
// Save page tags if present
- if(isset($input['tags'])) {
+ if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
}
}
// Save page tags if present
- if(isset($input['tags'])) {
+ if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($page, $input['tags']);
}
$draft->fill($data);
if (setting('app-editor') !== 'markdown') $draft->markdown = '';
-
+
$draft->save();
return $draft;
}
/**
* Gets a suitable slug for the resource
- * @param $name
- * @param $bookId
+ * @param string $name
+ * @param int $bookId
* @param bool|false $currentId
* @return string
*/
public function findSuitableSlug($name, $bookId, $currentId = false)
{
$slug = Str::slug($name);
+ if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
->visit('/books/create')
->type($book->name, '#name')
->type($book->description, '#description')
- ->press('Save Book')
- ->seePageIs('/books/my-first-book-2');
+ ->press('Save Book');
+
+ $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
+ $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
$book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
return $book;