Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds templates config file for managing templates #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 5 additions & 1 deletion config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
'body' => ':body'
]);

Configure::write('Notifier.recipientLists', []);
Configure::write('Notifier.recipientLists', []);

collection((array)Configure::read('Notifier.config'))->each(function ($file) {
Configure::load($file);
});
4 changes: 4 additions & 0 deletions src/Model/Entity/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected function _getTitle()

return Text::insert($template['title'], $vars);
}

return '';
}

Expand All @@ -117,6 +118,7 @@ protected function _getBody()

return Text::insert($template['body'], $vars);
}

return '';
}

Expand All @@ -132,6 +134,7 @@ protected function _getUnread()
if ($this->_properties['state'] === 1) {
return true;
}

return false;
}

Expand All @@ -147,6 +150,7 @@ protected function _getRead()
if ($this->_properties['state'] === 0) {
return true;
}

return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Utility/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function instance($manager = null)
if (empty(static::$_generalManager)) {
static::$_generalManager = new NotificationManager();
}

return static::$_generalManager;
}

Expand Down Expand Up @@ -203,6 +204,7 @@ public function getTemplate($name, $type = null)
if ($type == 'body') {
return $templates[$name]['body'];
}

return $templates[$name];
}

Expand All @@ -224,6 +226,7 @@ public function getTrackingId()
for ($i = 0; $i < 10; $i++) {
$trackingId .= $characters[rand(0, $charactersLength - 1)];
}

return $trackingId;
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Model/Table/NotificationsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class NotificationsTableTest extends TestCase
{

public $fixtures = [
'plugin.bakkerij\Notifier.notifications',
];
Expand Down