Dismissible.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Message\Notice; |
| 6 | |
| 7 | use AC\Ajax\Handler; |
| 8 | use AC\Ajax\NullHandler; |
| 9 | use AC\Asset\Script; |
| 10 | use AC\Message\Notice; |
| 11 | use AC\View; |
| 12 | |
| 13 | class Dismissible extends Notice |
| 14 | { |
| 15 | protected Handler $handler; |
| 16 | |
| 17 | public function __construct(string $message, ?Handler $handler = null, ?string $type = null) |
| 18 | { |
| 19 | parent::__construct($message, $type); |
| 20 | |
| 21 | $this->handler = $handler ?? new NullHandler(); |
| 22 | } |
| 23 | |
| 24 | public function render(): string |
| 25 | { |
| 26 | $data = [ |
| 27 | 'message' => $this->message, |
| 28 | 'type' => $this->type, |
| 29 | 'id' => $this->id, |
| 30 | 'dismissible_callback' => $this->handler->get_params(), |
| 31 | ]; |
| 32 | |
| 33 | $view = new View($data); |
| 34 | $view->set_template('message/notice/dismissible'); |
| 35 | |
| 36 | return $view->render(); |
| 37 | } |
| 38 | |
| 39 | public function enqueue_scripts(): void |
| 40 | { |
| 41 | parent::enqueue_scripts(); |
| 42 | |
| 43 | $script = new Script('ac-message'); |
| 44 | $script->enqueue(); |
| 45 | } |
| 46 | |
| 47 | } |
| 48 |