Skip to content

Commit

Permalink
Merge pull request #1034 from Victoire/feature/blog-upgrade-2
Browse files Browse the repository at this point in the history
Feature/blog upgrade 2
  • Loading branch information
lenybernard authored Feb 5, 2018
2 parents 61aef53 + e835c47 commit 9807f20
Show file tree
Hide file tree
Showing 52 changed files with 1,201 additions and 346 deletions.
5 changes: 2 additions & 3 deletions Bundle/BlogBundle/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ArticleController extends Controller
*
* @Route("/new/{id}", name="victoire_blog_article_new")
* @Method("GET")
* @Template()
*
* @return JsonResponse
*/
Expand Down Expand Up @@ -92,7 +91,7 @@ public function newPostAction(Request $request, Blog $blog)
'success' => true,
'url' => $this->generateUrl('victoire_core_page_show', [
'_locale' => $request->getLocale(),
'url' => $this->container->get('victoire_core.url_builder')->buildUrl($page),
'url' => $page->getUrl(),
]),
]);
}
Expand Down Expand Up @@ -181,7 +180,7 @@ public function settingsPostAction(Request $request, Article $article)
}

/**
* Delete a BLog Article.
* Delete a Blog Article.
*
* @param Article $article
*
Expand Down
185 changes: 99 additions & 86 deletions Bundle/BlogBundle/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Victoire\Bundle\BlogBundle\Entity\Article;
use Victoire\Bundle\BlogBundle\Entity\Blog;
use Victoire\Bundle\BlogBundle\Entity\Category;
use Victoire\Bundle\BlogBundle\Entity\BlogCategory;
use Victoire\Bundle\BlogBundle\Form\BlogCategoryType;
use Victoire\Bundle\BlogBundle\Form\BlogSettingsType;
use Victoire\Bundle\BlogBundle\Form\BlogType;
Expand All @@ -32,38 +32,52 @@ class BlogController extends BasePageController
* List all Blogs.
*
* @Route("/index/{blogId}/{tab}", name="victoire_blog_index", defaults={"blogId" = null, "tab" = "articles"})
* @ParamConverter("blog", class="VictoireBlogBundle:Blog", options={"id" = "blogId"})
*
* @param Request $request
*
* @throws \OutOfBoundsException
*
* @return JsonResponse
*/
public function indexAction(Request $request, $blogId = null, $tab = 'articles')
public function indexAction(Request $request, $blog = null, $tab = 'articles')
{
$blog = $this->getBlog($request, $blogId);
$template = $this->getBaseTemplatePath().':index.html.twig';
$chooseBlogForm = $this->createForm(ChooseBlogType::class, null, [
'blog' => $blog,
'locale' => $request->getLocale(),
]);
/** @var BlogRepository $blogRepo */
$blogRepo = $this->get('doctrine.orm.entity_manager')->getRepository('VictoireBlogBundle:Blog');

$chooseBlogForm->handleRequest($request);
if ($chooseBlogForm->isValid()) {
$template = $this->getBaseTemplatePath().':_blogItem.html.twig';
// Default value for locale
$locale = $request->getLocale();

// Overwrite locale when a locale is chosen in the form
if ($chooseBlog = $request->request->get('choose_blog')) {
if (array_key_exists('locale', $chooseBlog)) {
$locale = $chooseBlog['locale'];
}
}

return new JsonResponse(
[
'html' => $this->container->get('templating')->render(
$template,
[
'blog' => $blog,
'currentTab' => $tab,
'tabs' => ['articles', 'settings', 'category'],
'chooseBlogForm' => $chooseBlogForm->createView(),
'businessProperties' => $blog ? $this->getBusinessProperties($blog) : null,
]
),
]
$parameters = [
'locale' => $locale,
'blog' => $blog,
'currentTab' => $tab,
'tabs' => ['articles', 'settings', 'category'],
'businessProperties' => $blog ? $this->getBusinessProperties($blog) : null,
];
if ($blogRepo->hasMultipleBlog()) {
$chooseBlogForm = $this->createForm(ChooseBlogType::class, null, [
'blog' => $blog,
'locale' => $locale,
]);
$chooseBlogForm->handleRequest($request);
$parameters = array_merge(
$parameters,
['chooseBlogForm' => $chooseBlogForm->createView()],
$chooseBlogForm->getData()
);
}

return $this->render(
'VictoireBlogBundle:Blog:index.html.twig',
$parameters
);
}

Expand All @@ -83,10 +97,10 @@ public function feedAction(Request $request, Blog $blog)
if ($categoryId = $request->query->get('category')) {
$entityManager = $this->getDoctrine()->getManager();
/** @var Category $category */
$category = $entityManager->getRepository('VictoireBlogBundle:Category')->find($categoryId);
$category = $entityManager->getRepository('VictoireBlogBundle:BlogCategory')->find($categoryId);
$categoryIds = [];

function findIds(Category $category, &$categoryIds)
function findIds(BlogCategory $category, &$categoryIds)
{
$categoryIds[] = $category->getId();

Expand Down Expand Up @@ -128,7 +142,6 @@ public function newAction(Request $request, $isHomepage = false)
*
* @Route("/new", name="victoire_blog_new_post")
* @Method("POST")
* @Template()
*
* @return JsonResponse
*/
Expand All @@ -144,62 +157,38 @@ public function newPostAction(Request $request)
* @param BasePage $blog
*
* @Route("/{id}/settings", name="victoire_blog_settings")
* @Method("GET")
* @Method(methods={"GET", "POST"})
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*
* @return JsonResponse
* @throws \InvalidArgumentException
*
* @return Response
*/
public function settingsAction(Request $request, BasePage $blog)
{
$form = $this->createForm($this->getPageSettingsType(), $blog);
$form = $this->getSettingsForm($blog);

$form->handleRequest($request);
if ($request->isMethod('POST')) {
if ($form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$form = $this->getSettingsForm($blog);
} else {
$this->warn('error_occured');
}
}

return new Response($this->container->get('templating')->render(
$this->getBaseTemplatePath().':Tabs/_settings.html.twig',
return $this->render(
'VictoireBlogBundle:Blog/Tabs:_settings.html.twig',
[
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => [],
]
));
}

/**
* Save Blog settings.
*
* @Route("/{id}/settings", name="victoire_blog_settings_post")
* @Method("POST")
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*
* @return JsonResponse
*/
protected function settingsPostAction(Request $request, BasePage $blog)
{
$entityManager = $this->getDoctrine()->getManager();
$form = $this->createForm($this->getPageSettingsType(), $blog);

$form->handleRequest($request);

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

return new JsonResponse($this->getViewReferenceRedirect($request, $blog));
}

return new JsonResponse([
'success' => false,
'message' => $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form),
'html' => $this->container->get('templating')->render(
$this->getBaseTemplatePath().':Tabs/_settings.html.twig',
[
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => [],
]
),
]);
],
new Response(null, 200, [
'X-Inject-Alertify' => true,
])
);
}

/**
Expand Down Expand Up @@ -232,20 +221,23 @@ public function categoryAction(Request $request, BasePage $blog)
//we display the form
$errors = $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form);
if ($errors != '') {
return new JsonResponse(['html' => $this->container->get('templating')->render(
$this->getBaseTemplatePath().':Tabs/_category.html.twig',
[
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties,
]
),
'message' => $errors,
]
);
return new JsonResponse(
[
'html' => $this->container->get('templating')->render(
'VictoireBlogBundle:Blog:Tabs/_category.html.twig',
[
'blog' => $blog,
'form' => $form->createView(),
'businessProperties' => $businessProperties,
]
),
'message' => $errors,
]
);
}

return new Response($this->container->get('templating')->render(
return new Response(
$this->container->get('templating')->render(
$this->getBaseTemplatePath().':Tabs/_category.html.twig',
[
'blog' => $blog,
Expand All @@ -262,17 +254,20 @@ public function categoryAction(Request $request, BasePage $blog)
* @param Request $request
* @param BasePage $blog
*
* @Route("/{id}/articles", name="victoire_blog_articles")
* @Route("/{id}/articles/{articleLocale}", name="victoire_blog_articles")
* @ParamConverter("blog", class="VictoirePageBundle:BasePage")
*
* @throws \InvalidArgumentException
*
* @return Response
*/
public function articlesAction(Request $request, BasePage $blog)
public function articlesAction(Request $request, BasePage $blog, $articleLocale = null)
{
return new Response($this->container->get('templating')->render(
$this->getBaseTemplatePath().':Tabs/_articles.html.twig',
[
'blog' => $blog,
'locale' => $articleLocale ? $articleLocale : $request->getLocale(),
'blog' => $blog,
]
));
}
Expand Down Expand Up @@ -370,4 +365,22 @@ protected function getBlog(Request $request, $blogId)

return $blog;
}

/**
* @param Blog $blog
*
* @return \Symfony\Component\Form\Form
*/
private function getSettingsForm(Blog $blog)
{
return $this->createForm($this->getPageSettingsType(), $blog, [
'attr' => [
'novalidate' => true,
'v-ic-post-to' => $this->generateUrl('victoire_blog_settings', [
'id' => $blog->getId(),
]),
'v-ic-target' => '#victoire-blog-settings',
],
]);
}
}
33 changes: 30 additions & 3 deletions Bundle/BlogBundle/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Article
/**
* Categories of the article.
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
* @ORM\ManyToOne(targetEntity="BlogCategory", inversedBy="articles")
* @ORM\JoinColumn(onDelete="SET NULL")
* @VIC\BusinessProperty({"textable", "seoable"})
*/
Expand Down Expand Up @@ -111,6 +111,13 @@ class Article
*/
private $deletedAt;

/**
* @var bool
*
* @ORM\Column(name="promoted", type="boolean", nullable=true)
*/
private $promoted = false;

/**
* @Gedmo\Locale
*/
Expand Down Expand Up @@ -167,11 +174,11 @@ public function getCategory()
/**
* Set category.
*
* @param Category $category
* @param BlogCategory $category
*
* @return Article
*/
public function setCategory(Category $category)
public function setCategory(BlogCategory $category)
{
$this->category = $category;
}
Expand Down Expand Up @@ -479,4 +486,24 @@ public function setImage($image, $locale = null)
$this->translate($locale, false)->setImage($image);
$this->mergeNewTranslations();
}

/**
* @return bool
*/
public function isPromoted()
{
return $this->promoted;
}

/**
* @param bool $promoted
*
* @return Article
*/
public function setPromoted($promoted)
{
$this->promoted = $promoted;

return $this;
}
}
Loading

0 comments on commit 9807f20

Please sign in to comment.