-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mageworx/develop
init
- Loading branch information
Showing
10 changed files
with
662 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\HtmlSitemapGraphQl\Model\Resolver\DataProvider; | ||
|
||
use MageWorx\HtmlSitemap\Helper\Data as HelperData; | ||
|
||
class HtmlSitemap | ||
{ | ||
/** | ||
* @var HelperData | ||
*/ | ||
protected $helper; | ||
|
||
/** | ||
* HtmlSitemap constructor. | ||
* | ||
* @param HelperData $helper | ||
*/ | ||
public function __construct(HelperData $helper) | ||
{ | ||
$this->helper = $helper; | ||
} | ||
|
||
/** | ||
* @param int|null $storeId | ||
* @return array | ||
*/ | ||
public function getData(int $storeId = null): array | ||
{ | ||
return [ | ||
'title' => $this->helper->getTitle($storeId), | ||
'meta_description' => $this->helper->getMetaDescription($storeId), | ||
'meta_keywords' => $this->helper->getMetaKeywords($storeId), | ||
'store_id' => $storeId | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\HtmlSitemapGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use MageWorx\HtmlSitemapGraphQl\Model\Resolver\DataProvider\HtmlSitemap as HtmlSitemapDataProvider; | ||
|
||
class HtmlSitemap implements ResolverInterface | ||
{ | ||
/** | ||
* @var HtmlSitemapDataProvider | ||
*/ | ||
protected $dataProvider; | ||
|
||
/** | ||
* HtmlSitemap constructor. | ||
* | ||
* @param HtmlSitemapDataProvider $dataProvider | ||
*/ | ||
public function __construct(HtmlSitemapDataProvider $dataProvider) | ||
{ | ||
$this->dataProvider = $dataProvider; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$storeId = isset($args['storeId']) ? (int)$args['storeId'] : | ||
(int)$context->getExtensionAttributes()->getStore()->getId(); | ||
|
||
return $this->dataProvider->getData($storeId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\HtmlSitemapGraphQl\Model\Resolver\HtmlSitemap; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use MageWorx\HtmlSitemap\Helper\Data as HelperData; | ||
use MageWorx\HtmlSitemap\Helper\StoreUrl as StoreUrlHelper; | ||
|
||
class AdditionalLinks implements ResolverInterface | ||
{ | ||
/** | ||
* @var HelperData | ||
*/ | ||
protected $helper; | ||
|
||
/** | ||
* @var StoreUrlHelper | ||
*/ | ||
protected $storeUrlHelper; | ||
|
||
/** | ||
* AdditionalLinks constructor. | ||
* | ||
* @param HelperData $helper | ||
* @param StoreUrlHelper $storeUrlHelper | ||
*/ | ||
public function __construct(HelperData $helper, StoreUrlHelper $storeUrlHelper) | ||
{ | ||
$this->helper = $helper; | ||
$this->storeUrlHelper = $storeUrlHelper; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$storeId = isset($value['store_id']) ? (int)$value['store_id'] : | ||
(int)$context->getExtensionAttributes()->getStore()->getId(); | ||
|
||
return [ | ||
'items' => $this->getAdditionalLinks($storeId), | ||
]; | ||
} | ||
|
||
/** | ||
* @param int|null $storeId | ||
* @return array|null | ||
*/ | ||
protected function getAdditionalLinks(int $storeId = null): ?array | ||
{ | ||
if (!$this->helper->isShowLinks($storeId)) { | ||
return null; | ||
} | ||
|
||
$links = []; | ||
$addLinks = $this->helper->getAdditionalLinks($storeId); | ||
|
||
if (count($addLinks)) { | ||
foreach ($addLinks as $linkString) { | ||
$link = explode(',', $linkString, 2); | ||
|
||
if (count($link) !== 2) { | ||
continue; | ||
} | ||
|
||
$links[] = | ||
[ | ||
'url' => $this->buildUrl($link[0], $storeId), | ||
'title' => htmlspecialchars(strip_tags(trim($link[1]))) | ||
]; | ||
} | ||
} | ||
|
||
return $links ?: null; | ||
} | ||
|
||
/** | ||
* Convert URL to store URL if schema don't exist. | ||
* | ||
* @param string $rawUrl | ||
* @param int|null $storeId | ||
* @return string | ||
*/ | ||
protected function buildUrl(string $rawUrl, int $storeId = null): string | ||
{ | ||
$url = trim($rawUrl); | ||
|
||
return (strpos($url, '://') !== false) ? $url : $this->storeUrlHelper->getUrl(ltrim($url, '/'), $storeId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\HtmlSitemapGraphQl\Model\Resolver\HtmlSitemap; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use MageWorx\HtmlSitemap\Helper\Data as HelperData; | ||
use MageWorx\HtmlSitemap\Model\ResourceModel\Catalog\CategoryFactory; | ||
use MageWorx\HtmlSitemap\Model\ResourceModel\Catalog\ProductFactory; | ||
|
||
class Categories implements ResolverInterface | ||
{ | ||
/** | ||
* @var HelperData | ||
*/ | ||
protected $helper; | ||
|
||
/** | ||
* @var CategoryFactory | ||
*/ | ||
protected $categoryFactory; | ||
|
||
/** | ||
* @var ProductFactory | ||
*/ | ||
protected $productFactory; | ||
|
||
/** | ||
* @var array|null | ||
*/ | ||
protected $categories; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
protected $storeId; | ||
|
||
/** | ||
* Categories constructor. | ||
* | ||
* @param HelperData $helper | ||
* @param CategoryFactory $categoryFactory | ||
* @param ProductFactory $productFactory | ||
*/ | ||
public function __construct(HelperData $helper, CategoryFactory $categoryFactory, ProductFactory $productFactory) | ||
{ | ||
$this->helper = $helper; | ||
$this->categoryFactory = $categoryFactory; | ||
$this->productFactory = $productFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$storeId = isset($value['store_id']) ? (int)$value['store_id'] : | ||
(int)$context->getExtensionAttributes()->getStore()->getId(); | ||
|
||
return [ | ||
'items' => $this->getCategories($storeId) | ||
]; | ||
} | ||
|
||
/** | ||
* @param int|null $storeId | ||
* @return array|null | ||
*/ | ||
protected function getCategories(int $storeId = null): ?array | ||
{ | ||
if (!$this->helper->isShowCategories($storeId)) { | ||
return null; | ||
} | ||
|
||
$collection = $this->categoryFactory->create()->getCollection($storeId); | ||
$this->categories = []; | ||
$this->storeId = $storeId; | ||
|
||
if (!empty($collection)) { | ||
foreach ($collection as $key => $item) { | ||
if (!isset($level)) { | ||
$level = $item->getLevel(); | ||
} | ||
|
||
if ($item->getLevel() == $level) { | ||
$this->categories[] = $this->prepareCategoryData($item); | ||
|
||
unset($collection[$key]); | ||
$this->addChildren((int)$item->getId(), $collection); | ||
} | ||
} | ||
} | ||
|
||
return $this->categories; | ||
} | ||
|
||
/** | ||
* @param \Magento\Framework\DataObject $category | ||
* @return array | ||
*/ | ||
protected function prepareCategoryData(\Magento\Framework\DataObject $category): array | ||
{ | ||
return [ | ||
'title' => $category->getName(), | ||
'url' => $category->getUrl(), | ||
'level' => $category->getLevel(), | ||
'products' => [ | ||
'items' => $this->getCategoryProducts($category) | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Convert categories to tree | ||
* | ||
* @param int $parentId | ||
* @param array $collection | ||
*/ | ||
protected function addChildren(int $parentId, array &$collection): void | ||
{ | ||
foreach ($collection as $key => $item) { | ||
if ($item->getParentId() != $parentId) { | ||
continue; | ||
} | ||
|
||
$this->categories[] = $this->prepareCategoryData($item); | ||
|
||
if ($item->getChildrenCount()) { | ||
$this->addChildren((int)$item->getId(), $collection); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param \Magento\Framework\DataObject $category | ||
* @return array|null | ||
*/ | ||
protected function getCategoryProducts(\Magento\Framework\DataObject $category): ?array | ||
{ | ||
if (!$this->helper->isShowProducts($this->storeId)) { | ||
return null; | ||
} | ||
|
||
if ($this->helper->isUseCategoryDisplayMode($this->storeId)) { | ||
if ($category->getDisplayMode() == \Magento\Catalog\Model\Category::DM_PAGE) { | ||
return null; | ||
} | ||
} | ||
|
||
$products = []; | ||
$collection = $this->productFactory->create()->getCollection((int)$category->getId(), $this->storeId); | ||
|
||
foreach ($collection as $item) { | ||
$products[] = [ | ||
'title' => $item->getName(), | ||
'url' => $item->getUrl() | ||
]; | ||
} | ||
|
||
return $products; | ||
} | ||
} |
Oops, something went wrong.