|
| 1 | +<?php namespace Tatter\Alerts; |
| 2 | + |
| 3 | +/*** |
| 4 | +* Name: Alerts |
| 5 | +* Author: Matthew Gatner |
| 6 | +* Contact: mgatner@tattersoftware.com |
| 7 | +* Created: 2019-02-13 |
| 8 | +* |
| 9 | +* Description: Lightweight user notification library for CodeIgniter 4 |
| 10 | +* |
| 11 | +* Requirements: |
| 12 | +* >= PHP 7.1 |
| 13 | +* >= CodeIgniter 4.0 |
| 14 | +* CodeIgniter's Session Library (loaded automatically) |
| 15 | +* |
| 16 | +* Configuration: |
| 17 | +* Use Config/Alerts.php to override default behavior |
| 18 | +* |
| 19 | +* @package CodeIgniter4-Alerts |
| 20 | +* @author Matthew Gatner |
| 21 | +* @link https://github.com/tattersoftware/codeigniter4-alerts |
| 22 | +* |
| 23 | +***/ |
| 24 | + |
| 25 | +use CodeIgniter\Config\BaseConfig; |
| 26 | +use CodeIgniter\Config\Services; |
| 27 | +use Tatter\Alerts\Exceptions\AlertsException; |
| 28 | + |
| 29 | +/*** CLASS ***/ |
| 30 | +class Alerts { |
| 31 | + |
| 32 | + /** |
| 33 | + * Our configuration instance. |
| 34 | + * |
| 35 | + * @var \Tatter\Alerts\Config\Alerts |
| 36 | + */ |
| 37 | + protected $config; |
| 38 | + |
| 39 | + /** |
| 40 | + * The view engine to render the alerts with. |
| 41 | + * |
| 42 | + * @var RendererInterface |
| 43 | + */ |
| 44 | + protected $view; |
| 45 | + |
| 46 | + /** |
| 47 | + * The active user session, for loading and storing alerts. |
| 48 | + * |
| 49 | + * @var \CodeIgniter\Session\Session |
| 50 | + */ |
| 51 | + protected $session; |
| 52 | + |
| 53 | + // initiate library, check for existing session |
| 54 | + public function __construct(BaseConfig $config, RendererInterface $view = null) |
| 55 | + { |
| 56 | + // save configuration |
| 57 | + $this->config = $config; |
| 58 | + |
| 59 | + // initiate the Session library |
| 60 | + $this->session = Services::session(); |
| 61 | + |
| 62 | + // verify renderer |
| 63 | + if ($view instanceof RendererInterface) |
| 64 | + $this->view = $view; |
| 65 | + else |
| 66 | + $this->view = Services::renderer(); |
| 67 | + |
| 68 | + // validations |
| 69 | + if (empty($this->config->template)) |
| 70 | + throw AlertsException::forInvalidTemplate(''); |
| 71 | + |
| 72 | + $locator = Services::locator(); |
| 73 | + if (! $locator->locateFile($this->config->template)) |
| 74 | + throw AlertsException::forMissingTemplateView($this->config->template); |
| 75 | + } |
| 76 | + |
| 77 | + // add a new alert to the queue |
| 78 | + public function add($class, $text) |
| 79 | + { |
| 80 | + $alert = [ |
| 81 | + 'class' => $class, |
| 82 | + 'text' => $text |
| 83 | + ]; |
| 84 | + |
| 85 | + // start the queue if it doesn't exist |
| 86 | + if (! $this->session->has($this->config->prefix . 'queue')) |
| 87 | + $this->session->set($this->config->prefix . 'queue', [$alert]); |
| 88 | + |
| 89 | + // push onto the queue if it was already there |
| 90 | + else |
| 91 | + $this->session->push($this->config->prefix . 'queue', [$alert]); |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // clears the queue and returns template formatted alerts |
| 97 | + public function display() |
| 98 | + { |
| 99 | + // get any alerts |
| 100 | + $alerts = $this->session->get($this->config->prefix . 'queue') ?? [ ]; |
| 101 | + |
| 102 | + // clear alerts |
| 103 | + $this->session->remove($this->config->prefix . 'queue'); |
| 104 | + |
| 105 | + if (empty($alerts)) |
| 106 | + return; |
| 107 | + |
| 108 | + // render the specified view template |
| 109 | + return $this->view->setVar('prefix', $this->config->prefix) |
| 110 | + ->setVar('alerts', $alerts) |
| 111 | + ->render($this->config->template); |
| 112 | + } |
| 113 | + |
| 114 | + // returns default CSS as inline style sheet |
| 115 | + // should be injected into <head> |
| 116 | + public function css() |
| 117 | + { |
| 118 | + return $this->view->setVar('prefix', $this->config->prefix)->render("Tatter\Alerts\Views\css"); |
| 119 | + } |
| 120 | +} |
0 commit comments