Skip to content

Instantly share code, notes, and snippets.

@wiratama
Last active February 7, 2018 09:50
Show Gist options
  • Save wiratama/e3a033de7ee76227b5d98dcf4cc0479e to your computer and use it in GitHub Desktop.
Save wiratama/e3a033de7ee76227b5d98dcf4cc0479e 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>Artikel Terkait</h3>
<ul>
{% set counter = 0 %}
{% for relatedContent in related %}
{% if counter < settings.maxItem %}
{% if relatedContent and relatedContent.enabled and isArticle(relatedContent.associatedArticle) and relatedContent.associatedArticle.isActive() %}
{% set article = relatedContent.associatedArticle %}
{{ render(controller('MoventContentBundle:Content:item',{ content_id:article.id, category_id:0, is_default:true })) }}
{% set counter = counter + 1 %}
{% endif %}
{% endif %}
{% endfor %}
</ul>
</div>
{% else %}#}
<div class="artikelsidebar">
<h3>Artikel 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;
/* revision */
use Symfony\Component\HttpFoundation\Request;
class RelatedarticleBlock extends BaseBlockService{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Relatedarticle Block';
}
protected $container;
public function setContainer($container){
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function setDefaultSettings(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'relatedTo' => null,
'maxItem' => '4',
'sort' => 'date',
'itemId' => '0',
'template' => 'MoventContentBundle:Block:relatedarticle.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']));
if($settings['relatedTo']=='article') {
$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', 'Article');
$statement->bindValue('category_id', $catId);
$statement->bindValue('status', 'published');
$statement->execute();
$relatedArticle = $statement->fetchAll();
}
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
'content' => $content,
'related' => $settings['relatedTo'] == 'recipe' ? $content->getRecipeHasArticles() : $relatedArticle /*$content->getArticleHasArticles()*/
), $response);
}
protected function getSessionService()
{
if ($this->container->has('session')) {
return $this->container->get('session');
}
return null;
}
}
@wiratama
Copy link
Author

wiratama commented Feb 6, 2018

path

RelatedarticleBlock.php \src\Movent\ContentBundle\Block
relatedarticle.html.twig \src\Movent\ContentBundle\Resources\views\Block

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