Пример #1
0
 public function convertCategory(Models\Category\Category $category)
 {
     $struct = new Struct\Category();
     $struct->setId($category->getId());
     $struct->setName($category->getName());
     $struct->setPath($category->getPath());
     return $struct;
 }
Пример #2
0
 /**
  * Class constructor.
  */
 public function __construct(\Shopware\Models\Category\Category $category = null, $translationId = null, $customerGroupId = null)
 {
     $this->category = $category ?: Shopware()->Shop()->getCategory();
     $this->categoryId = $this->category->getId();
     $this->translationId = $translationId ?: (!Shopware()->Shop()->getDefault() ? Shopware()->Shop()->getId() : null);
     $this->customerGroupId = $customerGroupId ?: (int) Shopware()->Modules()->System()->sUSERGROUPDATA['id'];
 }
Пример #3
0
 public function __construct(\Shopware\Models\Category\Category $category = null, $translationId = null, $customerGroupId = null)
 {
     $container = Shopware()->Container();
     $this->category = $category ?: Shopware()->Shop()->getCategory();
     $this->categoryId = $this->category->getId();
     $this->translationId = $translationId ?: (!Shopware()->Shop()->getDefault() ? Shopware()->Shop()->getId() : null);
     $this->customerGroupId = $customerGroupId ?: (int) Shopware()->Modules()->System()->sUSERGROUPDATA['id'];
     $this->config = $container->get('config');
     $this->db = $container->get('db');
     $this->eventManager = $container->get('events');
     $this->contextService = $container->get('shopware_storefront.context_service');
     $this->listProductService = $container->get('shopware_storefront.list_product_service');
     $this->productService = $container->get('shopware_storefront.product_service');
     $this->productNumberSearch = $container->get('shopware_search.product_number_search');
     $this->configuratorService = $container->get('shopware_storefront.configurator_service');
     $this->propertyService = $container->get('shopware_storefront.property_service');
     $this->additionalTextService = $container->get('shopware_storefront.additional_text_service');
     $this->searchService = $container->get('shopware_search.product_search');
     $this->queryAliasMapper = $container->get('query_alias_mapper');
     $this->frontController = $container->get('front');
     $this->legacyStructConverter = $container->get('legacy_struct_converter');
     $this->legacyEventManager = $container->get('legacy_event_manager');
     $this->session = $container->get('session');
     $this->storeFrontCriteriaFactory = $container->get('shopware_search.store_front_criteria_factory');
     $this->productNumberService = $container->get('shopware_storefront.product_number_service');
     $this->articleComparisons = new sArticlesComparisons($this, $container);
 }
Пример #4
0
 /**
  * Class constructor.
  */
 public function __construct()
 {
     $this->category = Shopware()->Shop()->getCategory();
     $this->categoryId = $this->category->getId();
     $this->translationId = !Shopware()->Shop()->getDefault() ? Shopware()->Shop()->getId() : null;
     $this->customerGroupId = (int) Shopware()->Modules()->System()->sSYSTEM->sUSERGROUPDATA['id'];
 }
Пример #5
0
 /**
  * @param array $category
  * @return bool|int
  */
 public function import(array $category)
 {
     $category = $this->prepareCategoryData($category);
     // Try to find an existing category by name and parent
     $model = null;
     if (isset($category['parent']) && isset($category['name'])) {
         $model = $this->repository->findOneBy(['parent' => $category['parent'], 'name' => $category['name']]);
     }
     if (!$model instanceof Category) {
         $model = new Category();
     }
     $parentModel = null;
     if (isset($category['parent'])) {
         $parentModel = $this->repository->find((int) $category['parent']);
         if (!$parentModel instanceof Category) {
             $this->logger->error("Parent category {$category['parent']} not found!");
             return false;
         }
     }
     $model->fromArray($category);
     $model->setParent($parentModel);
     $this->em->persist($model);
     $this->em->flush();
     // Set category attributes
     $attributes = $this->prepareCategoryAttributesData($category);
     unset($category);
     $categoryId = $model->getId();
     if (!empty($attributes)) {
         $attributeID = $this->db->fetchOne("SELECT id FROM s_categories_attributes WHERE categoryID = ?", [$categoryId]);
         if ($attributeID === false) {
             $attributes['categoryID'] = $categoryId;
             $this->db->insert('s_categories_attributes', $attributes);
         } else {
             $this->db->update('s_categories_attributes', $attributes, ['categoryID = ?' => $categoryId]);
         }
     }
     return $categoryId;
 }
Пример #6
0
 /**
  * @param CategoryEntity $category
  * @return Category
  */
 public static function createFromCategoryEntity(CategoryEntity $category)
 {
     $struct = new self();
     $struct->setId($category->getId());
     $struct->setName($category->getName());
     $struct->setPosition($category->getPosition());
     $struct->setParentId($category->getParentId());
     $path = $category->getPath();
     if ($path) {
         $path = ltrim($path, '|');
         $path = rtrim($path, '|');
         $path = explode('|', $path);
         $struct->setPath(array_reverse($path));
     }
     return $struct;
 }
Пример #7
0
 /**
  * Sets the internal path field for given category based on it's parents
  *
  * @param Category $category
  * @return Category
  */
 public function setPathForCategory(Category $category)
 {
     $parent = $category->getParent();
     $parentId = $parent->getId();
     $parents = $this->getCategoryComponent()->getParentCategoryIds($parentId);
     $path = implode('|', $parents);
     if (empty($path)) {
         $path = null;
     } else {
         $path = '|' . $path . '|';
     }
     $category->internalSetPath($path);
     return $category;
 }
Пример #8
0
 /**
  * @param array $data
  * @param CategoryModel $categoryModel
  * @return array
  * @throws ApiException\CustomValidationException
  */
 private function prepareMediaData(array $data, CategoryModel $categoryModel)
 {
     if (!isset($data['media'])) {
         return $data;
     }
     $media = null;
     if (isset($data['media']['link'])) {
         /**@var $media MediaModel */
         $media = $this->getResource('media')->internalCreateMediaByFileLink($data['media']['link']);
     } elseif (!empty($data['media']['mediaId'])) {
         $media = $this->getManager()->find('Shopware\\Models\\Media\\Media', (int) $data['media']['mediaId']);
         if (!$media instanceof MediaModel) {
             throw new ApiException\CustomValidationException(sprintf("Media by mediaId %s not found", $data['media']['mediaId']));
         }
     }
     $categoryModel->setMedia($media);
     unset($data['media']);
     return $data;
 }
Пример #9
0
 /**
  * Helper function for the isChildOf function. This function is used for a recursive call.
  *
  * @param $category Category
  * @param $searched Category
  * @return bool
  */
 protected function isChildOfInternal(Category $category, Category $searched)
 {
     if ($category->getParent() && $category->getParent()->getId() === $searched->getId()) {
         return true;
     }
     if ($category->getParent() instanceof Category) {
         return $this->isChildOfInternal($category->getParent(), $searched);
     }
     return false;
 }
Пример #10
0
    /**
     * @param array $params
     * @return \Shopware\Models\Category\Category
     * @throws \Shopware\Components\Api\Exception\ValidationException
     * @throws \Exception
     */
    public function create(array $params)
    {
        $this->checkPrivilege('create');

        $params = $this->prepareCategoryData($params);

        $category = new \Shopware\Models\Category\Category();
        $category->fromArray($params);

        $violations = $this->getManager()->validate($category);
        if ($violations->count() > 0) {
            throw new ApiException\ValidationException($violations);
        }

        $this->getManager()->persist($category);
        $this->flush();

        return $category;
    }
Пример #11
0
 /**
  * @param array $params
  * @return \Shopware\Models\Category\Category
  * @throws \Shopware\Components\Api\Exception\ValidationException
  * @throws \Exception
  */
 public function create(array $params)
 {
     $this->checkPrivilege('create');
     $params = $this->prepareCategoryData($params);
     $category = new \Shopware\Models\Category\Category();
     $category->fromArray($params);
     if (isset($params['id'])) {
         $metaData = $this->getManager()->getMetadataFactory()->getMetadataFor('Shopware\\Models\\Category\\Category');
         $metaData->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
         $category->setPrimaryIdentifier($params['id']);
     }
     $violations = $this->getManager()->validate($category);
     if ($violations->count() > 0) {
         throw new ApiException\ValidationException($violations);
     }
     $this->getManager()->persist($category);
     $this->flush();
     return $category;
 }
 /**
  * {@inheritDoc}
  */
 public function setManyToOne($data, $model, $property)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setManyToOne', array($data, $model, $property));
     return parent::setManyToOne($data, $model, $property);
 }
 /**
  * Collects recursively category ids
  *
  * @param \Shopware\Models\Category\Category $categoryModel
  */
 protected function collectCategoryIds($categoryModel)
 {
     $categoryId = $categoryModel->getId();
     $this->setCategoryIdCollection($categoryId);
     $categories = $categoryModel->getChildren();
     if (!$categories) {
         return;
     }
     foreach ($categories as $category) {
         $this->collectCategoryIds($category);
     }
     return;
 }
 /**
  * @param int|null $categoryId
  * @return Category
  */
 private function createCategoryAndSetId($categoryId)
 {
     $category = new Category();
     if ($categoryId) {
         $category->setId($categoryId);
     }
     return $category;
 }
Пример #15
0
 /**
  * @param $number
  * @param ProductContext $context
  * @param Category $category
  * @return array
  */
 protected function getProduct($number, ProductContext $context, Category $category = null)
 {
     $product = $this->helper->getSimpleProduct($number, array_shift($context->getTaxRules()), $context->getCurrentCustomerGroup());
     $product['categories'] = [['id' => $context->getShop()->getCategory()->getId()]];
     if ($category) {
         $product['categories'] = array(array('id' => $category->getId()));
     }
     return $product;
 }
Пример #16
0
 /**
  * Check current category for child categories and
  * add ids to collection.
  *
  * @param Category $categoryModel
  */
 private function collectCategoryIds($categoryModel)
 {
     $categoryId = $categoryModel->getId();
     $this->setCategoryIdCollection($categoryId);
     $sql = "SELECT id FROM s_categories WHERE path LIKE ?";
     $categories = Shopware()->Db()->fetchAll($sql, ['%|' . $categoryId . '|%']);
     if (!$categories) {
         return;
     }
     foreach ($categories as $categoryId) {
         $this->setCategoryIdCollection($categoryId);
     }
     return;
 }