mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-31 21:23:38 +08:00
Moved models to folder, renamed managers to tools
Tools seems to fit better since the classes were a bit of a mixed bunch and did not always manage. Also simplified the structure of the SlugGenerator class. Also focused EntityContext on shelves and simplified to use session helper.
This commit is contained in:
42
app/Entities/Tools/ShelfContext.php
Normal file
42
app/Entities/Tools/ShelfContext.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php namespace BookStack\Entities\Tools;
|
||||
|
||||
use BookStack\Entities\Book;
|
||||
use BookStack\Entities\Bookshelf;
|
||||
|
||||
class ShelfContext
|
||||
{
|
||||
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
|
||||
|
||||
/**
|
||||
* Get the current bookshelf context for the given book.
|
||||
*/
|
||||
public function getContextualShelfForBook(Book $book): ?Bookshelf
|
||||
{
|
||||
$contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
|
||||
|
||||
if (!is_int($contextBookshelfId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$shelf = Bookshelf::visible()->find($contextBookshelfId);
|
||||
$shelfContainsBook = $shelf && $shelf->contains($book);
|
||||
|
||||
return $shelfContainsBook ? $shelf : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the current contextual shelf ID.
|
||||
*/
|
||||
public function setShelfContext(int $shelfId)
|
||||
{
|
||||
session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the session stored shelf context id.
|
||||
*/
|
||||
public function clearShelfContext()
|
||||
{
|
||||
session()->forget($this->KEY_SHELF_CONTEXT_ID);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user