Skip to content

Commit

Permalink
add model methods for dynamic menu listing for sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent authored and Laurent committed Jan 28, 2021
1 parent 75cb2d7 commit 4bd14f4
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Backend;
use System\Classes\PluginBase;
use Event;

/**
* Catalog Plugin Information File
Expand Down Expand Up @@ -40,7 +41,36 @@ public function register()
*/
public function boot()
{
// Register menu items
Event::listen('pages.menuitem.listTypes', function () {
return [
'catalog-category' => 'Catalog Category',
'all-catalog-categories' => 'All Catalog Categories',
'catalog-product' => 'Catalog Product',
'all-catalog-products' => 'All Catalog Products',
'category-catalog-products' => 'Catagorized Catalog Products'
];
});

Event::listen('pages.menuitem.getTypeInfo', function ($type) {
if ($type == 'catalog-category' || $type == 'all-catalog-categories') {
return \Lbaig\Catalog\Models\Category::getMenuTypeInfo($type);
}
elseif ($type == 'catalog-product' || $type == 'all-catalog-products' ||
$type == 'category-catalog-products') {
return \LBaig\Catalog\Models\Product::getMenuTypeInfo($type);
}
});

Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
if ($type == 'catalog-category' || $type == 'all-catalog-categories') {
return \Lbaig\Catalog\Models\Category::resolveMenuItem($item, $url, $theme);
}
elseif ($type == 'catalog-product' || $type == 'all-catalog-products' ||
$type == 'category-catalog-products') {
return \LBaig\Catalog\Models\Product::resolveMenuItem($item, $url, $theme);
}
});
}

/**
Expand Down
167 changes: 167 additions & 0 deletions models/Category.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace Lbaig\Catalog\Models;

use Model;
use Cms\Classes\Page as CmsPage;
use Cms\Classes\Theme;

/**
* Category Model
Expand Down Expand Up @@ -132,4 +134,169 @@ public function getAllProductsCountAttribute()
$all_product_count = $direct_product_count + $other_product_count;
return $all_product_count;
}

public static function getMenuTypeInfo($type)
{
$result = [];
if ($type == 'catalog-category') {
$result = [
'references' => self::listSubCategoryOptions(),
'nesting' => true,
'dynamicItems' => true
];
}

if ($type == 'all-catalog-categories') {
$result = [
'dynamicItems' => true
];
}

if ($result) {
$theme = Theme::getActiveTheme();
$pages = CmsPage::listInTheme($theme, true);
$cmsPages = [];
foreach ($pages as $page) {
if (!$page->hasComponent('CategoryItem')) {
continue;
}

$properties = $page->getComponentProperties('CategoryItem');
if (!isset($properties['slug']) || !preg_match('/{{\s*:/', $properties['slug'])) {
continue;
}

$cmsPages[] = $page;
}

$result['cmsPages'] = $cmsPages;
}

return $result;
}

protected static function listSubCategoryOptions()
{
$category = self::getNested();

$iterator = function($categories) use (&$iterator) {
$result = [];

foreach ($categories as $category) {
if (!$category->children) {
$result[$category->id] = $category->name;
}
else {
$result[$category->id] = [
'title' => $category->name,
'items' => $iterator($category->children)
];
}
}

return $result;
};

return $iterator($category);
}

public static function resolveMenuItem($item, $url, $theme)
{
$result = null;

if ($item->type == 'catalog-catagory') {
if (!$item->reference || $item->cmsPage) {
return;
}

$category = self::find($item->reference);
if (!$category) {
return;
}

$pageUrl = self::getCategoryPageUrl($item->cmsPage, $category, $theme);
if (!$pageUrl) {
return;
}

$pageUrl = Url::to($pageUrl);

$result = [];
$result['url'] = $pageUrl;
$result['isActive'] = $pageUrl == $url;
$result['mtime'] = $category->updated_at;

if ($item->nesting) {
$categories = $category->getNested();
$iterator = function($categories) use (&$iterator, &$item, &$theme, $url) {
$branch = [];

foreach ($categories as $category) {

$branchItem = [];
$branchItem['url'] = self::getCategoryPageUrl($item->cmsPage, $category, $theme);
$branchItem['isActive'] = $branchItem['url'] == $url;
$branchItem['title'] = $category->name;
$branchItem['mtime'] = $category->updated_at;

if ($category->children) {
$branchItem['items'] = $iterator($category->children);
}

$branch[] = $branchItem;
}

return $branch;
};

$result['items'] = $iterator($categories);
}
}
elseif ($item->type == 'all-catalog-categories') {
$result = [
'items' => []
];

$categories = self::orderBy('name')->get();
foreach ($categories as $category) {
$categoryItem = [
'title' => $category->name,
'url' => self::getCategoryPageUrl($item->cmsPage, $category, $theme),
'mtime' => $category->updated_at
];

$categoryItem['isActive'] = $categoryItem['url'] == $url;

$result['items'][] = $categoryItem;
}
}

return $result;
}

protected static function getCategoryPageUrl($pageCode, $category, $theme)
{
$page = CmsPage::loadCached($theme, $pageCode);
if (!$page) {
return;
}

$properties = $page->getComponentProperties('CategoryItem');
if (!isset($properties['slug'])) {
return;
}

/*
* Extract the routing parameter name from the category filter
* eg: {{ :someRouteParam }}
*/
if (!preg_match('/^\{\{([^\}]+)\}\}$/', $properties['slug'], $matches)) {
return;
}

$paramName = substr(trim($matches[1]), 1);
$url = CmsPage::url($page->getBaseFileName(), [$paramName => $category->slug]);

return $url;
}
}
Loading

0 comments on commit 4bd14f4

Please sign in to comment.