Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
263 lines (237 sloc) 9.59 KB
<?php
/**
* Created by PhpStorm.
* User: vincent.foulon
* Date: 04/09/2017
* Time: 13:44
*/
namespace VFou\Cms\BlogBundle\Type;
use Doctrine\ORM\EntityManagerInterface;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Button;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use VFou\Cms\BlogBundle\Entity\Blog;
use VFou\Cms\BlogBundle\Entity\Post;
use VFou\CmsBundle\Entity\Draft;
use VFou\CmsBundle\Entity\Page;
use VFou\CmsBundle\Form\Type\DateTimePickerType;
use VFou\CmsBundle\Service\CacheService;
use VFou\CmsBundle\Service\ConfigurationService;
use VFou\CmsBundle\Type\AbstractType;
use Twig\Environment;
class BlogType extends AbstractType
{
const INDEX = 0;
const DETAIL = 1;
/** @var RouterInterface */
protected $router;
/** @var ConfigurationService */
protected $conf;
protected $mode;
/** @var Post */
protected $post;
protected $token;
public function __construct(EntityManagerInterface $em, Environment $twig, RouterInterface $router, ConfigurationService $conf)
{
parent::__construct($em, $twig);
$this->router = $router;
$this->conf = $conf;
}
public function initialize($name = "Blog", $icon = "/bundles/vfoucmsblog/icons/blog.svg")
{
parent::initialize($name, $icon);
}
public function prepareDisplay(Request $request, Page $page, &$options)
{
if(empty($options["route"])){
$this->mode = self::INDEX;
} else {
$this->mode = self::DETAIL;
$this->post = $this->em->getRepository(Post::class)->complexfindOneBy([
"normalizedTitle" => "= '".$options["route"]."'",
"hidden" => "= 0",
"startDate" => "IS NULL OR p.startDate < CURRENT_TIMESTAMP()",
"endDate" => "IS NULL OR p.startDate > CURRENT_TIMESTAMP()"
]);
if(!empty($this->post)){
$options["title"] = $this->post->getTitle();
$content = strip_tags($this->post->getContent());
if(strlen($content) > 150){
$content = substr($content, 0, $this->conf->get("configuration", "blog_previewlength"))."...";
}
$options["description"] = $content;
} else {
throw new NotFoundHttpException();
}
}
if($request->query->has("fedit")) {
$this->token = $request->query->get("fedit");
if($request->query->has("new")) {
$this->mode = self::DETAIL;
}
}
}
public function display(Page $page, $options = []){
if($this->mode == self::INDEX){
setlocale(LC_TIME, "fr_FR.utf8", "fra");
$blog = $this->em->getRepository(Blog::class)->findOneById($page->getData());
$html = $this->twig->render("@VFouCmsBlog/Type/blog_index.html.twig", ["page"=>$page, "previewlength"=>$this->conf->get("configuration", "blog_previewlength"), "blog"=>$blog]);
} else {
$html = $this->twig->render("@VFouCmsBlog/Type/blog_detail.html.twig", ["post"=>$this->post]);
}
return $html;
}
public function addForm(FormBuilderInterface $fb, Page $page){
$blogRoute = $this->router->generate("admin_blog_index");
$selectedBlog = $this->em->getRepository(Blog::class)->findOneById($page->getData());
$fb->add("blog", EntityType::class, [
"class"=> Blog::class,
"label"=> "blog",
"choice_label"=> "title",
"mapped"=>false,
"data" => $selectedBlog,
"translation_domain" => "forms"
])
/*->add("manageBlog", ButtonType::class, [
"label" => "blog.manage",
"attr"=> [
"onClick"=>"window.location.href='$blogRoute';"
],
"translation_domain" => "forms"
])*/;
return $fb;
}
public function frontendEditing(FormBuilderInterface $fb, Page $page, Draft $draft, &$options)
{
if($this->mode == self::INDEX){
if(!empty($draft->getData()["blog"])){
$page->setData($draft->getData()["blog"]);
}
$this->addForm($fb, $page);
$options["beforeHTML"] = "<a class='btn btn-primary' href='?fedit=".$this->token."&new=1'>+</a>";
$options["afterHTML"] = $this->display($page, $options);
} else {
$form = $draft->getData();
if($this->post == null){
$this->post = new Post();
$this->post->setTitle("New Post");
$this->post->setContent("Hello !");
}
if(is_array($form) && !empty($form[$this->post->getId()])){
$form = $form[$this->post->getId()];
$this->post->setTitle($form["title"]);
$this->post->setContent($form["content"]);
}
$options["beforeHTML"] = "<h2 id='post-title'>".$this->post->getTitle()."</h2>";
$options["afterHTML"] = "<script>
window.onload = function(){
var input = $('#post-title');
input.bind('input', function(){
$('#form_title').val($('#post-title').html());
detectSaveNoRefresh();
});
input.prop('contentEditable', 'true');
//parent.hookSubmitSuccess = function(response){ window.location.href = '/".$page->getLink()."'+ response + window.location.search.replace('&new=1',''); }
}
</script>";
$fb->add("post_id", HiddenType::class, [
"data" => $this->post->getId() ?? -2
])
->add("title", HiddenType::class, [
"data" => $this->post->getTitle()
])
->add('content', CKEditorType::class, [
"config_name" => "inline",
'label_attr' => ["style"=>"display:none;"],
"input_sync" => true,
"inline" => true,
"data" => $this->post->getContent()
]);
}
return $fb;
}
public function fEditingHandle(Request $request, Page &$page, Draft &$draft)
{
$form = $request->request->get("form");
$posts = $draft->getData();
if(!is_array($posts)){
$posts = [];
}
if(empty($form["post_id"])){
// index
$posts["blog"] = $form["blog"];
$posts["lastEdited"] = -1;
} else {
// detail
$posts[$form["post_id"]]["title"] = $form["title"];
$posts[$form["post_id"]]["content"] = $form["content"];
$posts["lastEdited"] = $form["post_id"];
}
$draft->setData($posts);
}
public function fEditingSubmit(Page &$page, Draft $draft)
{
$data = $draft->getData();
if($data["lastEdited"] == -1){
// index
$page->setData($data["blog"]);
unset($data["blog"]);
$draft->setData($data);
$this->em->persist($draft);
} else {
// detail
$id = $data["lastEdited"];
$post = $this->em->getRepository(Post::class)->findOneById($id);
if($post === null){
$post = new Post();
$post->setBlog($this->em->getRepository(Blog::class)->findOneById($page->getData()));
$post->setStartDate(new \DateTime());
}
$post->setTitle(strip_tags($data[$id]["title"]));
$post->setContent($data[$id]["content"]);
$this->em->persist($post);
// remove draft of the post
unset($data[$id]);
unset($data["lastEdited"]);
$draft->setData($data);
$this->em->persist($draft);
$this->em->flush();
return $post->getNormalizedTitle();
}
return null;
}
public function getPrevious(Page $page)
{
if($this->mode == self::DETAIL){
return $page;
}
return $page->getPid();
}
public function handleRequest(Request $request, Page &$page){
$blog_id = $request->request->get("vfou_cmsbundle_page")["blog"];
$page->setData($blog_id);
}
public function handleUrl($route, Page &$page)
{
return $this->em->getRepository(Post::class)->findOneByNormalizedTitle($route) != null;
}
public function getCacheLevel(){return CacheService::CUSTOM_CHILDREN_DEPENDENCE;}
public function clearCache(CacheService $cache, Page $page)
{
$blog = $this->em->getRepository(Blog::class)->findOneById($page->getData());
$posts = $this->em->getRepository(Post::class)->findByBlog($blog);
foreach($posts as $post){
$cache->removeUrl("",$page->getLink().$post->getNormalizedTitle());
}
}
}
You can’t perform that action at this time.