StocksNew/Modules/Admin/app/Http/Controllers/CalendarController.php

111 lines
3.4 KiB
PHP
Raw Normal View History

2024-08-27 12:03:06 +00:00
<?php
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Admin\Models\Event;
use Modules\Admin\Models\Holiday;
use Modules\Admin\Repositories\EventRepository;
use Modules\Meeting\Models\Meeting;
class CalendarController extends Controller
{
private $eventRepository;
public function __construct(EventRepository $eventRepository)
{
$this->eventRepository = $eventRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'Calendar';
$filters['start_date'] = now()->toDateString();
$data['events'] = $this->eventRepository->findAll($filters);
return view('admin::calendars.index', $data);
}
public function calendarByAjax(Request $request)
{
$filters['start_date'] = $request->start;
$filters['end_date'] = $request->end;
$events = Event::when($filters, function ($query) use ($filters) {
if (isset($filters["start_date"])) {
$query->whereDate("start_date", ">=", $filters["start_date"]);
}
if (isset($filters["end_date"])) {
$query->whereDate("end_date", "<=", $filters["end_date"]);
}
})->get();
$list = [];
foreach ($events as $event) {
$list[] = [
"id" => $event->event_id,
"title" => $event->title,
"type" => 'event',
"start" => $event->start_date,
"end" => $event->end_date,
"className" => "bg-info-subtle",
"desc" => $event->description,
"location" => $event->location,
];
}
$meetings = Meeting::when($filters, function ($query) use ($filters) {
if (isset($filters["start_date"])) {
$query->whereDate("date", ">=", $filters["start_date"]);
}
if (isset($filters["end_date"])) {
$query->whereDate("date", "<=", $filters["end_date"]);
}
})->get();
foreach ($meetings as $meeting) {
$list[] = [
"id" => $meeting->meeting_id,
"title" => $meeting->title,
"type" => 'meeting',
"start" => $meeting->date,
"className" => "bg-warning-subtle",
"location" => $meeting->location,
"desc" => $event->description,
];
}
$holidays = Holiday::when($filters, function ($query) use ($filters) {
if (isset($filters["start_date"])) {
$query->whereDate("start_date", ">=", $filters["start_date"]);
}
if (isset($filters["end_date"])) {
$query->whereDate("end_date", "<=", $filters["end_date"]);
}
})->get();
foreach ($holidays as $holiday) {
$list[] = [
"id" => $holiday->holiday_id,
"title" => $holiday->title,
"type" => 'holiday',
"start" => $holiday->start_date,
"end" => $holiday->end_date,
"className" => "bg-danger",
"overlap" => false,
"display" => 'background',
"color" => '#ff9f89',
"desc" => $event->description,
];
}
return $list;
}
}