1 <?php namespace Oxbow\Repos;
4 use Illuminate\Support\Facades\Auth;
5 use Illuminate\Support\Str;
7 use Oxbow\PageRevision;
12 protected $pageRevision;
15 * PageRepo constructor.
17 * @param PageRevision $pageRevision
19 public function __construct(Page $page, PageRevision $pageRevision)
22 $this->pageRevision = $pageRevision;
25 public function idExists($id)
27 return $this->page->where('page_id', '=', $id)->count() > 0;
30 public function getById($id)
32 return $this->page->findOrFail($id);
35 public function getAll()
37 return $this->page->all();
40 public function getBySlug($slug, $bookId)
42 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
45 public function newFromInput($input)
47 $page = $this->page->fill($input);
51 public function countBySlug($slug, $bookId)
53 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
56 public function destroyById($id)
58 $page = $this->getById($id);
62 public function getBySearch($term, $whereTerms = [])
64 $terms = explode(' ', preg_quote(trim($term)));
65 $pages = $this->page->fullTextSearch(['name', 'text'], $terms, $whereTerms);
67 // Add highlights to page text.
68 $words = join('|', $terms);
69 //lookahead/behind assertions ensures cut between words
70 $s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
72 foreach ($pages as $page) {
73 preg_match_all('#(?<=[' . $s . ']).{1,30}((' . $words . ').{1,30})+(?=[' . $s . '])#uis', $page->text, $matches, PREG_SET_ORDER);
74 //delimiter between occurrences
76 foreach ($matches as $line) {
77 $results[] = htmlspecialchars($line[0], 0, 'UTF-8');
80 if (count($results) > $matchLimit) {
81 $results = array_slice($results, 0, $matchLimit);
83 $result = join('... ', $results);
86 $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $result);
87 if (strlen($result) < 5) {
88 $result = $page->getExcerpt(80);
90 $page->searchSnippet = $result;
96 * Updates a page with any fillable data and saves it into the database.
102 public function updatePage(Page $page, $book_id, $data)
105 $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
106 $page->text = strip_tags($page->html);
107 $page->updated_by = Auth::user()->id;
109 $this->saveRevision($page);
114 * Saves a page revision into the system.
118 public function saveRevision(Page $page)
120 $lastRevision = $this->getLastRevision($page);
121 if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
124 $revision = $this->pageRevision->fill($page->toArray());
125 $revision->page_id = $page->id;
126 $revision->created_by = Auth::user()->id;
128 // Clear old revisions
129 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
130 $this->pageRevision->where('page_id', '=', $page->id)
131 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
137 * Gets the most recent revision for a page.
141 public function getLastRevision(Page $page)
143 return $this->pageRevision->where('page_id', '=', $page->id)
144 ->orderBy('created_at', 'desc')->first();
148 * Gets a single revision via it's id.
152 public function getRevisionById($id)
154 return $this->pageRevision->findOrFail($id);
158 * Checks if a slug exists within a book already.
161 * @param bool|false $currentId
164 public function doesSlugExist($slug, $bookId, $currentId = false)
166 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
168 $query = $query->where('id', '!=', $currentId);
170 return $query->count() > 0;
174 * Gets a suitable slug for the resource
178 * @param bool|false $currentId
181 public function findSuitableSlug($name, $bookId, $currentId = false)
183 $slug = Str::slug($name);
184 while ($this->doesSlugExist($slug, $bookId, $currentId)) {
185 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);