/** * Delete action. * * @access public * @param Silex\Application $app Silex application * @param Symfony\Component\HttpFoundation\Request $request Request object * @return string Output */ public function delete(Application $app, Request $request) { try { $usersModel = new UsersModel($app); $idLoggedUser = $usersModel->getIdCurrentUser($app); $id = (int) $request->get('id', 0); $user = (int) $request->get('user', 0); if (!$app['security']->isGranted('ROLE_ADMIN')) { if ((int) $user !== (int) $idLoggedUser) { $app['session']->getFlashBag()->add('message', array('type' => 'danger', 'content' => 'This is not your ad - you can not delete it\'s photo.')); return $app['twig']->render('errors/403.twig'); } } } catch (\Exception $e) { $errors[] = 'Something went wrong in getting user'; $app['session']->getFlashBag()->add('message', array('type' => 'danger', 'content' => 'Something went wrong in getting user')); return $app['twig']->render('errors/404.twig'); } try { $data = array(); $form = $app['form.factory']->createBuilder(new DeleteForm(), $ad)->getForm(); $form->handleRequest($request); } catch (\Exception $e) { $errors[] = 'Something went wrong in creating form'; $app['session']->getFlashBag()->add('message', array('type' => 'danger', 'content' => 'Something went wrong in creating form')); return $app['twig']->render('errors/404.twig'); } if ($form->isValid()) { if ($form->get('No')->isClicked()) { return $app->redirect($app['url_generator']->generate('/'), 301); } else { try { $photosModel = new PhotosModel($app); $photo = $photosModel->getPhoto($id); $adId = $photo['ad_id']; $photosModel->deletePhoto($id); $app['session']->getFlashBag()->add('message', array('type' => 'success', 'content' => 'Photo has been deleted.')); return $app->redirect($app['url_generator']->generate('/ad/view', array('id' => $adId)), 301); } catch (\Exception $e) { $app['session']->getFlashBag()->add('message', array('type' => 'danger', 'content' => 'Photo not found')); return $app['twig']->render('404.twig'); } } } return $app['twig']->render('/ads/delete.twig', array('form' => $form->createView(), $data)); }
/** * Delete action. * * @access public * @param Silex\Application $app Silex application * @param Symfony\Component\HttpFoundation\Request $request Request object * @return string Output */ public function deleteAction(Application $app, Request $request) { try { $photosModel = new PhotosModel($app); $id = (int) $request->get('id', 0); $photo = $photosModel->getPhoto($id); if (!count($photo)) { return $app->redirect($app['url_generator']->generate('rooms'), 301); } $ok = (int) $request->get('ok', null); if ($ok) { $photosModel->delete($id); $app['session']->getFlashBag()->add('message', array('type' => 'success', 'content' => $app['translator']->trans('Photo deleted.'))); return $app->redirect($app['url_generator']->generate('rooms'), 301); } $this->view['id'] = $id; } catch (\PDOException $e) { $app->abort(500, $app['translator']->trans('Something went wrong.')); } return $app['twig']->render('photos/delete.twig', $this->view); }