/** * Create a post revision * * @param int $id * @param string $newTitle * @param string $newContent * @param Post $new post object * @param boolean $force */ public function create($currentTitle, $currentContent, Post $new, $force = false) { if (!$force) { if ($currentTitle == $new->getTitle() && $currentContent == $new->getContent()) { return; } } $em = $this->controller->getDoctrine()->getManager(); //create new $post = new Post(); $post->setTitle($currentTitle); $post->setContent($currentContent); $post->setType('history:' . $new->getId()); $post->setSlug($new->getSlug()); $post->setCreatedAt(new \DateTime()); $post->setUpdatedAt(new \DateTime()); $post->setDeletedAt(new \DateTime('0000-00-00')); $post->setPublishedAt($new->getPublishedAt()); $post->setStatus($new->getStatus()); $post->setUser($this->controller->getUser()); $em->persist($post); $em->flush(); }
/** * @Route("/blog/post/save", name="drafterbit_blog_post_save") * @Template() */ public function saveAction(Request $request) { $requestPage = $request->request->get('blog_post'); $id = $requestPage['id']; $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('DrafterbitBlogBundle:Post')->find($id); $isNew = false; if (!$post) { $post = new Post(); $isNew = true; } // for revision $currentTitle = $post->getTitle(); $currentContent = $post->getContent(); // tags needs sparate input $tagLabels = $request->request->get('tags'); if ($tagLabels) { foreach ($tagLabels as $label) { $tag = $em->getRepository('DrafterbitBlogBundle:Tag')->findOneBy(['label' => $tagLabels]); if (!$tag) { $tag = new Tag(); $tag->setLabel($label); $tag->setSlug(static::slug($label)); $em->persist($tag); $tag; } } } $em->flush(); $tags = $em->getRepository('DrafterbitBlogBundle:Tag')->findBy(['label' => $tagLabels]); $post->setTags($tags); $form = $this->createForm(new PostType(), $post); $form->handleRequest($request); if ($form->isValid()) { //save data to database $post = $form->getData(); $publishedAt = $form->get('published_at')->getData(); $post->setUser($this->getUser()); $post->setUpdatedAt(new \DateTime()); $post->setPublishedAt(new \DateTime($publishedAt)); if ($isNew) { $post->setCreatedAt(new \DateTime()); $post->setDeletedAt(new \DateTime('0000-00-00')); // @todo create revision $post->setType('standard'); } // create revision first if (!$isNew) { (new Revision($this))->create($currentTitle, $currentContent, $post); } $em->persist($post); $em->flush(); $id = $post->getId(); // log $logger = $this->get('logger'); $logger->info("%author% edited post %post%", ['author' => $this->getUser()->getId(), 'post' => $id]); $response = ['message' => $this->get('translator')->trans('Post saved'), 'status' => 'success', 'id' => $id]; } else { $errors = []; $formView = $form->createView(); // @todo clean this, make a recursive // create service, FormErrorExtractor maybe foreach ($formView as $inputName => $view) { if ($view->children) { foreach ($view->children as $name => $childView) { if (isset($childView->vars['errors'])) { foreach ($childView->vars['errors'] as $error) { $errors[$childView->vars['full_name']] = $error->getMessage(); } } } } if (isset($view->vars['errors'])) { foreach ($view->vars['errors'] as $error) { $errors[$view->vars['full_name']] = $error->getMessage(); } } } $response['error'] = ['type' => 'validation', 'messages' => $errors]; } return new JsonResponse($response); }