Example #1
0
 public function showAction($id)
 {
     if (!($post = Post::find((int) $id))) {
         throw new HttpNotFoundException('Page Not Found!');
     }
     return $this->render('show.html', array('post' => $post));
 }
Example #2
0
 public function editAction($postId)
 {
     if (!Service::get('security')->isAuthenticated()) {
         throw new AuthRequredException('You need authorizate for this action');
     }
     try {
         $post = new Post();
         $date = new \DateTime();
         $post->id = $postId;
         $post->title = $this->getRequest()->post('title');
         $post->content = trim($this->getRequest()->post('content'));
         $post->date = $date->format('Y-m-d H:i:s');
         $post->user_id = Service::get('security')->getUser()->id;
         $validator = new Validator($post);
         if ($validator->isValid()) {
             $post->save();
             return $this->redirect($this->generateRoute('home'), 'The data has been saved successfully');
         } else {
             $error = $validator->getErrors();
         }
     } catch (DatabaseException $e) {
         $error = $e->getMessage();
     }
     if (!($post = Post::find((int) $postId))) {
         throw new HttpNotFoundException(404);
     }
     return $this->render('add.html', array('post' => $post, 'errors' => isset($error) ? $error : null, 'action' => $this->generateRoute('edit_post', array('id' => $postId)), 'src' => array('src' => 'Blog', 'controller' => 'Post')));
 }
 /**
  * Serves for editing post
  *
  * @route /posts/{$id}/edit
  * @param $id
  * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect
  * @throws HttpNotFoundException
  */
 function editAction($id)
 {
     $id = (int) $id;
     if ($this->getRequest()->isPost()) {
         try {
             $post = new Post();
             $date = new \DateTime();
             $post->id = $id;
             $post->title = $this->getRequest()->post('title');
             $post->content = trim($this->getRequest()->post('content'));
             $post->date = $date->format('Y-m-d H:i:s');
             $validator = new Validator($post);
             if ($validator->isValid()) {
                 $post->save();
                 return $this->redirect($this->generateRoute('login'), 'The data has been update successfully');
             } else {
                 $error = $validator->getErrors();
             }
         } catch (DatabaseException $e) {
             $error = $e->getMessage();
         }
     }
     $post = Post::find($id);
     if (is_null($post)) {
         throw new HttpNotFoundException('Page Not Found!');
     }
     $date['post'] = $post;
     $date['action'] = $this->generateRoute('edit_post', $post->id);
     $date['errors'] = isset($error) ? $error : null;
     return $this->render('edit.html', $date);
 }
Example #4
0
 public function editAction($id)
 {
     $route = Service::get('route');
     $post = Post::find((int) $id);
     $session = Service::get('session');
     $user = $session->get('user');
     if (Service::get('security')->isAuthenticated()) {
         if ($user->role == 'ROLE_ADMIN') {
             if ($this->getRequest()->isPost()) {
                 try {
                     $post = new Post();
                     $date = new \DateTime();
                     $post->title = $this->getRequest()->post('title');
                     $post->content = trim($this->getRequest()->post('content'));
                     $post->date = $date->format('Y-m-d H:i:s');
                     $validator = new Validator($post);
                     if ($validator->isValid()) {
                         $post->update('id', $id);
                         return $this->redirect($this->generateRoute('home'), 'The data has been update successfully');
                     } else {
                         $error = $validator->getErrors();
                     }
                 } catch (DatabaseException $e) {
                     $error = $e->getMessage();
                 }
             }
         } else {
             throw new SecurityException('You are not allowed posts updating', $this->getRequest()->getReferrer());
         }
     } else {
         throw new SecurityException('Please, login', $route->buildRoute('login'));
     }
     $renderer = new Renderer();
     return new Response($renderer->render(__DIR__ . '/../../Blog/views/Post/add.html.php', array('action' => $this->generateRoute('edit'), 'post' => isset($post) ? $post : null, 'show' => 'check', 'errors' => isset($error) ? $error : null)));
 }
Example #5
0
 /**
  * Edit post.
  *
  * @param $id
  * @return \Framework\Response\ResponseRedirect
  * @throws HttpNotFoundException
  * @throws \Framework\Exception\DatabaseException
  */
 public function editAction($id)
 {
     $dirty_request = new Request(null, false);
     if ($this->getRequest()->isPost()) {
         try {
             $post = new Post();
             $date = new \DateTime();
             $post->id = $id;
             $post->title = $dirty_request->post('title');
             $post->content = $dirty_request->post('content');
             $post->date = $date->format('Y-m-d H:i:s');
             $post->users_id = Service::get('session')->get('authenticated')->id;
             $validator = new Validator($post);
             if ($validator->isValid()) {
                 $post->save($id);
                 return $this->redirect($this->generateRoute('home'), 'success', 'The post has been edit successfully');
             } else {
                 $error = $validator->getErrors();
             }
         } catch (DatabaseException $e) {
             $error = $e->getMessage();
         }
     }
     $post = Post::find((int) $id);
     return $this->render('add.html', array('post' => $post, 'action' => '/posts/' . $id . '/edit', 'errors' => isset($error) ? $error : null));
 }
Example #6
0
 public function showAction($id)
 {
     if (!($post = Post::find((int) $id))) {
         throw new HttpNotFoundException(404);
     }
     if (Service::get('security')->isAuthenticated() && $post->user_id === Service::get('security')->getUser()->id) {
         return $this->render('add.html', array('post' => $post, 'action' => $this->generateRoute('edit_post', array('id' => $post->id))));
     }
     return $this->render('show.html', array('post' => $post));
 }
Example #7
0
 public function editAction($id, Request $request)
 {
     $id = (int) $id;
     $errors = [];
     if ($request->isPost()) {
         try {
             $date = new \DateTime();
             $id = Post::where(['id' => $id])->update(['title' => $request->post('title'), 'content' => $request->post('content'), 'date' => $date->format('Y-m-d H:i:s')]);
             return $this->redirect($this->generateRoute('home'), 'The data has been updated successfully');
         } catch (DatabaseException $e) {
             $errors[] = $e->getMessage();
         }
     }
     return $this->render('edit.html', ['post' => Post::find($id), 'action' => $this->generateRoute('edit_post', ['id' => $id]), 'errors' => $errors ?? null]);
 }
 public function editAction($id)
 {
     if ($this->getRequest()->isPost()) {
         try {
             $post = new Post();
             $date = new \DateTime();
             $post->id = $id;
             $post->title = $this->getRequest()->post('title');
             $post->content = trim($this->getRequest()->post('content'));
             $post->date = $date->format('Y-m-d H:i:s');
             $validator = new Validator($post);
             if ($validator->isValid()) {
                 $post->save();
                 return $this->redirect($this->generateRoute('home'), 'The data has been saved successfully');
             } else {
                 $error = $validator->getErrors();
             }
         } catch (DatabaseException $e) {
             $error = $e->getMessage();
         }
     }
     return $this->render('update.html', array('post' => Post::find($id), 'action' => $this->generateRoute('edit_post'), 'errors' => isset($error) ? $error : null));
 }
Example #9
0
 /**
  * Performs the editing an appropriate post if it possible otherwise shows error messages
  * @param $id
  * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect
  */
 public function editAction($id)
 {
     $errors = array();
     if ($this->getRequest()->isPost()) {
         try {
             $post = Post::find($id);
             $post->title = $this->getRequest()->post('title');
             $post->content = $this->getRequest()->post('content');
             //Verifies if the table record meets the requirement
             $validator = new Validator($post);
             if ($validator->isValid()) {
                 $post->save();
                 return $this->redirect($this->generateRoute('profile'), 'You have successfully edited your article "' . $post->title . '"');
             } else {
                 $errors = $validator->getErrors();
             }
         } catch (DatabaseException $e) {
             $errors = array($e->getMessage());
         }
     }
     //Displays error messages on the page
     return $this->render('start_edit.html', array('errors' => isset($errors) ? $errors : null, 'post' => Post::find($id)));
 }
Example #10
0
 function listAction()
 {
     return $this->render('list.html', array('posts' => Post::find('all')));
 }