mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Added activity history to to all entities. Fixes #12
This commit is contained in:
38
app/Activity.php
Normal file
38
app/Activity.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Oxbow;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property string key
|
||||
* @property \User user
|
||||
* @property \Entity entity
|
||||
* @property string extra
|
||||
*/
|
||||
class Activity extends Model
|
||||
{
|
||||
public function entity()
|
||||
{
|
||||
if($this->entity_id) {
|
||||
return $this->morphTo('entity')->first();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Oxbow\User');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns text from the language files, Looks up by using the
|
||||
* activity key.
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return trans('activities.' . $this->key);
|
||||
}
|
||||
|
||||
}
|
11
app/Book.php
11
app/Book.php
@ -37,4 +37,15 @@ class Book extends Entity
|
||||
return $pages->sortBy('priority');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets only the most recent activity for this book
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function recentActivity($limit = 20, $page=0)
|
||||
{
|
||||
return $this->hasMany('Oxbow\Activity')->orderBy('created_at', 'desc')->skip($limit*$page)->take($limit)->get();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,4 +34,25 @@ class Entity extends Model
|
||||
{
|
||||
return [get_class($this), $this->id] === [get_class($entity), $entity->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the activity for this entity.
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
||||
*/
|
||||
public function activity()
|
||||
{
|
||||
return $this->morphMany('Oxbow\Activity', 'entity')->orderBy('created_at', 'desc');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets only the most recent activity
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @return mixed
|
||||
*/
|
||||
public function recentActivity($limit = 20, $page=0)
|
||||
{
|
||||
return $this->activity()->skip($limit*$page)->take($limit)->get();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Oxbow\Http\Controllers;
|
||||
|
||||
use Activity;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@ -65,6 +66,7 @@ class BookController extends Controller
|
||||
$book->created_by = Auth::user()->id;
|
||||
$book->updated_by = Auth::user()->id;
|
||||
$book->save();
|
||||
Activity::add($book, 'book_create', $book->id);
|
||||
return redirect('/books');
|
||||
}
|
||||
|
||||
@ -110,6 +112,7 @@ class BookController extends Controller
|
||||
$book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
|
||||
$book->updated_by = Auth::user()->id;
|
||||
$book->save();
|
||||
Activity::add($book, 'book_update', $book->id);
|
||||
return redirect($book->getUrl());
|
||||
}
|
||||
|
||||
@ -132,7 +135,9 @@ class BookController extends Controller
|
||||
*/
|
||||
public function destroy($bookSlug)
|
||||
{
|
||||
$bookName = $this->bookRepo->getBySlug($bookSlug)->name;
|
||||
$this->bookRepo->destroyBySlug($bookSlug);
|
||||
Activity::addMessage('book_delete', 0, $bookName);
|
||||
return redirect('/books');
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Oxbow\Http\Controllers;
|
||||
|
||||
use Activity;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@ -60,6 +61,7 @@ class ChapterController extends Controller
|
||||
$chapter->created_by = Auth::user()->id;
|
||||
$chapter->updated_by = Auth::user()->id;
|
||||
$book->chapters()->save($chapter);
|
||||
Activity::add($chapter, 'chapter_create', $book->id);
|
||||
return redirect($book->getUrl());
|
||||
}
|
||||
|
||||
@ -107,6 +109,7 @@ class ChapterController extends Controller
|
||||
$chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
|
||||
$chapter->updated_by = Auth::user()->id;
|
||||
$chapter->save();
|
||||
Activity::add($chapter, 'chapter_update', $book->id);
|
||||
return redirect($chapter->getUrl());
|
||||
}
|
||||
|
||||
@ -134,6 +137,7 @@ class ChapterController extends Controller
|
||||
{
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
|
||||
$chapterName = $chapter->name;
|
||||
if(count($chapter->pages) > 0) {
|
||||
foreach($chapter->pages as $page) {
|
||||
$page->chapter_id = 0;
|
||||
@ -141,6 +145,7 @@ class ChapterController extends Controller
|
||||
}
|
||||
}
|
||||
$chapter->delete();
|
||||
Activity::addMessage('chapter_delete', $book->id, $chapterName);
|
||||
return redirect($book->getUrl());
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
namespace Oxbow\Http\Controllers;
|
||||
|
||||
use Activity;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Oxbow\Http\Requests;
|
||||
use Oxbow\Repos\BookRepo;
|
||||
use Oxbow\Repos\ChapterRepo;
|
||||
@ -76,6 +76,7 @@ class PageController extends Controller
|
||||
$page->updated_by = Auth::user()->id;
|
||||
$page->save();
|
||||
$this->pageRepo->saveRevision($page);
|
||||
Activity::add($page, 'page_create', $book->id);
|
||||
return redirect($page->getUrl());
|
||||
}
|
||||
|
||||
@ -120,6 +121,7 @@ class PageController extends Controller
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
||||
$this->pageRepo->updatePage($page, $book->id, $request->all());
|
||||
Activity::add($page, 'page_update', $book->id);
|
||||
return redirect($page->getUrl());
|
||||
}
|
||||
|
||||
@ -187,6 +189,7 @@ class PageController extends Controller
|
||||
}
|
||||
$model->save();
|
||||
}
|
||||
Activity::add($book, 'book_sort', $book->id);
|
||||
return redirect($book->getUrl());
|
||||
}
|
||||
|
||||
@ -215,6 +218,7 @@ class PageController extends Controller
|
||||
{
|
||||
$book = $this->bookRepo->getBySlug($bookSlug);
|
||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
||||
Activity::addMessage('page_delete', $book->id, $page->name);
|
||||
$page->delete();
|
||||
return redirect($book->getUrl());
|
||||
}
|
||||
@ -254,6 +258,7 @@ class PageController extends Controller
|
||||
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
|
||||
$revision = $this->pageRepo->getRevisionById($revisionId);
|
||||
$page = $this->pageRepo->updatePage($page, $book->id, $revision->toArray());
|
||||
Activity::add($page, 'page_restore', $book->id);
|
||||
return redirect($page->getUrl());
|
||||
}
|
||||
}
|
||||
|
31
app/Providers/CustomFacadeProvider.php
Normal file
31
app/Providers/CustomFacadeProvider.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Oxbow\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Oxbow\Services\ActivityService;
|
||||
|
||||
class CustomFacadeProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('activity', function() {
|
||||
return new ActivityService($this->app->make('Oxbow\Activity'));
|
||||
});
|
||||
}
|
||||
}
|
57
app/Services/ActivityService.php
Normal file
57
app/Services/ActivityService.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php namespace Oxbow\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Oxbow\Activity;
|
||||
use Oxbow\Entity;
|
||||
|
||||
class ActivityService
|
||||
{
|
||||
protected $activity;
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* ActivityService constructor.
|
||||
* @param $activity
|
||||
*/
|
||||
public function __construct(Activity $activity)
|
||||
{
|
||||
$this->activity = $activity;
|
||||
$this->user = Auth::user();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add activity data to database.
|
||||
* @para Entity $entity
|
||||
* @param $activityKey
|
||||
* @param int $bookId
|
||||
*/
|
||||
public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
|
||||
{
|
||||
$this->activity->user_id = $this->user->id;
|
||||
$this->activity->book_id = $bookId;
|
||||
$this->activity->key = strtolower($activityKey);
|
||||
if($extra !== false) {
|
||||
$this->activity->extra = $extra;
|
||||
}
|
||||
$entity->activity()->save($this->activity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a activity history with a message & without binding to a entitiy.
|
||||
* @param $activityKey
|
||||
* @param int $bookId
|
||||
* @param bool|false $extra
|
||||
*/
|
||||
public function addMessage($activityKey, $bookId = 0, $extra = false)
|
||||
{
|
||||
$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();
|
||||
}
|
||||
|
||||
}
|
14
app/Services/Facades/Activity.php
Normal file
14
app/Services/Facades/Activity.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php namespace Oxbow\Services\Facades;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Activity extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor() { return 'activity'; }
|
||||
}
|
Reference in New Issue
Block a user