128 lines
3.6 KiB
PHP
Executable File
128 lines
3.6 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class CalendarEvent extends Model
|
||
{
|
||
use HasFactory;
|
||
protected $table = 'calendar_events';
|
||
protected $primaryKey = 'id';
|
||
protected $fillable = [
|
||
'title',
|
||
'date',
|
||
'description',
|
||
'notify_parent',
|
||
'notify_admin',
|
||
'notify_teacher',
|
||
'no_school',
|
||
'semester',
|
||
'school_year',
|
||
'notification_sent',
|
||
'created_at',
|
||
'updated_at',
|
||
];
|
||
|
||
// Equivalent to CI4 timestamps
|
||
public $timestamps = true;
|
||
|
||
protected $casts = [
|
||
'date' => 'date',
|
||
'notify_parent' => 'boolean',
|
||
'notify_admin' => 'boolean',
|
||
'notify_teacher'=> 'boolean',
|
||
'no_school' => 'boolean',
|
||
];
|
||
|
||
/* ----------------------------------------------------------------------
|
||
* GETTERS – Same behavior as CodeIgniter
|
||
* ---------------------------------------------------------------------- */
|
||
|
||
public function getEvents(): array
|
||
{
|
||
$events = $this->orderBy('date', 'ASC')->get()->toArray();
|
||
return $this->withNoSchoolFlag($events);
|
||
}
|
||
|
||
public function getEventByDate($date): array
|
||
{
|
||
$events = $this->whereDate('date', $date)->get()->toArray();
|
||
return $this->withNoSchoolFlag($events);
|
||
}
|
||
|
||
public function getEventsBySchoolYear(string $schoolYear): array
|
||
{
|
||
$events = $this->where('school_year', $schoolYear)
|
||
->orderBy('date', 'ASC')
|
||
->get()
|
||
->toArray();
|
||
|
||
return $this->withNoSchoolFlag($events);
|
||
}
|
||
|
||
public function getEventsBySchoolYearAndSemester(string $schoolYear, ?string $semester = null): array
|
||
{
|
||
$query = $this->where('school_year', $schoolYear);
|
||
|
||
if (!empty($semester)) {
|
||
$query->where('semester', $semester);
|
||
}
|
||
|
||
$events = $query->orderBy('date', 'ASC')->get()->toArray();
|
||
|
||
return $this->withNoSchoolFlag($events);
|
||
}
|
||
|
||
/* ----------------------------------------------------------------------
|
||
* CREATE EVENT — Eloquent version of addEvent()
|
||
* ---------------------------------------------------------------------- */
|
||
public function addEvent(array $data)
|
||
{
|
||
return $this->create($data);
|
||
}
|
||
|
||
/* ----------------------------------------------------------------------
|
||
* UTILITIES — SAME LOGIC YOU HAD IN CI4
|
||
* ---------------------------------------------------------------------- */
|
||
|
||
/**
|
||
* Adds computed no_school flag if missing.
|
||
*/
|
||
protected function withNoSchoolFlag(array $events): array
|
||
{
|
||
return array_map(function ($event) {
|
||
|
||
if (array_key_exists('no_school', $event)) {
|
||
$event['no_school'] = (int)$event['no_school'];
|
||
} else {
|
||
$event['no_school'] = $this->isNoSchoolEvent($event) ? 1 : 0;
|
||
}
|
||
|
||
return $event;
|
||
|
||
}, $events);
|
||
}
|
||
|
||
/**
|
||
* Heuristic detection of no-school days from text.
|
||
*/
|
||
protected function isNoSchoolEvent(array $event): bool
|
||
{
|
||
$title = strtolower($event['title'] ?? '');
|
||
$desc = strtolower($event['description'] ?? '');
|
||
$text = $title . ' ' . $desc;
|
||
|
||
$keywords = ['no school', 'no-school', 'cancel', 'vacation', 'holiday', 'break'];
|
||
|
||
foreach ($keywords as $kw) {
|
||
if ($kw !== '' && str_contains($text, $kw)) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|