2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
2019-04-07 13:28:11 -04:00
|
|
|
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2019-04-07 13:28:11 -04:00
|
|
|
|
2020-11-21 18:20:54 -05:00
|
|
|
class ShelfContext
|
2019-04-07 13:28:11 -04:00
|
|
|
{
|
|
|
|
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current bookshelf context for the given book.
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function getContextualShelfForBook(Book $book): ?Bookshelf
|
2019-04-07 13:28:11 -04:00
|
|
|
{
|
2020-11-21 18:20:54 -05:00
|
|
|
$contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
|
2019-04-07 13:28:11 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
if (!is_int($contextBookshelfId)) {
|
|
|
|
return null;
|
2019-04-07 13:28:11 -04:00
|
|
|
}
|
2019-10-05 07:55:01 -04:00
|
|
|
|
2022-07-16 15:55:32 -04:00
|
|
|
/** @var Bookshelf $shelf */
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = Bookshelf::visible()->find($contextBookshelfId);
|
|
|
|
$shelfContainsBook = $shelf && $shelf->contains($book);
|
|
|
|
|
|
|
|
return $shelfContainsBook ? $shelf : null;
|
2019-04-07 13:28:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the current contextual shelf ID.
|
|
|
|
*/
|
|
|
|
public function setShelfContext(int $shelfId)
|
|
|
|
{
|
2020-11-21 18:20:54 -05:00
|
|
|
session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
|
2019-04-07 13:28:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the session stored shelf context id.
|
|
|
|
*/
|
|
|
|
public function clearShelfContext()
|
|
|
|
{
|
2020-11-21 18:20:54 -05:00
|
|
|
session()->forget($this->KEY_SHELF_CONTEXT_ID);
|
2019-04-07 13:28:11 -04:00
|
|
|
}
|
2019-05-05 09:54:37 -04:00
|
|
|
}
|