Skip to content

Commit

Permalink
Improve the Content module
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Jan 2, 2020
1 parent 58734ca commit c187486
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
36 changes: 0 additions & 36 deletions modules/Content/Controllers/Admin/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,42 +455,6 @@ public function destroy($id)
->with('success', __d('content', 'The {0} <b>#{1}</b> was successfully deleted.', $postType->label('name'), $post->id));
}

public function restore($id)
{
try {
$revision = Post::where('type', 'revision')->findOrFail($id);
}
catch (ModelNotFoundException $e) {
return Redirect::back()->with('danger', __d('content', 'Record not found: #{0}', $id));
}

$post = $revision->parent()->first();

// Restore the Post's title, content and excerpt.
$post->content = $revision->content;
$post->excerpt = $revision->excerpt;
$post->title = $revision->title;

$post->save();

// Handle the MetaData.
if (preg_match('#^(?:\d+)-revision-v(\d+)$#', $revision->name, $matches) !== 1) {
$version = 0;
} else {
$post->saveMeta('version', $version = (int) $matches[1]);
}

// Invalidate the content caches.
Cache::section('content')->flush();

//
$postType = PostType::make($post->type);

$status = __d('content', 'The {0} <b>#{1}</b> was successfully restored to the revision: <b>{2}</b>', $postType->label('name'), $post->id, $version);

return Redirect::back()->with('success', $status);
}

protected function generateCategories(array $categories = array(), $taxonomies = null, $level = 0)
{
$result = '';
Expand Down
39 changes: 38 additions & 1 deletion modules/Content/Controllers/Admin/Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Modules\Content\Controllers\Admin;

use Nova\Database\ORM\ModelNotFoundException;
use Nova\Support\Facades\Cache;
use Nova\Support\Facades\Redirect;

use Modules\Content\Models\Post;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function index($id)
//
$name = $postType->label('name');

return $this->createView(compact('type', 'name', 'post', 'revisions'))
return $this->createView(compact('name', 'post', 'revisions', 'postType'))
->shares('title', __d('content', 'Revisions of the {0} : {1}', $name, $post->title));
}

Expand Down Expand Up @@ -61,4 +62,40 @@ protected function destroy($id)

return Redirect::back()->with('success', $status);
}

public function restore($id)
{
try {
$revision = Post::where('type', 'revision')->findOrFail($id);
}
catch (ModelNotFoundException $e) {
return Redirect::back()->with('danger', __d('content', 'Record not found: #{0}', $id));
}

$post = $revision->parent()->first();

// Restore the Post's title, content and excerpt.
$post->content = $revision->content;
$post->excerpt = $revision->excerpt;
$post->title = $revision->title;

$post->save();

// Handle the MetaData.
if (preg_match('#^(?:\d+)-revision-v(\d+)$#', $revision->name, $matches) !== 1) {
$version = 0;
} else {
$post->saveMeta('version', $version = (int) $matches[1]);
}

// Invalidate the content caches.
Cache::section('content')->flush();

//
$postType = PostType::make($post->type);

$status = __d('content', 'The {0} <b>#{1}</b> was successfully restored to the revision: <b>{2}</b>', $postType->label('name'), $post->id, $version);

return Redirect::back()->with('success', $status);
}
}
2 changes: 1 addition & 1 deletion modules/Content/Routes/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@
Route::get( 'content/{id}/edit', 'Posts@edit');
Route::post('content/{id}', 'Posts@update');
Route::post('content/{id}/destroy', 'Posts@destroy');
Route::post('content/{id}/restore', 'Posts@restore');

// The Post Revisions CRUD.
Route::get( 'content/{id}/revisions', 'Revisions@index');
Route::post('content/{id}/revisions', 'Revisions@destroy');
Route::post('content/{id}/restore', 'Revisions@restore');

// The Post editor's Tags management
Route::group(array('namespace' => 'Editor'), function ()
Expand Down
2 changes: 1 addition & 1 deletion modules/Content/Views/Admin/Revisions/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
$('#modal-delete-revision-dialog').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal

$('#modal-delete-revision-form').attr('action', '<?= site_url("admin/content"); ?>/' + button.data('id') + '/destroy');
$('#modal-delete-revision-form').attr('action', '<?= site_url("admin/content"); ?>/' + button.data('id') + '/revisions');
});
});

Expand Down

0 comments on commit c187486

Please sign in to comment.