diff --git a/Block/Menu.php b/Block/Menu.php
index 212be5a..71de3ff 100644
--- a/Block/Menu.php
+++ b/Block/Menu.php
@@ -1,8 +1,8 @@
* @@Create Date: 2016-02-28 10:10:00
@@ -82,6 +82,8 @@ class Menu extends \Magento\Catalog\Block\Navigation
protected $_recursionLevel;
+ protected $_cacheHelper;
+
protected $extData = array();
/**
@@ -101,17 +103,20 @@ public function __construct(
\Magento\Catalog\Helper\Category $catalogCategory,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Model\Indexer\Category\Flat\State $flatState,
+ \Magiccart\Magicmenu\Helper\Cache $cacheHelper,
// +++++++++add new +++++++++
// \Magiccart\Magicmenu\Model\CategoryFactory $categoryFactory,
\Magiccart\Magicmenu\Model\ResourceModel\Magicmenu\CollectionFactory $magicmenuCollectionFactory,
array $data = []
- ) {
+ )
+ {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogLayer = $layerResolver->get();
$this->httpContext = $httpContext;
+ $this->_cacheHelper = $cacheHelper;
$this->_catalogCategory = $catalogCategory;
$this->_registry = $registry;
$this->flatState = $flatState;
@@ -119,7 +124,7 @@ public function __construct(
// +++++++++add new +++++++++
$this->_magicmenuCollectionFactory = $magicmenuCollectionFactory;
- $this->_sysCfg= (object) $context->getScopeConfig()->getValue(
+ $this->_sysCfg = (object)$context->getScopeConfig()->getValue(
'magicmenu',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
@@ -127,8 +132,8 @@ public function __construct(
parent::__construct($context, $categoryFactory, $productCollectionFactory, $layerResolver, $httpContext, $catalogCategory, $registry, $flatState, $data);
$this->_urlMedia = $this->_storeManager->getStore()->getBaseUrl(
- \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
- );
+ \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
+ );
$this->_dirMedia = $this->getMediaDirectory()->getAbsolutePath();
@@ -141,36 +146,11 @@ public function __construct(
);
-
}
public function getIsHomePage()
{
- return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
- }
-
- public function isCategoryActive($catId)
- {
- return $this->getCurrentCategory() ? in_array($catId, $this->getCurrentCategory()->getPathIds()) : false;
- }
-
- public function isHasActive($catId)
- {
- return ($catId != $this->getCurrentCategory()->getId()) ? true : false;
- }
-
- protected function _getActiveClasses($catId)
- {
- $classes = '';
- if ($this->isCategoryActive($catId)) {
- if($this->isHasActive($catId)){
- $classes = 'has-active active';
- } else {
- $classes = 'active';
- }
-
- }
- return $classes;
+ return $this->getUrl('') == $this->getUrl('*/*/*', array('_current' => true, '_use_rewrite' => true));
}
public function getLogo()
@@ -186,19 +166,19 @@ public function getRootName()
public function drawHomeMenu()
{
- if($this->hasData('homeMenu')) return $this->getData('homeMenu');
+ if ($this->hasData('homeMenu')) return $this->getData('homeMenu');
$drawHomeMenu = '';
- $active = ($this->getIsHomePage()) ? ' active' : '';
- $drawHomeMenu .= '
';
- $drawHomeMenu .= '' .__('Home') .'';
+
+ $drawHomeMenu .= '';
+ $drawHomeMenu .= '' . __('Home') . '';
$drawHomeMenu .= '';
- if($this->_sysCfg->topmenu['demo']){
+ if ($this->_sysCfg->topmenu['demo']) {
$demo = '';
$currentStore = $this->_storeManager->getStore();
$switcher = $this->getLayout()->createBlock('Magento\Store\Block\Switcher');
foreach ($this->_storeManager->getWebsites() as $website) {
$groups = $website->getGroups();
- if(count($groups) > 1){
+ if (count($groups) > 1) {
foreach ($groups as $group) {
$store = $group->getDefaultStore();
if ($store && !$store->getIsActive()) {
@@ -207,17 +187,17 @@ public function drawHomeMenu()
if ($store->getIsActive()) break;
else $store = '';
}
- }
- if($store){
- if( $store->getCode() == $currentStore->getCode() ) $demo .= ''. $group->getName(). '';
- else $demo .= ''. $group->getName(). '';
+ }
+ if ($store) {
+ if ($store->getCode() == $currentStore->getCode()) $demo .= '' . $group->getName() . '';
+ else $demo .= '' . $group->getName() . '';
}
}
}
}
- if($demo) $drawHomeMenu .= '';
+ if ($demo) $drawHomeMenu .= '';
}
$drawHomeMenu .= '';
@@ -227,12 +207,34 @@ public function drawHomeMenu()
public function drawMainMenu()
{
- if($this->hasData('mainMenu')) return $this->getData('mainMenu');
+ if ($this->hasData('mainMenu')) return $this->getData('mainMenu');
+
+ /*
+ * Check if menu is saved in cache
+ */
+ $cacheId = $this->_cacheHelper->getId("mainMenu");
+ $mobilecacheId = $this->_cacheHelper->getId("mobileMenu");
+
+ if ($cache = $this->_cacheHelper->load($cacheId)) {
+
+ $menu = array();
+
+ $mobilecache = $this->_cacheHelper->load($mobilecacheId);
+
+ $menu['desktop'] = explode("\n", $cache);
+ $menu['mobile'] = $mobilecache;
+
+ $this->setData('mainMenu', $menu);
+
+ return $menu;
+
+ }
+
$desktopHtml = array();
- $mobileHtml = array();
+ $mobileHtml = array();
$rootCatId = $this->_storeManager->getStore()->getRootCategoryId();
$catListTop = $this->getChildExt($rootCatId);
- $contentCatTop = $this->getContentCatTop();
+ $contentCatTop = $this->getContentCatTop();
foreach ($contentCatTop as $ext) {
$this->extData[$ext->getCatId()] = $ext->getData();
@@ -240,94 +242,100 @@ public function drawMainMenu()
$last = count($catListTop);
$dropdownIds = explode(',', $this->_sysCfg->general['dropdown']);
$counter = 1;
-
- foreach ($catListTop as $catTop){
+
+ foreach ($catListTop as $catTop) {
$parentPositionClass = '';
$itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
- $idTop = $catTop->getEntityId();
- $urlTop = '' .$this->getThumbnail($catTop). '' .__($catTop->getName()) . $this->getCatLabel($catTop). '';
+ $idTop = $catTop->getEntityId();
+ $urlTop = '' . $this->getThumbnail($catTop) . '' . __($catTop->getName()) . $this->getCatLabel($catTop) . '';
$itemPositionClassPrefixTop = $itemPositionClassPrefix . $counter;
- $classTop = $itemPositionClassPrefixTop . ' ' . $this->_getActiveClasses($idTop);
+ $classTop = $itemPositionClassPrefixTop;
$isDropdown = in_array($idTop, $dropdownIds) ? ' dropdown' : '';
// drawMainMenu
- $options = '';
- if($this->_recursionLevel == 1){
- $menu = array('desktop' => '', 'mobile' => '');
- }else {
- if($isDropdown){
+ $options = '';
+ if ($this->_recursionLevel == 1) {
+ $menu = array('desktop' => '', 'mobile' => '');
+ } else {
+ if ($isDropdown) {
$classTop .= $isDropdown;
$childHtml = $this->getTreeCategoriesExt($idTop, $itemPositionClassPrefixTop); // include magic_label
// $childHtml = $this->getTreeCategoriesExtra($idTop, $itemPositionClassPrefixTop); // include magic_label and Maximal Depth
$menu = array('desktop' => $childHtml, 'mobile' => $childHtml);
} else { // Draw Mega Menu
- $idTop = $catTop->getEntityId();
- $data = isset($this->extData[$idTop]) ? $this->extData[$idTop] : '';
- $blocks = array('top'=>'', 'left'=>'', 'right'=>'', 'bottom'=>'');
- if($data){
+ $idTop = $catTop->getEntityId();
+ $data = isset($this->extData[$idTop]) ? $this->extData[$idTop] : '';
+ $blocks = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '');
+ if ($data) {
foreach ($blocks as $key => $value) {
- $proportion = $key .'_proportion';
+ $proportion = $key . '_proportion';
$html = $this->getStaticBlock($data[$key]);
- if($html) $blocks[$key] = "".$html.'
';
+ if ($html) $blocks[$key] = "" . $html . '
';
}
- $remove = array('top'=>'', 'left'=>'', 'right'=>'', 'bottom'=>'', 'cat_id'=>'');
+ $remove = array('top' => '', 'left' => '', 'right' => '', 'bottom' => '', 'cat_id' => '');
foreach ($remove as $key => $value) {
unset($data[$key]);
}
- $opt = json_encode($data);
+ $opt = json_encode($data);
$options = $opt ? " data-options='$opt'" : '';
}
$menu = $this->getMegamenu($catTop, $blocks, $itemPositionClassPrefixTop);
- }
+ }
}
- if($menu['desktop']) $classTop .= ' hasChild parent';
+ if ($menu['desktop']) $classTop .= ' hasChild parent';
- $desktopHtml[$idTop] = '' . $urlTop . $menu['desktop'] . '';
- $mobileHtml[$idTop] = '' . $urlTop . $menu['mobile'] . '';
- $counter++;
+ $desktopHtml[$idTop] = '' . $urlTop . $menu['desktop'] . '';
+ $mobileHtml[$idTop] = '' . $urlTop . $menu['mobile'] . '';
+ $counter++;
}
$menu['desktop'] = $desktopHtml;
$menu['mobile'] = implode("\n", $mobileHtml);
+
+ $this->_cacheHelper->save(implode("\n", $desktopHtml), $cacheId);
+ $this->_cacheHelper->save(implode("\n", $mobileHtml), $mobilecacheId);
+
$this->setData('mainMenu', $menu);
return $menu;
}
public function getMegamenu($catTop, $blocks, $itemPositionClassPrefix)
{
- // Draw Mega Menu
- $idTop = $catTop->getEntityId();
+ // Draw Mega Menu
+ $idTop = $catTop->getEntityId();
+ if(count($this->getChildExt($idTop)) < 1) return;
+
$hasChild = $catTop->hasChildren();
- $desktopTmp = $mobileTmp = '';
- if($hasChild || $blocks['top'] || $blocks['left'] || $blocks['right'] || $blocks['bottom']) :
+ $desktopTmp = $mobileTmp = '';
+ if ($hasChild || $blocks['top'] || $blocks['left'] || $blocks['right'] || $blocks['bottom']) :
$desktopTmp .= ''; /* Wrap Mega */
- $desktopTmp .='
'; /* Content Mega */
- $desktopTmp .= $blocks['top'];
- $desktopTmp .= '
';
- $desktopTmp .= $blocks['left'];
- if($hasChild) :
- $desktopTmp .= '
';
- $mobileTmp .= ''; // end cat-mega
- $mobileTmp .= '
';
- endif;
- $desktopTmp .= $blocks['right'];
- $desktopTmp .= '
';
- $desktopTmp .= $blocks['bottom'];
- $desktopTmp .= '
'; /* End Content mega */
+ $desktopTmp .= '
'; /* Content Mega */
+ $desktopTmp .= $blocks['top'];
+ $desktopTmp .= '
';
+ $desktopTmp .= $blocks['left'];
+ if ($hasChild) :
+ $desktopTmp .= '
';
+ $mobileTmp .= ''; // end cat-mega
+ $mobileTmp .= '
';
+ endif;
+ $desktopTmp .= $blocks['right'];
+ $desktopTmp .= '
';
+ $desktopTmp .= $blocks['bottom'];
+ $desktopTmp .= '
'; /* End Content mega */
$desktopTmp .= '
'; /* Warp Mega */
endif;
return array('desktop' => $desktopTmp, 'mobile' => $mobileTmp);
@@ -335,27 +343,26 @@ public function getMegamenu($catTop, $blocks, $itemPositionClassPrefix)
public function drawExtraMenu()
{
- if($this->hasData('extraMenu')) return $this->getData('extraMenu');
- $extMenu = $this->getExtraMenu();
+ if ($this->hasData('extraMenu')) return $this->getData('extraMenu');
+ $extMenu = $this->getExtraMenu();
$count = count($extMenu);
$drawExtraMenu = '';
- if($count){
- $i = 1; $class = 'first';
+ if ($count) {
+ $i = 1;
+ $class = 'first';
$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]); //$this->getCurrentUrl();
- foreach ($extMenu as $ext) {
+ foreach ($extMenu as $ext) {
$link = $ext->getLink();
- $url = (filter_var($link, FILTER_VALIDATE_URL)) ? $link : $this->getUrl('', array('_direct'=>$link));
- $active = ( $link && $url == $currentUrl) ? ' active' : '';
+ $url = (filter_var($link, FILTER_VALIDATE_URL)) ? $link : $this->getUrl('', array('_direct' => $link));
$html = $this->getStaticBlock($ext->getExtContent());
$class .= $ext->getCatCol() ? ' ' . $ext->getCatCol() : ' dropdown';
- if($html) $active .=' hasChild parent';
- $drawExtraMenu .= "";
- if($link) $drawExtraMenu .= '' .__($ext->getName()) . $this->getCatLabel($ext). '';
- else $drawExtraMenu .= '' .__($ext->getName()) . $this->getCatLabel($ext). '';
- if($html) $drawExtraMenu .= $html; //$drawExtraMenu .= ''.$html.'
';
+ $drawExtraMenu .= "";
+ if ($link) $drawExtraMenu .= '' . __($ext->getName()) . $this->getCatLabel($ext) . '';
+ else $drawExtraMenu .= '' . __($ext->getName()) . $this->getCatLabel($ext) . '';
+ if ($html) $drawExtraMenu .= $html; //$drawExtraMenu .= ''.$html.'
';
$drawExtraMenu .= '';
$i++;
- $class = ($i == $count) ? 'last' : '';
+ $class = ($i == $count) ? 'last' : '';
}
}
$this->setData('extraMenu', $drawExtraMenu);
@@ -365,11 +372,11 @@ public function drawExtraMenu()
public function getChildExt($parentId)
{
$collection = $this->_categoryInstance->getCollection()
- ->addAttributeToSelect(array('entity_id','name','magic_label','url_path'))
- ->addAttributeToFilter('parent_id', $parentId)
- ->addAttributeToFilter('include_in_menu', 1)
- ->addIsActiveFilter()
- ->addAttributeToSort('position', 'asc'); //->addOrderField('name');
+ ->addAttributeToSelect(array('entity_id', 'name', 'magic_label', 'url_path'))
+ ->addAttributeToFilter('parent_id', $parentId)
+ ->addAttributeToFilter('include_in_menu', 1)
+ ->addIsActiveFilter()
+ ->addAttributeToSort('position', 'asc'); //->addOrderField('name');
return $collection;
}
@@ -377,11 +384,11 @@ public function getExtraMenu()
{
$store = $this->_storeManager->getStore()->getStoreId();
$collection = $this->_magicmenuCollectionFactory->create()
- ->addFieldToSelect(array('link','name', 'cat_col', 'magic_label','ext_content','order'))
- ->addFieldToFilter('extra', 1)
- ->addFieldToFilter('status', 1);
+ ->addFieldToSelect(array('link', 'name', 'cat_col', 'magic_label', 'ext_content', 'order'))
+ ->addFieldToFilter('extra', 1)
+ ->addFieldToFilter('status', 1);
$collection->getSelect()->where('find_in_set(0, stores) OR find_in_set(?, stores)', $store)->order('order');
- return $collection;
+ return $collection;
}
public function getStaticBlock($id)
@@ -393,59 +400,56 @@ public function getContentCatTop()
{
$store = $this->_storeManager->getStore()->getStoreId();
$collection = $this->_magicmenuCollectionFactory->create()
- ->addFieldToSelect(array(
- 'cat_id','cat_col','cat_proportion','top',
- 'right','right_proportion','bottom','left','left_proportion'
- ))
- ->addFieldToFilter('stores',array( array('finset' => 0), array('finset' => $store)))
- ->addFieldToFilter('status', 1);
+ ->addFieldToSelect(array(
+ 'cat_id', 'cat_col', 'cat_proportion', 'top',
+ 'right', 'right_proportion', 'bottom', 'left', 'left_proportion'
+ ))
+ ->addFieldToFilter('stores', array(array('finset' => 0), array('finset' => $store)))
+ ->addFieldToFilter('status', 1);
return $collection;
}
- public function getTreeCategoriesExt($parentId, $itemPositionClassPrefix) // include Magic_Label
- {
+ public function getTreeCategoriesExt($parentId, $itemPositionClassPrefix) // include Magic_Label
+ {
$categories = $this->_categoryInstance->getCollection()
- ->addAttributeToSelect(array('name','magic_label','url_path'))
- ->addAttributeToFilter('include_in_menu', 1)
- ->addAttributeToFilter('parent_id', $parentId)
- ->addIsActiveFilter()
- ->addAttributeToSort('position', 'asc');
+ ->addAttributeToSelect(array('name', 'magic_label', 'url_path'))
+ ->addAttributeToFilter('include_in_menu', 1)
+ ->addAttributeToFilter('parent_id', $parentId)
+ ->addIsActiveFilter()
+ ->addAttributeToSort('position', 'asc');
$html = '';
$counter = 1;
- foreach($categories as $category)
- {
+ foreach ($categories as $category) {
$level = $category->getLevel();
- $childHtml = ( $this->_recursionLevel == 0 || ($level -1 < $this->_recursionLevel) ) ? $this->getTreeCategoriesExt($category->getId(), $itemPositionClassPrefix) : '';
+ $childHtml = ($this->_recursionLevel == 0 || ($level - 1 < $this->_recursionLevel)) ? $this->getTreeCategoriesExt($category->getId(), $itemPositionClassPrefix) : '';
$childClass = $childHtml ? ' hasChild parent category-item ' : ' category-item ';
- $childClass .= $itemPositionClassPrefix . '-' .$counter;
- $childClass .= $this->_getActiveClasses($category->getId());
- $html .= '' . $category->getName() . $this->getCatLabel($category) . "\n" . $childHtml . '';
+ $childClass .= $itemPositionClassPrefix . '-' . $counter;
+ $html .= '' . $category->getName() . $this->getCatLabel($category) . "\n" . $childHtml . '';
$counter++;
}
- if($html) $html = '';
+ if ($html) $html = '';
return $html;
}
- public function getTreeCategoriesExtra($parentId, $itemPositionClassPrefix) // include Magic_Label and Maximal Depth
+ public function getTreeCategoriesExtra($parentId, $itemPositionClassPrefix) // include Magic_Label and Maximal Depth
{
$html = '';
$categories = $this->_categoryInstance->getCategories($parentId);
$counter = 1;
- foreach($categories as $category) {
+ foreach ($categories as $category) {
$cat = $this->_categoryInstance->load($category->getId());
$count = $cat->getProductCount();
$level = $cat->getLevel();
- $childHtml = ( $this->_recursionLevel == 0 || ($level -1 < $this->_recursionLevel) ) ? $this->getTreeCategoriesExtra($category->getId(), $itemPositionClassPrefix) : '';
- $childClass = $childHtml ? ' hasChild parent' : '';
- $childClass .= $itemPositionClassPrefix . '-' .$counter;
- $childClass .= ' category-item ' . $this->_getActiveClasses($category->getId());
- $html .= '' . $cat->getName() . "(".$count.")" . $this->getCatLabel($cat) . "\n";
+ $childHtml = ($this->_recursionLevel == 0 || ($level - 1 < $this->_recursionLevel)) ? $this->getTreeCategoriesExtra($category->getId(), $itemPositionClassPrefix) : '';
+ $childClass = $childHtml ? ' hasChild parent' : '';
+ $childClass .= $itemPositionClassPrefix . '-' . $counter;
+ $html .= 'getId() . '">' . $cat->getName() . "(" . $count . ")" . $this->getCatLabel($cat) . "\n";
$html .= $childHtml;
$html .= '';
$counter++;
}
- if($html) $html = '';
- return $html;
+ if ($html) $html = '';
+ return $html;
}
public function getCatLabel($cat)
@@ -453,7 +457,7 @@ public function getCatLabel($cat)
$html = '';
$label = explode(',', $cat->getMagicLabel());
foreach ($label as $lab) {
- if($lab) $html .= ''.__(trim($lab)) .'';
+ if ($lab) $html .= '' . __(trim($lab)) . '';
}
return $html;
}
@@ -461,17 +465,17 @@ public function getCatLabel($cat)
public function getImage($object)
{
$url = '';
- $image = $this->_dirMedia . 'magiccart/magicmenu/images/' . $object->getId() .'.png';
- if(file_exists($image)) $url = $this->_urlMedia . 'magiccart/magicmenu/images/' . $object->getId() .'.png';
- if($url) return '';
+ $image = $this->_dirMedia . 'magiccart/magicmenu/images/' . $object->getId() . '.png';
+ if (file_exists($image)) $url = $this->_urlMedia . 'magiccart/magicmenu/images/' . $object->getId() . '.png';
+ if ($url) return '';
}
public function getThumbnail($object)
{
$url = '';
- $image = $this->_dirMedia . 'magiccart/magicmenu/thumbnail/' . $object->getId() .'.png';
- if(file_exists($image)) $url = $this->_urlMedia . 'magiccart/magicmenu/thumbnail/' . $object->getId() .'.png';
- if($url) return '';
+ $image = $this->_dirMedia . 'magiccart/magicmenu/thumbnail/' . $object->getId() . '.png';
+ if (file_exists($image)) $url = $this->_urlMedia . 'magiccart/magicmenu/thumbnail/' . $object->getId() . '.png';
+ if ($url) return '';
}
}
diff --git a/Helper/Cache.php b/Helper/Cache.php
new file mode 100644
index 0000000..7edba00
--- /dev/null
+++ b/Helper/Cache.php
@@ -0,0 +1,76 @@
+cache = $cache;
+ $this->cacheState = $cacheState;
+ $this->storeManager = $storeManager;
+ $this->storeId = $storeManager->getStore()->getId();
+ parent::__construct($context);
+ }
+
+ /**
+ * @param $method
+ * @param array $vars
+ * @return string
+ */
+ public function getId($method, $vars = array())
+ {
+ return base64_encode($this->storeId . self::CACHE_ID . $method . implode('', $vars));
+ }
+
+ /**
+ * @param $cacheId
+ * @return bool|string
+ */
+ public function load($cacheId)
+ {
+ if ($this->cacheState->isEnabled(self::CACHE_ID)) {
+ return $this->cache->load($cacheId);
+ }
+
+ return FALSE;
+ }
+
+ /**
+ * @param $data
+ * @param $cacheId
+ * @param int $cacheLifetime
+ * @return bool
+ */
+ public function save($data, $cacheId, $cacheLifetime = self::CACHE_LIFETIME)
+ {
+ if ($this->cacheState->isEnabled(self::CACHE_ID)) {
+ $this->cache->save($data, $cacheId, array(self::CACHE_TAG), $cacheLifetime);
+ return TRUE;
+ }
+ return FALSE;
+ }
+}
\ No newline at end of file
diff --git a/Model/Cache/Type.php b/Model/Cache/Type.php
new file mode 100644
index 0000000..ba6402d
--- /dev/null
+++ b/Model/Cache/Type.php
@@ -0,0 +1,25 @@
+get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
+}
+
+}
\ No newline at end of file
diff --git a/etc/cache.xml b/etc/cache.xml
new file mode 100644
index 0000000..ee637cc
--- /dev/null
+++ b/etc/cache.xml
@@ -0,0 +1,7 @@
+
+
+
+
+ Main Menu Cache
+
+
\ No newline at end of file
diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml
index a1fb0e7..dee6f7c 100644
--- a/view/frontend/layout/default.xml
+++ b/view/frontend/layout/default.xml
@@ -21,5 +21,8 @@
+
+
+