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
166 changes: 149 additions & 17 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,12 @@
* @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\ORM\TableRegistry;

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

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

/**
* initialize
*
Expand All @@ -51,6 +59,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 +99,149 @@ 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 = ['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);
*
* // 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']);
}

if (array_key_exists('order', $options)) {
$order = array_merge($whereConditions, $options['order']);
}

return $query->toArray();
return $this->table
->find()
->where($whereConditions)
->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 @@ -166,24 +300,22 @@ 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
'state' => Notification::UNREAD_STATUS
]);
} else {
$query = $model->find('all')->where([
$query = $this->table->find('all')->where([
'user_id' => $user,
'id' => $notificationId

]);
}

foreach ($query as $item) {
$item->set('state', 0);
$model->save($item);
$item->set('state', Notification::READ_STATUS);
$this->table->save($item);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if save fails?

}
}

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
6 changes: 3 additions & 3 deletions src/Model/Table/NotificationsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
44 changes: 28 additions & 16 deletions src/Utility/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* @since 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Bakkerij\Notifier\Utility;

use Bakkerij\Notifier\Model\Entity\Notification;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\ORM\TableRegistry;
use Cake\Utility\Text;

/**
* Notifier component
Expand All @@ -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 @@ -75,36 +76,47 @@ public static function instance($manager = null)
*/
public function notify($data)
{
$model = TableRegistry::get('Bakkerij/Notifier.Notifications');
steinkel marked this conversation as resolved.
Show resolved Hide resolved
$model = TableRegistry::getTableLocator()->get('Bakkerij/Notifier.Notifications');

$_data = [
'users' => [],
'recipientLists' => [],
'template' => 'default',
'vars' => [],
'tracking_id' => $this->getTrackingId()
'tracking_id' => $this->getTrackingId(),
'state' => Notification::UNREAD_STATUS,
];

$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);

$entities = [];
foreach ((array)$data['users'] as $userId) {
$finalData = array_merge($data, ['user_id' => $userId]);
$entity = $model->newEntity($finalData);
$entity->set('vars', $data['vars']);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I needed it to do this because for any reason, passing the data in the newEntities with the arrays of arrays was not entering in the property 'vars' of the Entity which is in charge of jsonEncode. Was setting up the vars as empty string

$entities[] = $entity;
}

foreach ((array)$data['users'] as $user) {
$entity = $model->newEntity();
$model->saveMany($entities);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if save fails?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I will handle the error.


$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);
return $data['tracking_id'];
}

$model->save($entity);
/**
* @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 $data['tracking_id'];
return $users;
}

/**
Expand Down