Skip to content

Commit

Permalink
Added activity history to to all entities. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Aug 16, 2015
1 parent 41eb2fb commit 5d9d096
Show file tree
Hide file tree
Showing 19 changed files with 672 additions and 250 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Homestead.yaml
/public/js
/public/uploads
/public/bower
/storage/images
/storage/images
_ide_helper.php
38 changes: 38 additions & 0 deletions app/Activity.php
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions app/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ public function children()
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();
}

}
21 changes: 21 additions & 0 deletions app/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,25 @@ public function matches($entity)
{
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();
}

}
5 changes: 5 additions & 0 deletions app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oxbow\Http\Controllers;

use Activity;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function store(Request $request)
$book->created_by = Auth::user()->id;
$book->updated_by = Auth::user()->id;
$book->save();
Activity::add($book, 'book_create', $book->id);
return redirect('/books');
}

Expand Down Expand Up @@ -110,6 +112,7 @@ public function update(Request $request, $slug)
$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());
}

Expand All @@ -132,7 +135,9 @@ public function showDelete($bookSlug)
*/
public function destroy($bookSlug)
{
$bookName = $this->bookRepo->getBySlug($bookSlug)->name;
$this->bookRepo->destroyBySlug($bookSlug);
Activity::addMessage('book_delete', 0, $bookName);
return redirect('/books');
}
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/ChapterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oxbow\Http\Controllers;

use Activity;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function store($bookSlug, Request $request)
$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());
}

Expand Down Expand Up @@ -107,6 +109,7 @@ public function update(Request $request, $bookSlug, $chapterSlug)
$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());
}

Expand Down Expand Up @@ -134,13 +137,15 @@ public function destroy($bookSlug, $chapterSlug)
{
$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;
$page->save();
}
}
$chapter->delete();
Activity::addMessage('chapter_delete', $book->id, $chapterName);
return redirect($book->getUrl());
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +76,7 @@ public function store(Request $request, $bookSlug)
$page->updated_by = Auth::user()->id;
$page->save();
$this->pageRepo->saveRevision($page);
Activity::add($page, 'page_create', $book->id);
return redirect($page->getUrl());
}

Expand Down Expand Up @@ -120,6 +121,7 @@ public function update(Request $request, $bookSlug, $pageSlug)
$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());
}

Expand Down Expand Up @@ -187,6 +189,7 @@ public function savePageSort($bookSlug, Request $request)
}
$model->save();
}
Activity::add($book, 'book_sort', $book->id);
return redirect($book->getUrl());
}

Expand Down Expand Up @@ -215,6 +218,7 @@ public function destroy($bookSlug, $pageSlug)
{
$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());
}
Expand Down Expand Up @@ -254,6 +258,7 @@ public function restoreRevision($bookSlug, $pageSlug, $revisionId)
$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 changes: 31 additions & 0 deletions app/Providers/CustomFacadeProvider.php
Original file line number Diff line number Diff line change
@@ -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 changes: 57 additions & 0 deletions app/Services/ActivityService.php
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions app/Services/Facades/Activity.php
Original file line number Diff line number Diff line change
@@ -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'; }
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"intervention/image": "^2.3"
"intervention/image": "^2.3",
"barryvdh/laravel-ide-helper": "^2.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 5d9d096

Please sign in to comment.