Also cleaned up any other bits along the way.
namespace BookStack\Actions;
use BookStack\Auth\User;
+use BookStack\Entities\Entity;
use BookStack\Model;
/**
- * @property string key
- * @property \User user
- * @property \Entity entity
- * @property string extra
+ * @property string $key
+ * @property User $user
+ * @property Entity $entity
+ * @property string $extra
+ * @property string $entity_type
+ * @property int $entity_id
+ * @property int $user_id
+ * @property int $book_id
*/
class Activity extends Model
{
use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\Entity;
-use Session;
class ActivityService
{
/**
* ActivityService constructor.
- * @param \BookStack\Actions\Activity $activity
+ * @param Activity $activity
* @param PermissionService $permissionService
*/
public function __construct(Activity $activity, PermissionService $permissionService)
/**
* Add activity data to database.
- * @param Entity $entity
- * @param $activityKey
+ * @param \BookStack\Entities\Entity $entity
+ * @param string $activityKey
* @param int $bookId
- * @param bool $extra
*/
- public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
+ public function add(Entity $entity, string $activityKey, int $bookId = null)
{
- $activity = $this->activity->newInstance();
- $activity->user_id = $this->user->id;
- $activity->book_id = $bookId;
- $activity->key = strtolower($activityKey);
- if ($extra !== false) {
- $activity->extra = $extra;
- }
+ $activity = $this->newActivityForUser($activityKey, $bookId);
$entity->activity()->save($activity);
$this->setNotification($activityKey);
}
/**
- * Adds a activity history with a message & without binding to a entity.
- * @param $activityKey
+ * Adds a activity history with a message, without binding to a entity.
+ * @param string $activityKey
+ * @param string $message
* @param int $bookId
- * @param bool|false $extra
*/
- public function addMessage($activityKey, $bookId = 0, $extra = false)
+ public function addMessage(string $activityKey, string $message, int $bookId = null)
{
- $this->activity->user_id = $this->user->id;
- $this->activity->book_id = $bookId;
- $this->activity->key = strtolower($activityKey);
- if ($extra !== false) {
- $this->activity->extra = $extra;
- }
- $this->activity->save();
+ $this->newActivityForUser($activityKey, $bookId)->forceFill([
+ 'extra' => $message
+ ])->save();
+
$this->setNotification($activityKey);
}
+ /**
+ * Get a new activity instance for the current user.
+ * @param string $key
+ * @param int|null $bookId
+ * @return Activity
+ */
+ protected function newActivityForUser(string $key, int $bookId = null)
+ {
+ return $this->activity->newInstance()->forceFill([
+ 'key' => strtolower($key),
+ 'user_id' => $this->user->id,
+ 'book_id' => $bookId ?? 0,
+ ]);
+ }
/**
* Removes the entity attachment from each of its activities
{
$activityList = $this->permissionService
->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
- ->orderBy('created_at', 'desc')->with('user', 'entity')->skip($count * $page)->take($count)->get();
+ ->orderBy('created_at', 'desc')
+ ->with('user', 'entity')
+ ->skip($count * $page)
+ ->take($count)
+ ->get();
return $this->filterSimilar($activityList);
}
$notificationTextKey = 'activities.' . $activityKey . '_notification';
if (trans()->has($notificationTextKey)) {
$message = trans($notificationTextKey);
- Session::flash('success', $message);
+ session()->flash('success', $message);
}
}
}
use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\Entity;
use BookStack\Entities\EntityProvider;
+use DB;
use Illuminate\Support\Collection;
class ViewService
/**
* ViewService constructor.
- * @param \BookStack\Actions\View $view
- * @param \BookStack\Auth\Permissions\PermissionService $permissionService
+ * @param View $view
+ * @param PermissionService $permissionService
* @param EntityProvider $entityProvider
*/
public function __construct(View $view, PermissionService $permissionService, EntityProvider $entityProvider)
/**
* Add a view to the given entity.
- * @param Entity $entity
+ * @param \BookStack\Entities\Entity $entity
* @return int
*/
public function add(Entity $entity)
$skipCount = $count * $page;
$query = $this->permissionService
->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action)
- ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
+ ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
->groupBy('viewable_id', 'viewable_type')
->orderBy('view_count', 'desc');
use BookStack\Uploads\Image;
+/**
+ * Class Book
+ * @property string $description
+ * @property int $image_id
+ * @property Image|null $cover
+ * @package BookStack\Entities
+ */
class Book extends Entity
{
public $searchFactor = 2;
* The base class for book-like items such as pages, chapters & books.
* This is not a database model in itself but extended.
*
- * @property integer $id
+ * @property int $id
* @property string $name
* @property string $slug
* @property Carbon $created_at
$view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books'));
$sort = setting()->getUser($this->currentUser, 'books_sort', 'name');
$order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc');
- $sortOptions = [
- 'name' => trans('common.sort_name'),
- 'created_at' => trans('common.sort_created_at'),
- 'updated_at' => trans('common.sort_updated_at'),
- ];
$books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order);
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false;
'view' => $view,
'sort' => $sort,
'order' => $order,
- 'sortOptions' => $sortOptions,
]);
}
* @throws NotFoundException
* @throws ImageUploadException
* @throws ValidationException
+ * @throws Throwable
*/
public function store(Request $request, string $shelfSlug = null)
{
* @throws ImageUploadException
* @throws NotFoundException
* @throws ValidationException
+ * @throws Throwable
*/
public function update(Request $request, string $slug)
{
{
$book = $this->bookRepo->getBySlug($bookSlug);
$this->checkOwnablePermission('book-delete', $book);
- Activity::addMessage('book_delete', 0, $book->name);
+ Activity::addMessage('book_delete', $book->name);
if ($book->cover) {
$this->imageRepo->destroyImage($book->cover);
{
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
$this->checkOwnablePermission('bookshelf-delete', $shelf);
- Activity::addMessage('bookshelf_delete', 0, $shelf->name);
+ Activity::addMessage('bookshelf_delete', $shelf->name);
if ($shelf->cover) {
$this->imageRepo->destroyImage($shelf->cover);
$chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
$book = $chapter->book;
$this->checkOwnablePermission('chapter-delete', $chapter);
- Activity::addMessage('chapter_delete', $book->id, $chapter->name);
+ Activity::addMessage('chapter_delete', $chapter->name, $book->id);
$this->entityRepo->destroyChapter($chapter);
return redirect($book->getUrl());
}
namespace BookStack\Http\Controllers;
use BookStack\Auth\User;
+use BookStack\Entities\Entity;
+use BookStack\Facades\Activity;
use BookStack\Ownable;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
$this->checkOwnablePermission('page-delete', $page);
$this->pageRepo->destroyPage($page);
- Activity::addMessage('page_delete', $book->id, $page->name);
+ Activity::addMessage('page_delete', $page->name, $book->id);
$this->showSuccessNotification( trans('entities.pages_delete_success'));
return redirect($book->getUrl());
}
<h1 class="list-heading">{{ trans('entities.books') }}</h1>
<div class="text-m-right my-m">
- @include('partials.sort', ['options' => $sortOptions, 'order' => $order, 'sort' => $sort, 'type' => 'books'])
+ @include('partials.sort', ['options' => [
+ 'name' => trans('common.sort_name'),
+ 'created_at' => trans('common.sort_created_at'),
+ 'updated_at' => trans('common.sort_updated_at'),
+ ], 'order' => $order, 'sort' => $sort, 'type' => 'books'])
</div>
</div>