2015-07-27 15:17:08 -04:00
|
|
|
<?php namespace Oxbow\Repos;
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Oxbow\Chapter;
|
|
|
|
|
|
|
|
class ChapterRepo
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $chapter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterRepo constructor.
|
|
|
|
* @param $chapter
|
|
|
|
*/
|
|
|
|
public function __construct(Chapter $chapter)
|
|
|
|
{
|
|
|
|
$this->chapter = $chapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getById($id)
|
|
|
|
{
|
|
|
|
return $this->chapter->findOrFail($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return $this->chapter->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBySlug($slug, $bookId)
|
|
|
|
{
|
|
|
|
return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function newFromInput($input)
|
|
|
|
{
|
|
|
|
return $this->chapter->fill($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroyById($id)
|
|
|
|
{
|
|
|
|
$page = $this->getById($id);
|
|
|
|
$page->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doesSlugExist($slug, $bookId, $currentId = false)
|
|
|
|
{
|
|
|
|
$query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
|
|
|
|
if($currentId) {
|
|
|
|
$query = $query->where('id', '!=', $currentId);
|
|
|
|
}
|
|
|
|
return $query->count() > 0;
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:57:13 -04:00
|
|
|
public function findSuitableSlug($name, $bookId, $currentId = false)
|
2015-07-27 15:17:08 -04:00
|
|
|
{
|
|
|
|
$slug = Str::slug($name);
|
2015-07-28 15:57:13 -04:00
|
|
|
while($this->doesSlugExist($slug, $bookId, $currentId)) {
|
2015-07-27 15:17:08 -04:00
|
|
|
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
|
|
|
|
}
|
|
|
|
return $slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|