before(function(Request $request) use ($app) { $app['firewall']->requireNotGuest(); }); /** * Read all notifications * * name : read_notifications_full * * description : Read full notification * * method : GET * * parameters : none * * return : JSON Response */ $controllers->get('/', $this->call('listNotifications')) ->bind('get_notifications'); /** * Set notifications as readed * * name : set_notifications_readed * * description : Set notifications as readed * * method : POST * * parameters : none * * return : JSON Response */ $controllers->post('/read/', $this->call('readNotifications')) ->bind('set_notifications_readed'); return $controllers; } /** * Set notifications as readed * * @param Application $app * @param Request $request * @return JsonResponse */ public function readNotifications(Application $app, Request $request) { if (!$request->isXmlHttpRequest()) { $app->abort(400); } try { $app['events-manager']->read( explode('_', (string) $request->request->get('notifications')), $app['authentication']->getUser()->get_id() ); return $app->json(array('success' => true, 'message' => '')); } catch (\Exception $e) { return $app->json(array('success' => false, 'message' => $e->getMessage())); } } /** * Get all notifications * * @param Application $app * @param Request $request * @return JsonResponse */ public function listNotifications(Application $app, Request $request) { if (!$request->isXmlHttpRequest()) { $app->abort(400); } $page = (int) $request->query->get('page', 0); return $app->json($app['events-manager']->get_notifications_as_array(($page < 0 ? 0 : $page))); } /** * Prefix the method to call with the controller class name * * @param string $method The method to call * @return string */ private function call($method) { return sprintf('%s::%s', __CLASS__, $method); } }