diff --git a/README.md b/README.md index 1cd0504..4f971f3 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,48 @@ After loading the plugin you need to migrate the tables for the plugin using: #### Templates -Before sending any notification, we need to register a template. An example about how to add templates: +Before sending any notification, we need to register a template. There are two ways to add templates: + +##### 1. Using NotificationManager utility class to create templates ```php + use Bakkerij\Notifier\Utility\NotificationManager; + + $notificationManager = NotificationManager::instance(); $notificationManager->addTemplate('newBlog', [ 'title' => 'New blog by :username', 'body' => ':username has posted a new blog named :name' ]); ``` +##### 2. Using config files to define templates + +Create a file in your config folder named `templates.php` and create your templates as below: + +```php + return [ + 'Notifier' => [ + 'templates' => [ + 'newBlog' => [ + 'title' => 'New blog by :username', + 'body' => ':username has posted a new blog named :name' + ], + 'newMessage' => [ + 'title' => 'New message from :username', + 'body' => ':username sent you a message on :timeago' + ] + ] + ] + ]; +``` + +Then load your file from your bootstrap.php file: + +```php + Configure::write('Notifier.config', ['templates']); +``` + + When adding a new template, you have to add a `title` and a `body`. Both are able to contain variables like `:username` and `:name`. Later on we will tell more about these variables. diff --git a/config/bootstrap.php b/config/bootstrap.php index a4dc4c1..54897ca 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -20,4 +20,8 @@ 'body' => ':body' ]); -Configure::write('Notifier.recipientLists', []); \ No newline at end of file +Configure::write('Notifier.recipientLists', []); + +collection((array)Configure::read('Notifier.config'))->each(function ($file) { + Configure::load($file); +}); \ No newline at end of file diff --git a/src/Model/Entity/Notification.php b/src/Model/Entity/Notification.php index 5da078e..2b6fca0 100644 --- a/src/Model/Entity/Notification.php +++ b/src/Model/Entity/Notification.php @@ -94,6 +94,7 @@ protected function _getTitle() return Text::insert($template['title'], $vars); } + return ''; } @@ -117,6 +118,7 @@ protected function _getBody() return Text::insert($template['body'], $vars); } + return ''; } @@ -132,6 +134,7 @@ protected function _getUnread() if ($this->_properties['state'] === 1) { return true; } + return false; } @@ -147,6 +150,7 @@ protected function _getRead() if ($this->_properties['state'] === 0) { return true; } + return false; } diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index ecdea55..5a630ca 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -43,6 +43,7 @@ public static function instance($manager = null) if (empty(static::$_generalManager)) { static::$_generalManager = new NotificationManager(); } + return static::$_generalManager; } @@ -203,6 +204,7 @@ public function getTemplate($name, $type = null) if ($type == 'body') { return $templates[$name]['body']; } + return $templates[$name]; } @@ -224,6 +226,7 @@ public function getTrackingId() for ($i = 0; $i < 10; $i++) { $trackingId .= $characters[rand(0, $charactersLength - 1)]; } + return $trackingId; } } diff --git a/tests/TestCase/Model/Table/NotificationsTableTest.php b/tests/TestCase/Model/Table/NotificationsTableTest.php index 7ac241c..9169ce1 100644 --- a/tests/TestCase/Model/Table/NotificationsTableTest.php +++ b/tests/TestCase/Model/Table/NotificationsTableTest.php @@ -23,7 +23,7 @@ */ class NotificationsTableTest extends TestCase { - + public $fixtures = [ 'plugin.bakkerij\Notifier.notifications', ];