first change
This commit is contained in:
0
Modules/Menu/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Menu/app/Http/Controllers/.gitkeep
Normal file
245
Modules/Menu/app/Http/Controllers/MenuController.php
Normal file
245
Modules/Menu/app/Http/Controllers/MenuController.php
Normal file
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\CCMS\Models\Blog;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CCMS\Models\Page;
|
||||
use Modules\CCMS\Models\Service;
|
||||
use Modules\Menu\Interfaces\MenuInterface;
|
||||
use Modules\Menu\Models\Menu;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class MenuController extends Controller
|
||||
{
|
||||
private $menu;
|
||||
|
||||
public function __construct(MenuInterface $menu)
|
||||
{
|
||||
$this->menu = $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (request()->ajax()) {
|
||||
$model = Menu::query()->orderBy('order');
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->addColumn('parent', function (Menu $menu) {
|
||||
return $menu->parent?->title ?? '-';
|
||||
})
|
||||
->addColumn('parameter', function (Menu $menu) {
|
||||
return $menu->url_parameter;
|
||||
})
|
||||
->editColumn('type', '{!! config("constants.menu_type_options")[$type] !!}')
|
||||
->editColumn('status', function (Menu $menu) {
|
||||
$status = $menu->status ? 'Published' : 'Draft';
|
||||
$color = $menu->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('action', 'menu::menu.datatable.action')
|
||||
->rawColumns(['status', 'action'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('menu::menu.index', [
|
||||
'title' => 'Menu List',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$data['title'] = 'Create Menu';
|
||||
$data['editable'] = false;
|
||||
$data['menuOptions'] = $this->menu->pluck();
|
||||
$data['menuTypes'] = config('constants.menu_type_options');
|
||||
return view('menu::menu.create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
|
||||
$maxOrder = Menu::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->merge([
|
||||
'alias' => Str::slug($request->title),
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'menu_location_id' => 'required',
|
||||
'title' => 'required',
|
||||
'alias' => 'required',
|
||||
'type' => 'required',
|
||||
'icon' => 'nullable', 'string',
|
||||
'image' => 'nullable', 'string',
|
||||
'target' => 'nullable', 'string',
|
||||
'parameter' => 'required',
|
||||
'order' => 'required', 'integer',
|
||||
'parent_id' => 'nullable', 'integer',
|
||||
'status' => 'required', 'integer',
|
||||
]);
|
||||
|
||||
try {
|
||||
|
||||
$menu = $this->menu->create($validated);
|
||||
flash()->success("Menu for {$menu->title} has been created!");
|
||||
return redirect()->route('menu.index');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
return redirect()->back()->with('error', $th->getMessage())->withInput();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('cms::menu.show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$data['title'] = 'Edit Menu';
|
||||
$data['editable'] = true;
|
||||
$data['menu'] = $this->menu->findOne($id);
|
||||
$data['menuOptions'] = $this->menu->pluck();
|
||||
$data['menuTypes'] = config('constants.menu_type_options');
|
||||
return view('menu::menu.edit', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->merge([
|
||||
'alias' => Str::slug($request->title),
|
||||
]);
|
||||
|
||||
try {
|
||||
|
||||
$validated = $request->validate([
|
||||
'menu_location_id' => 'required',
|
||||
'title' => 'required',
|
||||
'alias' => 'required',
|
||||
'icon' => 'nullable', 'string',
|
||||
'image' => 'nullable', 'string',
|
||||
'target' => 'nullable', 'string',
|
||||
'type' => 'required',
|
||||
'parameter' => 'required',
|
||||
'status' => 'integer',
|
||||
'parent_id' => 'nullable', 'integer',
|
||||
]);
|
||||
|
||||
$this->menu->update($id, $request->all());
|
||||
flash()->success('Menu has been updated!');
|
||||
return redirect()->route('menu.index');
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
flash()->error($th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
|
||||
$this->menu->delete($id);
|
||||
$higherOrders = Menu::where('id', '>', $id)->get();
|
||||
|
||||
if ($higherOrders) {
|
||||
foreach ($higherOrders as $higherOrder) {
|
||||
$higherOrder->order--;
|
||||
$higherOrder->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['status' => 200, 'message' => 'Menu has been deleted!']);
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with('error', $th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function getMenuTypeOptions(Request $request)
|
||||
{
|
||||
$tableName = $request->tableName;
|
||||
|
||||
switch ($tableName) {
|
||||
|
||||
case 'pages':
|
||||
$menuTypeOptions = Page::where(['type' => 'page', 'status' => 1])->pluck('title', 'id');
|
||||
break;
|
||||
|
||||
case 'blogs':
|
||||
$menuTypeOptions = Blog::where('status', 1)->pluck('title', 'id');
|
||||
break;
|
||||
|
||||
case 'services':
|
||||
$menuTypeOptions = Service::where('status', 1)->pluck('title', 'id');
|
||||
break;
|
||||
|
||||
case 'countries':
|
||||
$menuTypeOptions = Country::where('status', 1)->pluck('title', 'id');
|
||||
break;
|
||||
|
||||
default:
|
||||
$menuTypeOptions = [];
|
||||
|
||||
}
|
||||
|
||||
return response()->json(['status' => 200, 'data' => $menuTypeOptions], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
|
||||
$menus = $this->menu->findAll();
|
||||
|
||||
foreach ($menus as $menu) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $menu->id) {
|
||||
$menu->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => 200, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$menu = $this->menu->findOne($id);
|
||||
$menu->update(['status' => !$menu->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
12
Modules/Menu/app/Interfaces/MenuInterface.php
Normal file
12
Modules/Menu/app/Interfaces/MenuInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Modules\Menu\Interfaces;
|
||||
|
||||
interface MenuInterface
|
||||
{
|
||||
public function findAll();
|
||||
public function findOne($menuId);
|
||||
public function create($menuDetails);
|
||||
public function update($menuId, array $newDetails);
|
||||
public function delete($menuId);
|
||||
public function pluck();
|
||||
}
|
132
Modules/Menu/app/Models/Menu.php
Normal file
132
Modules/Menu/app/Models/Menu.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Modules\CCMS\Models\Blog;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CCMS\Models\Page;
|
||||
use Modules\CCMS\Models\Service;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'menu_location_id',
|
||||
'title',
|
||||
'alias',
|
||||
'target',
|
||||
'image',
|
||||
'icon',
|
||||
'parent_id',
|
||||
'order',
|
||||
'type',
|
||||
'parameter',
|
||||
'status',
|
||||
'order',
|
||||
];
|
||||
|
||||
public $appends = ['location'];
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(Menu::class, 'parent_id');
|
||||
}
|
||||
|
||||
protected function urlParameter(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (?string $value, ?array $attributes) {
|
||||
$parameter = null;
|
||||
|
||||
switch ($attributes['type']) {
|
||||
case 'single-link':
|
||||
$parameter = $attributes['parameter'];
|
||||
break;
|
||||
|
||||
default:
|
||||
$model = config('constants.menu_type_options')[$attributes['type']] ?? null;
|
||||
if ($model) {
|
||||
$modelClass = "Modules\\CCMS\\Models\\$model";
|
||||
$parameter = optional($modelClass::find($attributes['parameter']))->slug;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $parameter;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Menu::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function location(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
return config('constants.menu_location_options')[$attributes['menu_location_id']];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function hasSubMenu()
|
||||
{
|
||||
return $this->relationLoaded('children') ? $this->children->isNotEmpty() : $this->children()->exists();
|
||||
}
|
||||
|
||||
|
||||
protected function routeName(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
|
||||
$route = null;
|
||||
|
||||
switch ($attributes['type']) {
|
||||
case 'pages':
|
||||
$route = route('page.load', Page::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'services':
|
||||
$route = route('service.single', Service::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'countries':
|
||||
$route = route('country.single', Country::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'blogs':
|
||||
$route = route('blog.single', Blog::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
default:
|
||||
$route = ($attributes['parameter'] == '#')
|
||||
? 'javascript:void(0)'
|
||||
: (preg_match('/^https/', $attributes['parameter'])
|
||||
? $attributes['parameter']
|
||||
: config('app.url') . $attributes['parameter']);
|
||||
|
||||
}
|
||||
|
||||
return $route;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function image(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn($value) => asset($value),
|
||||
);
|
||||
}
|
||||
}
|
0
Modules/Menu/app/Providers/.gitkeep
Normal file
0
Modules/Menu/app/Providers/.gitkeep
Normal file
32
Modules/Menu/app/Providers/EventServiceProvider.php
Normal file
32
Modules/Menu/app/Providers/EventServiceProvider.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
protected $listen = [];
|
||||
|
||||
/**
|
||||
* Indicates if events should be discovered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $shouldDiscoverEvents = true;
|
||||
|
||||
/**
|
||||
* Configure the proper event listeners for email verification.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configureEmailVerification(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
123
Modules/Menu/app/Providers/MenuServiceProvider.php
Normal file
123
Modules/Menu/app/Providers/MenuServiceProvider.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\Menu\Interfaces\MenuInterface;
|
||||
use Modules\Menu\Repositories\MenuRepository;
|
||||
|
||||
class MenuServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Menu';
|
||||
|
||||
protected string $moduleNameLower = 'menu';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->bind(MenuInterface::class, MenuRepository::class);
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder', '')));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
49
Modules/Menu/app/Providers/RouteServiceProvider.php
Normal file
49
Modules/Menu/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
|
||||
$this->mapWebRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')->group(module_path('Menu', '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api')->name('api.')->group(module_path('Menu', '/routes/api.php'));
|
||||
}
|
||||
}
|
46
Modules/Menu/app/Repositories/MenuRepository.php
Normal file
46
Modules/Menu/app/Repositories/MenuRepository.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace Modules\Menu\Repositories;
|
||||
|
||||
use Modules\Menu\Interfaces\MenuInterface;
|
||||
use Modules\Menu\Models\Menu;
|
||||
|
||||
class MenuRepository implements MenuInterface
|
||||
{
|
||||
public function findAll()
|
||||
{
|
||||
return Menu::orderBy('order')->get();
|
||||
}
|
||||
|
||||
public function findOne($menuId)
|
||||
{
|
||||
return Menu::findOrFail($menuId);
|
||||
}
|
||||
|
||||
public function create($menuDetails)
|
||||
{
|
||||
return Menu::create($menuDetails);
|
||||
}
|
||||
|
||||
public function update($menuId, array $newDetails)
|
||||
{
|
||||
$menu = Menu::whereId($menuId)->first();
|
||||
$menu->update($newDetails);
|
||||
return $menu;
|
||||
}
|
||||
|
||||
public function delete($menuId)
|
||||
{
|
||||
return Menu::destroy($menuId);
|
||||
}
|
||||
|
||||
public function pluck()
|
||||
{
|
||||
$menus = Menu::where('status', 1)->get();
|
||||
|
||||
return $menus->mapWithKeys(function ($item) {
|
||||
$menuLocation = config('constants.menu_location_options')[$item->menu_location_id];
|
||||
return [$item->id => "{$item->title} ({$menuLocation})"];
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user