165 lines
4.8 KiB
PHP
165 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Meeting\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Modules\Meeting\Models\Event;
|
|
use Modules\Meeting\Repositories\EventRepository;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class EventController extends Controller
|
|
{
|
|
private $eventRepository;
|
|
|
|
public function __construct(EventRepository $eventRepository)
|
|
{
|
|
$this->eventRepository = $eventRepository;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$data['title'] = 'Event Lists';
|
|
|
|
if (request()->ajax()) {
|
|
|
|
$model = Event::query();
|
|
return DataTables::eloquent($model)
|
|
->addIndexColumn()
|
|
->setRowClass('{{"align-middle"}}')
|
|
->addColumn('type', '{{ config("constants.event_type_options")[$type] }}')
|
|
->addColumn('date', function (Event $event) {
|
|
$start = $event->start_date->format('d M, Y');
|
|
$end = $event->end_date->format('d M, Y');
|
|
return "<p class=\"text-success mb-0\">{$start}</p><p class=\"text-danger mb-0\">{$end}</p>";
|
|
})
|
|
|
|
->addColumn('status', '{!! $status_name !!}')
|
|
->addColumn('action', 'meeting::events.datatables.action-btn')
|
|
->rawColumns(['type', 'date', 'status', 'action'])
|
|
->toJson();
|
|
}
|
|
|
|
return view('meeting::events.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Event';
|
|
$data['editable'] = false;
|
|
$data['status'] = Event::STATUS;
|
|
return view('meeting::events.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'start_date' => 'required | date ',
|
|
'type' => 'required',
|
|
]);
|
|
if ($validator->fails()) {
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
|
|
return to_route('event.create')->withError($validator->errors()->all());
|
|
}
|
|
try {
|
|
if (!$request->has('status')) {
|
|
$request->merge(['status' => 11]);
|
|
}
|
|
|
|
$eventModel = $this->eventRepository->create($request->all());
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => $eventModel,
|
|
'msg' => 'Event Created',
|
|
]);
|
|
}
|
|
|
|
return redirect()->route('event.index')->withSucess('Event Created');
|
|
} catch (\Throwable $th) {
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $th->getMessage(),
|
|
]);
|
|
}
|
|
return redirect()->route('event.index')->withError($th->getMessage(), 422);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$data['title'] = 'View Event';
|
|
$data['item'] = $this->eventRepository->getEventById($id);
|
|
return view('meeting::events.show', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
try {
|
|
$data['title'] = 'Edit Event';
|
|
$data['editable'] = true;
|
|
$data['status'] = Event::STATUS;
|
|
|
|
$data['event'] = $this->eventRepository->getEventById($id);
|
|
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
|
|
return view('meeting::events.edit', $data);
|
|
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id): RedirectResponse
|
|
{
|
|
try {
|
|
$this->eventRepository->update($id, $request->except(['_method', '_token']));
|
|
|
|
return redirect()->route('event.index')->withSucess('Event Updated');
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->route('event.index')->withSucess($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$this->eventRepository->delete($id);
|
|
flash()->success('Event Deleted Successfully');
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
}
|
|
}
|