Beispiel #1
0
 /**
  * Returns and processes form for adding post
  *
  * @return string
  */
 public function addAction()
 {
     if (!$this->getUser()) {
         $this->redirect('/', 'Please, login first!');
     }
     $post = new \stdClass();
     $errors = array();
     if (Request::isPost()) {
         $model = new Post();
         $date = new \DateTime();
         $date->setTimezone(new \DateTimeZone(\Application::getConfig('timezone')));
         $model->set('title', Request::get('title'))->set('content', Request::get('content'))->set('author_id', $this->getUser()->id)->set('updated_at', $date->format('Y-m-d H:i:s'));
         if ($model->isValid()) {
             try {
                 $model->insert();
                 $this->redirect('/', 'The data has been saved successfully');
             } catch (DatabaseException $e) {
                 array_push($errors, $e->getMessage());
             }
         } else {
             $errors = $model->getErrors();
             $post = $model->getFieldsObject();
         }
     }
     return $this->_renderView('form.html', array('post' => $post, 'errors' => $errors, 'action' => '/posts/add'));
 }
Beispiel #2
0
 /**
  * Renders login form and authenticates user after form submitting
  *
  * @return string
  */
 public function loginAction()
 {
     $this->_redirectIfLoggedIn();
     $errors = array();
     if (Request::isPost()) {
         $model = new SecurityModel();
         if ($item = $model->set('email', Request::get('email'))->getItem()) {
             if (0 === strcmp(Token::cryptPassword(Request::get('password'), $item->salt), $item->password)) {
                 Token::setUser($item);
                 $this->redirect('/');
             }
         }
         array_push($errors, 'Invalid username or password');
     }
     return $this->_renderView('login.html', array('errors' => $errors));
 }