reconstruction of the project
This commit is contained in:
+90
-106
@@ -7,7 +7,10 @@ use App\Models\BaseModel;
|
||||
class Message extends BaseModel
|
||||
{
|
||||
protected $table = 'messages';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
// CI: useTimestamps = false (manual date fields)
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'sender_id',
|
||||
'recipient_id',
|
||||
@@ -24,134 +27,115 @@ class Message extends BaseModel
|
||||
'school_year',
|
||||
];
|
||||
|
||||
public $timestamps = false; // Since you're manually handling date fields
|
||||
protected $casts = [
|
||||
'sender_id' => 'integer',
|
||||
'recipient_id' => 'integer',
|
||||
'read_status' => 'boolean',
|
||||
'message_number' => 'integer',
|
||||
|
||||
/**
|
||||
* Get unread messages for a specific recipient.
|
||||
*
|
||||
* @param int $recipientId
|
||||
* @return array
|
||||
*/
|
||||
public function getUnreadMessages($recipientId)
|
||||
'sent_datetime' => 'datetime',
|
||||
'read_datetime' => 'datetime',
|
||||
];
|
||||
|
||||
/* Optional relationships */
|
||||
public function sender()
|
||||
{
|
||||
return $this->where('recipient_id', $recipientId)
|
||||
return $this->belongsTo(User::class, 'sender_id');
|
||||
}
|
||||
|
||||
public function recipient()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'recipient_id');
|
||||
}
|
||||
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
public static function getUnreadMessages(int $recipientId)
|
||||
{
|
||||
return static::query()
|
||||
->where('recipient_id', $recipientId)
|
||||
->where('read_status', 0)
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a message as read and update the read_datetime.
|
||||
*
|
||||
* @param int $messageId
|
||||
* @return bool
|
||||
*/
|
||||
public function markAsRead($messageId)
|
||||
public static function markAsRead(int $messageId): bool
|
||||
{
|
||||
return $this->update($messageId, [
|
||||
'read_status' => 1,
|
||||
'read_datetime' => utc_now() // Set the current datetime when marked as read
|
||||
]);
|
||||
$affected = static::query()
|
||||
->whereKey($messageId)
|
||||
->update([
|
||||
'read_status' => 1,
|
||||
'read_datetime' => now(), // set app timezone (configure to UTC if desired)
|
||||
]);
|
||||
|
||||
return $affected > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages by priority.
|
||||
*
|
||||
* @param string $priority
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagesByPriority($priority = 'normal')
|
||||
public static function getMessagesByPriority(string $priority = 'normal')
|
||||
{
|
||||
return $this->where('priority', $priority)
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
return static::query()
|
||||
->where('priority', $priority)
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages by status.
|
||||
*
|
||||
* @param string $status
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagesByStatus($status = 'sent')
|
||||
public static function getMessagesByStatus(string $status = 'sent')
|
||||
{
|
||||
return $this->where('status', $status)
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
return static::query()
|
||||
->where('status', $status)
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get inbox messages for a user.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getInboxMessages($userId)
|
||||
public static function getInboxMessages(int $userId)
|
||||
{
|
||||
return $this->where('recipient_id', $userId)
|
||||
return static::query()
|
||||
->where('recipient_id', $userId)
|
||||
->where('status', 'received')
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sent messages for a user.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getSentMessages($userId)
|
||||
public static function getSentMessages(int $userId)
|
||||
{
|
||||
return $this->where('sender_id', $userId)
|
||||
->where('status', 'sent')
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get draft messages for a user.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getDraftMessages($userId)
|
||||
{
|
||||
return $this->where('sender_id', $userId)
|
||||
->where('status', 'draft')
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get trashed messages for a user.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getTrashedMessages($userId)
|
||||
{
|
||||
return $this->groupStart()
|
||||
return static::query()
|
||||
->where('sender_id', $userId)
|
||||
->orWhere('recipient_id', $userId)
|
||||
->groupEnd()
|
||||
->where('status', 'trashed')
|
||||
->orderBy('sent_datetime', 'DESC')
|
||||
->findAll();
|
||||
->where('status', 'sent')
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages for a specific semester and school year.
|
||||
*
|
||||
* @param string $semester
|
||||
* @param string|null $school_year
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagesBySemesterAndYear($semester, $school_year = null)
|
||||
public static function getDraftMessages(int $userId)
|
||||
{
|
||||
$builder = $this->where('semester', $semester);
|
||||
if ($school_year) {
|
||||
$builder->where('school_year', $school_year);
|
||||
return static::query()
|
||||
->where('sender_id', $userId)
|
||||
->where('status', 'draft')
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getTrashedMessages(int $userId)
|
||||
{
|
||||
return static::query()
|
||||
->where(function ($q) use ($userId) {
|
||||
$q->where('sender_id', $userId)
|
||||
->orWhere('recipient_id', $userId);
|
||||
})
|
||||
->where('status', 'trashed')
|
||||
->orderByDesc('sent_datetime')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getMessagesBySemesterAndYear(string $semester, ?string $schoolYear = null)
|
||||
{
|
||||
$q = static::query()->where('semester', $semester);
|
||||
|
||||
if (!empty($schoolYear)) {
|
||||
$q->where('school_year', $schoolYear);
|
||||
}
|
||||
return $builder->orderBy('sent_datetime', 'DESC')->findAll();
|
||||
|
||||
return $q->orderByDesc('sent_datetime')->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user