Fixed entity messages on delete. Fixes #21.

This commit is contained in:
Dan Brown
2015-08-23 14:20:34 +01:00
parent 40b629d35d
commit 0b222c7734
6 changed files with 27 additions and 11 deletions

View File

@ -135,9 +135,9 @@ class BookController extends Controller
*/ */
public function destroy($bookSlug) public function destroy($bookSlug)
{ {
$bookName = $this->bookRepo->getBySlug($bookSlug)->name; $book = $this->bookRepo->getBySlug($bookSlug);
Activity::addMessage('book_delete', 0, $book->name);
$this->bookRepo->destroyBySlug($bookSlug); $this->bookRepo->destroyBySlug($bookSlug);
Activity::addMessage('book_delete', 0, $bookName);
return redirect('/books'); return redirect('/books');
} }
} }

View File

@ -137,15 +137,15 @@ class ChapterController extends Controller
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
$chapterName = $chapter->name;
if(count($chapter->pages) > 0) { if(count($chapter->pages) > 0) {
foreach($chapter->pages as $page) { foreach($chapter->pages as $page) {
$page->chapter_id = 0; $page->chapter_id = 0;
$page->save(); $page->save();
} }
} }
Activity::removeEntity($chapter);
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
$chapter->delete(); $chapter->delete();
Activity::addMessage('chapter_delete', $book->id, $chapterName);
return redirect($book->getUrl()); return redirect($book->getUrl());
} }
} }

View File

@ -4,15 +4,11 @@ namespace Oxbow\Http\Controllers;
use Illuminate\Filesystem\Filesystem as File; use Illuminate\Filesystem\Filesystem as File;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image as ImageTool; use Intervention\Image\Facades\Image as ImageTool;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Oxbow\Http\Requests; use Oxbow\Http\Requests;
use Oxbow\Image; use Oxbow\Image;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
class ImageController extends Controller class ImageController extends Controller
{ {

View File

@ -219,6 +219,7 @@ class PageController extends Controller
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id); $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
Activity::addMessage('page_delete', $book->id, $page->name); Activity::addMessage('page_delete', $book->id, $page->name);
Activity::removeEntity($page);
$page->delete(); $page->delete();
return redirect($book->getUrl()); return redirect($book->getUrl());
} }

View File

@ -70,7 +70,6 @@ Route::group(['middleware' => 'auth'], function() {
Route::get('/images/all', 'ImageController@getAll'); Route::get('/images/all', 'ImageController@getAll');
Route::put('/images/update/{imageId}', 'ImageController@update'); Route::put('/images/update/{imageId}', 'ImageController@update');
Route::delete('/images/{imageId}', 'ImageController@destroy'); Route::delete('/images/{imageId}', 'ImageController@destroy');
Route::get('/images/{imageId}/delete', 'ImageController@destroy');
Route::get('/images/all/{page}', 'ImageController@getAll'); Route::get('/images/all/{page}', 'ImageController@getAll');
Route::get('/images/{any}', 'ImageController@getImage')->where('any', '.*'); Route::get('/images/{any}', 'ImageController@getImage')->where('any', '.*');

View File

@ -21,9 +21,10 @@ class ActivityService
/** /**
* Add activity data to database. * Add activity data to database.
* @para Entity $entity * @param Entity $entity
* @param $activityKey * @param $activityKey
* @param int $bookId * @param int $bookId
* @param bool $extra
*/ */
public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false) public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
{ {
@ -53,6 +54,25 @@ class ActivityService
$this->activity->save(); $this->activity->save();
} }
/**
* Removes the entity attachment from each of its activities
* and instead uses the 'extra' field with the entities name.
* Used when an entity is deleted.
* @param Entity $entity
* @return mixed
*/
public function removeEntity(Entity $entity)
{
$activities = $entity->activity;
foreach($activities as $activity) {
$activity->extra = $entity->name;
$activity->entity_id = 0;
$activity->entity_type = null;
$activity->save();
}
return $activities;
}
/** /**
* Gets the latest activity. * Gets the latest activity.
* @param int $count * @param int $count