85 lines
2.1 KiB
PHP
85 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class NotificationModel extends Model
|
|
{
|
|
protected $table = 'notifications';
|
|
protected $primaryKey = 'id';
|
|
protected $allowedFields = [
|
|
'title',
|
|
'message',
|
|
'target_group',
|
|
'delivery_channels',
|
|
'priority',
|
|
'status',
|
|
'action_url',
|
|
'attachment_path',
|
|
'semester',
|
|
'school_year',
|
|
'scheduled_at'
|
|
];
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
protected $useSoftDeletes = true;
|
|
|
|
/**
|
|
* Get active (non-deleted, non-expired) notifications
|
|
*/
|
|
public function getActiveNotifications($targetGroup = null)
|
|
{
|
|
$builder = $this->where('scheduled_at <= NOW()')
|
|
->where('(expires_at IS NULL OR expires_at > NOW())');
|
|
|
|
if (!empty($targetGroup)) {
|
|
$builder->where('target_group', $targetGroup);
|
|
}
|
|
|
|
return $builder->orderBy('priority', 'DESC')
|
|
->orderBy('scheduled_at', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Get soft-deleted (archived) notifications
|
|
*/
|
|
public function getDeletedNotifications()
|
|
{
|
|
return $this->onlyDeleted()
|
|
->orderBy('deleted_at', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Get all notifications including soft-deleted
|
|
*/
|
|
public function getAllNotificationsWithDeleted()
|
|
{
|
|
return $this->withDeleted()
|
|
->orderBy('created_at', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Restore a soft-deleted notification by ID
|
|
*/
|
|
public function restoreNotification($id)
|
|
{
|
|
return $this->update($id, ['deleted_at' => null]);
|
|
}
|
|
|
|
/**
|
|
* Cleanup expired notifications (optional for cron job use)
|
|
*/
|
|
public function deleteExpiredNotifications()
|
|
{
|
|
return $this->where('expires_at IS NOT NULL')
|
|
->where('expires_at < NOW()')
|
|
->delete();
|
|
}
|
|
}
|