Skip to content

Feature/blog #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 170 additions & 9 deletions Bundle/BlogBundle/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Victoire\Bundle\BlogBundle\Entity\Blog;
use Victoire\Bundle\PageBundle\Controller\BasePageController;
use Victoire\Bundle\PageBundle\Entity\BasePage;
use Victoire\Bundle\BlogBundle\Form\ChooseBlogType;

/**
* blog Controller
Expand All @@ -30,6 +32,8 @@ public function __construct()
'new' => 'victoire_blog_index',
'show' => 'victoire_core_page_show',
'settings' => 'victoire_blog_settings',
'articles' => 'victoire_blog_articles',
'category' => 'victoire_blog_category',
'translate' => 'victoire_blog_translate',
'delete' => 'victoire_blog_delete',
);
Expand All @@ -39,21 +43,43 @@ public function __construct()
* New page
*
* @Route("/", name="victoire_blog_index")
* @Template()
*
* @return JsonResponse
*/
public function indexAction($isHomepage = false)
public function indexAction(Request $request, $isHomepage = false)
{
$blogs = $this->get('doctrine.orm.entity_manager')
->getRepository('VictoireBlogBundle:Blog')
->getAll()->run();
$blogRepo = $this->get('doctrine.orm.entity_manager')
->getRepository('VictoireBlogBundle:Blog');
$blogs = $blogRepo->getAll()->run();
$blog = reset($blogs);
$options['blog'] = $blog;
$template = $this->getBaseTemplatePath() . ':index.html.twig';
$chooseBlogForm = $this->createForm(new ChooseBlogType(), null, $options);


$chooseBlogForm->handleRequest($request);
if ($chooseBlogForm->isValid()) {
$blog = $chooseBlogForm->getData()['blog'];
$template = $this->getBaseTemplatePath() . ':_blogItem.html.twig';
$chooseBlogForm = $this->createForm(new ChooseBlogType(), null, array('blog' => $blog));
}
$businessProperties = array();

if ($blog instanceof BusinessEntityPagePattern) {
//we can use the business entity properties on the seo
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($blog->getBusinessEntityName());
$businessProperties = $businessEntity->getBusinessPropertiesByType('seoable');
}

return new JsonResponse(
array(
'html' => $this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath().':index.html.twig', array(
'blogs' => $blogs
$template,
array(
'blog' => $blog,
'tabs' => array('articles', 'settings', 'category'),
'chooseBlogForm' => $chooseBlogForm->createView(),
'businessProperties' => $businessProperties
)
)
)
Expand Down Expand Up @@ -81,12 +107,138 @@ public function newAction($isHomepage = false)
*
* @return JsonResponse
* @Route("/{id}/settings", name="victoire_blog_settings")
* @Template()
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*/
public function settingsAction(Request $request, BasePage $blog)
{
return new JsonResponse(parent::settingsAction($request, $blog));
$entityManager = $this->getDoctrine()->getManager();
$form = $this->createForm($this->getPageSettingsType(), $blog);
$businessProperties = array();

//if the page is a business entity page
if ($blog instanceof BusinessEntityPagePattern) {
//we can use the business entity properties on the seo
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($blog->getBusinessEntityName());
$businessProperties = $businessEntity->getBusinessPropertiesByType('seoable');
}

$form->handleRequest($request);

if ($form->isValid()) {
$entityManager->persist($blog);
$entityManager->flush();

return new JsonResponse(array(
'success' => true,
'url' => $this->generateUrl('victoire_core_page_show', array('_locale' => $blog->getLocale(), 'url' => $blog->getUrl()))));
}
//we display the form
$errors = $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form);
if($errors != '')
{
return new JsonResponse(array('html' => $this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath() . ':Tabs/_settings.html.twig',
array(
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties
)
),
'message' => $errors
)
);
}
return new Response($this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath() . ':Tabs/_settings.html.twig',
array(
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties
)
)
);
}

/**
* Blog settings
*
* @param Request $request
* @param Page $blog
*
* @return template
* @Route("/{id}/category", name="victoire_blog_category")
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*/
public function categoryAction(Request $request, BasePage $blog)
{
$entityManager = $this->getDoctrine()->getManager();
$form = $this->createForm($this->getPageCategoryType(), $blog);
$businessProperties = array();

//if the page is a business entity page
if ($blog instanceof BusinessEntityPagePattern) {
//we can use the business entity properties on the seo
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($blog->getBusinessEntityName());
$businessProperties = $businessEntity->getBusinessPropertiesByType('seoable');
}

$form->handleRequest($request);

if ($form->isValid()) {
$entityManager->persist($blog);
$entityManager->flush();

return new JsonResponse(array(
'success' => true,
'url' => $this->generateUrl('victoire_core_page_show', array('_locale' => $blog->getLocale(), 'url' => $blog->getUrl()))));
}
//we display the form
$errors = $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form);
if($errors != '')
{
return new JsonResponse(array('html' => $this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath() . ':Tabs/_category.html.twig',
array(
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties
)
),
'message' => $errors
)
);
}
return new Response($this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath() . ':Tabs/_category.html.twig',
array(
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties
)
)
);
}

/**
* Blog settings
*
* @param Request $request
* @param Page $blog
*
* @return template
* @Route("/{id}/articles", name="victoire_blog_articles")
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*/
public function articlesAction(Request $request, BasePage $blog)
{

return new Response($this->container->get('victoire_templating')->render(
$this->getBaseTemplatePath() . ':Tabs/_articles.html.twig',
array(
'blog' => $blog,
)
)
);
}

/**
Expand Down Expand Up @@ -133,6 +285,15 @@ protected function getPageSettingsType()
return 'victoire_blog_settings_type';
}

/**
*
* @return string
*/
protected function getPageCategoryType()
{
return 'victoire_blog_category_type';
}

/**
*
* @return string
Expand Down
1 change: 1 addition & 0 deletions Bundle/BlogBundle/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Article
/**
* Categories of the article
* @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
* @ORM\JoinColumn(onDelete="SET NULL")
*/
private $category;

Expand Down
45 changes: 45 additions & 0 deletions Bundle/BlogBundle/Entity/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,49 @@ public function getCategories()
{
return $this->categories;
}

/**
* Get root categories
*
* @return string
*/
public function getRootCategories()
{
$rootCategories = [];
foreach ($this->categories as $categories) {
if ($categories->getLvl() == 0) {
$rootCategories[] = $categories;
}
}
return $rootCategories;
}

/**
* Add rootCategory
*
* @param string $rootCategory
*
* @return Article
*/
public function addRootCategory($rootCategory)
{
$rootCategory->setBlog($this);
$this->categories[] = $rootCategory;

return $this;
}

/**
* Remove rootCategory
*
* @param string $rootCategory
*
* @return Article
*/
public function removeRootCategory($rootCategory)
{
$this->categories->removeElement($rootCategory);

return $this;
}
}
10 changes: 9 additions & 1 deletion Bundle/BlogBundle/Form/ArticleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$viewToIdTransformer = new ViewToIdTransformer($this->entityManager);
$blog = $builder->getData()->getBlog();
$categoryRepo = $this->entityManager->getRepository('Victoire\Bundle\BlogBundle\Entity\Category');
if($blog)
{
$queryBuilder = $categoryRepo->getOrderedCategories($blog)->getInstance();
}else{
$queryBuilder = $categoryRepo->getAll()->getInstance();
}
$builder
->add('name')
->add('description', null, array(
'required' => false))
->add('image', 'media')
->add('category', 'hierarchy_tree', array(
'class' => "Victoire\Bundle\BlogBundle\Entity\Category",
'query_builder' => $categoryRepo->getOrderedCategories($blog)->getInstance(),
'query_builder' => $queryBuilder,
'empty_value' => "Pas de catégorie",
"empty_data" => null
)
)
->add(
Expand Down
56 changes: 56 additions & 0 deletions Bundle/BlogBundle/Form/BlogCategoryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Victoire\Bundle\BlogBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;

/**
* Edit Blog categories Type
*/
class BlogCategoryType extends AbstractType
{

/**
* define form fields
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm( FormBuilderInterface $builder, array $options)
{
$builder
->add(
'rootCategories',
'collection',
array(
'type' => 'victoire_form_blog_category',
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true, )
);
}

/**
* bind to Page entity
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(
array(
'cascade_validation' => 'true'
)
);
}

/**
* get form name
*/
public function getName()
{
return 'victoire_blog_category_type';
}
}
13 changes: 1 addition & 12 deletions Bundle/BlogBundle/Form/BlogSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('publishedAt', null, array(
'widget' => 'single_text',
'datetimepicker' => true
))
->add(
'categories',
'collection',
array(
'type' => 'victoire_form_blog_category',
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,)
);
));
}

/**
Expand Down
Loading