From e37d4975bc0ecbcfa5b417d43ebed75c1bfcd6b7 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sat, 17 Feb 2024 00:30:15 +0100 Subject: [PATCH 01/15] created factory method for notifications, replace depreacted functions --- .../Component/NotifierComponent.php | 160 ++++++++++++++++-- src/Utility/NotificationManager.php | 45 +++-- 2 files changed, 176 insertions(+), 29 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index 8a070b9..c0879b9 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -12,11 +12,11 @@ * @since 1.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ + namespace Bakkerij\Notifier\Controller\Component; use Bakkerij\Notifier\Utility\NotificationManager; use Cake\Controller\Component; -use Cake\Core\Configure; use Cake\ORM\TableRegistry; /** @@ -40,6 +40,13 @@ class NotifierComponent extends Component */ private $Controller = null; + /** + * The controller. + * + * @var \Cake\ORM\Table + */ + private $table = null; + /** * initialize * @@ -51,6 +58,7 @@ public function initialize(array $config) parent::initialize($config); $this->Controller = $this->_registry->getController(); + $this->table = TableRegistry::getTableLocator()->get('Bakkerij/Notifier.Notifications'); } /** @@ -90,22 +98,150 @@ 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 + * TODO add version in deprecated + * @deprecated 1 use getReadNotifications or getUnreadNotifications instead. */ public function getNotifications($userId = null, $state = null) { - if (!$userId) { + $stateCondition = []; + if (isset($state)) { + $stateCondition = ['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, $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 getReadNotificationsBy($userId, $options) + { + $readCondition = ['state' => 0]; + $conditions = array_merge($options, $unreadCondition); + + 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 = ['state' => 1]; + $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('state', $options)) { + $whereConditions = array_merge($whereConditions, $options['state']); + } + + if (array_key_exists('whereConditions', $options)) { + $whereConditions = array_merge($whereConditions, $options['whereConditions']); + } + + if (array_key_exists('order', $options)) { + $order = array_merge($whereConditions, $options['order']); } - return $query->toArray(); + return $this->table + ->find() + ->where($whereConditions) + ->order() + ->toArray(); } /** @@ -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]); @@ -166,15 +300,13 @@ public function markAsRead($notificationId = null, $user = null) $user = $this->Controller->Auth->user('id'); } - $model = TableRegistry::get('Bakkerij/Notifier.Notifications'); - if (!$notificationId) { - $query = $model->find('all')->where([ + $query = $this->table->find('all')->where([ 'user_id' => $user, 'state' => 1 ]); } else { - $query = $model->find('all')->where([ + $query = $this->table->find('all')->where([ 'user_id' => $user, 'id' => $notificationId diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index ecdea55..6c8b6a5 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -12,12 +12,11 @@ * @since 1.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ + namespace Bakkerij\Notifier\Utility; use Cake\Core\Configure; -use Cake\Core\Plugin; use Cake\ORM\TableRegistry; -use Cake\Utility\Text; /** * Notifier component @@ -43,6 +42,7 @@ public static function instance($manager = null) if (empty(static::$_generalManager)) { static::$_generalManager = new NotificationManager(); } + return static::$_generalManager; } @@ -75,7 +75,7 @@ public static function instance($manager = null) */ public function notify($data) { - $model = TableRegistry::get('Bakkerij/Notifier.Notifications'); + $model = TableRegistry::getTableLocator()->get('Bakkerij/Notifier.Notifications'); $_data = [ 'users' => [], @@ -87,26 +87,41 @@ public function notify($data) $data = array_merge($_data, $data); - foreach ((array)$data['recipientLists'] as $recipientList) { - $list = (array)$this->getRecipientList($recipientList); - $data['users'] = array_merge($data['users'], $list); - } + $data['users'] = $this->mergeRecipientList($data); - foreach ((array)$data['users'] as $user) { - $entity = $model->newEntity(); - $entity->set('template', $data['template']); - $entity->set('tracking_id', $data['tracking_id']); - $entity->set('vars', $data['vars']); - $entity->set('state', 1); - $entity->set('user_id', $user); + $commonData = [ + 'template' => $data['template'], + 'tracking_id' => $data['tracking_id'], + 'vars' => $data['vars'], + 'state' => 1 + ]; - $model->save($entity); + $entities = []; + foreach ((array)$data['users'] as $userId) { + $entities[] = array_merge($commonData, ['user_id' => $userId]); } + $entity = $model->newEntities(); + $model->saveMany($entity); + return $data['tracking_id']; } + /** + * @param array $data + * @return array + */ + private function mergeRecipientList($data) { + $users = $data['users']; + foreach ((array)$data['recipientLists'] as $recipientList) { + $list = (array)$this->getRecipientList($recipientList); + $users = array_merge($users, $list); + } + + return $users; + } + /** * addRecipientList * From c9d9e93ec263e9fc789f092d4c3b8ad80a769b18 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sat, 17 Feb 2024 00:44:00 +0100 Subject: [PATCH 02/15] version suggested --- src/Controller/Component/NotifierComponent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index c0879b9..c4ce3bd 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -98,8 +98,8 @@ 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 - * TODO add version in deprecated - * @deprecated 1 use getReadNotifications or getUnreadNotifications instead. + * + * @deprecated 1.3 use getReadNotifications or getUnreadNotifications instead. */ public function getNotifications($userId = null, $state = null) { From 97fc268358d83c5026bb5b9f50f85c1ab8e1d960 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sat, 17 Feb 2024 01:02:18 +0100 Subject: [PATCH 03/15] created constants --- src/Controller/Component/NotifierComponent.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index c4ce3bd..dea0a11 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -24,6 +24,8 @@ */ class NotifierComponent extends Component { + const UNREAD_STATUS = 1; + const READ_STATUS = 0; /** * Default configuration. * @@ -157,6 +159,9 @@ public function getAllNotificationsBy($userId, $options) * // 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 @@ -168,7 +173,7 @@ public function getAllNotificationsBy($userId, $options) */ public function getReadNotificationsBy($userId, $options) { - $readCondition = ['state' => 0]; + $readCondition = ['whereConditions' => ['state' => self::READ_STATUS]]; $conditions = array_merge($options, $unreadCondition); return $this->getNotificationsFactory($userId, $conditions); @@ -198,7 +203,7 @@ public function getReadNotificationsBy($userId, $options) */ public function getUnReadNotificationsBy($userId, $options) { - $unreadCondition = ['state' => 1]; + $readCondition = ['whereConditions' => ['state' => self::UNREAD_STATUS]]; $conditions = array_merge($options, $unreadCondition); return $this->getNotificationsFactory($userId, $conditions); @@ -225,10 +230,6 @@ private function getNotificationsFactory($userId, $options) $order = ['created' => 'desc']; - if (array_key_exists('state', $options)) { - $whereConditions = array_merge($whereConditions, $options['state']); - } - if (array_key_exists('whereConditions', $options)) { $whereConditions = array_merge($whereConditions, $options['whereConditions']); } @@ -303,7 +304,7 @@ public function markAsRead($notificationId = null, $user = null) if (!$notificationId) { $query = $this->table->find('all')->where([ 'user_id' => $user, - 'state' => 1 + 'state' => self::UNREAD_STATUS ]); } else { $query = $this->table->find('all')->where([ @@ -314,7 +315,7 @@ public function markAsRead($notificationId = null, $user = null) } foreach ($query as $item) { - $item->set('state', 0); + $item->set('state', self::READ_STATUS); $model->save($item); } } From 66c55be7b33ea7c75372e0e27e2e448a727f9400 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sat, 17 Feb 2024 15:51:37 +0100 Subject: [PATCH 04/15] add save many for performance and move status to entity --- .../Component/NotifierComponent.php | 15 ++++++------ src/Model/Entity/Notification.php | 24 ++++++++----------- src/Model/Table/NotificationsTable.php | 6 ++--- src/Utility/NotificationManager.php | 23 ++++++++---------- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index dea0a11..a8e7ab6 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -15,6 +15,7 @@ namespace Bakkerij\Notifier\Controller\Component; +use Bakkerij\Notifier\Model\Entity\Notification; use Bakkerij\Notifier\Utility\NotificationManager; use Cake\Controller\Component; use Cake\ORM\TableRegistry; @@ -24,8 +25,6 @@ */ class NotifierComponent extends Component { - const UNREAD_STATUS = 1; - const READ_STATUS = 0; /** * Default configuration. * @@ -173,8 +172,8 @@ public function getAllNotificationsBy($userId, $options) */ public function getReadNotificationsBy($userId, $options) { - $readCondition = ['whereConditions' => ['state' => self::READ_STATUS]]; - $conditions = array_merge($options, $unreadCondition); + $readCondition = ['whereConditions' => ['state' => Notification::READ_STATUS]]; + $conditions = array_merge($options, $readCondition); return $this->getNotificationsFactory($userId, $conditions); } @@ -203,7 +202,7 @@ public function getReadNotificationsBy($userId, $options) */ public function getUnReadNotificationsBy($userId, $options) { - $readCondition = ['whereConditions' => ['state' => self::UNREAD_STATUS]]; + $unreadCondition = ['whereConditions' => ['state' => Notification::UNREAD_STATUS]]; $conditions = array_merge($options, $unreadCondition); return $this->getNotificationsFactory($userId, $conditions); @@ -304,7 +303,7 @@ public function markAsRead($notificationId = null, $user = null) if (!$notificationId) { $query = $this->table->find('all')->where([ 'user_id' => $user, - 'state' => self::UNREAD_STATUS + 'state' => Notification::UNREAD_STATUS ]); } else { $query = $this->table->find('all')->where([ @@ -315,8 +314,8 @@ public function markAsRead($notificationId = null, $user = null) } foreach ($query as $item) { - $item->set('state', self::READ_STATUS); - $model->save($item); + $item->set('state', Notification::READ_STATUS); + $this->table->save($item); } } diff --git a/src/Model/Entity/Notification.php b/src/Model/Entity/Notification.php index 5da078e..d6c6788 100644 --- a/src/Model/Entity/Notification.php +++ b/src/Model/Entity/Notification.php @@ -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(). * @@ -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); } @@ -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); } @@ -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; } /** @@ -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; } /** diff --git a/src/Model/Table/NotificationsTable.php b/src/Model/Table/NotificationsTable.php index 4620429..cb80fff 100644 --- a/src/Model/Table/NotificationsTable.php +++ b/src/Model/Table/NotificationsTable.php @@ -39,9 +39,9 @@ class NotificationsTable extends Table */ public function initialize(array $config) { - $this->table('notifications'); - $this->displayField('title'); - $this->primaryKey('id'); + $this->setTable('notifications'); + $this->setDisplayField('title'); + $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); } diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index 6c8b6a5..7aeda49 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -15,6 +15,7 @@ namespace Bakkerij\Notifier\Utility; +use Bakkerij\Notifier\Model\Entity\Notification; use Cake\Core\Configure; use Cake\ORM\TableRegistry; @@ -82,28 +83,23 @@ public function notify($data) 'recipientLists' => [], 'template' => 'default', 'vars' => [], - 'tracking_id' => $this->getTrackingId() + 'tracking_id' => $this->getTrackingId(), + 'state' => Notification::UNREAD_STATUS, ]; $data = array_merge($_data, $data); $data['users'] = $this->mergeRecipientList($data); - - $commonData = [ - 'template' => $data['template'], - 'tracking_id' => $data['tracking_id'], - 'vars' => $data['vars'], - 'state' => 1 - ]; - $entities = []; foreach ((array)$data['users'] as $userId) { - $entities[] = array_merge($commonData, ['user_id' => $userId]); + $finalData = array_merge($data, ['user_id' => $userId]); + $entity = $model->newEntity($finalData); + $entity->set('vars', $data['vars']); + $entities[] = $entity; } - $entity = $model->newEntities(); - $model->saveMany($entity); + $model->saveMany($entities); return $data['tracking_id']; } @@ -112,7 +108,8 @@ public function notify($data) * @param array $data * @return array */ - private function mergeRecipientList($data) { + private function mergeRecipientList($data) + { $users = $data['users']; foreach ((array)$data['recipientLists'] as $recipientList) { $list = (array)$this->getRecipientList($recipientList); From a4d0a8690f458cee87c03bd316fb4c65d3498e4e Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sat, 17 Feb 2024 23:39:25 +0100 Subject: [PATCH 05/15] run scripts update 4 --- composer.json | 2 +- config/Migrations/schema-dump-default.lock | Bin 0 -> 2086 bytes src/Model/Entity/Notification.php | 13 +++++++------ .../Component/NotifierComponentTest.php | 4 ++-- .../Model/Table/NotificationsTableTest.php | 4 ++-- .../TestCase/Utility/NotificationManagerTest.php | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 config/Migrations/schema-dump-default.lock diff --git a/composer.json b/composer.json index d06c2c4..62e4470 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "cakephp-plugin", "require": { "php": ">=5.4.16", - "cakephp/cakephp": "3.5.*" + "cakephp/cakephp": "^4.0" }, "require-dev": { "phpunit/phpunit": "*", diff --git a/config/Migrations/schema-dump-default.lock b/config/Migrations/schema-dump-default.lock new file mode 100644 index 0000000000000000000000000000000000000000..5807e8dc194355a665a2d41b14177e9e87593b7f GIT binary patch literal 2086 zcmd5-v2NQi5Y4x6>}1zX`6xl=_ zd3^79_ny&neQ5N2p_Q+q-Pz1UTlq$T5N7OSyT*Mr|f_$1^nXLi>Q>8z$f^hxvPO;1&!75;Dgk2}%2) z7jwj-EoH1Pw-8SRQoTO1uXcAgnPrtj(moO>*1#A{_5?}j`&KkCsn&-aHq`SZ->l9* zOg)xwdAX!n@kE0#ih2)12#TH5baLNIOFGWuY{QU@BWdf29H*@Y5JL}4qLar+;3R~t z&uLF0j5B@U0MM}AWiU!SCP+(AoD^{CU`rF8xj>7lqy38tk;Dse+=_?weu#^bNilG% z!||BFs_NdU4IK0!n_-XEpJwdd`|9o+PdjW2_rOKOwl4SxXNE$Fjlget('state') == self::UNREAD_STATUS; } @@ -141,7 +142,7 @@ protected function _getUnread() * * @return bool */ - protected function _getRead() + protected function _getRead(): bool { return $this->get('state') == self::READ_STATUS; } diff --git a/tests/TestCase/Controller/Component/NotifierComponentTest.php b/tests/TestCase/Controller/Component/NotifierComponentTest.php index ea9f9c2..3b8986b 100644 --- a/tests/TestCase/Controller/Component/NotifierComponentTest.php +++ b/tests/TestCase/Controller/Component/NotifierComponentTest.php @@ -32,7 +32,7 @@ class NotifierComponentTest extends TestCase 'plugin.bakkerij\Notifier.notifications' ]; - public function setUp() + public function setUp(): void { parent::setUp(); @@ -58,7 +58,7 @@ public function setUp() $this->Notifier = new NotifierComponent($registry); } - public function tearDown() + public function tearDown(): void { unset($this->Notifier); unset($this->Manager); diff --git a/tests/TestCase/Model/Table/NotificationsTableTest.php b/tests/TestCase/Model/Table/NotificationsTableTest.php index 7ac241c..a7e6ee2 100644 --- a/tests/TestCase/Model/Table/NotificationsTableTest.php +++ b/tests/TestCase/Model/Table/NotificationsTableTest.php @@ -28,13 +28,13 @@ class NotificationsTableTest extends TestCase 'plugin.bakkerij\Notifier.notifications', ]; - public function setUp() + public function setUp(): void { parent::setUp(); $this->Notifications = TableRegistry::get('Bakkerij/Notifier.Notifications'); } - public function tearDown() + public function tearDown(): void { unset($this->Notifications); diff --git a/tests/TestCase/Utility/NotificationManagerTest.php b/tests/TestCase/Utility/NotificationManagerTest.php index b3e208d..471ceaf 100644 --- a/tests/TestCase/Utility/NotificationManagerTest.php +++ b/tests/TestCase/Utility/NotificationManagerTest.php @@ -26,7 +26,7 @@ class NotificationManagerTest extends TestCase 'plugin.bakkerij\Notifier.notifications' ]; - public function setUp() + public function setUp(): void { parent::setUp(); $this->Manager = NotificationManager::instance(); @@ -34,7 +34,7 @@ public function setUp() $this->Model = TableRegistry::get('Bakkerij/Notifier.Notifications'); } - public function tearDown() + public function tearDown(): void { unset($this->Manager); unset($this->Model); From da35c72f38a197c417987b41e61429876c9be911 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sun, 18 Feb 2024 01:31:17 +0100 Subject: [PATCH 06/15] updated types --- src/Model/Entity/Notification.php | 8 ++++---- src/Model/Table/NotificationsTable.php | 15 +++++++-------- src/Utility/NotificationManager.php | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Model/Entity/Notification.php b/src/Model/Entity/Notification.php index 031ad31..42f4889 100644 --- a/src/Model/Entity/Notification.php +++ b/src/Model/Entity/Notification.php @@ -49,7 +49,7 @@ class Notification extends Entity * @param string $vars Data. * @return mixed */ - protected function _getVars($vars) : array + protected function _getVars($vars): array|string|null { $array = json_decode($vars, true); @@ -68,7 +68,7 @@ protected function _getVars($vars) : array * @param array $vars Data. * @return string */ - protected function _setVars($vars) : array + protected function _setVars($vars): string|null { if (is_array($vars)) { return json_encode($vars); @@ -86,7 +86,7 @@ protected function _setVars($vars) : array * * @return string */ - protected function _getTitle() : string + protected function _getTitle(): string { $templates = Configure::read('Notifier.templates'); @@ -130,7 +130,7 @@ protected function _getBody(): string * * @return bool */ - protected function _getUnread() : bool + protected function _getUnread(): bool { return $this->get('state') == self::UNREAD_STATUS; } diff --git a/src/Model/Table/NotificationsTable.php b/src/Model/Table/NotificationsTable.php index cb80fff..b552358 100644 --- a/src/Model/Table/NotificationsTable.php +++ b/src/Model/Table/NotificationsTable.php @@ -12,11 +12,10 @@ * @since 1.0 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ + namespace Bakkerij\Notifier\Model\Table; -use Cake\ORM\RulesChecker; use Cake\ORM\Table; -use Cake\ORM\TableRegistry; use Cake\Validation\Validator; /** @@ -37,7 +36,7 @@ class NotificationsTable extends Table * @param array $config The configuration for the Table. * @return void */ - public function initialize(array $config) + public function initialize(array $config): void { $this->setTable('notifications'); $this->setDisplayField('title'); @@ -51,15 +50,15 @@ public function initialize(array $config) * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ - public function validationDefault(Validator $validator) + public function validationDefault(Validator $validator): Validator { $validator ->add('id', 'valid', ['rule' => 'numeric']) - ->allowEmpty('id', 'create') - ->allowEmpty('title') - ->allowEmpty('body') + ->allowEmptyString('id', 'create') + ->allowEmptyString('title') + ->allowEmptyString('body') ->add('state', 'valid', ['rule' => 'numeric']) - ->allowEmpty('state'); + ->allowEmptyString('state'); return $validator; } diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index 7aeda49..2cd5dca 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -74,7 +74,7 @@ public static function instance($manager = null) * @param array $data Data with options. * @return string The tracking_id to follow the notification. */ - public function notify($data) + public function notify($data): string { $model = TableRegistry::getTableLocator()->get('Bakkerij/Notifier.Notifications'); From 3a5a306b2d06ef9d217830230eaef73677ee5b5a Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sun, 18 Feb 2024 01:35:00 +0100 Subject: [PATCH 07/15] update types retunr --- src/Utility/NotificationManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index 2cd5dca..b43127b 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -35,7 +35,7 @@ class NotificationManager * @param null $manager Possible different manager. (Helpfull for testing). * @return NotificationManager */ - public static function instance($manager = null) + public static function instance($manager = null): NotificationManager { if ($manager instanceof NotificationManager) { static::$_generalManager = $manager; @@ -108,7 +108,7 @@ public function notify($data): string * @param array $data * @return array */ - private function mergeRecipientList($data) + private function mergeRecipientList($data): ?array { $users = $data['users']; foreach ((array)$data['recipientLists'] as $recipientList) { @@ -136,7 +136,7 @@ private function mergeRecipientList($data) * @param array $userIds Array with id's of users. * @return void */ - public function addRecipientList($name, $userIds) + public function addRecipientList($name, $userIds): void { Configure::write('Notifier.recipientLists.' . $name, $userIds); } @@ -150,7 +150,7 @@ public function addRecipientList($name, $userIds) * @param string $name The name of the list. * @return array|null */ - public function getRecipientList($name) + public function getRecipientList($name): ?array { return Configure::read('Notifier.recipientLists.' . $name); } From b6c9bb2d4ec9f662d4202b1c0948d52028498cf6 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sun, 18 Feb 2024 01:38:24 +0100 Subject: [PATCH 08/15] updated types component --- .../Component/NotifierComponent.php | 21 +++++++++---------- src/Utility/NotificationManager.php | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index a8e7ab6..fec79f0 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -54,7 +54,7 @@ class NotifierComponent extends Component * @param array $config Config. * @return void */ - public function initialize(array $config) + public function initialize(array $config): void { parent::initialize($config); @@ -70,7 +70,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; } @@ -102,7 +102,7 @@ public function setController($controller) * * @deprecated 1.3 use getReadNotifications or getUnreadNotifications instead. */ - public function getNotifications($userId = null, $state = null) + public function getNotifications($userId = null, $state = null): array { $stateCondition = []; if (isset($state)) { @@ -113,7 +113,6 @@ public function getNotifications($userId = null, $state = null) } - /** * getNotifications * @@ -136,7 +135,7 @@ public function getNotifications($userId = null, $state = null) * } * * @return array */ - public function getAllNotificationsBy($userId, $options) + public function getAllNotificationsBy($userId, $options): array { if (array_key_exists('state', $options)) { unset($options['state']); @@ -170,7 +169,7 @@ public function getAllNotificationsBy($userId, $options) * } * * @return array */ - public function getReadNotificationsBy($userId, $options) + public function getReadNotificationsBy($userId, $options): array { $readCondition = ['whereConditions' => ['state' => Notification::READ_STATUS]]; $conditions = array_merge($options, $readCondition); @@ -200,7 +199,7 @@ public function getReadNotificationsBy($userId, $options) * } * @return array */ - public function getUnReadNotificationsBy($userId, $options) + public function getUnReadNotificationsBy($userId, $options): array { $unreadCondition = ['whereConditions' => ['state' => Notification::UNREAD_STATUS]]; $conditions = array_merge($options, $unreadCondition); @@ -217,7 +216,7 @@ public function getUnReadNotificationsBy($userId, $options) * } * @return array */ - private function getNotificationsFactory($userId, $options) + private function getNotificationsFactory($userId, $options): array { if (!isset($userId)) { $userId = $this->Controller->Auth->user('id'); @@ -269,7 +268,7 @@ private function getNotificationsFactory($userId, $options) * @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($userId = null, $state = null): int { if (!$userId) { $userId = $this->Controller->Auth->user('id'); @@ -294,7 +293,7 @@ public function countNotifications($userId = null, $state = null) * @param int|null $user Id of the user. Else the id of the session will be taken. * @return void */ - public function markAsRead($notificationId = null, $user = null) + public function markAsRead($notificationId = null, $user = null): void { if (!$user) { $user = $this->Controller->Auth->user('id'); @@ -346,7 +345,7 @@ 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); } diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index b43127b..d9a2579 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -182,7 +182,7 @@ public function getRecipientList($name): ?array * @param array $options Options. * @return void */ - public function addTemplate($name, $options = []) + public function addTemplate($name, $options = []): void { $_options = [ 'title' => 'Notification', @@ -204,7 +204,7 @@ public function addTemplate($name, $options = []) * @param string|null $type The type like `title` or `body`. Leave empty to get the whole template. * @return array|string|bool */ - public function getTemplate($name, $type = null) + public function getTemplate($name, $type = null): array|string|bool { $templates = Configure::read('Notifier.templates'); From 666ff77b4175177bda69ea80bd59a2f4db0ac8d4 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sun, 18 Feb 2024 02:25:30 +0100 Subject: [PATCH 09/15] fix params and types --- .../Component/NotifierComponent.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index fec79f0..61b2e61 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -106,7 +106,7 @@ public function getNotifications($userId = null, $state = null): array { $stateCondition = []; if (isset($state)) { - $stateCondition = ['state' => $state]; + $stateCondition = ['whereConditions' => ['state' => $state]]; } return $this->getNotificationsFactory($userId, $stateCondition); @@ -135,13 +135,13 @@ public function getNotifications($userId = null, $state = null): array * } * * @return array */ - public function getAllNotificationsBy($userId, $options): array + public function getAllNotificationsBy($userId, $options = []): array { if (array_key_exists('state', $options)) { unset($options['state']); } - return $this->getNotificationsFactory($userId, $conditions); + return $this->getNotificationsFactory($userId, $options); } /** @@ -169,7 +169,7 @@ public function getAllNotificationsBy($userId, $options): array * } * * @return array */ - public function getReadNotificationsBy($userId, $options): array + public function getReadNotificationsBy($userId, $options = []): array { $readCondition = ['whereConditions' => ['state' => Notification::READ_STATUS]]; $conditions = array_merge($options, $readCondition); @@ -199,7 +199,7 @@ public function getReadNotificationsBy($userId, $options): array * } * @return array */ - public function getUnReadNotificationsBy($userId, $options): array + public function getUnReadNotificationsBy($userId, $options = []): array { $unreadCondition = ['whereConditions' => ['state' => Notification::UNREAD_STATUS]]; $conditions = array_merge($options, $unreadCondition); @@ -216,7 +216,7 @@ public function getUnReadNotificationsBy($userId, $options): array * } * @return array */ - private function getNotificationsFactory($userId, $options): array + private function getNotificationsFactory($userId, $options = []): array { if (!isset($userId)) { $userId = $this->Controller->Auth->user('id'); @@ -239,7 +239,7 @@ private function getNotificationsFactory($userId, $options): array return $this->table ->find() ->where($whereConditions) - ->order() + ->order($order) ->toArray(); } @@ -268,7 +268,7 @@ private function getNotificationsFactory($userId, $options): array * @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): int + public function countNotifications(?int $userId = null, ?int $state = null): int { if (!$userId) { $userId = $this->Controller->Auth->user('id'); @@ -300,12 +300,12 @@ public function markAsRead($notificationId = null, $user = null): void } if (!$notificationId) { - $query = $this->table->find('all')->where([ + $query = $this->table->find()->where([ 'user_id' => $user, 'state' => Notification::UNREAD_STATUS ]); } else { - $query = $this->table->find('all')->where([ + $query = $this->table->find()->where([ 'user_id' => $user, 'id' => $notificationId From 9769c7d8a451c145ec24fc145535c8d2b9094f94 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Sun, 18 Feb 2024 02:41:18 +0100 Subject: [PATCH 10/15] fix params --- .../Component/NotifierComponent.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index a8e7ab6..364e695 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -106,14 +106,13 @@ public function getNotifications($userId = null, $state = null) { $stateCondition = []; if (isset($state)) { - $stateCondition = ['state' => $state]; + $stateCondition = ['whereConditions' => ['state' => $state]]; } return $this->getNotificationsFactory($userId, $stateCondition); } - /** * getNotifications * @@ -136,13 +135,13 @@ public function getNotifications($userId = null, $state = null) * } * * @return array */ - public function getAllNotificationsBy($userId, $options) + public function getAllNotificationsBy($userId, $options = []) { if (array_key_exists('state', $options)) { unset($options['state']); } - return $this->getNotificationsFactory($userId, $conditions); + return $this->getNotificationsFactory($userId, $options); } /** @@ -170,7 +169,7 @@ public function getAllNotificationsBy($userId, $options) * } * * @return array */ - public function getReadNotificationsBy($userId, $options) + public function getReadNotificationsBy($userId, $options = []) { $readCondition = ['whereConditions' => ['state' => Notification::READ_STATUS]]; $conditions = array_merge($options, $readCondition); @@ -200,7 +199,7 @@ public function getReadNotificationsBy($userId, $options) * } * @return array */ - public function getUnReadNotificationsBy($userId, $options) + public function getUnReadNotificationsBy($userId, $options = []) { $unreadCondition = ['whereConditions' => ['state' => Notification::UNREAD_STATUS]]; $conditions = array_merge($options, $unreadCondition); @@ -217,7 +216,7 @@ public function getUnReadNotificationsBy($userId, $options) * } * @return array */ - private function getNotificationsFactory($userId, $options) + private function getNotificationsFactory($userId, $options = []) { if (!isset($userId)) { $userId = $this->Controller->Auth->user('id'); @@ -240,7 +239,7 @@ private function getNotificationsFactory($userId, $options) return $this->table ->find() ->where($whereConditions) - ->order() + ->order($order) ->toArray(); } @@ -301,12 +300,12 @@ public function markAsRead($notificationId = null, $user = null) } if (!$notificationId) { - $query = $this->table->find('all')->where([ + $query = $this->table->find()->where([ 'user_id' => $user, 'state' => Notification::UNREAD_STATUS ]); } else { - $query = $this->table->find('all')->where([ + $query = $this->table->find()->where([ 'user_id' => $user, 'id' => $notificationId From 039df2191048310eb52dcd4ea67233b22a9408e2 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Mon, 19 Feb 2024 20:20:08 +0100 Subject: [PATCH 11/15] handling errors --- src/Controller/Component/NotifierComponent.php | 2 ++ src/Utility/NotificationManager.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index 364e695..cf73c30 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -312,6 +312,8 @@ public function markAsRead($notificationId = null, $user = null) ]); } + dd($query); + foreach ($query as $item) { $item->set('state', Notification::READ_STATUS); $this->table->save($item); diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index 7aeda49..00eeb29 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -17,6 +17,7 @@ use Bakkerij\Notifier\Model\Entity\Notification; use Cake\Core\Configure; +use Cake\ORM\Exception\PersistenceFailedException; use Cake\ORM\TableRegistry; /** @@ -72,7 +73,7 @@ public static function instance($manager = null) * ``` * * @param array $data Data with options. - * @return string The tracking_id to follow the notification. + * @return string|false The tracking_id to follow the notification. */ public function notify($data) { @@ -99,7 +100,12 @@ public function notify($data) $entities[] = $entity; } - $model->saveMany($entities); + + $notificationsSaved = $model->saveMany($entities); + + if (!$notificationsSaved) { + return $notificationsSaved; + } return $data['tracking_id']; } From 5d0158e887d2b5c31ad837d31d5d13f0cf3d5250 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Mon, 19 Feb 2024 21:33:32 +0100 Subject: [PATCH 12/15] handle error and version update --- composer.json | 2 +- src/Controller/Component/NotifierComponent.php | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index d06c2c4..4cb8ac2 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "cakephp-plugin", "require": { "php": ">=5.4.16", - "cakephp/cakephp": "3.5.*" + "cakephp/cakephp": "^3.5" }, "require-dev": { "phpunit/phpunit": "*", diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index cf73c30..1ceee35 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -18,6 +18,7 @@ use Bakkerij\Notifier\Model\Entity\Notification; use Bakkerij\Notifier\Utility\NotificationManager; use Cake\Controller\Component; +use Cake\Database\Expression\QueryExpression; use Cake\ORM\TableRegistry; /** @@ -291,7 +292,7 @@ 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) { @@ -312,11 +313,16 @@ public function markAsRead($notificationId = null, $user = null) ]); } - dd($query); + $notifications = []; + foreach ($query as $notification) { + $notification->set('state', Notification::READ_STATUS); + $notifications[] = $notification; + } + + $savedNotifications = $this->table->saveMany($notifications); - foreach ($query as $item) { - $item->set('state', Notification::READ_STATUS); - $this->table->save($item); + if (!$savedNotifications) { + return false; } } From ee35e9ff0219c8d5f765468067afbb2b86813469 Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Mon, 19 Feb 2024 21:50:16 +0100 Subject: [PATCH 13/15] handle error and add translation --- .../Component/NotifierComponent.php | 26 ++++++++++++++----- src/Utility/NotificationManager.php | 6 ++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Controller/Component/NotifierComponent.php b/src/Controller/Component/NotifierComponent.php index 61b2e61..9695837 100644 --- a/src/Controller/Component/NotifierComponent.php +++ b/src/Controller/Component/NotifierComponent.php @@ -291,9 +291,9 @@ public function countNotifications(?int $userId = null, ?int $state = null): int * * @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 false */ - public function markAsRead($notificationId = null, $user = null): void + public function markAsRead($notificationId = null, $user = null): bool { if (!$user) { $user = $this->Controller->Auth->user('id'); @@ -312,10 +312,19 @@ public function markAsRead($notificationId = null, $user = null): void ]); } - foreach ($query as $item) { - $item->set('state', Notification::READ_STATUS); - $this->table->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; } /** @@ -347,6 +356,11 @@ public function markAsRead($notificationId = null, $user = null): void */ 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; } } diff --git a/src/Utility/NotificationManager.php b/src/Utility/NotificationManager.php index d9a2579..62481ad 100644 --- a/src/Utility/NotificationManager.php +++ b/src/Utility/NotificationManager.php @@ -99,7 +99,11 @@ public function notify($data): string $entities[] = $entity; } - $model->saveMany($entities); + $notificationsSaved = $model->saveMany($entities); + + if (!$notificationsSaved) { + return $notificationsSaved; + } return $data['tracking_id']; } From 406c76efd15f821440dc0c9550a0576f89d72f1f Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Tue, 20 Feb 2024 16:30:14 +0100 Subject: [PATCH 14/15] contribution --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1cd0504..2e8ddf2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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([ @@ -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); @@ -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); @@ -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'); ``` @@ -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! \ No newline at end of file From 28a7b3c7b7565f3b8f5dda1d8ab5f935f253ba5d Mon Sep 17 00:00:00 2001 From: Luis del Amo Date: Tue, 20 Feb 2024 16:45:01 +0100 Subject: [PATCH 15/15] deprecated method docu --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e8ddf2..854cdac 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,10 @@ 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`