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
/**
* @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
*/
......@@ -52,14 +52,14 @@ class BlogsController extends Controller
}
/**
* @param ManageBlogsRequest $request
* @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
*
* @return mixed
*/
public function create(ManageBlogsRequest $request)
{
$blogTags = BlogTag::getSelectData();
$blogCategories = BlogCategory::getSelectData();
$blogTags = BlogTag::getSelectData();
return view('backend.blogs.create')->with([
'blogCategories' => $blogCategories,
......@@ -69,22 +69,22 @@ class BlogsController extends Controller
}
/**
* @param StoreBlogsRequest $request
* @param \App\Http\Requests\Backend\Blogs\StoreBlogsRequest $request
*
* @return mixed
*/
public function store(StoreBlogsRequest $request)
{
$input = $request->all();
$this->blogs->create($input, $tagsArray, $categoriesArray);
$this->blog->create($request->except('_token'));
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 ManageBlogsRequest $request
* @param \App\Models\Blogs\Blog $blog
* @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
*
* @return mixed
*/
......@@ -107,8 +107,8 @@ class BlogsController extends Controller
}
/**
* @param Blog $blog
* @param UpdateBlogsRequest $request
* @param \App\Models\Blogs\Blog $blog
* @param \App\Http\Requests\Backend\Blogs\UpdateBlogsRequest $request
*
* @return mixed
*/
......@@ -116,21 +116,25 @@ class BlogsController extends Controller
{
$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 ManageBlogsRequest $request
* @param \App\Models\Blogs\Blog $blog
* @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
*
* @return mixed
*/
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;
*/
class BlogsTableController extends Controller
{
/**
* @var BlogsRepository
*/
protected $blogs;
/**
* @param BlogsRepository $cmspages
* @param \App\Repositories\Backend\Blogs\BlogsRepository $cmspages
*/
public function __construct(BlogsRepository $blogs)
{
......@@ -27,7 +24,7 @@ class BlogsTableController extends Controller
}
/**
* @param ManageBlogsRequest $request
* @param \App\Http\Requests\Backend\Blogs\ManageBlogsRequest $request
*
* @return mixed
*/
......@@ -39,13 +36,13 @@ class BlogsTableController extends Controller
return $blogs->status;
})
->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) {
return $blogs->user_name;
})
->addColumn('created_at', function ($blogs) {
return Carbon::parse($blogs->created_at)->toDateString();
return $blogs->created_at->toDateString();
})
->addColumn('actions', function ($blogs) {
return $blogs->action_buttons;
......
......@@ -17,9 +17,6 @@ use App\Repositories\Backend\Pages\PagesRepository;
*/
class PagesController extends Controller
{
/**
* @var PagesRepository
*/
protected $pages;
/**
......
......@@ -13,9 +13,6 @@ use Yajra\DataTables\Facades\DataTables;
*/
class PagesTableController extends Controller
{
/**
* @var PagesRepository
*/
protected $pages;
/**
......
......@@ -17,6 +17,26 @@ class Blog extends BaseModel
// 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.
*
......@@ -27,6 +47,6 @@ class Blog extends BaseModel
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('access.blogs_table');
$this->table = config('module.blogs.table');
}
}
......@@ -2,17 +2,19 @@
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\BlogDeleted;
use App\Events\Backend\Blogs\BlogUpdated;
use App\Exceptions\GeneralException;
use App\Http\Utilities\FileUploads;
use App\Models\BlogCategories\BlogCategory;
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.
......@@ -30,14 +32,14 @@ class BlogsRepository extends BaseRepository
public function getForDataTable()
{
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([
config('access.blogs_table').'.id',
config('access.blogs_table').'.name',
config('access.blogs_table').'.publish_datetime',
config('access.blogs_table').'.status',
config('access.blogs_table').'.created_by',
config('access.blogs_table').'.created_at',
config('module.blogs.table').'.id',
config('module.blogs.table').'.name',
config('module.blogs.table').'.publish_datetime',
config('module.blogs.table').'.status',
config('module.blogs.table').'.created_by',
config('module.blogs.table').'.created_at',
config('access.users_table').'.first_name as user_name',
]);
}
......@@ -45,46 +47,34 @@ class BlogsRepository extends BaseRepository
/**
* @param array $input
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* @return bool
*/
public function create(array $input)
{
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
DB::transaction(function () use ($input, $tagsArray, $categoriesArray) {
$blogs = self::MODEL;
$blogs = new $blogs();
$blogs->name = $input['name'];
$blogs->slug = str_slug($input['name']);
$blogs->content = $input['content'];
$blogs->publish_datetime = Carbon::parse($input['publish_datetime']);
// 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()) {
$tagsArray = $this->createTags($input['tags']);
$categoriesArray = $this->createCategories($input['categories']);
unset($input['tags'], $input['categories']);
DB::transaction(function () use ($input, $tagsArray, $categoriesArray) {
$input['slug'] = str_slug($input['name']);
$input['publish_datetime'] = Carbon::parse($input['publish_datetime']);
$input = $this->uploadImage($input);
$input['created_by'] = access()->user()->id;
if ($blog = Blog::create($input)) {
// Inserting associated category's id in mapper table
if (count($categoriesArray)) {
$blogs->categories()->sync($categoriesArray);
$blog->categories()->sync($categoriesArray);
}
// Inserting associated tag's id in mapper table
if (count($tagsArray)) {
$blogs->tags()->sync($tagsArray);
$blog->tags()->sync($tagsArray);
}
event(new BlogCreated($blogs));
event(new BlogCreated($blog));
return true;
}
......@@ -96,46 +86,39 @@ class BlogsRepository extends BaseRepository
/**
* Update Blog.
*
* @param $blogs
* @param \App\Models\Blogs\Blog $blog
* @param array $input
*/
public function update($blogs, array $input)
public function update(Blog $blog, array $input)
{
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
$blogs->name = $input['name'];
$blogs->slug = str_slug($input['name']);
$blogs->content = $input['content'];
$blogs->publish_datetime = Carbon::parse($input['publish_datetime']);
$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;
$tagsArray = $this->createTags($input['tags']);
$categoriesArray = $this->createCategories($input['categories']);
unset($input['tags'], $input['categories']);
$input['slug'] = str_slug($input['name']);
$input['publish_datetime'] = Carbon::parse($input['publish_datetime']);
$input['updated_by'] = access()->user()->id;
// Uploading Image
if (array_key_exists('featured_image', $input)) {
$this->deleteOldFile($blogs);
$this->deleteOldFile($blog);
$input = $this->uploadImage($input);
$blogs->featured_image = $input['featured_image'];
}
DB::transaction(function () use ($blogs, $input, $tagsArray, $categoriesArray) {
if ($blogs->save()) {
DB::transaction(function () use ($blog, $input, $tagsArray, $categoriesArray) {
if ($blog->update($input)) {
// Updateing associated category's id in mapper table
if (count($categoriesArray)) {
$blogs->categories()->sync($categoriesArray);
$blog->categories()->sync($categoriesArray);
}
// Updating associated tag's id in mapper table
if (count($tagsArray)) {
$blogs->tags()->sync($tagsArray);
$blog->tags()->sync($tagsArray);
}
event(new BlogUpdated($blogs));
event(new BlogUpdated($blog));
return true;
}
......@@ -147,13 +130,13 @@ class BlogsRepository extends BaseRepository
}
/**
* Creating Tags Array.
* Creating Tags.
*
* @param Array($tags)
*
* @return array
*/
public function createTagsArray($tags)
public function createTags($tags)
{
//Creating a new array for tags (newly created)
$tags_array = [];
......@@ -171,13 +154,13 @@ class BlogsRepository extends BaseRepository
}
/**
* Creating Tags Array.
* Creating Categories.
*
* @param Array($tags)
* @param Array($categories)
*
* @return array
*/
public function createCategoriesArray($categories)
public function createCategories($categories)
{
//Creating a new array for categories (newly created)
$categories_array = [];
......@@ -196,13 +179,13 @@ class BlogsRepository extends BaseRepository
}
/**
* @param Model $blog
* @param \App\Models\Blogs\Blog $blog
*
* @throws GeneralException
*
* @return bool
*/
public function delete(Model $blog)
public function delete(Blog $blog)
{
DB::transaction(function () use ($blog) {
if ($blog->delete()) {
......
......@@ -14,5 +14,8 @@ return [
],
'blog_categories' => [
'table' => 'blog_categories'
],
'blogs' => [
'table' => 'blogs'
]
];
......@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.create') }}</h3>
<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-header -->
......
......@@ -18,7 +18,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.edit') }}</h3>
<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-header -->
......
......@@ -12,7 +12,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogs.management') }}</h3>
<div class="box-tools pull-right">
@include('backend.includes.partials.blogs-header-buttons')
@include('backend.blogs.partials.blogs-header-buttons')
</div>
</div><!-- /.box-header -->
......@@ -76,11 +76,11 @@
type: 'post'
},
columns: [
{data: 'name', name: '{{config('access.blogs_table')}}.name'},
{data: 'publish_datetime', name: '{{config('access.blogs_table')}}.publish_datetime'},
{data: 'status', name: '{{config('access.blogs_table')}}.status'},
{data: 'created_by', name: '{{config('access.blogs_table')}}.created_by'},
{data: 'created_at', name: '{{config('access.blogs_table')}}.created_at'},
{data: 'name', name: '{{config('module.blogs.table')}}.name'},
{data: 'publish_datetime', name: '{{config('module.blogs.table')}}.publish_datetime'},
{data: 'status', name: '{{config('module.blogs.table')}}.status'},
{data: 'created_by', name: '{{config('module.blogs.table')}}.created_by'},
{data: 'created_at', name: '{{config('module.blogs.table')}}.created_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
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 @@
<h3 class="box-title">{{ trans('labels.backend.pages.create') }}</h3>
<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-header -->
......
......@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.pages.edit') }}</h3>
<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-header -->
......
......@@ -12,7 +12,7 @@
<h3 class="box-title">{{ trans('labels.backend.pages.management') }}</h3>
<div class="box-tools pull-right">
@include('backend.includes.partials.pages-header-buttons')
@include('backend.pages.partials.pages-header-buttons')
</div>
</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