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 all 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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,17 @@ The `Bakkerij/Notifier.Notifier` component can be used in Controllers:

The component has the following methods available:

- `getNotifications`
- `getNotifications` (deprecated)
- `getAllNotificationsBy`
- `getReadNotificationsBy`
- `getUnReadNotificationsBy`
- `countNotifications`
- `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": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand Down
Binary file added config/Migrations/schema-dump-default.lock
Binary file not shown.
198 changes: 172 additions & 26 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,17 +42,25 @@ class NotifierComponent extends Component
*/
private $Controller = null;

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

/**
* initialize
*
* @param array $config Config.
* @return void
*/
public function initialize(array $config)
public function initialize(array $config): void
{
parent::initialize($config);

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

/**
Expand All @@ -61,7 +71,7 @@ public function initialize(array $config)
* @param \Cake\Controller\Controller $controller Controller.
* @return void
*/
public function setController($controller)
public function setController($controller): void
{
$this->Controller = $controller;
}
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)
public function getNotifications($userId = null, $state = null): array
{
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 = []): array
{
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 = []): array
{
$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 = []): array
{
$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 = []): array
{
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 @@ -133,15 +269,13 @@ public function getNotifications($userId = null, $state = null)
* @param bool|null $state The state of notifications: `true` for unread, `false` for read, `null` for all.
* @return int
*/
public function countNotifications($userId = null, $state = null)
public function countNotifications(?int $userId = null, ?int $state = null): int
{
if (!$userId) {
$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,33 +292,40 @@ 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)
public function markAsRead($notificationId = null, $user = null): bool
{
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;
}

return true;
}

/**
Expand Down Expand Up @@ -214,8 +355,13 @@ public function markAsRead($notificationId = null, $user = null)
* @param array $data Data with options.
* @return string
*/
public function notify($data)
public function notify($data): string
{
return NotificationManager::instance()->notify($data);
$notification = NotificationManager::instance()->notify($data);
if (!$notification) {
$this->getController()->Flash->error(__d('bakkerij/notifier', 'An error occurred sending the notifications'));
}

return $notification;
}
}
Loading