injections['entityManager']; } protected function getUser() { return $this->injections['user']; } protected function getMetadata() { return $this->injections['metadata']; } public function notifyAboutMentionInPost($userId, $noteId) { $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array( 'type' => 'MentionInPost', 'data' => array('noteId' => $noteId), 'userId' => $userId )); $this->getEntityManager()->saveEntity($notification); } public function notifyAboutNote($userId, $noteId) { $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array( 'type' => 'Note', 'data' => array('noteId' => $noteId), 'userId' => $userId )); $this->getEntityManager()->saveEntity($notification); } public function notifyAboutNoteFromJob($data) { $userIdList = $data['userIdList']; $noteId = $data['noteId']; foreach ($userIdList as $userId) { $this->notifyAboutNote($userId, $noteId); } return true; } public function getNotReadCount($userId) { $searchParams = array(); $searchParams['whereClause'] = array( 'userId' => $userId ); return $this->getEntityManager()->getRepository('Notification')->where(array( 'userId' => $userId, 'read' => 0, ))->count(); } public function markAllRead($userId) { $pdo = $this->getEntityManager()->getPDO(); $sql = "UPDATE notification SET `read` = 1 WHERE user_id = ".$pdo->quote($userId)." AND `read` = 0"; $pdo->prepare($sql)->execute(); return true; } public function getList($userId, array $params = array()) { $searchParams = array(); $searchParams['whereClause'] = array( 'userId' => $userId ); if (array_key_exists('offset', $params)) { $searchParams['offset'] = $params['offset']; } if (array_key_exists('maxSize', $params)) { $searchParams['limit'] = $params['maxSize']; } $searchParams['orderBy'] = 'createdAt'; $searchParams['order'] = 'DESC'; $collection = $this->getEntityManager()->getRepository('Notification')->find($searchParams); $count = $this->getEntityManager()->getRepository('Notification')->count($searchParams); $ids = array(); foreach ($collection as $k => $entity) { $ids[] = $entity->id; $data = $entity->get('data'); if (empty($data)) { continue; } switch ($entity->get('type')) { case 'Note': case 'MentionInPost': $note = $this->getEntityManager()->getEntity('Note', $data->noteId); if ($note) { if ($note->get('parentId') && $note->get('parentType')) { $parent = $this->getEntityManager()->getEntity($note->get('parentType'), $note->get('parentId')); if ($parent) { $note->set('parentName', $parent->get('name')); } } $entity->set('noteData', $note->toArray()); } else { unset($collection[$k]); $count--; $this->getEntityManager()->removeEntity($entity); } break; } } if (!empty($ids)) { $pdo = $this->getEntityManager()->getPDO(); $sql = "UPDATE notification SET `read` = 1 WHERE id IN ('" . implode("', '", $ids) ."')"; $s = $pdo->prepare($sql); $s->execute(); } return array( 'total' => $count, 'collection' => $collection ); } }