Skip to content

Instantly share code, notes, and snippets.

@wiratama
Created February 14, 2018 07:28
Show Gist options
  • Save wiratama/234fe9f65d696713b1c32b69e49d13af to your computer and use it in GitHub Desktop.
Save wiratama/234fe9f65d696713b1c32b69e49d13af to your computer and use it in GitHub Desktop.
....
public function itemproductAction(Request $request)
{
$contentId = $request->get('content_id');
$categoryId = $request->get('category_id');
$isDefault = $request->get('is_default', false);
$isMini = $request->get('is_mini', false);
$category = null;
if (!empty($categoryId)) {
$category = $this->getCategoryManager()->getCategoryById($categoryId);
}
$content = null;
if ($contentManager = $this->getContentManager()) {
$content = $contentManager->getCacheContent($contentId);
}
if ($content) {
//$itemTemplate = sprintf("MoventContentBundle:Content/Layout/base/list%s:item.html.twig", ($isMini?'/mini':''));
$itemTemplate = "MoventContentBundle:Content/Layout/base/list:itemproduct.html.twig";
$response = $this->render($itemTemplate,
array(
'item' => $content,
'category' => $category,
'isDefault' => $isDefault,
'isMini' => $isMini
)
);
$response->setSharedMaxAge(600);
$response->setPublic();
return $response;
}
// throw not found exception
throw new NotFoundHttpException(sprintf("Content '%s' cannot be found!",$contentId));
}
{% set CategoryCacheConfigPath = 'Movent\\CategoryBundle\\Services\\CategoryConfig' %}
{% set ContentEntityPath = 'Movent\\ContentBundle\\Entity\\Content' %}
{% if item.contentType == constant(ContentEntityPath ~ '::TYPE_ARTICLE') %}
{% set typeFileName = 'article' %}
{% set typeLabel = 'Article' %}
{% elseif item.contentType == constant(ContentEntityPath ~ '::TYPE_RECIPE') %}
{% set typeFileName = 'recipe' %}
{% set typeLabel = 'Recipe' %}
{% elseif item.contentType == constant(ContentEntityPath ~ '::TYPE_PRODUCT') %}
{% set typeFileName = 'product' %}
{% set typeLabel = 'Product' %}
{% endif %}
{% if category %}
{% set categoryId = category.id %}
{#<!-- Default context -->#}
{% if isDefault is defined and isDefault %}
{% if item.defaultCategory %}
{#<!-- get category children from cache if any --> #}
{% set childrenCat = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_CHILDREN')) %}
{% set childrenCat = childrenCat|merge([categoryId]) %}
{#<!-- make sure that this default category is the current category or part of its children -->#}
{% if item.defaultCategory.id not in childrenCat %}
{#<!-- force to use the current category instead -->#}
{% set url = movent_content_slug_path(item.slug, category, true) %}
{% else %}
{#<!-- set url to the default category -->#}
{% set url = movent_content_slug_path(item.slug, item.defaultCategory, true) %}
{% endif %}
{% else %}
{#<!-- set url to the canonical instead -->#}
{% set url = movent_content_slug_path(item.slug, null, true) %}
{#<!-- use default layout -->#}
{% set categoryId = 0 %}
{% endif %}
{% else %}
{#<!-- set url to the current category -->#}
{% set url = movent_content_slug_path(item.slug, category, true) %}
{% endif %}
{% else %}
{% set categoryId = 0 %}
{#<!-- set url to the canonical instead -->#}
{% set url = movent_content_slug_path(item.slug, null, true) %}
{% if isDefault is defined and isDefault %}
{% if item.defaultCategory %}
{% set categoryId = item.defaultCategory.id %}
{% set url = movent_content_slug_path(item.slug, item.defaultCategory, true) %}
{% endif %}
{% endif %}
{% endif %}
{% if isMini is defined and isMini %}
{% set miniPath = '/mini' %}
{% else %}
{% set miniPath = '' %}
{% endif %}
{% set catTitle = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_TITLE')) %}
{% set icon_path_hover = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_ICON_23x23_HOVER')) %}
{% set desktop_banner = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_BANNER_DESKTOP')) %}
{% set mobile_banner = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_BANNER_MOBILE')) %}
{% set backgroundColor = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_COLOR1')) %}
{% set layoutName = movent_category_cached_config(categoryId, constant(CategoryCacheConfigPath ~ '::PROP_CAT_LAYOUT_NAME')) %}
{% set contentTemplate = "MoventContentBundle:Content/Layout/default/%s/list%s:%s.html.twig"|format(layoutName, miniPath, typeFileName) %}
{% set defaultTemplate = "MoventContentBundle:Content/Layout/default/Default/list%s:%s.html.twig"|format(miniPath,typeFileName) %}
{% set baseTemplate = "MoventContentBundle:Content/Layout/base/list%s:base.html.twig"|format(miniPath) %}
{# <!-- Fallback template hierarchy --> #}
{% include [contentTemplate, defaultTemplate, baseTemplate] ignore missing with { category:category } %}
{% extends 'MoventContentBundle:Content/Layout/base:base.html.twig' %}
{% block content %}
{% include "MoventSiteBundle:Social:socialinit.html.twig" %}
<div class="main innerpage">
<div class="wrapper-center">
<div id="product-{{ object.id }}" class="productdetail">
<div class="recipe-header-breadcrumbs">
{{ sonata_block_render_event('breadcrumb', { 'context': 'content_view', 'category': category}) }}
</div>
<div class="productmain">
<div class="productimage">
<img class="main" alt="{{ object.imageMainLabel|raw }}" src="{{ movent_media_public_url(object.imageMain, 'image_395x527') }}" />
{% if object.productGallery and object.productGallery|length >= 1 %}
<div class="productThumbs">
<img alt="{{ object.imageMainLabel|raw }}" data-large="{{ movent_media_public_url(object.imageMain, 'image_395x527') }}" src="{{ movent_media_public_url(object.imageMain, 'image_113x151') }}" />
{% set gallery = object.productGallery %}
{% set index = 1 %}
{% for image in gallery.galleryHasMedias if index < 3 %}
<img alt="{{ object.imageMainLabel|raw }}" data-large="{{ movent_media_public_url(image.media, 'image_395x527') }}" src="{{ movent_media_public_url(image.media, 'image_113x151') }}" />
{% set index = index + 1 %}
{% endfor %}
</div>
{% endif %}
</div>
<div class="fleft-500px">
<div class="productinfo">
<h1>{{ object.title }}</h1>
{% if object.buyNowId is not null %}
<a data-sku-id="{{ object.buyNowId }}" class="productViewBuyNow buynow-btn"><span class="test">Beli Sekarang</span></a>
{% endif %}
{{ object.abstract }}
<div class="productBtnbox">
{% if object.imageNutritionFact %}
<a href="{% path object.imageNutritionFact, 'reference' %}" class="infomasi">Informasi gizi</a>
{% endif %}
</div>
</div>
{#<div class="resellerbox">
{% if object.productProductReseller|length %}
<h1>Reseller</h1>
<ul>
{% for reseller in object.productProductReseller %}
<li>
<!--<span class="resellerprice">Rp {{ priceFeed(reseller.priceFeed) }}</span>-->
<div class="resellerthumb">
<img src="{{ movent_media_public_url(reseller.image, 'image_115x115') }}" />
</div>
<div class="resellerinfo">
<h2>{{ reseller.name }}</h2>
<p>{{ reseller.description }}</p>
{% if reseller.isPromo %}
<a href="{{ reseller.url }}" target="_blank" class="belisekarangpromo"></a>
{% else %}
<a href="{{ reseller.url }}" target="_blank" class="belisekarang"></a>
{% endif %}
</div>
</li>
{% endfor %}
</ul>
{% endif %}
</div>#}
</div>
{{ sonata_block_render({ 'type': 'relatedproduct.block.service' }, {
'itemId': object.id, 'maxItem': 4, 'relatedTo': contentType
}) }}
</div>
{% if object.allowComment %}
<div class="fbcommentbox">
{% include "MoventContentBundle:Content/Layout/base:comment.html.twig" with {'width' : '857' } %}
</div>
{% endif %}
</div>
</div>
</div>
{% endblock content %}
{% block site_footer_scripts %}
{{ parent() }}
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function($){
$(".infomasi").fancybox();
{#var buy_now = $(".productViewBuyNow");
var lightbox = null;
if (buy_now.length) {
lightbox = new fusepump.lightbox.buynow(buy_now.attr('data-buynow-id'));
buy_now.click(function(e) {
e.preventDefault();
lightbox.show();
});
}#}
$(".productThumbs img").click(function(){
$(".productThumbs img").removeClass("active");
$(this).addClass("active");
$(".productimage img.main").attr('src',$(this).data('large'));
});
});
//]]>
</script>
{% endblock %}
{#{% include is_mobile() ? "MoventContentBundle:Content/Layout/default/Product/list:product.mobile.html.twig" : "MoventContentBundle:Content/Layout/default/Product/list:product.desktop.html.twig" %}#}
{% include "MoventContentBundle:Content/Layout/default/Product/list:productbaru.desktop.html.twig" %}
{% block content %}
<div class="item" style="float:left;width: 200px;">
<div class="mov-related-products_image">
<img style="width:50%;" alt="{{ item.imageMainLabel|raw }}" src="{{ movent_media_public_url(item.imageMain, 'image_113x151') }}" />
</div>
<h4>{{ item.title|raw|truncate(14, false, ' …') }}</h4>
<p>{{ item.abstract|raw|truncate(80, true, ' …') }}</p>
<div class="relatedProduct_buttons">
<p><a href="{{ url }}" class="tab-link" style="background:red;">{{"Selengkapnya"|trans}}</a>
{% if item.buyNowId is not null %}
<a data-sku-id="{{ item.buyNow }}" class="productViewBuyNow tab-link buy-now-button buynow-btn" style="background:red;"> {{"Beli Sekarang"|trans}}</a></p>
{% endif %}
</div>
</div>
{% endblock %}
{% extends sonata_block.templates.block_base %}
{% block block %}
{% if related and related|length %}
<div class="relatedProducts">
<h3>{{ "Produk Terkait"|trans }}</h3>
<div class="items">
{% for relatedContent in related %}
{{ render(controller('MoventContentBundle:Content:itemproduct',{ content_id:relatedContent.content_id, category_id:0, is_default:true })) }}
{% endfor %}
</div>
</div>
{% 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\ContentBundle\Entity\Article;
use Movent\ContentBundle\Entity\Recipe;
class RelatedproductBlock extends BaseBlockService{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Relatedproduct 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:relatedproduct.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']));
$relatedProducts = null;
// if ($content instanceof Article) {
// $relatedProducts = $content->getArticleHasProducts();
// } elseif($content instanceof Recipe) {
// $relatedProducts = $content->getRecipeHasProducts();
// }
if($settings['relatedTo']=='product') {
$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', 'Product');
$statement->bindValue('category_id', $catId);
$statement->bindValue('status', 'published');
$statement->execute();
$relatedProducts = $statement->fetchAll();
}
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
'content' => $content,
'related' => $relatedProducts
), $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\Content\Layout\default\Product\list
* product.html.twig
* productbaru.desktop.html.twig

\src\Movent\ContentBundle\Resources\views\Content\Layout\base\list
* itemproduct.html.twig

\src\Movent\ContentBundle\Controller
* ContentController.php

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

\src\Movent\ContentBundle\Resources\views\Content\Layout\default\Product
* product.desktop.html.twig

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

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