Commit 6e849abe authored by Vipul Basapati's avatar Vipul Basapati

Refactored Pages Module

parent faa59fc4
......@@ -2,15 +2,15 @@
namespace App\Http\Controllers\Backend\Pages;
use App\Models\Page\Page;
use App\Http\Controllers\Controller;
use App\Repositories\Backend\Pages\PagesRepository;
use App\Http\Requests\Backend\Pages\EditPageRequest;
use App\Http\Requests\Backend\Pages\StorePageRequest;
use App\Http\Requests\Backend\Pages\CreatePageRequest;
use App\Http\Requests\Backend\Pages\DeletePageRequest;
use App\Http\Requests\Backend\Pages\EditPageRequest;
use App\Http\Requests\Backend\Pages\ManagePageRequest;
use App\Http\Requests\Backend\Pages\StorePageRequest;
use App\Http\Requests\Backend\Pages\UpdatePageRequest;
use App\Models\Page\Page;
use App\Repositories\Backend\Pages\PagesRepository;
/**
* Class PagesController.
......@@ -23,7 +23,7 @@ class PagesController extends Controller
protected $pages;
/**
* @param PagesRepository $pages
* @param \App\Repositories\Backend\Pages\PagesRepository $pages
*/
public function __construct(PagesRepository $pages)
{
......@@ -31,7 +31,7 @@ class PagesController extends Controller
}
/**
* @param ManagePageRequest $request
* @param \App\Http\Requests\Backend\Pages\ManagePageRequest $request
*
* @return mixed
*/
......@@ -41,7 +41,7 @@ class PagesController extends Controller
}
/**
* @param CreatePageRequest $request
* @param \App\Http\Requests\Backend\Pages\CreatePageRequest $request
*
* @return mixed
*/
......@@ -51,45 +51,45 @@ class PagesController extends Controller
}
/**
* @param StorePageRequest $request
* @param \App\Http\Requests\Backend\Pages\StorePageRequest $request
*
* @return mixed
*/
public function store(StorePageRequest $request)
{
$this->pages->create($request->all());
$this->pages->create($request->except(['_token']));
return redirect()->route('admin.pages.index')->withFlashSuccess(trans('alerts.backend.pages.created'));
}
/**
* @param Page $page
* @param EditPageRequest $request
* @param \App\Models\Page\Page $page
* @param \App\Http\Requests\Backend\Pages\EditPageRequest $request
*
* @return mixed
*/
public function edit(Page $page, EditPageRequest $request)
{
return view('backend.pages.edit')
->withCmspage($page);
->withPage($page);
}
/**
* @param Page $page
* @param UpdatePageRequest $request
* @param \App\Models\Page\Page $page
* @param \App\Http\Requests\Backend\Pages\UpdatePageRequest $request
*
* @return mixed
*/
public function update(Page $page, UpdatePageRequest $request)
{
$this->pages->update($page, $request->all());
$this->pages->update($page, $request->except(['_method', '_token']));
return redirect()->route('admin.pages.index')->withFlashSuccess(trans('alerts.backend.pages.updated'));
}
/**
* @param Page $page
* @param DeletePageRequest $request
* @param \App\Models\Page\Page $page
* @param \App\Http\Requests\Backend\Pages\DeletePageRequest $request
*
* @return mixed
*/
......
......@@ -2,11 +2,11 @@
namespace App\Http\Controllers\Backend\Pages;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\Pages\ManagePageRequest;
use App\Repositories\Backend\Pages\PagesRepository;
use Carbon\Carbon;
use App\Http\Controllers\Controller;
use Yajra\DataTables\Facades\DataTables;
use App\Repositories\Backend\Pages\PagesRepository;
use App\Http\Requests\Backend\Pages\ManagePageRequest;
/**
* Class PagesTableController.
......@@ -36,11 +36,7 @@ class PagesTableController extends Controller
return Datatables::of($this->pages->getForDataTable())
->escapeColumns(['title'])
->addColumn('status', function ($pages) {
if ($pages->status) {
return '<span class="label label-success">Active</span>';
}
return '<span class="label label-danger">Inactive</span>';
return $pages->status_label;
})
->addColumn('created_at', function ($pages) {
return Carbon::parse($pages->created_at)->toDateString();
......
......@@ -4,8 +4,8 @@ namespace App\Models\Page;
use App\Models\BaseModel;
use App\Models\ModelTrait;
use App\Models\Page\Traits\Attribute\PageAttribute;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Page\Traits\Attribute\PageAttribute;
class Page extends BaseModel
{
......@@ -22,9 +22,16 @@ class Page extends BaseModel
*/
protected $table;
/**
* The guarded field which are not mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('access.cms_pages_table');
$this->table = config('module.pages.table');
}
}
......@@ -17,4 +17,24 @@ trait PageAttribute
'.$this->getDeleteButtonAttribute('delete-cms-pages', 'admin.pages.destroy').'
</div>';
}
/**
* @return string
*/
public function getStatusLabelAttribute()
{
if ($this->isActive()) {
return "<label class='label label-success'>".trans('labels.general.active').'</label>';
}
return "<label class='label label-danger'>".trans('labels.general.inactive').'</label>';
}
/**
* @return bool
*/
public function isActive()
{
return $this->status == 1;
}
}
......@@ -2,13 +2,12 @@
namespace App\Repositories\Backend\Pages;
use App\Models\Page\Page;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use App\Events\Backend\Pages\PageCreated;
use App\Events\Backend\Pages\PageDeleted;
use App\Events\Backend\Pages\PageUpdated;
use App\Exceptions\GeneralException;
use App\Models\Page\Page;
use App\Repositories\BaseRepository;
use DB;
/**
* Class PagesRepository.
......@@ -27,18 +26,18 @@ class PagesRepository extends BaseRepository
{
return $this->query()
->select([
config('access.pages_table').'.id',
config('access.pages_table').'.title',
config('access.pages_table').'.status',
config('access.pages_table').'.created_at',
config('access.pages_table').'.updated_at',
config('module.pages.table').'.id',
config('module.pages.table').'.title',
config('module.pages.table').'.status',
config('module.pages.table').'.created_at',
config('module.pages.table').'.updated_at',
]);
}
/**
* @param array $input
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* @return bool
*/
......@@ -47,81 +46,63 @@ class PagesRepository extends BaseRepository
if ($this->query()->where('title', $input['title'])->first()) {
throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
}
//Making extra fields
$input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0;
$input['created_by'] = access()->user()->id;
if( $page = Page::create($input) ) {
event(new PageCreated($page));
return true;
}
DB::transaction(function () use ($input) {
$pages = self::MODEL;
$pages = new $pages();
$pages->title = $input['title'];
$pages->page_slug = str_slug($input['title']);
$pages->description = $input['description'];
$pages->cannonical_link = $input['cannonical_link'];
$pages->seo_title = $input['seo_title'];
$pages->seo_keyword = $input['seo_keyword'];
$pages->seo_description = $input['seo_description'];
$pages->status = (isset($input['status']) && $input['status'] == 1) ? 1 : 0;
$pages->created_by = access()->user()->id;
if ($pages->save()) {
event(new PageCreated($pages));
return true;
}
throw new GeneralException(trans('exceptions.backend.pages.create_error'));
});
throw new GeneralException(trans('exceptions.backend.pages.create_error'));
}
/**
* @param Model $permission
* @param $input
* @param \App\Models\Page\Page $page
* @param array $input
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* return bool
* @return bool
*/
public function update(Model $page, array $input)
public function update($page, array $input)
{
if ($this->query()->where('title', $input['title'])->where('id', '!=', $page->id)->first()) {
throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
}
$page->title = $input['title'];
$page->page_slug = str_slug($input['title']);
$page->description = $input['description'];
$page->cannonical_link = $input['cannonical_link'];
$page->seo_title = $input['seo_title'];
$page->seo_keyword = $input['seo_keyword'];
$page->seo_description = $input['seo_description'];
$page->status = (isset($input['status']) && $input['status'] == 1) ? 1 : 0;
$page->updated_by = access()->user()->id;
DB::transaction(function () use ($page, $input) {
if ($page->save()) {
event(new PageUpdated($page));
//Making extra fields
$input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0;
$input['updated_by'] = access()->user()->id;
return true;
}
if ( $page->update($input) ) {
event(new PageUpdated($page));
throw new GeneralException(trans('exceptions.backend.pages.update_error'));
});
return true;
}
throw new GeneralException(trans('exceptions.backend.pages.update_error'));
}
/**
* @param Model $page
* @param \App\Models\Page\Page $page
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* @return bool
*/
public function delete(Model $page)
public function delete($page)
{
DB::transaction(function () use ($page) {
if ($page->delete()) {
event(new PageDeleted($page));
if ( $page->delete() ) {
event(new PageDeleted($page));
return true;
}
return true;
}
throw new GeneralException(trans('exceptions.backend.pages.delete_error'));
});
throw new GeneralException(trans('exceptions.backend.pages.delete_error'));
}
}
......@@ -60,11 +60,6 @@ return [
*/
'cities_table' => 'cities',
/*
* cmspages table used to store cmspages
*/
'pages_table' => 'pages',
/*
* email template placeholders table used to store email template placeholders
*/
......
<?php
return [
'pages' => [
'table' => 'pages'
]
];
\ No newline at end of file
......@@ -10,7 +10,7 @@
@endsection
@section('content')
{{ Form::model($cmspage, ['route' => ['admin.pages.update', $cmspage], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'PATCH', 'id' => 'edit-role']) }}
{{ Form::model($page, ['route' => ['admin.pages.update', $page], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'PATCH', 'id' => 'edit-role']) }}
<div class="box box-success">
<div class="box-header with-border">
......@@ -76,7 +76,7 @@
<div class="col-lg-10">
<div class="control-group">
<label class="control control--checkbox">
{{ Form::checkbox('status', 1, ($cmspage->status == 1) ? true : false ) }}
{{ Form::checkbox('status', 1, ($page->status == 1) ? true : false ) }}
<div class="control__indicator"></div>
</label>
</div>
......
......@@ -74,10 +74,10 @@
type: 'post'
},
columns: [
{data: 'title', name: '{{config('access.pages_table')}}.title'},
{data: 'status', name: '{{config('access.pages_table')}}.status'},
{data: 'created_at', name: '{{config('access.pages_table')}}.created_at'},
{data: 'updated_at', name: '{{config('access.pages_table')}}.updated_at'},
{data: 'title', name: '{{config('module.pages.table')}}.title'},
{data: 'status', name: '{{config('module.pages.table')}}.status'},
{data: 'created_at', name: '{{config('module.pages.table')}}.created_at'},
{data: 'updated_at', name: '{{config('module.pages.table')}}.updated_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
order: [[3, "asc"]],
......
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