public function getUrl($path = false)
{
if ($path !== false) {
- return baseUrl('/books/' . $this->slug . '/' . trim($path, '/'));
+ return baseUrl('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
}
- return baseUrl('/books/' . $this->slug);
+ return baseUrl('/books/' . urlencode($this->slug));
}
/*
{
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
if ($path !== false) {
- return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug . '/' . trim($path, '/'));
+ return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
}
- return baseUrl('/books/' . $bookSlug. '/chapter/' . $this->slug);
+ return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
}
/**
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
$this->checkOwnablePermission('chapter-update', $chapter);
+ if ($chapter->name !== $request->get('name')) {
+ $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id);
+ }
$chapter->fill($request->all());
- $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
$chapter->updated_by = user()->id;
$chapter->save();
Activity::add($chapter, 'chapter_update', $book->id);
{
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
$midText = $this->draft ? '/draft/' : '/page/';
- $idComponent = $this->draft ? $this->id : $this->slug;
+ $idComponent = $this->draft ? $this->id : urlencode($this->slug);
if ($path !== false) {
- return baseUrl('/books/' . $bookSlug . $midText . $idComponent . '/' . trim($path, '/'));
+ return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent . '/' . trim($path, '/'));
}
- return baseUrl('/books/' . $bookSlug . $midText . $idComponent);
+ return baseUrl('/books/' . urlencode($bookSlug) . $midText . $idComponent);
}
/**
*/
public function updateFromInput(Book $book, $input)
{
+ if ($book->name !== $input['name']) {
+ $book->slug = $this->findSuitableSlug($input['name'], $book->id);
+ }
$book->fill($input);
- $book->slug = $this->findSuitableSlug($book->name, $book->id);
$book->updated_by = user()->id;
$book->save();
$this->permissionService->buildJointPermissionsForEntity($book);
*/
public function findSuitableSlug($name, $currentId = false)
{
- $slug = Str::slug($name);
- if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
+ $slug = $this->nameToSlug($name);
while ($this->doesSlugExist($slug, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
*/
public function findSuitableSlug($name, $bookId, $currentId = false)
{
- $slug = Str::slug($name);
- if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
+ $slug = $this->nameToSlug($name);
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
$this->permissionService->buildJointPermissionsForEntities($collection);
}
+ /**
+ * Format a name as a url slug.
+ * @param $name
+ * @return string
+ */
+ protected function nameToSlug($name)
+ {
+ $slug = str_replace(' ', '-', strtolower($name));
+ $slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', $slug);
+ if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
+ return $slug;
+ }
+
}
*/
public function findSuitableSlug($name, $bookId, $currentId = false)
{
- $slug = Str::slug($name);
- if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
+ $slug = $this->nameToSlug($name);
while ($this->doesSlugExist($slug, $bookId, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}