/** * Gets all rooms to choice list. * * @access private * @param Silex\Application $app Silex application * @return array Result */ private function getRooms(Application $app) { $return = array(); $photosModel = new PhotosModel($app); $choice = $photosModel->getRooms(); foreach ($choice as $photo) { $return[$photo['room_id']] = $photo['number']; } return $return; }
/** * View action. * * @access public * @param Silex\Application $app Silex application * @param Symfony\Component\HttpFoundation\Request $request Request object * * @return string Output */ public function viewAction(Application $app, Request $request) { $id = (int) $request->get('id', null); $newsModel = new NewsModel($app); $this->view['posts'] = $newsModel->getPost($id); $photosModel = new PhotosModel($app); $this->view['photos'] = $photosModel->getPhotos($id); return $app['twig']->render('post.twig', $this->view); // try { // $adsModel = new AdsModel($app); // $ad = $adsModel->getAd($id); // $number = $usersModel-> getPhone($ad['user_id']); // if (!$ad) { // $app['session']->getFlashBag()->add( // 'message', // array( // 'type' => 'danger', // 'content' => 'Ad not found' // ) // ); // return $app['twig']->render( // 'errors/404.twig' // ); // } // $photosModel = new PhotosModel($app); // $photoTab= $photosModel->getPhoto($ad['id']); // $photo = $photoTab['name']; // } catch (\Exception $e) { // $errors[] = 'Something went wrong'; // return $app['twig']->render( // 'errors/404.twig' // ); // } // return $app['twig']->render( // 'ads/view.twig', // array( // 'ad' => $ad, // 'photo' => $photo, // 'loggedUser' => $idLoggedUser, // 'number' => $number['phone_number'], // ) // ); }
/** * Add action. * * @access public * * @param Application $app Silex application * @param Request $request Request object * * @return string Output */ public function addAction(Application $app, Request $request) { // default values: $data = array(); $form = $app['form.factory']->createBuilder(new PhotoForm(), $data)->getForm(); if ($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { try { $files = $request->files->get($form->getName()); $mediaPath = dirname(dirname(dirname(__FILE__))) . '/web/upload'; $photosModel = new PhotosModel($app); $photosModel->saveImage($files, $mediaPath); $app['session']->getFlashBag()->add('message', array('type' => 'success', 'content' => $app['translator']->trans('File successfully uploaded.'))); } catch (Exception $e) { $app['session']->getFlashBag()->add('message', array('type' => 'error', 'content' => $app['translator']->trans('Can not upload file.'))); } } else { $app['session']->getFlashBag()->add('message', array('type' => 'error', 'content' => $app['translator']->trans('Form contains invalid data.'))); } } $this->view['form'] = $form->createView(); return $app['twig']->render('photos/add.twig', $this->view); }
/** * 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); }