Diff #4 - trunk/src/Models/Alert.php
1,952 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: Alert.php
--- Alert.php (nonexistent) +++ Alert.php (revision 4) @@ -0,0 +1,91 @@ +<?php +declare(strict_types = 1); + +namespace App\Webapp\Models; + +use Apex\Svc{App, Db}; +use Apex\App\Adapters\ArmorAdapter; +use Apex\App\Base\Model\MagicModel; +use DateTime; + +/** + * Alert Model + */ +class Alert extends MagicModel +{ + + /** + * Database table + * + * @var string + */ + protected static string $dbtable = 'alerts'; + + #[Inject(App::class)] + protected App $app; + + #[Inject(Db::class)] + protected Db $db; + + #[Inject(ArmorAdapter::class)] + private ArmorAdapter $armor_adapter; + + /** + * Constructor + */ + public function __construct( + protected int $id, + protected bool $is_read = false, + protected string $type = 'alert', + protected string $uuid = '', + protected string $sender = '', + protected string $badge = '', + protected string $url = '', + protected string $title = '', + protected string $contents = '', + protected ?DateTime $created_at = null, + protected ?DateTime $read_at = null + ) { + + } + + /** + * Mark read + */ + public function markRead():void + { + + // Check + if ($this->is_read === true || $this->uuid != $this->app->getUuid()) { + return; + } + + // Update record + $this->is_read = true; + $this->save(); + } + + /** + * To display array + */ + public function toDisplayArray():array + { + + // Get array + $vars = $this->toArray(); + $to = $this->armor_adapter->getUuid($this->db, $vars['uuid']); + $sender = $this->armor_adapter->getUuid($this->db, $vars['sender']); + + // Format + $vars['recipient'] = $to->getFullName() . ' (' . $to->getUsername() . ')'; + $vars['sender'] = $sender->getFullName() . ' (' . $sender->getUsername() . ')'; + $vars['created_at'] = $this->convert->date($vars['created_at'], true); + + + // Return + return $vars; + } + + +} +
Full Code
<?php declare(strict_types = 1);
namespace App\Webapp\Models;
use Apex\Svc{App, Db}; use Apex\App\Adapters\ArmorAdapter; use Apex\App\Base\Model\MagicModel; use DateTime;
/** * Alert Model */ class Alert extends MagicModel {
/**
* Database table
*
* @var string
*/
protected static string $dbtable = 'alerts';
#[Inject(App::class)]
protected App $app;
#[Inject(Db::class)]
protected Db $db;
#[Inject(ArmorAdapter::class)]
private ArmorAdapter $armor_adapter;
/**
* Constructor
*/
public function __construct(
protected int $id,
protected bool $is_read = false,
protected string $type = 'alert',
protected string $uuid = '',
protected string $sender = '',
protected string $badge = '',
protected string $url = '',
protected string $title = '',
protected string $contents = '',
protected ?DateTime $created_at = null,
protected ?DateTime $read_at = null
) {
}
/**
* Mark read
*/
public function markRead():void
{
// Check
if ($this->is_read === true || $this->uuid != $this->app->getUuid()) {
return;
}
// Update record
$this->is_read = true;
$this->save();
}
/**
* To display array
*/
public function toDisplayArray():array
{
// Get array
$vars = $this->toArray();
$to = $this->armor_adapter->getUuid($this->db, $vars['uuid']);
$sender = $this->armor_adapter->getUuid($this->db, $vars['sender']);
// Format
$vars['recipient'] = $to->getFullName() . ' (' . $to->getUsername() . ')';
$vars['sender'] = $sender->getFullName() . ' (' . $sender->getUsername() . ')';
$vars['created_at'] = $this->convert->date($vars['created_at'], true);
// Return
return $vars;
}
}