Beispiel #1
0
 public function editAction(Application $app, Request $request)
 {
     $adsModel = new AdsModel($app);
     $id = (int) $request->get('id', 0);
     $ad = $adsModel->getAd($id);
     $categoriesModel = new CategoriesModel($app);
     $ad['categories'] = $categoriesModel->getAll();
     if (count($ad)) {
         $form = $app['form.factory']->createBuilder(new AdForm(), $ad)->getForm();
         $form->handleRequest($request);
         if ($form->isValid()) {
             $data = $form->getData();
             $data['category_id'] = $data['category'];
             unset($data['categories'], $data['category']);
             $adsModel = new AdsModel($app);
             $adsModel->saveAd($data);
             $app['session']->getFlashBag()->add('message', array('type' => 'success', 'content' => $app['translator']->trans('Ad edited.')));
             return $app->redirect($app['url_generator']->generate('ads_index'), 301);
         }
         $this->view['id'] = $id;
         $this->view['form'] = $form->createView();
     } else {
         return $app->redirect($app['url_generator']->generate('ads_add'), 301);
     }
     return $app['twig']->render('ads/edit.twig', $this->view);
 }
Beispiel #2
0
 public function editAction(Application $app, Request $request)
 {
     $adsModel = new AdsModel($app);
     $id = (int) $request->get('id', 0);
     $ad = $adsModel->getAd($id);
     $ad_owner = $ad['user_id'];
     $userModel = new UsersModel($app);
     $current_user_id = $userModel->getCurrentUserId($app);
     $current_user_role = $userModel->getUserRoles($current_user_id);
     if ($current_user_role == 'ROLE_ADMIN' || $current_user_role == 'ROLE_MOD' || $ad_owner == $current_user_id) {
         $categoriesModel = new CategoriesModel($app);
         $ad['categories'] = $categoriesModel->getAll();
         if (count($ad)) {
             $form = $app['form.factory']->createBuilder(new AdForm(), $ad)->getForm();
             $form->handleRequest($request);
             if ($form->isValid()) {
                 $data = $form->getData();
                 $data['category_id'] = $data['category'];
                 unset($data['categories'], $data['category']);
                 $adsModel = new AdsModel($app);
                 $adsModel->saveAd($data);
                 $app['session']->getFlashBag()->add('message', array('type' => 'success', 'content' => $app['translator']->trans('Ad updated')));
                 return $app->redirect($app['url_generator']->generate('ads_index'), 301);
             }
             $this->view['id'] = $id;
             $this->view['form'] = $form->createView();
         } else {
             return $app->redirect($app['url_generator']->generate('ads_add'), 301);
         }
     } else {
         throw new ForbiddenException("You lack authority", 403);
     }
     return $app['twig']->render('ads/edit.twig', $this->view);
 }