/** * 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); }
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)); }
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'))); }
public function updateAction() { $errors = array(); $msgs = array(); $user_id = Service::get('Session')->user->id; if (isset(Service::get('Session')->profile)) { $profile = Service::get('Session')->profile; } else { $profile = Profile::getProfile($user_id); } if (!$profile) { $profile = new Profile(); $profile->user_id = $user_id; } if ($this->getRequest()->isPost()) { try { $profile->name = $this->getRequest()->post('name'); $profile->second_name = $this->getRequest()->post('second_name'); $profile->info = $this->getRequest()->post('info'); $validator = new Validator($profile); if ($validator->isValid()) { $profile->save(); return $this->redirect($this->generateRoute('home'), 'The data has been saved successfully'); } else { $errors = $validator->getErrors(); } } catch (DatabaseException $e) { $msgs = $e->getMessage(); } } return $this->render('profile.html', array('errors' => $errors, 'msgs' => $msgs, 'profile' => $profile)); }
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))); }
/** * 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)); }
/** * 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)); }
/** * Length validation * * @access public * * @param string $param * @param string $paramName * @param Validator $validator * * @return void */ public function validate($param, $paramName, Validator $validator) { $strLength = strlen($param); if ($strLength < $this->minLength) { $validator->setError($paramName, "Enter more than {$this->minLength} characters!"); } elseif ($strLength > $this->maxLength) { $validator->setError($paramName, "Enter less than {$this->maxLength} characters!"); } }
/** * Set new avatar * * @return mixed */ public function setAvatarAction() { if ($this->getRequest()->isPost()) { $user = new User(); $user->user_avatar = $this->getRequest()->file('avatarupload'); $validator = new Validator($user); if ($validator->isAvatarValid()) { $user->avatarSave(); } else { $error = $validator->getErrors(); } } return true; }
/** * Save avatar * * @return \Framework\Response\Response */ public function avatarAction() { if ($this->getRequest()->isPost()) { $user = new User(); $user->user_avatar = $this->getRequest()->file('avatarupload'); $validator = new Validator($user); if ($validator->isAvatarValid()) { $user->avatarSave(); } else { $error = $validator->getErrors(); } } $profile = User::findInfo(); return $this->render('profile.html', ['info' => $profile, '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)); }
/** * 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))); }
/** * NotBlank validation * * @access public * * @param string $param * @param string $paramName * @param Validator $validator * * @return void */ public function validate($param, $paramName, Validator $validator) { if (trim($param) == null) { $validator->setError($paramName, "Please fill this field!"); } }
/** * Filter value of transmitted variable * * @param $value * @param string $filter * @return mixed|null */ protected function filter($value, $filter = 'string') { $validator = new Validator(); return $validator->validation($value, $filter); }