/**
  * 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 #2
0
 public function addAction()
 {
     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()) {
                 $lastId = $post->save();
                 $userPosts = new UserPosts();
                 $userPosts->post_id = (int) $lastId;
                 $userPosts->user_id = (int) Service::get('security')->getUser()->id;
                 $userPosts->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('add.html', array('action' => $this->generateRoute('add_post'), 'errors' => isset($error) ? $error : null));
 }
Example #3
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')));
 }
 /**
  * Add post action
  *
  * @access public
  *
  * @return Response|\Framework\Response\ResponseRedirect
  * @throws BadTokenException
  */
 public function addAction()
 {
     if ($this->getRequest()->isPost()) {
         if (!$this->getRequest()->checkToken('token')) {
             throw new BadTokenException('You do not have permission for this operation !', 403);
         }
         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->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('add.html', array('action' => $this->generateRoute('add_post'), '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));
 }
 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));
 }
    $user = new User();
    $user->setUsername($username);
    // save user
    $user->save();
    writeln($user);
}
foreach (array('Web', 'Life', 'Open Source', 'PHP') as $value) {
    $category = new Category();
    $category->setName($value);
    $user = UserQuery::create()->findPk(rand(1, 3));
    $category->setUser($user);
    // save category
    $category->save();
    // create action
    $actionManager->createAction($category->getUser(), Category::CREATE_CATEGORY, $category);
    writeln($category);
}
for ($i = 0; $i <= 20; $i++) {
    $post = new Post();
    $post->setTitle('Post title ' . $i);
    $post->setBody('Post body ' . $i);
    $user = UserQuery::create()->findPk(rand(1, 3));
    $post->setUser($user);
    $category = $user = CategoryQuery::create()->findPk(rand(1, 9));
    $post->setCategory($category);
    // create post
    $post->save();
    // create action
    $actionManager->createAction($post->getUser(), Post::CREATE_POST, $post, $post->getCategory());
    writeln($post);
}