Skip to content

Instantly share code, notes, and snippets.

@wiratama
Last active February 14, 2018 06:39
Show Gist options
  • Save wiratama/9f8e887d21b0b7d2cc8eefe202c93284 to your computer and use it in GitHub Desktop.
Save wiratama/9f8e887d21b0b7d2cc8eefe202c93284 to your computer and use it in GitHub Desktop.
{% extends sonata_block.templates.block_base %}
{% block block %}
{% if related and related|length %}
{% if is_mobile() %}
<div class="artikelsidebar-mobile">
<h3>Resep Terkait</h3>
<ul>
{% set counter = 0 %}
{% for relatedContent in related %}
{% if counter < settings.maxItem %}
{{ render(controller('MoventContentBundle:Content:item',{ content_id:relatedContent.content_id, category_id:0, is_default:true })) }}
{% set counter = counter + 1 %}
{% endif %}
{% endfor %}
</ul>
</div>
{% else %}
<div class="artikelsidebar">
<h3>Resep Terkait</h3>
<ul>
{% set counter = 0 %}
{% for relatedContent in related %}
{% if counter < settings.maxItem %}
{{ render(controller('MoventContentBundle:Content:item',{ content_id:relatedContent.content_id, category_id:0, is_default:true })) }}
{% set counter = counter + 1 %}
{% endif %}
{% endfor %}
</ul>
</div>
{% endif %}
{% endif %}
{% endblock %}
<?php
namespace Movent\ContentBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Sonata\AdminBundle\Form\FormMapper;
//use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Movent\CategoryBundle\Controller\CategoryController;
class RelatedrecipeBlock extends BaseBlockService{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Relatedrecipe Block';
}
protected $container;
public function setContainer($container){
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function setDefaultSettings(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'relatedTo' => null,
'maxItem' => '3',
'sort' => 'date',
'itemId' => '0',
'template' => 'MoventContentBundle:Block:relatedrecipe.html.twig',
));
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('itemId', 'hidden', array('required' => false,'label' => ' ')),
array('maxItem', 'integer', array('required' => false, 'label' => 'Max Item','attr'=>array('class'=>'span12'))),
array('sort', 'choice', array('choices' => array('date' => 'Date Publish', 'title' => 'Title'), 'label' => 'Sort Order')),
)
));
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
$errorElement
->with('settings[maxItem]')
->assertNotNull(array())
->assertNotBlank()
->end()
->with('settings[sort]')
->assertNotNull(array())
->assertNotBlank()
->end();
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = $blockContext->getSettings();
$content = $this->container->get('movent_content.content.entity.manager')->findOneBy(array('id' => $settings['itemId']));
$relatedRecipe = null;
if($settings['relatedTo']=='recipe') {
$request = $this->container->get('request');
$parameters = explode('/', $request->getRequestURI());
$catId = 0;
if(count($parameters) > 4) {
$categoryData = $this->container->get('doctrine')->getRepository('MoventCategoryBundle:Category')->findOneBy(array('slug' => (string)$parameters[3]));
$catId = $categoryData->getId();
} else {
$categoryData = $this->container->get('doctrine')->getRepository('MoventCategoryBundle:Category')->findOneBy(array('slug' => (string)$parameters[2]));
$catId = $categoryData->getId();
}
$entityManager = $this->container->get('doctrine')->getManager();
$connection = $entityManager->getConnection();
$statement = $connection->prepare("SELECT * FROM movent__content_entity_content_category ctr JOIN movent__content_entity_content c ON ctr.content_id=c.id WHERE ctr.content_id <> :content_id AND c.content_type = :content_type AND ctr.category_id = :category_id AND c.status = :status ORDER BY RAND() LIMIT 4");
$statement->bindValue('content_id', $content->getId());
$statement->bindValue('content_type', 'Recipe');
$statement->bindValue('category_id', $catId);
$statement->bindValue('status', 'published');
$statement->execute();
$relatedRecipe = $statement->fetchAll();
}
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
'content' => $content,
'related' => $settings['relatedTo'] == 'article' ? $content->getArticleHasRecipes() : $relatedRecipe /*$content->getRecipeHasRecipes()*/
), $response);
}
protected function getSessionService()
{
if ($this->container->has('session')) {
return $this->container->get('session');
}
return null;
}
}
@wiratama
Copy link
Author

path

\src\Movent\ContentBundle\Resources\views\Block
* relatedrecipe.html.twig

\src\Movent\ContentBundle\Block
* RelatedrecipeBlock.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment