diff --git a/application/Espo/Hooks/Note/Notifications.php b/application/Espo/Hooks/Note/Notifications.php index e07aafdcba..b262d453e3 100644 --- a/application/Espo/Hooks/Note/Notifications.php +++ b/application/Espo/Hooks/Note/Notifications.php @@ -78,17 +78,7 @@ class Notifications extends \Espo\Core\Hooks\Base } } if (!empty($userIdList)) { - $job = $this->getEntityManager()->getEntity('Job'); - $job->set(array( - 'serviceName' => 'Notification', - 'method' => 'notifyAboutNoteFromJob', - 'data' => array( - 'userIdList' => $userIdList, - 'noteId' => $entity->id - ), - 'executeTime' => date('Y-m-d H:i:s'), - )); - $this->getEntityManager()->saveEntity($job); + $this->getNotificationService()->notifyAboutNote($userIdList, $entity); } } } diff --git a/application/Espo/Resources/metadata/entityDefs/Notification.json b/application/Espo/Resources/metadata/entityDefs/Notification.json index bf29992d47..8adc26d605 100644 --- a/application/Espo/Resources/metadata/entityDefs/Notification.json +++ b/application/Espo/Resources/metadata/entityDefs/Notification.json @@ -1,5 +1,9 @@ { "fields": { + "number": { + "type": "autoincrement", + "index": true + }, "data": { "type": "jsonObject" }, @@ -28,7 +32,7 @@ } }, "collection": { - "sortBy": "createdAt", + "sortBy": "number", "asc": false }, "indexes": { diff --git a/application/Espo/Services/Notification.php b/application/Espo/Services/Notification.php index a1e3f63b77..7359fbfa53 100644 --- a/application/Espo/Services/Notification.php +++ b/application/Espo/Services/Notification.php @@ -27,6 +27,8 @@ use \Espo\Core\Exceptions\NotFound; use Espo\ORM\Entity; +use Espo\Core\Utils\Json; + class Notification extends \Espo\Core\Services\Base { protected $dependencies = array( @@ -61,26 +63,28 @@ class Notification extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($notification); } - public function notifyAboutNote($userId, $noteId) + public function notifyAboutNote(array $userIds, \Espo\Entities\Note $note) { - $notification = $this->getEntityManager()->getEntity('Notification'); - $notification->set(array( - 'type' => 'Note', - 'data' => array('noteId' => $noteId), - 'userId' => $userId - )); - $this->getEntityManager()->saveEntity($notification); - } + $data = array('noteId' => $note->id); + $encodedData = Json::encode($data); - public function notifyAboutNoteFromJob($data) - { - $userIdList = $data['userIdList']; - $noteId = $data['noteId']; + $now = date('Y-m-d H:i:s'); - foreach ($userIdList as $userId) { - $this->notifyAboutNote($userId, $noteId); + $pdo = $this->getEntityManager()->getPDO(); + + $sql = "INSERT INTO `notification` (`id`, `data`, `type`, `user_id`, `created_at`) VALUES "; + $arr = []; + foreach ($userIds as $userId) { + $id = uniqid(); + $arr[] = "(".$pdo->quote($id).", ".$pdo->quote($encodedData).", ".$pdo->quote('Note').", ".$pdo->quote($userId).", ".$pdo->quote($now).")"; } - return true; + + if (empty($arr)) { + return; + } + + $sql .= implode(", ", $arr); + $pdo->query($sql); } public function getNotReadCount($userId) diff --git a/frontend/client/src/views/notifications/panel.js b/frontend/client/src/views/notifications/panel.js index dfbece9a01..51e7881bf1 100644 --- a/frontend/client/src/views/notifications/panel.js +++ b/frontend/client/src/views/notifications/panel.js @@ -17,17 +17,17 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ Espo.define('Views.Notifications.Panel', 'View', function (Dep) { return Dep.extend({ template: 'notifications.panel', - + events: { 'click [data-action="markAllNotificationsRead"]': function () { - $.ajax({ + $.ajax({ url: 'Notification/action/markAllRead', type: 'POST' }).done(function (count) { @@ -35,17 +35,17 @@ Espo.define('Views.Notifications.Panel', 'View', function (Dep) { }.bind(this)); }, }, - + setup: function () { this.wait(true); this.getCollectionFactory().create('Notification', function (collection) { this.collection = collection; collection.maxSize = 5; this.wait(false); - }, this); + }, this); }, - - afterRender: function () { + + afterRender: function () { this.listenToOnce(this.collection, 'sync', function () { this.createView('list', 'Record.ListExpanded', { el: this.options.el + ' .list-container', @@ -60,7 +60,7 @@ Espo.define('Views.Notifications.Panel', 'View', function (Dep) { params: { containerEl: this.options.el }, - } + } ] ], right: { @@ -72,10 +72,10 @@ Espo.define('Views.Notifications.Panel', 'View', function (Dep) { }, function (view) { view.render(); }); - }.bind(this)); + }.bind(this)); this.collection.fetch(); }, - + }); - + });