reconstruction of the project
This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CalendarEvent extends Model
|
||||
class CalendarEvent extends BaseModel
|
||||
{
|
||||
use HasFactory;
|
||||
protected $table = 'calendar_events';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
// ✅ CI uses created_at / updated_at
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'date',
|
||||
@@ -25,103 +28,129 @@ class CalendarEvent extends Model
|
||||
'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',
|
||||
// If your column is DATE use 'date', if it's DATETIME use 'datetime'
|
||||
'date' => 'date',
|
||||
'notify_parent' => 'boolean',
|
||||
'notify_admin' => 'boolean',
|
||||
'notify_teacher' => 'boolean',
|
||||
'no_school' => 'integer',
|
||||
];
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* GETTERS – Same behavior as CodeIgniter
|
||||
* ---------------------------------------------------------------------- */
|
||||
private static ?bool $hasEventTypeColumn = null;
|
||||
|
||||
public function getEvents(): array
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
public static function getEvents()
|
||||
{
|
||||
$events = $this->orderBy('date', 'ASC')->get()->toArray();
|
||||
return $this->withNoSchoolFlag($events);
|
||||
$events = static::query()->get()->toArray();
|
||||
return static::withNoSchoolFlag($events);
|
||||
}
|
||||
|
||||
public function getEventByDate($date): array
|
||||
public static function getEventByDate(string $date): array
|
||||
{
|
||||
$events = $this->whereDate('date', $date)->get()->toArray();
|
||||
return $this->withNoSchoolFlag($events);
|
||||
$events = static::query()->where('date', $date)->get()->toArray();
|
||||
return static::withNoSchoolFlag($events);
|
||||
}
|
||||
|
||||
public function getEventsBySchoolYear(string $schoolYear): array
|
||||
public static function addEvent(array $data): int
|
||||
{
|
||||
$events = $this->where('school_year', $schoolYear)
|
||||
->orderBy('date', 'ASC')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
return $this->withNoSchoolFlag($events);
|
||||
$row = static::create($data);
|
||||
return (int) $row->id;
|
||||
}
|
||||
|
||||
public function getEventsBySchoolYearAndSemester(string $schoolYear, ?string $semester = null): array
|
||||
public static function getEventsBySchoolYear(string $schoolYear): array
|
||||
{
|
||||
$query = $this->where('school_year', $schoolYear);
|
||||
$events = static::query()
|
||||
->where('school_year', $schoolYear)
|
||||
->orderBy('date', 'asc')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
if (!empty($semester)) {
|
||||
$query->where('semester', $semester);
|
||||
return static::withNoSchoolFlag($events);
|
||||
}
|
||||
|
||||
public static function getEventsBySchoolYearAndSemester(string $schoolYear, ?string $semester = null): array
|
||||
{
|
||||
$q = static::query()->where('school_year', $schoolYear);
|
||||
|
||||
if (is_string($semester) && $semester !== '') {
|
||||
$q->where('semester', $semester);
|
||||
}
|
||||
|
||||
$events = $query->orderBy('date', 'ASC')->get()->toArray();
|
||||
$events = $q->orderBy('date', 'asc')->get()->toArray();
|
||||
|
||||
return $this->withNoSchoolFlag($events);
|
||||
return static::withNoSchoolFlag($events);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* CREATE EVENT — Eloquent version of addEvent()
|
||||
* ---------------------------------------------------------------------- */
|
||||
public function addEvent(array $data)
|
||||
public static function supportsEventType(): bool
|
||||
{
|
||||
return $this->create($data);
|
||||
if (static::$hasEventTypeColumn === null) {
|
||||
static::$hasEventTypeColumn = static::columnExists((new static)->getTable(), 'event_type');
|
||||
}
|
||||
return (bool) static::$hasEventTypeColumn;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* UTILITIES — SAME LOGIC YOU HAD IN CI4
|
||||
* ---------------------------------------------------------------------- */
|
||||
/* =========================
|
||||
* Helpers (ported from CI)
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Adds computed no_school flag if missing.
|
||||
* Add computed no_school flag to each event without requiring DB schema changes.
|
||||
*/
|
||||
protected function withNoSchoolFlag(array $events): array
|
||||
protected static function withNoSchoolFlag(array $events): array
|
||||
{
|
||||
return array_map(function ($event) {
|
||||
$event = (array) $event;
|
||||
|
||||
// Respect DB value if present; otherwise infer heuristically
|
||||
if (array_key_exists('no_school', $event)) {
|
||||
$event['no_school'] = (int)$event['no_school'];
|
||||
$event['no_school'] = (int) ($event['no_school'] ?? 0);
|
||||
} else {
|
||||
$event['no_school'] = $this->isNoSchoolEvent($event) ? 1 : 0;
|
||||
$event['no_school'] = static::isNoSchoolEvent($event) ? 1 : 0;
|
||||
}
|
||||
|
||||
return $event;
|
||||
|
||||
}, $events);
|
||||
}
|
||||
|
||||
/**
|
||||
* Heuristic detection of no-school days from text.
|
||||
* Heuristic to determine "no school" days from event text.
|
||||
*/
|
||||
protected function isNoSchoolEvent(array $event): bool
|
||||
protected static function isNoSchoolEvent(array $event): bool
|
||||
{
|
||||
$title = strtolower($event['title'] ?? '');
|
||||
$desc = strtolower($event['description'] ?? '');
|
||||
$title = strtolower((string)($event['title'] ?? ''));
|
||||
$desc = strtolower((string)($event['description'] ?? ''));
|
||||
$text = $title . ' ' . $desc;
|
||||
|
||||
$keywords = ['no school', 'no-school', 'cancel', 'vacation', 'holiday', 'break'];
|
||||
|
||||
foreach ($keywords as $kw) {
|
||||
if ($kw !== '' && str_contains($text, $kw)) {
|
||||
if ($kw !== '' && strpos($text, $kw) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static function columnExists(string $table, string $column): bool
|
||||
{
|
||||
$db = DB::connection();
|
||||
if ($db->getDriverName() === 'sqlite') {
|
||||
return Schema::hasColumn($table, $column);
|
||||
}
|
||||
|
||||
$database = $db->getDatabaseName();
|
||||
|
||||
$count = $db->table('information_schema.columns')
|
||||
->where('table_schema', $database)
|
||||
->where('table_name', $table)
|
||||
->where('column_name', $column)
|
||||
->count();
|
||||
|
||||
return $count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user