Exemple #1
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)));
 }
 public function findPost($id)
 {
     $postData = $this->data[$id];
     $model = new Post();
     $model->setId($postData['id']);
     $model->setTitle($postData['title']);
     $model->setText($postData['text']);
     return $model;
 }
Exemple #3
0
 public function install()
 {
     $post = new Post();
     $user = new User();
     $category = new Category();
     $postcategory = new PostCategory();
     Model::execute($category->install());
     Model::execute($user->install());
     Model::execute($post->install());
     Model::execute($postcategory->install());
 }
 public function showAction($id)
 {
     if (!($post = Post::find((int) $id))) {
         throw new HttpNotFoundException('Page Not Found!');
     }
     return $this->render('show.html', array('post' => $post));
 }
 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')));
 }
 /**
  * Delete from database.
  *
  * @param $id
  * @return \Framework\Response\ResponseRedirect
  * @throws HttpNotFoundException
  */
 public function deleteAction($id)
 {
     if ($post = Post::delete((int) $id)) {
         return $this->redirect($this->generateRoute('home'), 'success', 'The post has been delete successfully!');
     } else {
         throw new HttpNotFoundException('Page Not Found!');
     }
 }
 /**
  * Saves a comment and sends an email
  *
  * @param array $data
  * @return boolean
  **/
 public function save($data = array())
 {
     $data['comment'] = nl2br($data['comment']);
     $this->form->setData($data);
     if ($this->form->isValid()) {
         $comment = $this->form->getData();
         $comment->setDateAdded(new DateTime());
         $comment->setPost($this->post);
         $comment->setStatus(1);
         $comment->setIpAddress($_SERVER['REMOTE_ADDR']);
         //For display purposes
         if ($comment->getParentId()) {
             foreach ($this->post->getComments() as $oldComment) {
                 if ($oldComment->getId() == $comment->getParentId()) {
                     if ($oldComment->getParentId()) {
                         $comment->setParentId($oldComment->getParentId());
                     }
                     break;
                 }
             }
         }
         $this->em->persist($comment);
         $this->em->flush();
         $this->post->addComment($comment);
         $this->form->setData(array('name' => null, 'website' => null, 'email' => null, 'comment' => null, 'parent' => array('id' => 0)));
         $message = strip_tags($comment->getComment()) . "\r\n\r\n";
         $message .= $comment->getEmail() . "\r\n\r\n";
         $message .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
         $mail = new Message();
         $mail->setBody($message);
         $mail->setFrom('*****@*****.**');
         $mail->addTo('*****@*****.**', 'Rob Keplin');
         $mail->setSubject('New comment on robkeplin.com');
         $transport = new Sendmail();
         try {
             $transport->send($mail);
         } catch (RuntimeException $e) {
             $this->addMessage('A notification email was not sent due to sendmail failing. Doh!', self::MSG_ERROR);
         }
         $this->addMessage('Thanks for commenting.', self::MSG_NOTICE);
         return true;
     }
     $this->addMessage('Hey, Wait!  Something went wrong down there.  Please try commenting again, it did not go through.', self::MSG_ERROR);
     return false;
 }
 /**
  * Displays profile if it possible otherwise redirects to login page
  * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect
  */
 public function getAction()
 {
     if (Service::get('security')->isAuthenticated()) {
         return $this->render('get.html', array('posts' => Post::findByParams(array('user_id' => Service::get('session')->get('user')->id))));
     }
     //Sets the return page
     Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']);
     return $this->redirect($this->generateRoute('login'), 'You need to login');
 }
 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));
 }
 public function newAction()
 {
     //get CategoryTable thong qua TableGateway va truyen vao PostForm()
     $table = $this->getServiceLocator()->get('Blog\\Model\\CategoryTable');
     $form = new PostForm($table);
     //        print "<pre>";
     //            print_r($form);
     //            exit();
     $form->get('submit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = new Post();
         $form->setInputFilter($post->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $post->exchangeArray($form->getData());
             $this->getPostTable()->savePost($post);
             return $this->redirect()->toRoute('posts');
         }
     }
     return array('form' => $form);
 }
 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));
 }
 public function addAction()
 {
     $formPost = new PostForm($this->categoryTable);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = new Post();
         $formPost->setInputFilter($post->getInputFilter());
         $nonFiles = $this->getRequest()->getPost()->toArray();
         $files = $this->getRequest()->getFiles()->toArray();
         // Pour ZF 2.2.x uniquement
         $data = array_merge_recursive($nonFiles, $files);
         $formPost->setData($data);
         if ($formPost->isValid()) {
             $size = new Size(array('max' => 716800));
             $adapter = new \Zend\File\Transfer\Adapter\Http();
             $adapter->setValidators(array($size), $files['image']);
             if (!$adapter->isValid()) {
                 $dataError = $adapter->getMessages();
                 $error = array();
                 foreach ($dataError as $key => $row) {
                     $error[] = $row;
                 }
                 $formPost->setMessages(array('image' => $error));
             } else {
                 $adapter->setDestination('./public/img/');
                 if ($adapter->receive($files['image']['name'])) {
                     $post->exchangeArray($formPost->getData());
                     $post->image = $files['image']['name'];
                     $this->postTable->savePost($post);
                     return $this->redirect()->toRoute('home');
                 }
             }
         }
     }
     return new ViewModel(array('form' => $formPost));
 }
Exemple #13
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]);
 }
 /**
  * Creates a blog post
  *
  * @parmam void
  * @return mixed {Zend\Http\PhpEnvironment\Response, ViewModel}
  * @throws AccessProhibitedException
  **/
 public function createAction()
 {
     $this->_checkAcl('create');
     $userService = new UserService($this->_em);
     $auth = $userService->getAuthService();
     $user = $userService->findById($auth->getIdentity()->getId());
     $post = new Post();
     $post->setDateAdded(new DateTime());
     $post->setUser($user);
     $form = new PostForm();
     $form->bind($post);
     $categoryService = new CategoryService($this->_em);
     $form->setCategoryList($categoryService->getAll());
     $service = new PostService($this->_em);
     $service->setForm($form);
     $request = $this->getRequest();
     if ($request->isPost()) {
         if ($service->save($request->getPost())) {
             $params = array('controller' => 'admin', 'action' => 'index');
             return $this->redirect()->toRoute('blog/default', $params);
         }
     }
     return new ViewModel(array('form' => $form, 'messages' => $this->_postService->getMessages(PostService::MSG_NOTICE), 'errors' => $this->_postService->getMessages(PostService::MSG_ERROR)));
 }
 /**
  * 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)));
 }
 /**
  * @param	Post $post The post object to add.
  */
 protected function doAddPost($post)
 {
     $this->collPosts[] = $post;
     $post->setUser($this);
 }
 /**
  * @param	Post $post The post object to add.
  */
 protected function doAddPost($post)
 {
     $this->collPosts[] = $post;
     $post->setCategory($this);
 }
    $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);
}
 /**
  * Serves for removing post
  *
  * @route /posts/{$id}/remove
  * @param $id
  * @return \Framework\Response\ResponseRedirect
  */
 function removeAction($id)
 {
     Post::remove($id);
     return $this->redirect('/', 'The post has been deleted');
 }
 /**
  * Filter the query by a related Post object
  *
  * @param     Post $post  the related object to use as filter
  * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
  *
  * @return    UserQuery The current query, for fluid interface
  */
 public function filterByPost($post, $comparison = null)
 {
     if ($post instanceof Post) {
         return $this->addUsingAlias(UserPeer::ID, $post->getUserId(), $comparison);
     } elseif ($post instanceof PropelCollection) {
         return $this->usePostQuery()->filterByPrimaryKeys($post->getPrimaryKeys())->endUse();
     } else {
         throw new PropelException('filterByPost() only accepts arguments of type Post or PropelCollection');
     }
 }
 /**
  * Exclude object from result
  *
  * @param     Post $post Object to remove from the list of results
  *
  * @return    PostQuery The current query, for fluid interface
  */
 public function prune($post = null)
 {
     if ($post) {
         $this->addUsingAlias(PostPeer::ID, $post->getId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
Exemple #22
0
 /**
  * Show user all posts
  *
  * @param $id
  * @return \Framework\Response\ResponseRedirect
  * @throws HttpNotFoundException
  */
 public function showUserPostsAction($id)
 {
     if ($posts = Post::findByUsers_id((int) $id, 50)) {
         $user = User::findById((int) $id);
         $name = $user->name ? $user->name : 'NoNaMe person';
         foreach ($posts as $post) {
             $post->name = $name;
         }
         return $this->render('index.html', array('posts' => $posts));
     } else {
         throw new HttpNotFoundException('Page Not Found!');
     }
 }
Exemple #23
0
 function removeAction($id)
 {
     Post::remove((int) $id);
     return $this->redirect($this->generateRoute('list_post'), 'Post ' . $id . ' was remove successfully!');
 }