Skip to content

Commit

Permalink
Merge pull request #52 from magento-goinc/goinc-bugsfixing
Browse files Browse the repository at this point in the history
[GoInc] bugsfixing
  • Loading branch information
Korshenko, Olexii(okorshenko) committed Oct 28, 2015
2 parents 80c5e2b + af352eb commit 46dc2c0
Show file tree
Hide file tree
Showing 29 changed files with 563 additions and 261 deletions.
39 changes: 39 additions & 0 deletions app/code/Magento/Catalog/Api/Data/ProductWebsiteLinkInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Api\Data;

/**
* @api
*/
interface ProductWebsiteLinkInterface
{
/**
* @return string
*/
public function getSku();

/**
* @param string $sku
* @return $this
*/
public function setSku($sku);

/**
* Get website ids
*
* @return int
*/
public function getWebsiteId();

/**
* Set website id
*
* @param int $websiteId
* @return $this
*/
public function setWebsiteId($websiteId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Api;

interface ProductWebsiteLinkRepositoryInterface
{
/**
* Assign a product to the website
*
* @param \Magento\Catalog\Api\Data\ProductWebsiteLinkInterface $productWebsiteLink
* @return bool will returned True if website successfully assigned to product
*
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
*/
public function save(Data\ProductWebsiteLinkInterface $productWebsiteLink);

/**
* Remove the website assignment from the product
*
* @param \Magento\Catalog\Api\Data\ProductWebsiteLinkInterface $productWebsiteLink
* @return bool will returned True if website successfully unassigned from product
*
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function delete(Data\ProductWebsiteLinkInterface $productWebsiteLink);

/**
* Remove the website assignment from the product by product sku
*
* @param string $sku
* @param int $websiteId
* @return bool will returned True if website successfully unassigned from product
*
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function deleteById($sku, $websiteId);
}
51 changes: 51 additions & 0 deletions app/code/Magento/Catalog/Model/ProductWebsiteLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model;

class ProductWebsiteLink extends \Magento\Framework\Api\AbstractSimpleObject implements
\Magento\Catalog\Api\Data\ProductWebsiteLinkInterface
{
/**#@+
* Field names
*/
const KEY_SKU = 'sku';
const WEBSITE_ID = 'website_id';
/**#@-*/

/**
* {@inheritdoc}
*/
public function getSku()
{
return $this->_get(self::KEY_SKU);
}

/**
* {@inheritdoc}
*/
public function getWebsiteId()
{
return $this->_get(self::WEBSITE_ID);
}

/**
* @param string $sku
* @return $this
*/
public function setSku($sku)
{
return $this->setData(self::KEY_SKU, $sku);
}

/**
* {@inheritdoc}
*/
public function setWebsiteId($websiteId)
{
return $this->setData(self::WEBSITE_ID, $websiteId);
}
}
84 changes: 84 additions & 0 deletions app/code/Magento/Catalog/Model/ProductWebsiteLinkRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Catalog\Api\Data\ProductWebsiteLinkInterface;

class ProductWebsiteLinkRepository implements \Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;

/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
*/
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}

/**
* {@inheritdoc}
*/
public function save(ProductWebsiteLinkInterface $productWebsiteLink)
{
if (!$productWebsiteLink->getWebsiteId()) {
throw new InputException(__('There are not websites for assign to product'));
}
$product = $this->productRepository->get($productWebsiteLink->getSku());
$product->setWebsiteIds(array_merge($product->getWebsiteIds(), [$productWebsiteLink->getWebsiteId()]));
try {
$product->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(
__(
'Could not assign product "%1" to websites "%2"',
$product->getId(),
$productWebsiteLink->getWebsiteId()
),
$e
);
}
return true;
}

/**
* {@inheritdoc}
*/
public function delete(ProductWebsiteLinkInterface $productLink)
{
return $this->deleteById($productLink->getSku(), $productLink->getSku());
}

/**
* {@inheritdoc}
*/
public function deleteById($sku, $websiteId)
{
$product = $this->productRepository->get($sku);
$product->setWebsiteIds(array_diff($product->getWebsiteIds(), [$websiteId]));

try {
$product->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(
__(
'Could not save product "%1" with websites %2',
$product->getId(),
implode(', ', $product->getWebsiteIds())
),
$e
);
}
return true;
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@
<preference for="Magento\Catalog\Api\ProductLinkManagementInterface" type="Magento\Catalog\Model\ProductLink\Management" />
<preference for="Magento\Catalog\Api\Data\ProductLinkInterface" type="Magento\Catalog\Model\ProductLink\Link" />
<preference for="\Magento\Catalog\Api\CategoryLinkManagementInterface" type="\Magento\Catalog\Model\CategoryLinkManagement" />
<preference for="Magento\Catalog\Api\Data\ProductWebsiteLinkInterface" type="Magento\Catalog\Model\ProductWebsiteLink" />
<preference for="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" type="Magento\Catalog\Model\ProductWebsiteLinkRepository" />
<preference for="\Magento\Catalog\Api\CategoryLinkRepositoryInterface" type="\Magento\Catalog\Model\CategoryLinkRepository" />
<preference for="Magento\Catalog\Api\Data\ProductCustomOptionInterface" type="Magento\Catalog\Model\Product\Option" />
<preference for="Magento\Catalog\Api\ProductCustomOptionRepositoryInterface" type="\Magento\Catalog\Model\Product\Option\Repository" />
Expand Down
20 changes: 20 additions & 0 deletions app/code/Magento/Catalog/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,24 @@
<resource ref="Magento_Catalog::categories" />
</resources>
</route>

<!-- Product Website Links -->
<route url="/V1/products/:sku/websites" method="POST">
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="save" />
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<route url="/V1/products/:sku/websites" method="PUT">
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="save" />
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
<route url="/V1/products/:sku/websites/:websiteId" method="DELETE">
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="deleteById" />
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function getUrlPath($category)
return $category->getUrlPath();
}
if ($this->isNeedToGenerateUrlPathForParent($category)) {
$parentPath = $this->getUrlPath($this->categoryRepository->get($category->getParentId()));
$parentPath = $this->getUrlPath(
$this->categoryRepository->get($category->getParentId(), $category->getStoreId())
);
$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
}
return $path;
Expand Down Expand Up @@ -141,7 +143,7 @@ public function getCanonicalUrlPath($category)
* @param \Magento\Catalog\Model\Category $category
* @return string
*/
public function generateUrlKey($category)
public function getUrlKey($category)
{
$urlKey = $category->getUrlKey();
return $category->formatUrlKey($urlKey === '' || $urlKey === null ? $category->getName() : $urlKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getCanonicalUrlPath($product, $category = null)
* @param \Magento\Catalog\Model\Product $product
* @return string
*/
public function generateUrlKey($product)
public function getUrlKey($product)
{
return $product->getUrlKey() === false ? false : $this->prepareProductUrlKey($product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
use Magento\Framework\Event\Observer;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\Store;

class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
{
Expand All @@ -19,16 +21,22 @@ class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider */
protected $childrenCategoriesProvider;

/** @var StoreViewService */
protected $storeViewService;

/**
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
* @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService
*/
public function __construct(
CategoryUrlPathGenerator $categoryUrlPathGenerator,
ChildrenCategoriesProvider $childrenCategoriesProvider
ChildrenCategoriesProvider $childrenCategoriesProvider,
StoreViewService $storeViewService
) {
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->storeViewService = $storeViewService;
}

/**
Expand All @@ -40,7 +48,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var Category $category */
$category = $observer->getEvent()->getCategory();
if ($category->getUrlKey() !== false) {
$category->setUrlKey($this->categoryUrlPathGenerator->generateUrlKey($category))
$category->setUrlKey($this->categoryUrlPathGenerator->getUrlKey($category))
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
if (!$category->isObjectNew()) {
$category->getResource()->saveAttribute($category, 'url_path');
Expand All @@ -57,10 +65,48 @@ public function execute(\Magento\Framework\Event\Observer $observer)
*/
protected function updateUrlPathForChildren(Category $category)
{
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
$childCategory->unsUrlPath();
$childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');
$children = $this->childrenCategoriesProvider->getChildren($category, true);

if ($this->isGlobalScope($category->getStoreId())) {
foreach ($children as $child) {
foreach ($category->getStoreIds() as $storeId) {
if ($this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
$storeId,
$child->getId(),
Category::ENTITY
)) {
$child->setStoreId($storeId);
$this->updateUrlPathForCategory($child);
}
}
}
} else {
foreach ($children as $child) {
$child->setStoreId($category->getStoreId());
$this->updateUrlPathForCategory($child);
}
}
}

/**
* Check is global scope
*
* @param int|null $storeId
* @return bool
*/
protected function isGlobalScope($storeId)
{
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
}

/**
* @param Category $category
* @return void
*/
protected function updateUrlPathForCategory(Category $category)
{
$category->unsUrlPath();
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
$category->getResource()->saveAttribute($category, 'url_path');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var Product $product */
$product = $observer->getEvent()->getProduct();
$product->setUrlKey($this->productUrlPathGenerator->generateUrlKey($product));
$product->setUrlKey($this->productUrlPathGenerator->getUrlKey($product));
}
}
Loading

0 comments on commit 46dc2c0

Please sign in to comment.