improve notifications

This commit is contained in:
yuri
2015-02-11 11:00:43 +02:00
parent 00e12b50b7
commit e0958cfeec
4 changed files with 37 additions and 39 deletions
+1 -11
View File
@@ -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);
}
}
}
@@ -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": {
+20 -16
View File
@@ -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)
@@ -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();
},
});
});