Commit 86e360dd authored by Vipul Basapati's avatar Vipul Basapati

Refactored Blog Module and removed unnecessary files

parent 7456b1f3
...@@ -29,18 +29,18 @@ class BlogsController extends Controller ...@@ -29,18 +29,18 @@ class BlogsController extends Controller
/** /**
* @var BlogsRepository * @var BlogsRepository
*/ */
protected $blogs; protected $blog;
/** /**
* @param BlogsRepository $blogs * @param \App\Repositories\Backend\Blogs\BlogsRepository $blog
*/ */
public function __construct(BlogsRepository $blogs) public function __construct(BlogsRepository $blog)
{ {
$this->blogs = $blogs; $this->blog = $blog;
} }
/** /**
* @param ManageBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
...@@ -52,14 +52,14 @@ class BlogsController extends Controller ...@@ -52,14 +52,14 @@ class BlogsController extends Controller
} }
/** /**
* @param ManageBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
public function create(ManageBlogsRequest $request) public function create(ManageBlogsRequest $request)
{ {
$blogTags = BlogTag::getSelectData();
$blogCategories = BlogCategory::getSelectData(); $blogCategories = BlogCategory::getSelectData();
$blogTags = BlogTag::getSelectData();
return view('backend.blogs.create')->with([ return view('backend.blogs.create')->with([
'blogCategories' => $blogCategories, 'blogCategories' => $blogCategories,
...@@ -69,22 +69,22 @@ class BlogsController extends Controller ...@@ -69,22 +69,22 @@ class BlogsController extends Controller
} }
/** /**
* @param StoreBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\StoreBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
public function store(StoreBlogsRequest $request) public function store(StoreBlogsRequest $request)
{ {
$input = $request->all(); $this->blog->create($request->except('_token'));
$this->blogs->create($input, $tagsArray, $categoriesArray);
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.created')); return redirect()
->route('admin.blogs.index')
->with('flash_success', trans('alerts.backend.blogs.created'));
} }
/** /**
* @param Blog $blog * @param \App\Models\Blogs\Blog $blog
* @param ManageBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
...@@ -107,8 +107,8 @@ class BlogsController extends Controller ...@@ -107,8 +107,8 @@ class BlogsController extends Controller
} }
/** /**
* @param Blog $blog * @param \App\Models\Blogs\Blog $blog
* @param UpdateBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\UpdateBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
...@@ -116,21 +116,25 @@ class BlogsController extends Controller ...@@ -116,21 +116,25 @@ class BlogsController extends Controller
{ {
$input = $request->all(); $input = $request->all();
$this->blogs->update($blog, $input); $this->blog->update($blog, $request->except(['_token', '_method']));
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.updated')); return redirect()
->route('admin.blogs.index')
->with('flash_success', trans('alerts.backend.blogs.updated'));
} }
/** /**
* @param Blog $blog * @param \App\Models\Blogs\Blog $blog
* @param ManageBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
public function destroy(Blog $blog, ManageBlogsRequest $request) public function destroy(Blog $blog, ManageBlogsRequest $request)
{ {
$this->blogs->delete($blog); $this->blog->delete($blog);
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.deleted')); return redirect()
->route('admin.blogs.index')
->with('flash_success', trans('alerts.backend.blogs.deleted'));
} }
} }
...@@ -13,13 +13,10 @@ use Yajra\DataTables\Facades\DataTables; ...@@ -13,13 +13,10 @@ use Yajra\DataTables\Facades\DataTables;
*/ */
class BlogsTableController extends Controller class BlogsTableController extends Controller
{ {
/**
* @var BlogsRepository
*/
protected $blogs; protected $blogs;
/** /**
* @param BlogsRepository $cmspages * @param \App\Repositories\Backend\Blogs\BlogsRepository $cmspages
*/ */
public function __construct(BlogsRepository $blogs) public function __construct(BlogsRepository $blogs)
{ {
...@@ -27,7 +24,7 @@ class BlogsTableController extends Controller ...@@ -27,7 +24,7 @@ class BlogsTableController extends Controller
} }
/** /**
* @param ManageBlogsRequest $request * @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
* *
* @return mixed * @return mixed
*/ */
...@@ -39,13 +36,13 @@ class BlogsTableController extends Controller ...@@ -39,13 +36,13 @@ class BlogsTableController extends Controller
return $blogs->status; return $blogs->status;
}) })
->addColumn('publish_datetime', function ($blogs) { ->addColumn('publish_datetime', function ($blogs) {
return Carbon::parse($blogs->publish_datetime)->format('d/m/Y h:i A'); return $blogs->publish_datetime->format('d/m/Y h:i A');
}) })
->addColumn('created_by', function ($blogs) { ->addColumn('created_by', function ($blogs) {
return $blogs->user_name; return $blogs->user_name;
}) })
->addColumn('created_at', function ($blogs) { ->addColumn('created_at', function ($blogs) {
return Carbon::parse($blogs->created_at)->toDateString(); return $blogs->created_at->toDateString();
}) })
->addColumn('actions', function ($blogs) { ->addColumn('actions', function ($blogs) {
return $blogs->action_buttons; return $blogs->action_buttons;
......
...@@ -17,9 +17,6 @@ use App\Repositories\Backend\Pages\PagesRepository; ...@@ -17,9 +17,6 @@ use App\Repositories\Backend\Pages\PagesRepository;
*/ */
class PagesController extends Controller class PagesController extends Controller
{ {
/**
* @var PagesRepository
*/
protected $pages; protected $pages;
/** /**
......
...@@ -13,9 +13,6 @@ use Yajra\DataTables\Facades\DataTables; ...@@ -13,9 +13,6 @@ use Yajra\DataTables\Facades\DataTables;
*/ */
class PagesTableController extends Controller class PagesTableController extends Controller
{ {
/**
* @var PagesRepository
*/
protected $pages; protected $pages;
/** /**
......
...@@ -17,6 +17,26 @@ class Blog extends BaseModel ...@@ -17,6 +17,26 @@ class Blog extends BaseModel
// BlogAttribute::getEditButtonAttribute insteadof ModelTrait; // BlogAttribute::getEditButtonAttribute insteadof ModelTrait;
} }
protected $fillable = [
'name',
'slug',
'publish_datetime',
'content',
'meta_title',
'cannonical_link',
'meta_keywords',
'meta_description',
'status',
'featured_image',
'created_by'
];
protected $dates = [
'publish_datetime',
'created_at',
'updated_at'
];
/** /**
* The database table used by the model. * The database table used by the model.
* *
...@@ -27,6 +47,6 @@ class Blog extends BaseModel ...@@ -27,6 +47,6 @@ class Blog extends BaseModel
public function __construct(array $attributes = []) public function __construct(array $attributes = [])
{ {
parent::__construct($attributes); parent::__construct($attributes);
$this->table = config('access.blogs_table'); $this->table = config('module.blogs.table');
} }
} }
...@@ -2,17 +2,19 @@ ...@@ -2,17 +2,19 @@
namespace App\Repositories\Backend\Blogs; namespace App\Repositories\Backend\Blogs;
use DB;
use Carbon\Carbon;
use App\Models\Blogs\Blog;
use App\Models\BlogTags\BlogTag;
use App\Http\Utilities\FileUploads;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use App\Models\BlogMapTags\BlogMapTag;
use App\Events\Backend\Blogs\BlogUpdated;
use App\Events\Backend\Blogs\BlogCreated; use App\Events\Backend\Blogs\BlogCreated;
use App\Events\Backend\Blogs\BlogDeleted; use App\Events\Backend\Blogs\BlogDeleted;
use App\Events\Backend\Blogs\BlogUpdated; use App\Models\BlogCategories\BlogCategory;
use App\Exceptions\GeneralException;
use App\Http\Utilities\FileUploads;
use App\Models\BlogMapCategories\BlogMapCategory; use App\Models\BlogMapCategories\BlogMapCategory;
use App\Models\BlogMapTags\BlogMapTag;
use App\Models\Blogs\Blog;
use App\Repositories\BaseRepository;
use Carbon\Carbon;
use DB;
/** /**
* Class BlogsRepository. * Class BlogsRepository.
...@@ -30,14 +32,14 @@ class BlogsRepository extends BaseRepository ...@@ -30,14 +32,14 @@ class BlogsRepository extends BaseRepository
public function getForDataTable() public function getForDataTable()
{ {
return $this->query() return $this->query()
->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('access.blogs_table').'.created_by') ->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('module.blogs.table').'.created_by')
->select([ ->select([
config('access.blogs_table').'.id', config('module.blogs.table').'.id',
config('access.blogs_table').'.name', config('module.blogs.table').'.name',
config('access.blogs_table').'.publish_datetime', config('module.blogs.table').'.publish_datetime',
config('access.blogs_table').'.status', config('module.blogs.table').'.status',
config('access.blogs_table').'.created_by', config('module.blogs.table').'.created_by',
config('access.blogs_table').'.created_at', config('module.blogs.table').'.created_at',
config('access.users_table').'.first_name as user_name', config('access.users_table').'.first_name as user_name',
]); ]);
} }
...@@ -45,46 +47,34 @@ class BlogsRepository extends BaseRepository ...@@ -45,46 +47,34 @@ class BlogsRepository extends BaseRepository
/** /**
* @param array $input * @param array $input
* *
* @throws GeneralException * @throws \App\Exceptions\GeneralException
* *
* @return bool * @return bool
*/ */
public function create(array $input) public function create(array $input)
{ {
$tagsArray = $this->createTagsArray($input['tags']); $tagsArray = $this->createTags($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']); $categoriesArray = $this->createCategories($input['categories']);
unset($input['tags'], $input['categories']);
DB::transaction(function () use ($input, $tagsArray, $categoriesArray) {
$blogs = self::MODEL; DB::transaction(function () use ($input, $tagsArray, $categoriesArray) {
$blogs = new $blogs(); $input['slug'] = str_slug($input['name']);
$blogs->name = $input['name']; $input['publish_datetime'] = Carbon::parse($input['publish_datetime']);
$blogs->slug = str_slug($input['name']); $input = $this->uploadImage($input);
$blogs->content = $input['content']; $input['created_by'] = access()->user()->id;
$blogs->publish_datetime = Carbon::parse($input['publish_datetime']);
if ($blog = Blog::create($input)) {
// Image Upload
$image = $this->uploadImage($input);
$blogs->featured_image = $image['featured_image'];
$blogs->meta_title = $input['meta_title'];
$blogs->cannonical_link = $input['cannonical_link'];
$blogs->meta_keywords = $input['meta_keywords'];
$blogs->meta_description = $input['meta_description'];
$blogs->status = $input['status'];
$blogs->created_by = access()->user()->id;
if ($blogs->save()) {
// Inserting associated category's id in mapper table // Inserting associated category's id in mapper table
if (count($categoriesArray)) { if (count($categoriesArray)) {
$blogs->categories()->sync($categoriesArray); $blog->categories()->sync($categoriesArray);
} }
// Inserting associated tag's id in mapper table // Inserting associated tag's id in mapper table
if (count($tagsArray)) { if (count($tagsArray)) {
$blogs->tags()->sync($tagsArray); $blog->tags()->sync($tagsArray);
} }
event(new BlogCreated($blogs)); event(new BlogCreated($blog));
return true; return true;
} }
...@@ -96,46 +86,39 @@ class BlogsRepository extends BaseRepository ...@@ -96,46 +86,39 @@ class BlogsRepository extends BaseRepository
/** /**
* Update Blog. * Update Blog.
* *
* @param $blogs * @param \App\Models\Blogs\Blog $blog
* @param array $input * @param array $input
*/ */
public function update($blogs, array $input) public function update(Blog $blog, array $input)
{ {
$tagsArray = $this->createTagsArray($input['tags']); $tagsArray = $this->createTags($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']); $categoriesArray = $this->createCategories($input['categories']);
unset($input['tags'], $input['categories']);
$blogs->name = $input['name'];
$blogs->slug = str_slug($input['name']); $input['slug'] = str_slug($input['name']);
$blogs->content = $input['content']; $input['publish_datetime'] = Carbon::parse($input['publish_datetime']);
$blogs->publish_datetime = Carbon::parse($input['publish_datetime']); $input['updated_by'] = access()->user()->id;
$blogs->meta_title = $input['meta_title'];
$blogs->cannonical_link = $input['cannonical_link'];
$blogs->meta_keywords = $input['meta_keywords'];
$blogs->meta_description = $input['meta_description'];
$blogs->status = $input['status'];
$blogs->updated_by = access()->user()->id;
// Uploading Image // Uploading Image
if (array_key_exists('featured_image', $input)) { if (array_key_exists('featured_image', $input)) {
$this->deleteOldFile($blogs); $this->deleteOldFile($blog);
$input = $this->uploadImage($input); $input = $this->uploadImage($input);
$blogs->featured_image = $input['featured_image'];
} }
DB::transaction(function () use ($blogs, $input, $tagsArray, $categoriesArray) { DB::transaction(function () use ($blog, $input, $tagsArray, $categoriesArray) {
if ($blogs->save()) { if ($blog->update($input)) {
// Updateing associated category's id in mapper table // Updateing associated category's id in mapper table
if (count($categoriesArray)) { if (count($categoriesArray)) {
$blogs->categories()->sync($categoriesArray); $blog->categories()->sync($categoriesArray);
} }
// Updating associated tag's id in mapper table // Updating associated tag's id in mapper table
if (count($tagsArray)) { if (count($tagsArray)) {
$blogs->tags()->sync($tagsArray); $blog->tags()->sync($tagsArray);
} }
event(new BlogUpdated($blogs)); event(new BlogUpdated($blog));
return true; return true;
} }
...@@ -147,13 +130,13 @@ class BlogsRepository extends BaseRepository ...@@ -147,13 +130,13 @@ class BlogsRepository extends BaseRepository
} }
/** /**
* Creating Tags Array. * Creating Tags.
* *
* @param Array($tags) * @param Array($tags)
* *
* @return array * @return array
*/ */
public function createTagsArray($tags) public function createTags($tags)
{ {
//Creating a new array for tags (newly created) //Creating a new array for tags (newly created)
$tags_array = []; $tags_array = [];
...@@ -171,13 +154,13 @@ class BlogsRepository extends BaseRepository ...@@ -171,13 +154,13 @@ class BlogsRepository extends BaseRepository
} }
/** /**
* Creating Tags Array. * Creating Categories.
* *
* @param Array($tags) * @param Array($categories)
* *
* @return array * @return array
*/ */
public function createCategoriesArray($categories) public function createCategories($categories)
{ {
//Creating a new array for categories (newly created) //Creating a new array for categories (newly created)
$categories_array = []; $categories_array = [];
...@@ -196,13 +179,13 @@ class BlogsRepository extends BaseRepository ...@@ -196,13 +179,13 @@ class BlogsRepository extends BaseRepository
} }
/** /**
* @param Model $blog * @param \App\Models\Blogs\Blog $blog
* *
* @throws GeneralException * @throws GeneralException
* *
* @return bool * @return bool
*/ */
public function delete(Model $blog) public function delete(Blog $blog)
{ {
DB::transaction(function () use ($blog) { DB::transaction(function () use ($blog) {
if ($blog->delete()) { if ($blog->delete()) {
......
...@@ -14,5 +14,8 @@ return [ ...@@ -14,5 +14,8 @@ return [
], ],
'blog_categories' => [ 'blog_categories' => [
'table' => 'blog_categories' 'table' => 'blog_categories'
],
'blogs' => [
'table' => 'blogs'
] ]
]; ];
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.create') }}</h3> <h3 class="box-title">{{ trans('labels.backend.blogs.create') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.blogs-header-buttons') @include('backend.blogs.partials.blogs-header-buttons')
</div><!--box-tools pull-right--> </div><!--box-tools pull-right-->
</div><!-- /.box-header --> </div><!-- /.box-header -->
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.edit') }}</h3> <h3 class="box-title">{{ trans('labels.backend.blogs.edit') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.blogs-header-buttons') @include('backend.blogs.partials.blogs-header-buttons')
</div><!--box-tools pull-right--> </div><!--box-tools pull-right-->
</div><!-- /.box-header --> </div><!-- /.box-header -->
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.management') }}</h3> <h3 class="box-title">{{ trans('labels.backend.blogs.management') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.blogs-header-buttons') @include('backend.blogs.partials.blogs-header-buttons')
</div> </div>
</div><!-- /.box-header --> </div><!-- /.box-header -->
...@@ -76,11 +76,11 @@ ...@@ -76,11 +76,11 @@
type: 'post' type: 'post'
}, },
columns: [ columns: [
{data: 'name', name: '{{config('access.blogs_table')}}.name'}, {data: 'name', name: '{{config('module.blogs.table')}}.name'},
{data: 'publish_datetime', name: '{{config('access.blogs_table')}}.publish_datetime'}, {data: 'publish_datetime', name: '{{config('module.blogs.table')}}.publish_datetime'},
{data: 'status', name: '{{config('access.blogs_table')}}.status'}, {data: 'status', name: '{{config('module.blogs.table')}}.status'},
{data: 'created_by', name: '{{config('access.blogs_table')}}.created_by'}, {data: 'created_by', name: '{{config('module.blogs.table')}}.created_by'},
{data: 'created_at', name: '{{config('access.blogs_table')}}.created_at'}, {data: 'created_at', name: '{{config('module.blogs.table')}}.created_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false} {data: 'actions', name: 'actions', searchable: false, sortable: false}
], ],
order: [[3, "asc"]], order: [[3, "asc"]],
......
<!--Action Button-->
@if(Active::checkUriPattern('admin/blogcategories'))
<div class="btn-group">
<button type="button" class="btn btn-warning btn-flat dropdown-toggle" data-toggle="dropdown">Export
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li id="copyButton"><a href="#"><i class="fa fa-clone"></i> Copy</a></li>
<li id="csvButton"><a href="#"><i class="fa fa-file-text-o"></i> CSV</a></li>
<li id="excelButton"><a href="#"><i class="fa fa-file-excel-o"></i> Excel</a></li>
<li id="pdfButton"><a href="#"><i class="fa fa-file-pdf-o"></i> PDF</a></li>
<li id="printButton"><a href="#"><i class="fa fa-print"></i> Print</a></li>
</ul>
</div>
@endif
<!--Action Button-->
<div class="btn-group">
<button type="button" class="btn btn-primary btn-flat dropdown-toggle" data-toggle="dropdown">Action
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="{{route('admin.blogcategories.index')}}"><i class="fa fa-list-ul"></i> {{trans('menus.backend.blogcategories.all')}}</a></li>
@permission('create-blog-category')
<li><a href="{{route('admin.blogcategories.create')}}"><i class="fa fa-plus"></i> {{trans('menus.backend.blogcategories.create')}}</a></li>
@endauth
</ul>
</div>
\ No newline at end of file
<!--Action Button-->
@if(Active::checkUriPattern('admin/modules'))
<div class="btn-group">
<button type="button" class="btn btn-warning btn-flat dropdown-toggle" data-toggle="dropdown">Export
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li id="copyButton"><a href="#"><i class="fa fa-clone"></i> Copy</a></li>
<li id="csvButton"><a href="#"><i class="fa fa-file-text-o"></i> CSV</a></li>
<li id="excelButton"><a href="#"><i class="fa fa-file-excel-o"></i> Excel</a></li>
<li id="pdfButton"><a href="#"><i class="fa fa-file-pdf-o"></i> PDF</a></li>
<li id="printButton"><a href="#"><i class="fa fa-print"></i> Print</a></li>
</ul>
</div>
@endif
<!--Action Button-->
<div class="btn-group">
<button type="button" class="btn btn-primary btn-flat dropdown-toggle" data-toggle="dropdown">Action
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="{{route('admin.modules.index')}}"><i class="fa fa-list-ul"></i> {{trans('menus.backend.modules.all')}}</a></li>
{{-- Will fill the permission later --}}
{{-- @permission('create-faq') --}}
<li><a href="{{route('admin.modules.create')}}"><i class="fa fa-plus"></i> {{trans('menus.backend.modules.create')}}</a></li>
{{-- @endauth --}}
</ul>
</div>
<div class="clearfix"></div>
\ No newline at end of file
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.pages.create') }}</h3> <h3 class="box-title">{{ trans('labels.backend.pages.create') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.pages-header-buttons') @include('backend.pages.partials.pages-header-buttons')
</div><!--box-tools pull-right--> </div><!--box-tools pull-right-->
</div><!-- /.box-header --> </div><!-- /.box-header -->
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.pages.edit') }}</h3> <h3 class="box-title">{{ trans('labels.backend.pages.edit') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.pages-header-buttons') @include('backend.pages.partials.pages-header-buttons')
</div><!--box-tools pull-right--> </div><!--box-tools pull-right-->
</div><!-- /.box-header --> </div><!-- /.box-header -->
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<h3 class="box-title">{{ trans('labels.backend.pages.management') }}</h3> <h3 class="box-title">{{ trans('labels.backend.pages.management') }}</h3>
<div class="box-tools pull-right"> <div class="box-tools pull-right">
@include('backend.includes.partials.pages-header-buttons') @include('backend.pages.partials.pages-header-buttons')
</div> </div>
</div><!-- /.box-header --> </div><!-- /.box-header -->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment