/** * Action - test/addpost * add post via Active Record * * @return string */ public function addpostAction() { $request = $this->getRequest(); $locale = $this->getLocale(); $format = $locale == 'ru' ? 'd.m.Y' : 'm/d/Y'; $db = $this->app['ar']; //-------------------- try { // Initialization $this->init(__CLASS__ . "/" . __FUNCTION__); // Create object NewPost and set values $ormPost = new Post(); if ($this->isMethod('GET')) { $ormPost->setCreated(date($format)); } // Create form $form = $this->createForm(new PostForm(), $ormPost, array('action' => "/test/addpost")); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Get username $user = $this->getUser(); $username = $user->getUsername(); // Get user $user = $db->find('user', 'first', array('conditions' => "username='******'")); // Create model post and set values $arPost = $db->create('post', $ormPost->getValues()); $arPost->user_id = $user->id; $arPost->saveModel(); // Send flash message $this->addFlash('info_message', $this->trans('added_new_message', array('{{ title }}' => $ormPost->getTitle()))); // Go to "/account" return $this->redirect("/account"); } // Show form return $this->showView(array('form' => $form->createView())); } catch (\Exception $exc) { return $this->showError($exc); } }
/** * Action - blog/edit * edit post for current user * * @param int $id * @return string */ public function editAction($id) { $models = $this->app['models']; $request = $this->getRequest(); $locale = $this->getLocale(); $format = $locale == 'ru' ? 'd.m.Y' : 'm/d/Y'; $db = $this->app['db']; //-------------------- try { // Initialization $this->init(__CLASS__ . "/" . __FUNCTION__); // Get post for id $arrPost = $models->load('Post', 'getPost', $id); $date = new \DateTime($arrPost['created']); $arrPost['created'] = $date->format($format); // Set values $ormPost = new Post(); $ormPost->setValues($arrPost); // Create form $form = $this->createForm(new PostForm(), $ormPost, array('action' => "/blog/edit/{$id}")); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Save post $models->load('Post', 'editPost', array('edit_post' => $ormPost)); $this->addFlash('info_message', $this->trans('edited_this_message', array('{{ title }}' => $ormPost->getTitle()))); return $this->redirect("/account"); } // Show form return $this->showView(array('form' => $form->createView())); } catch (\Exception $exc) { return $this->showError($exc); } }