Commit afa63fb5 authored by Viral Solani's avatar Viral Solani

Refactor Blog Module

parent eb880c0d
......@@ -16,6 +16,16 @@ use App\Repositories\Backend\Blogs\BlogsRepository;
*/
class BlogsController extends Controller
{
/**
* Blog Status
*/
protected $status = [
'Published' => 'Published',
'Draft' => 'Draft',
'Inactive' => 'Inactive',
'Scheduled' => 'Scheduled',
];
/**
* @var BlogsRepository
*/
......@@ -36,14 +46,9 @@ class BlogsController extends Controller
*/
public function index(ManageBlogsRequest $request)
{
$status = [
'Published' => 'Published',
'Draft' => 'Draft',
'Inactive' => 'Inactive',
'Scheduled' => 'Scheduled',
];
return view('backend.blogs.index', compact('status'));
return view('backend.blogs.index')->with([
'status'=> $this->status
]);
}
/**
......@@ -53,16 +58,14 @@ class BlogsController extends Controller
*/
public function create(ManageBlogsRequest $request)
{
$blogCategories = BlogCategory::where('status', 1)->pluck('name', 'id');
$blogTags = BlogTag::where('status', 1)->pluck('name', 'id');
$status = [
'Published' => 'Published',
'Draft' => 'Draft',
'Inactive' => 'Inactive',
'Scheduled' => 'Scheduled',
];
return view('backend.blogs.create', compact('blogCategories', 'blogTags', 'status'));
$blogCategories = BlogCategory::getSelectData();
$blogTags = BlogTag::getSelectData();
return view('backend.blogs.create')->with([
'blogCategories' => $blogCategories,
'blogTags' => $blogTags,
'status'=> $this->status
]);
}
/**
......@@ -73,8 +76,7 @@ class BlogsController extends Controller
public function store(StoreBlogsRequest $request)
{
$input = $request->all();
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
$this->blogs->create($input, $tagsArray, $categoriesArray);
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.created'));
......@@ -88,25 +90,20 @@ class BlogsController extends Controller
*/
public function edit(Blog $blog, ManageBlogsRequest $request)
{
$blogCategories = BlogCategory::where('status', 1)->pluck('name', 'id');
$blogTags = BlogTag::where('status', 1)->pluck('name', 'id');
$status = [
'Published' => 'Published',
'Draft' => 'Draft',
'InActive' => 'InActive',
'Scheduled' => 'Scheduled',
];
$blogCategories = BlogCategory::getSelectData();
$blogTags = BlogTag::getSelectData();
$selectedCategories = $blog->categories->pluck('id')->toArray();
$selectedtags = $blog->tags->pluck('id')->toArray();
return view('backend.blogs.edit', compact(
'blogCategories',
'blogTags',
'status',
'selectedCategories',
'selectedtags')
)
->withBlog($blog);
return view('backend.blogs.edit')->with([
'blog' => $blog,
'blogCategories' => $blogCategories,
'blogTags' => $blogTags,
'selectedCategories' => $selectedCategories,
'selectedtags' => $selectedtags,
'status'=> $this->status
]);
}
/**
......@@ -118,10 +115,8 @@ class BlogsController extends Controller
public function update(Blog $blog, UpdateBlogsRequest $request)
{
$input = $request->all();
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
$this->blogs->update($blog, $input, $tagsArray, $categoriesArray);
$this->blogs->update($blog, $input);
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.updated'));
}
......@@ -138,53 +133,4 @@ class BlogsController extends Controller
return redirect()->route('admin.blogs.index')->withFlashSuccess(trans('alerts.backend.blogs.deleted'));
}
/**
* Creating Tags Array.
*
* @param Array($tags)
*
* @return array
*/
public function createTagsArray($tags)
{
//Creating a new array for tags (newly created)
$tags_array = [];
foreach ($tags as $tag) {
if (is_numeric($tag)) {
$tags_array[] = $tag;
} else {
$newTag = BlogTag::create(['name' => $tag, 'status' => 1, 'created_by' => 1]);
$tags_array[] = $newTag->id;
}
}
return $tags_array;
}
/**
* Creating Tags Array.
*
* @param Array($tags)
*
* @return array
*/
public function createCategoriesArray($categories)
{
//Creating a new array for categories (newly created)
$categories_array = [];
foreach ($categories as $category) {
if (is_numeric($category)) {
$categories_array[] = $category;
} else {
$newCategory = BlogCategory::create(['name' => $category, 'status' => 1, 'created_by' => 1]);
$categories_array[] = $newCategory->id;
}
}
return $categories_array;
}
}
......@@ -44,16 +44,16 @@ class BlogsRepository extends BaseRepository
/**
* @param array $input
* @param array $tagsArray
* @param array $categoriesArray
*
* @throws GeneralException
*
* @return bool
*/
public function create(array $input, array $tagsArray, array $categoriesArray)
public function create(array $input)
{
// dd(Carbon::createFromFormat('d/m/Y h:i a',$input['publish_datetime']));
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
DB::transaction(function () use ($input, $tagsArray, $categoriesArray) {
$blogs = self::MODEL;
$blogs = new $blogs();
......@@ -94,15 +94,16 @@ class BlogsRepository extends BaseRepository
}
/**
* Update Blog
*
* @param $blogs
* @param array $input
* @param array $tagsArray
* @param array $categoriesArray
*/
public function update($blogs, array $input, array $tagsArray, array $categoriesArray)
public function update($blogs, array $input)
{
// dd( Carbon::parse($input['publish_datetime']));
// dd($input['publish_datetime']);
$tagsArray = $this->createTagsArray($input['tags']);
$categoriesArray = $this->createCategoriesArray($input['categories']);
$blogs->name = $input['name'];
$blogs->slug = str_slug($input['name']);
$blogs->content = $input['content'];
......@@ -145,6 +146,55 @@ class BlogsRepository extends BaseRepository
});
}
/**
* Creating Tags Array.
*
* @param Array($tags)
*
* @return array
*/
public function createTagsArray($tags)
{
//Creating a new array for tags (newly created)
$tags_array = [];
foreach ($tags as $tag) {
if (is_numeric($tag)) {
$tags_array[] = $tag;
} else {
$newTag = BlogTag::create(['name' => $tag, 'status' => 1, 'created_by' => 1]);
$tags_array[] = $newTag->id;
}
}
return $tags_array;
}
/**
* Creating Tags Array.
*
* @param Array($tags)
*
* @return array
*/
public function createCategoriesArray($categories)
{
//Creating a new array for categories (newly created)
$categories_array = [];
foreach ($categories as $category) {
if (is_numeric($category)) {
$categories_array[] = $category;
} else {
$newCategory = BlogCategory::create(['name' => $category, 'status' => 1, 'created_by' => 1]);
$categories_array[] = $newCategory->id;
}
}
return $categories_array;
}
/**
* @param Model $blog
*
......
{
"/js/frontend.js": "/js/frontend.f5faf334d6b2231b48c8.js",
"/js/backend.js": "/js/backend.d106d5f7e2110ee3cea7.js",
"/js/frontend.js": "/js/frontend.945469bbbc12df3ad9e1.js",
"/js/backend.js": "/js/backend.79d9e4698dadbc0d93c7.js",
"/mix.js": "/mix.247ab120fe7680658924.js",
"/css/frontend.css": "/css/frontend.90a13bfbf8d4ea6a30a8eb218e8d48b3.css",
"/css/backend.css": "/css/backend.7b6d826816293ff35f4185341567f559.css",
"/css/backend-custom.css": "/css/backend-custom.187b92dacd8c501e4a19407d700d279b.css",
"/js/backend-custom.js": "/js/backend-custom.3ee9346acb0cd5e1f3edf70e9117e1f4.js",
"/js/dataTable.js": "/js/dataTable.0db0f52a09a62d485aa1229ed981b1cf.js"
"/css/frontend.css": "/css/frontend.3af0a6cbd7d1d8d042f2a37e97008b7c.css",
"/css/backend.css": "/css/backend.f8550f50504e5b8ef6055285205f223a.css",
"/css/backend-custom.css": "/css/backend-custom.50f14193ab908e3cf471dea6cb6616ae.css",
"/js/backend-custom.js": "/js/backend-custom.e6ea05e1824d0dd8e7c62027c135b7f2.js",
"/js/dataTable.js": "/js/dataTable.f968d300a6a0b871f138f114361259c8.js"
}
\ No newline at end of file
......@@ -10,6 +10,7 @@
@endsection
@section('content')
{{ Form::model($blog, ['route' => ['admin.blogs.update', $blog], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'PATCH', 'id' => 'edit-role', 'files' => true]) }}
<div class="box box-success">
......
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