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

Deprecate methods #2

Open
wants to merge 17 commits into
base: 1.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Notifier plugin for CakePHP

[![Travis](https://img.shields.io/travis/bakkerij/notifier.svg?style=flat-square)](https://travis-ci.org/bakkerij/notifier)
[![Travis](https://img.shields.io/travis/bakkerij/notifier.svg?style=flat-square)](https://travis-ci.org/bakkerij/notifier)
[![Packagist](https://img.shields.io/packagist/dt/cakemanager/cakephp-notifier.svg?style=flat-square)](https://packagist.org/packages/bakkerij/notifier)
[![Packagist](https://img.shields.io/packagist/v/bakkerij/notifier.svg?style=flat-square)](https://packagist.org/packages/bakkerij/notifier)
[![Gitter](https://img.shields.io/gitter/room/bakkerij/notifier.js.svg?style=flat-square)](https://gitter.im/bakkerij/notifier)

This plugin allows you to integrate a simple notification system into your application.
This plugin allows you to integrate a simple notification system into your application.

## Installation

Expand Down Expand Up @@ -81,10 +81,10 @@ You can register them with:
```php
$notificationManager->addRecipientList('administrators', [1,2,3,4]);
```

Now we have created a list of recipients called `administrators`.

This can be used later on when we send a new notification:
This can be used later on when we send a new notification:

```php
$notificationManager->notify([
Expand All @@ -106,7 +106,7 @@ You can easily retrieve notifications via the `getNotifications` method. Some ex

// getting a list of all notifications of the user with id 2
$this->Notifier->getNotifications(2);

// getting a list of all unread notifications
$this->Notifier->allNotificationList(2, true);

Expand All @@ -124,7 +124,7 @@ Getting counts of read/unread notifications can be done via the `countNotificati

// getting a number of all notifications of the user with id 2
$this->Notifier->countNotifications(2);

// getting a number of all unread notifications
$this->Notifier->countNotificationList(2, true);

Expand Down Expand Up @@ -153,11 +153,11 @@ The following getters can be used at your notifications entity:
- `read` - Boolean if the notification is read yet.

Example:

```php
// returns true or false
$entity->get('unread');

// returns the full output like 'Bob Mulder has posted a new blog named My Great New Post'
$entity->get('body');
```
Expand Down Expand Up @@ -207,8 +207,8 @@ The component has the following methods available:
- `markAsRead`
- `notify`

## Keep in touch
## Credits

If you need some help or got ideas for this plugin, feel free to chat at [Gitter](https://gitter.im/bakkerij/notifier).
This plugin have been forked from [Norifier](https://github.com/bakkerij/notifier) originally developed by [bakkerij](https://github.com/bakkerij).
Thank you for their work and contributions to the open-source community.

Pull Requests are always more than welcome!
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "cakephp-plugin",
"require": {
"php": ">=5.4.16",
"cakephp/cakephp": "3.5.*"
"cakephp/cakephp": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand Down
177 changes: 158 additions & 19 deletions src/Controller/Component/NotifierComponent.php
steinkel marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
* @since 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Bakkerij\Notifier\Controller\Component;

use Bakkerij\Notifier\Model\Entity\Notification;
use Bakkerij\Notifier\Utility\NotificationManager;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\TableRegistry;

/**
Expand All @@ -40,6 +42,13 @@ class NotifierComponent extends Component
*/
private $Controller = null;

/**
* The controller.
*
* @var \Cake\ORM\Table
*/
private $table = null;

/**
* initialize
*
Expand All @@ -51,6 +60,7 @@ public function initialize(array $config)
parent::initialize($config);

$this->Controller = $this->_registry->getController();
$this->table = TableRegistry::getTableLocator()->get('Bakkerij/Notifier.Notifications');
}

/**
Expand Down Expand Up @@ -90,22 +100,148 @@ public function setController($controller)
* @param int|null $userId Id of the user.
* @param bool|null $state The state of notifications: `true` for unread, `false` for read, `null` for all.
* @return array
*
* @deprecated 1.3 use getReadNotifications or getUnreadNotifications instead.
*/
public function getNotifications($userId = null, $state = null)
{
if (!$userId) {
$stateCondition = [];
if (isset($state)) {
$stateCondition = ['whereConditions' => ['state' => $state]];
}

return $this->getNotificationsFactory($userId, $stateCondition);
}


/**
* getNotifications
*
* Returns a list of notifications.
*
* ### Examples
* ```
* // if the user is logged in, this is the way to get all notifications
* $this->Notifier->getNotifications();
*
* // for a specific user, use the first parameter for the user_id
* $this->Notifier->getNotifications(1);
*
*
* ```
* @param int $userId
* @param array $options {
* @type array $whereConditions Conditions used for filtering.
* @type string $order The order in which results should be returned.
* }
* * @return array
*/
public function getAllNotificationsBy($userId, $options = [])
{
if (array_key_exists('state', $options)) {
unset($options['state']);
}

return $this->getNotificationsFactory($userId, $options);
}

/**
* getNotifications
*
* Returns a list of notifications.
*
* ### Examples
* ```
* // if the user is logged in, this is the way to get all notifications
* $this->Notifier->getNotifications();
*
* // for a specific user, use the first parameter for the user_id
* $this->Notifier->getNotifications(1);
*
* // for and specific user you can add also ORM conditions for the where and order
*
* $this->Notifier->getNotifications(1, [whereConditions => ['']]);
*
* ```
* @param int $userId
* @param array $options {
* @type array $whereConditions Conditions used for filtering.
* @type string $order The order in which results should be returned.
* }
* * @return array
*/
public function getReadNotificationsBy($userId, $options = [])
{
$readCondition = ['whereConditions' => ['state' => Notification::READ_STATUS]];
$conditions = array_merge($options, $readCondition);

return $this->getNotificationsFactory($userId, $conditions);
}

/**
* getNotifications
*
* Returns a list of notifications.
*
* ### Examples
* ```
* // if the user is logged in, this is the way to get all notifications
* $this->Notifier->getNotifications();
*
* // for a specific user, use the first parameter for the user_id
* $this->Notifier->getNotifications(1);
*
*
* ```
* @param int $userId
* @param array $options {
* @type array $whereConditions Conditions used for filtering.
* @type string $order The order in which results should be returned.
* }
* @return array
*/
public function getUnReadNotificationsBy($userId, $options = [])
{
$unreadCondition = ['whereConditions' => ['state' => Notification::UNREAD_STATUS]];
$conditions = array_merge($options, $unreadCondition);

return $this->getNotificationsFactory($userId, $conditions);
}

/**
* @param int $userId
* @param array $options {
* @type array $whereConditions Conditions used for filtering.
* @type string $order The order in which results should be returned.
* @type string $state The state of the items to be processed.
* }
* @return array
*/
private function getNotificationsFactory($userId, $options = [])
{
if (!isset($userId)) {
$userId = $this->Controller->Auth->user('id');
}

$model = TableRegistry::get('Bakkerij/Notifier.Notifications');
$whereConditions = [
'Notifications.user_id' => $userId,
];

$query = $model->find()->where(['Notifications.user_id' => $userId])->order(['created' => 'desc']);
$order = ['created' => 'desc'];

if (!is_null($state)) {
$query->where(['Notifications.state' => $state]);
if (array_key_exists('whereConditions', $options)) {
$whereConditions = array_merge($whereConditions, $options['whereConditions']);
}

return $query->toArray();
if (array_key_exists('order', $options)) {
$order = array_merge($whereConditions, $options['order']);
}

return $this->table
->find()
->where($whereConditions)
->order($order)
->toArray();
}

/**
Expand Down Expand Up @@ -139,9 +275,7 @@ public function countNotifications($userId = null, $state = null)
$userId = $this->Controller->Auth->user('id');
}

$model = TableRegistry::get('Bakkerij/Notifier.Notifications');

$query = $model->find()->where(['Notifications.user_id' => $userId]);
$query = $this->table->find()->where(['Notifications.user_id' => $userId]);

if (!is_null($state)) {
$query->where(['Notifications.state' => $state]);
Expand All @@ -158,32 +292,37 @@ public function countNotifications($userId = null, $state = null)
*
* @param int $notificationId Id of the notification.
* @param int|null $user Id of the user. Else the id of the session will be taken.
* @return void
* @return void|false
*/
public function markAsRead($notificationId = null, $user = null)
{
if (!$user) {
$user = $this->Controller->Auth->user('id');
}

$model = TableRegistry::get('Bakkerij/Notifier.Notifications');

if (!$notificationId) {
$query = $model->find('all')->where([
$query = $this->table->find()->where([
'user_id' => $user,
'state' => 1
'state' => Notification::UNREAD_STATUS
]);
} else {
$query = $model->find('all')->where([
$query = $this->table->find()->where([
'user_id' => $user,
'id' => $notificationId

]);
}

foreach ($query as $item) {
$item->set('state', 0);
$model->save($item);
$notifications = [];
foreach ($query as $notification) {
$notification->set('state', Notification::READ_STATUS);
$notifications[] = $notification;
}

$savedNotifications = $this->table->saveMany($notifications);

if (!$savedNotifications) {
return false;
}
}

Expand Down
24 changes: 10 additions & 14 deletions src/Model/Entity/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class Notification extends Entity
{

const UNREAD_STATUS = 1;
const READ_STATUS = 0;
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
Expand Down Expand Up @@ -87,10 +89,10 @@ protected function _getTitle()
{
$templates = Configure::read('Notifier.templates');

if (array_key_exists($this->_properties['template'], $templates)) {
$template = $templates[$this->_properties['template']];
if (array_key_exists($this->get('template'), $templates)) {
$template = $templates[$this->get('template')];

$vars = json_decode($this->_properties['vars'], true);
$vars = json_decode($this->get('vars'), true);

return Text::insert($template['title'], $vars);
}
Expand All @@ -110,10 +112,10 @@ protected function _getBody()
{
$templates = Configure::read('Notifier.templates');

if (array_key_exists($this->_properties['template'], $templates)) {
$template = $templates[$this->_properties['template']];
if (array_key_exists($this->get('template'), $templates)) {
$template = $templates[$this->get('template')];

$vars = json_decode($this->_properties['vars'], true);
$vars = json_decode($this->get('vars'), true);

return Text::insert($template['body'], $vars);
}
Expand All @@ -129,10 +131,7 @@ protected function _getBody()
*/
protected function _getUnread()
{
if ($this->_properties['state'] === 1) {
return true;
}
return false;
return $this->get('state') == self::UNREAD_STATUS;
}

/**
Expand All @@ -144,10 +143,7 @@ protected function _getUnread()
*/
protected function _getRead()
{
if ($this->_properties['state'] === 0) {
return true;
}
return false;
return $this->get('state') == self::READ_STATUS;
}

/**
Expand Down
Loading