| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace OpenMind\System;
- use DB\SQL\Mapper;
- class Notifications extends Mapper
- {
- public const FB_GENERIC = 0x01;
- public const FB_IMPORT = 0x02;
- public const FB_ADMIN = 0x03;
- public const FB_NOTALIVE = 0x0B;
- /**
- * If you need custom notification or to simply send a command to client use
- * FB_CUSTOM_NOTIFICATION+xxx where xxx is a number that identify your notification.
- */
- public const FB_CUSTOM_NOTIFICATION = 0x4000;
- public const FB_REFRESH_FILTERS = self::FB_CUSTOM_NOTIFICATION + 1;
- private const TABLE_NAME = 'notifications';
- private const CODE_CONDITION = 'code=?';
- /**
- * Initialize the notification mapper.
- *
- * @side-effects Connects the mapper to the configured database.
- */
- public function __construct()
- {
- global $f3;
- parent::__construct($f3->get('DB'), self::TABLE_NAME);
- }
- /**
- * Remove all notifications matching the supplied code.
- *
- * @param int $code Notification code to remove from the database.
- * @return void
- * @side-effects Deletes matching notification records.
- */
- public function Remove(int $code)
- {
- $this->reset();
- $this->erase([self::CODE_CONDITION, $code]);
- }
- /**
- * Remove every notification from the table.
- *
- * @return void
- * @side-effects Truncates the notifications table.
- */
- public function emptyAll()
- {
- global $f3;
- $f3->get('DB')->exec('TRUNCATE TABLE ' . self::TABLE_NAME);
- }
- /**
- * Send a custom notification to the system.
- *
- * @param int $code Notification code, such as FB_GENERIC or FB_IMPORT.
- * @param string|null $caption Notification caption.
- * @param mixed $link Optional notification link.
- * @return void
- * @side-effects Persists the notification when the code is allowed.
- */
- public function notify(int $code, string $caption = null, $link = null)
- {
- if ($code === $this::FB_GENERIC || $this->check($code)) {
- $this->reset();
- $this->code = $code;
- $this->caption = $caption;
- $this->link = $link;
- $this->save();
- }
- }
- /**
- * Check whether a notification with the supplied code exists.
- *
- * @param int $code Notification code to check.
- * @return bool Whether a matching notification exists.
- * @side-effects Loads matching records into the mapper.
- */
- public function check(int $code)
- {
- $this->load([self::CODE_CONDITION, $code]);
- return $this->dry();
- }
- }
|