2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
2019-04-07 19:28:11 +02:00
|
|
|
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2019-04-07 19:28:11 +02:00
|
|
|
|
2020-11-22 00:20:54 +01:00
|
|
|
class ShelfContext
|
2019-04-07 19:28:11 +02:00
|
|
|
{
|
|
|
|
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2020-11-22 00:20:54 +01:00
|
|
|
$contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
|
2019-04-07 19:28:11 +02:00
|
|
|
|
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
|
|
|
|
2022-07-16 21:55:32 +02:00
|
|
|
/** @var Bookshelf $shelf */
|
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.
|
|
|
|
*/
|
|
|
|
public function setShelfContext(int $shelfId)
|
|
|
|
{
|
2020-11-22 00:20:54 +01:00
|
|
|
session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
|
2019-04-07 19:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the session stored shelf context id.
|
|
|
|
*/
|
|
|
|
public function clearShelfContext()
|
|
|
|
{
|
2020-11-22 00:20:54 +01:00
|
|
|
session()->forget($this->KEY_SHELF_CONTEXT_ID);
|
2019-04-07 19:28:11 +02:00
|
|
|
}
|
2019-05-05 15:54:37 +02:00
|
|
|
}
|