feat: Implement Event management features including CRUD operations and routing
This commit is contained in:
147
Modules/CCMS/app/Http/Controllers/EventController.php
Normal file
147
Modules/CCMS/app/Http/Controllers/EventController.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CCMS\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\CCMS\Models\Event;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class EventController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if (request()->ajax()) {
|
||||
$model = Event::query()->orderBy('order');
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('image', function (Event $event) {
|
||||
return $event->getRawOriginal('image') ? "<img src='{$event->image}' alt='{$event->title}' class='rounded avatar-sm material-shadow ms-2 img-thumbnail'>" : '-';
|
||||
})
|
||||
->editColumn('parent_id', function (Event $event) {
|
||||
return $event->parent ? "<span class='badge bg-primary p-1'>{$event->parent?->title}</span>" : '-';
|
||||
})
|
||||
->editColumn('status', function (Event $event) {
|
||||
$status = $event->status ? 'Published' : 'Draft';
|
||||
$color = $event->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('action', 'ccms::event.datatable.action')
|
||||
->rawColumns(['parent_id', 'image', 'status', 'action'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('ccms::event.index', [
|
||||
'title' => 'Event List',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$eventOptions = Event::where('status', 1)->pluck('title', 'id');
|
||||
return view('ccms::event.create', [
|
||||
'title' => 'Create Event',
|
||||
'editable' => false,
|
||||
'eventOptions' => $eventOptions
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$maxOrder = Event::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'slug' => Str::slug($request->title),
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required',
|
||||
]);
|
||||
Event::create($request->all());
|
||||
flash()->success("Event has been created!");
|
||||
return redirect()->route('event.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('ccms::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$eventOptions = Event::where('status', 1)->pluck('title', 'id');
|
||||
$event = Event::findOrFail($id);
|
||||
return view('ccms::event.edit', [
|
||||
'title' => 'Edit Event',
|
||||
'editable' => true,
|
||||
'event' => $event,
|
||||
'eventOptions' => $eventOptions
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->merge([
|
||||
'slug' => Str::slug($request->title),
|
||||
]);
|
||||
$validated = $request->validate([]);
|
||||
$event = Event::findOrFail($id);
|
||||
$event->update($request->all());
|
||||
flash()->success("Event has been updated.");
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$event = Event::findOrFail($id);
|
||||
$event->delete();
|
||||
return response()->json(['status' => 200, 'message' => "Event has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$events = Event::all();
|
||||
foreach ($events as $event) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $event->id) {
|
||||
$event->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$event = Event::findOrFail($id);
|
||||
$event->update(['status' => !$event->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user