Commit f892e015 authored by Vipul Basapati's avatar Vipul Basapati

Refactored BlogTag Module

parent 043e08c5
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Artisan;
class ModuleGenerator extends Command
{
use DetectsApplicationNamespace;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:module {--model= : Model Name} {--controller= : Name of the controller} {--t= : If table Controller should be made} {--res : If the controller should be resourceful} {--table= : Name of the table} {--routes= : Route Name} {--route_controller= : Route Controller} {--views= : If the views should be made} {--el= : If events and listeners should be made} {--repo= : If Repository should be made}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'It creates module\'s basic scaffolding like (Controllers, Model, Migration, Routes and Views)';
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//Making model
if ($this->option('model')) {
$model = $this->parseModel($this->option('model'));
$a = Artisan::call('make:model', ['name' => $model]);
$this->line(Artisan::output());
}
//Making Controller
if ($this->option('controller')) {
$controller = $this->parseModel($this->option('controller'));
Artisan::call('make:controller', ['name' => $controller, '--resource' => $this->option('res')]);
$this->line(Artisan::output());
//Making Table Controller
if ($this->option('t')) {
$tableController = $this->parseModel($this->option('t'));
Artisan::call('make:controller', ['name' => $tableController]);
$this->line('Table '.Artisan::output());
}
}
//Making Migration
if ($table = $this->option('table')) {
Artisan::call('make:migration', [
'name' => 'create_'.$table.'_table',
'--create' => $table,
]);
$this->line(Artisan::output());
}
//Making Events and Listeners
if ($this->option('el')) {
$el = explode(',', $this->option('el'));
foreach ($el as $e) {
$event = $this->parseModel($e);
Artisan::call('make:event', [
'name' => $event,
]);
$this->line(Artisan::output());
Artisan::call('make:listener', [
'name' => $event.'Listener',
'--event' => $event,
]);
$this->line(Artisan::output());
}
}
//Creating Routes
if (($route_name = $this->option('routes')) && $this->option('route_controller')) {
$base_path = base_path('routes/Backend');
$controller = class_basename($this->option('controller'));
if ($this->option('model')) {
$filename = class_basename($this->option('model'));
} else {
$filename = ucfirst($route_name);
}
$this->checkAndCreateDir($base_path);
$file_content = $this->files->get($this->getStub($this->option('res') ? 'resource' : false));
$file_content = str_replace(
['dummy_name', 'DummyModel', 'DummyController'],
[$route_name, $filename, $controller],
$file_content
);
$this->files->put($base_path.'/'.$filename.'.php', $file_content);
$this->line('Route File generated Successfully');
}
//Creating Views
if ($this->option('views')) {
$view_folder_name = strtolower($this->option('views'));
$base_path = resource_path('views/backend');
$path = $base_path.'/'.$view_folder_name;
$this->checkAndCreateDir($path);
$this->createViewFiles($path);
$this->line('View Files generated successfully');
}
//Creating Repository
if ($repo = $this->option('repo')) {
$class_name = class_basename($repo);
$repo = str_replace($class_name, '', $repo);
$base_path = base_path('app/Repositories/'.$repo);
$this->checkAndCreateDir($base_path);
$this->createRepoFile($repo, $class_name);
$this->line('Repository generated successfully');
}
}
/**
* Creating REpository File.
*
* @param string $path
* @param string $class
*
* @return none
*/
public function createRepoFile($path, $class)
{
$rootNamespace = $this->laravel->getNamespace().'Repositories\\';
$newPath = str_replace('/', '\\', $path);
$namespace = trim(str_replace('/', '\\', $rootNamespace.$newPath), '\\');
$file_contents = $this->files->get(__DIR__.'/Stubs/repository.stub');
$file_contents = str_replace(
['DummyNamespace', 'DummyClass'],
[$namespace, $class],
$file_contents
);
$path = base_path('app/Repositories/'.$path.'/'.$class);
$this->files->put($path, $file_contents);
}
/**
* Creating View Files (index, create, edit, form).
*
* @param string $path
*
* @return none
*/
public function createViewFiles($path)
{
$files = ['/index.blade.php', '/create.blade.php', '/edit.blade.php', '/form.blade.php'];
foreach ($files as $file) {
$this->files->put($path.$file, $this->files->get($this->getStub(false, true)));
}
}
/**
* Return Stub.
*
* @param bool $res
*
* @return file
*/
public function getStub($res = false, $view = false)
{
if ($view) {
return __DIR__.'/Stubs/view.stub';
} else {
if ($res && $res == 'resource') {
return __DIR__.'/Stubs/resourceRoute.stub';
} else {
return __DIR__.'/Stubs/route.stub';
}
}
}
/**
* Creating Directory.
*
* @param string $model
*
* @return string
*/
public function checkAndCreateDir($path)
{
if (is_dir($path)) {
return $path;
}
mkdir($path, 0777, true);
return $path;
}
/**
* Get the fully-qualified model class name.
*
* @param string $model
*
* @return string
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Name contains invalid characters.');
}
$model = trim(str_replace('/', '\\', $model), '\\');
return $model;
}
}
......@@ -14,13 +14,13 @@ class BlogTagCreated
/**
* @var
*/
public $blogtags;
public $blogtag;
/**
* @param $blogtags
* @param $blogtag
*/
public function __construct($blogtags)
public function __construct($blogtag)
{
$this->blogtags = $blogtags;
$this->blogtag = $blogtag;
}
}
......@@ -14,13 +14,13 @@ class BlogTagDeleted
/**
* @var
*/
public $blogtags;
public $blogtag;
/**
* @param $blogtags
* @param $blogtag
*/
public function __construct($blogtags)
public function __construct($blogtag)
{
$this->blogtags = $blogtags;
$this->blogtag = $blogtag;
}
}
......@@ -14,13 +14,13 @@ class BlogTagUpdated
/**
* @var
*/
public $blogtags;
public $blogtag;
/**
* @param $blogtags
* @param $blogtag
*/
public function __construct($blogtags)
public function __construct($blogtag)
{
$this->blogtags = $blogtags;
$this->blogtag = $blogtag;
}
}
......@@ -18,20 +18,20 @@ use App\Repositories\Backend\BlogTags\BlogTagsRepository;
class BlogTagsController extends Controller
{
/**
* @var BlogTagsRepository
* @var \App\Repositories\Backend\BlogTags\BlogTagsRepository
*/
protected $blogtags;
protected $blogtag;
/**
* @param blogtagsRepository $blogtags
* @param \App\Repositories\Backend\BlogTags\BlogTagsRepository $blogtag
*/
public function __construct(BlogTagsRepository $blogtags)
public function __construct(BlogTagsRepository $blogtag)
{
$this->blogtags = $blogtags;
$this->blogtag = $blogtag;
}
/**
* @param ManageBlogTagsRequest $request
* @param \App\Http\Requests\Backend\BlogTags\ManageBlogTagsRequest $request
*
* @return mixed
*/
......@@ -41,7 +41,7 @@ class BlogTagsController extends Controller
}
/**
* @param CreateBlogTagsRequest $request
* @param \App\Http\Requests\Backend\BlogTags\CreateBlogTagsRequest $request
*
* @return mixed
*/
......@@ -51,52 +51,58 @@ class BlogTagsController extends Controller
}
/**
* @param StoreblogtagsRequest $request
* @param \App\Http\Requests\Backend\BlogTags\StoreBlogTagsRequest $request
*
* @return mixed
*/
public function store(StoreBlogTagsRequest $request)
{
$this->blogtags->create($request->all());
$this->blogtag->create($request->except('token'));
return redirect()->route('admin.blogtags.index')->withFlashSuccess(trans('alerts.backend.blogtags.created'));
return redirect()
->route('admin.blogtags.index')
->with('flash_success', trans('alerts.backend.blogtags.created'));
}
/**
* @param BlogTag $blogtag
* @param EditBlogTagsRequest $request
* @param \App\Models\BlogTags\BlogTag $blogtag
* @param \App\Http\Requests\Backend\BlogTags\EditBlogTagsRequest $request
*
* @return mixed
*/
public function edit(BlogTag $blogtag, EditBlogTagsRequest $request)
{
return view('backend.blogtags.edit')
->withBlogtag($blogtag);
->with('blogtag', $blogtag);
}
/**
* @param BlogTag $blogtag
* @param UpdateblogtagsRequest $request
* @param \App\Models\BlogTags\BlogTag $blogtag
* @param \App\Http\Requests\Backend\BlogTags\UpdateBlogTagsRequest $request
*
* @return mixed
*/
public function update(BlogTag $blogtag, UpdateBlogTagsRequest $request)
{
$this->blogtags->update($blogtag, $request->all());
$this->blogtag->update($blogtag, $request->except(['_method', '_token']));
return redirect()->route('admin.blogtags.index')->withFlashSuccess(trans('alerts.backend.blogtags.updated'));
return redirect()
->route('admin.blogtags.index')
->with('flash_success', trans('alerts.backend.blogtags.updated'));
}
/**
* @param BlogTag $blogtag
* @param DeleteBlogTagsRequest $request
* @param \App\Models\BlogTags\BlogTag $blogtag
* @param \App\Http\Requests\Backend\BlogTags\DeleteBlogTagsRequest $request
*
* @return mixed
*/
public function destroy(BlogTag $blogtag, DeleteBlogTagsRequest $request)
{
$this->blogtags->delete($blogtag);
$this->blogtag->delete($blogtag);
return redirect()->route('admin.blogtags.index')->withFlashSuccess(trans('alerts.backend.blogtags.deleted'));
return redirect()
->route('admin.blogtags.index')
->with('flash_success', trans('alerts.backend.blogtags.deleted'));
}
}
......@@ -14,12 +14,12 @@ use Yajra\DataTables\Facades\DataTables;
class BlogTagsTableController extends Controller
{
/**
* @var BlogTagsRepository
* @var \App\Repositories\Backend\BlogTags\BlogTagsRepository
*/
protected $blogtags;
/**
* @param BlogTagsRepository $cmspages
* @param \App\Repositories\Backend\BlogTags\BlogTagsRepository $blogtags
*/
public function __construct(BlogTagsRepository $blogtags)
{
......@@ -27,7 +27,7 @@ class BlogTagsTableController extends Controller
}
/**
* @param ManageBlogTagsRequest $request
* @param \App\Http\Requests\Backend\BlogTags\ManageBlogTagsRequest $request
*
* @return mixed
*/
......@@ -36,11 +36,7 @@ class BlogTagsTableController extends Controller
return Datatables::of($this->blogtags->getForDataTable())
->escapeColumns(['name'])
->addColumn('status', function ($blogtags) {
if ($blogtags->status) {
return '<span class="label label-success">Active</span>';
}
return '<span class="label label-danger">Inactive</span>';
return $blogtags->status_label;
})
->addColumn('created_by', function ($blogtags) {
return $blogtags->user_name;
......
......@@ -18,8 +18,8 @@ class BlogTagEventListener
public function onCreated($event)
{
history()->withType($this->history_slug)
->withEntity($event->blogtags->id)
->withText('trans("history.backend.blogtags.created") <strong>'.$event->blogtags->name.'</strong>')
->withEntity($event->blogtag->id)
->withText('trans("history.backend.blogtags.created") <strong>'.$event->blogtag->name.'</strong>')
->withIcon('plus')
->withClass('bg-green')
->log();
......@@ -31,8 +31,8 @@ class BlogTagEventListener
public function onUpdated($event)
{
history()->withType($this->history_slug)
->withEntity($event->blogtags->id)
->withText('trans("history.backend.blogtags.updated") <strong>'.$event->blogtags->name.'</strong>')
->withEntity($event->blogtag->id)
->withText('trans("history.backend.blogtags.updated") <strong>'.$event->blogtag->name.'</strong>')
->withIcon('save')
->withClass('bg-aqua')
->log();
......@@ -44,8 +44,8 @@ class BlogTagEventListener
public function onDeleted($event)
{
history()->withType($this->history_slug)
->withEntity($event->blogtags->id)
->withText('trans("history.backend.blogtags.deleted") <strong>'.$event->blogtags->name.'</strong>')
->withEntity($event->blogtag->id)
->withText('trans("history.backend.blogtags.deleted") <strong>'.$event->blogtag->name.'</strong>')
->withIcon('trash')
->withClass('bg-maroon')
->log();
......
......@@ -17,4 +17,24 @@ trait BlogTagAttribute
'.$this->getDeleteButtonAttribute('delete-blog-tag', 'admin.blogtags.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;
}
}
......@@ -26,13 +26,13 @@ class BlogTagsRepository extends BaseRepository
public function getForDataTable()
{
return $this->query()
->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('access.blog_tags_table').'.created_by')
->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('module.blog_tags.table').'.created_by')
->select([
config('access.blog_tags_table').'.id',
config('access.blog_tags_table').'.name',
config('access.blog_tags_table').'.status',
config('access.blog_tags_table').'.created_by',
config('access.blog_tags_table').'.created_at',
config('module.blog_tags.table').'.id',
config('module.blog_tags.table').'.name',
config('module.blog_tags.table').'.status',
config('module.blog_tags.table').'.created_by',
config('module.blog_tags.table').'.created_at',
config('access.users_table').'.first_name as user_name',
]);
}
......@@ -40,7 +40,7 @@ class BlogTagsRepository extends BaseRepository
/**
* @param array $input
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* @return bool
*/
......@@ -49,16 +49,13 @@ class BlogTagsRepository extends BaseRepository
if ($this->query()->where('name', $input['name'])->first()) {
throw new GeneralException(trans('exceptions.backend.blogtags.already_exists'));
}
DB::transaction(function () use ($input) {
$blogtags = self::MODEL;
$blogtags = new $blogtags();
$blogtags->name = $input['name'];
$blogtags->status = (isset($input['status']) && $input['status'] == 1)
? 1 : 0;
$blogtags->created_by = access()->user()->id;
$input['status'] = isset($input['status']) ? 1 : 0;
$input['created_by'] = access()->user()->id;
if ($blogtags->save()) {
event(new BlogTagCreated($blogtags));
if ($blogtag = BlogTag::create($input)) {
event(new BlogTagCreated($blogtag));
return true;
}
......@@ -68,27 +65,25 @@ class BlogTagsRepository extends BaseRepository
}
/**
* @param Model $permission
* @param \App\Models\BlogTags\BlogTag $blogtag
* @param $input
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* return bool
*/
public function update(Model $blogtags, array $input)
public function update(BlogTag $blogtag, array $input)
{
if ($this->query()->where('name', $input['name'])->where('id', '!=', $blogtags->id)->first()) {
if ($this->query()->where('name', $input['name'])->where('id', '!=', $blogtag->id)->first()) {
throw new GeneralException(trans('exceptions.backend.blogtags.already_exists'));
}
$blogtags->name = $input['name'];
$blogtags->status = (isset($input['status']) && $input['status'] == 1)
? 1 : 0;
$blogtags->updated_by = access()->user()->id;
DB::transaction(function () use ($blogtags, $input) {
if ($blogtags->save()) {
event(new BlogTagUpdated($blogtags));
DB::transaction(function () use ($blogtag, $input) {
$input['status'] = isset($input['status']) ? 1 : 0;
$input['updated_by'] = access()->user()->id;
if ($blogtag->update($input)) {
event(new BlogTagUpdated($blogtag));
return true;
}
......@@ -100,13 +95,13 @@ class BlogTagsRepository extends BaseRepository
}
/**
* @param Model $blogtag
* @param \App\Models\BlogTags\BlogTag $blogtag
*
* @throws GeneralException
* @throws \App\Exceptions\GeneralException
*
* @return bool
*/
public function delete(Model $blogtag)
public function delete(BlogTag $blogtag)
{
DB::transaction(function () use ($blogtag) {
if ($blogtag->delete()) {
......
......@@ -75,11 +75,6 @@ return [
*/
'blog_categories_table' => 'blog_categories',
/*
* Blog Tags table used to store Blog Tag
*/
'blog_tags_table' => 'blog_tags',
/*
* Blog Tags table used to store Blog Tag
*/
......
......@@ -9,4 +9,7 @@ return [
'placeholders_table' => 'email_template_placeholders',
'types_table' => 'email_template_types',
],
'blog_tags' => [
'table' => 'blog_tags'
]
];
......@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogtags.create') }}</h3>
<div class="box-tools pull-right">
@include('backend.includes.partials.blogtags-header-buttons')
@include('backend.blogtags.partials.blogtags-header-buttons')
</div><!--box-tools pull-right-->
</div><!-- /.box-header -->
......
......@@ -17,7 +17,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogtags.edit') }}</h3>
<div class="box-tools pull-right">
@include('backend.includes.partials.blogtags-header-buttons')
@include('backend.blogtags.partials.blogtags-header-buttons')
</div><!--box-tools pull-right-->
</div><!-- /.box-header -->
......
......@@ -12,7 +12,7 @@
<h3 class="box-title">{{ trans('labels.backend.blogtags.management') }}</h3>
<div class="box-tools pull-right">
@include('backend.includes.partials.blogtags-header-buttons')
@include('backend.blogtags.partials.blogtags-header-buttons')
</div>
</div><!-- /.box-header -->
......@@ -74,10 +74,10 @@
type: 'post'
},
columns: [
{data: 'name', name: '{{config('access.blog_tags_table')}}.name'},
{data: 'status', name: '{{config('access.blog_tags_table')}}.status'},
{data: 'created_by', name: '{{config('access.blog_tags_table')}}.created_by'},
{data: 'created_at', name: '{{config('access.blog_tags_table')}}.created_at'},
{data: 'name', name: '{{config('module.blog_tags.table')}}.name'},
{data: 'status', name: '{{config('module.blog_tags.table')}}.status'},
{data: 'created_by', name: '{{config('module.blog_tags.table')}}.created_by'},
{data: 'created_at', name: '{{config('module.blog_tags.table')}}.created_at'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
order: [[3, "asc"]],
......
<!--Action Button-->
@if(Active::checkUriPattern('admin/blogtags'))
<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.blogtags.index')}}"><i class="fa fa-list-ul"></i> {{trans('menus.backend.blogtags.all')}}</a></li>
@permission('create-blog-tag')
<li><a href="{{route('admin.blogtags.create')}}"><i class="fa fa-plus"></i> {{trans('menus.backend.blogtags.create')}}</a></li>
@endauth
</ul>
</div>
<div class="clearfix"></div>
\ No newline at end of file
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