2019-10-05 13:55:01 +02:00
|
|
|
<?php namespace BookStack\Entities\Managers;
|
2019-04-07 19:28:11 +02:00
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
use BookStack\Entities\Book;
|
|
|
|
use BookStack\Entities\Bookshelf;
|
2019-04-07 19:28:11 +02:00
|
|
|
use Illuminate\Session\Store;
|
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
class EntityContext
|
2019-04-07 19:28:11 +02:00
|
|
|
{
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EntityContextManager constructor.
|
|
|
|
*/
|
2019-10-05 13:55:01 +02:00
|
|
|
public function __construct(Store $session)
|
2019-04-07 19:28:11 +02:00
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current bookshelf context for the given book.
|
|
|
|
*/
|
2019-10-05 13:55:01 +02:00
|
|
|
public function getContextualShelfForBook(Book $book): ?Bookshelf
|
2019-04-07 19:28:11 +02:00
|
|
|
{
|
|
|
|
$contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
|
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
if (!is_int($contextBookshelfId)) {
|
|
|
|
return null;
|
2019-04-07 19:28:11 +02:00
|
|
|
}
|
2019-10-05 13:55:01 +02:00
|
|
|
|
|
|
|
$shelf = Bookshelf::visible()->find($contextBookshelfId);
|
|
|
|
$shelfContainsBook = $shelf && $shelf->contains($book);
|
|
|
|
|
|
|
|
return $shelfContainsBook ? $shelf : null;
|
2019-04-07 19:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the current contextual shelf ID.
|
|
|
|
* @param int $shelfId
|
|
|
|
*/
|
|
|
|
public function setShelfContext(int $shelfId)
|
|
|
|
{
|
|
|
|
$this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the session stored shelf context id.
|
|
|
|
*/
|
|
|
|
public function clearShelfContext()
|
|
|
|
{
|
|
|
|
$this->session->forget($this->KEY_SHELF_CONTEXT_ID);
|
|
|
|
}
|
2019-05-05 15:54:37 +02:00
|
|
|
}
|