/**
  * Sets the categories. Non-existing categories will be created immediately.
  */
 protected function setCategories()
 {
     // No categories
     if (is_null($this->ItemBase->Categories)) {
         return;
     }
     if (is_null(self::$CategoryApi)) {
         self::$CategoryApi = Shopware\Components\Api\Manager::getResource('Category');
     }
     if (is_null(self::$CategoryRepository)) {
         self::$CategoryRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category');
     }
     // Categories
     /** @var PlentySoapObject_ItemCategory $Category */
     foreach ($this->ItemBase->Categories->item as $Category) {
         // FIX: corrupt category within plenty
         if ((int) $Category->ItemCategoryID <= 0 || empty($Category->ItemCategoryPathNames)) {
             continue;
         }
         try {
             $categoryId = PlentymarketsMappingEntityCategory::getCategoryByPlentyID($Category->ItemCategoryID, $this->storeId);
             $this->categories[] = array('id' => $categoryId);
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             $importEntityItemCategoryTree = new PlentymarketsImportEntityItemCategoryTree($Category, $this->storeId);
             $categoryId = $importEntityItemCategoryTree->import();
             // Only create a mapping and connect the category to the item,
             // of nothing went wrong during creation
             if ($categoryId) {
                 $this->categories[] = array('id' => $categoryId);
             }
         }
     }
 }
 /**
  * I am the constructor
  *
  * @param PlentySoapObject_ItemCategoryTreeNode|PlentySoapObject_ItemCategory $categoryNode
  * @param integer $storeId
  * @throws Exception
  */
 public function __construct($categoryNode, $storeId, $categoryNodeLang = 'de')
 {
     $category = array();
     if (property_exists($categoryNode, 'ItemCategoryPath')) {
         $categoryPath = explode(';', $categoryNode->ItemCategoryPath);
         $categoryPathNames = explode(';', $categoryNode->ItemCategoryPathNames);
     } else {
         if (property_exists($categoryNode, 'CategoryPath')) {
             $categoryPath = explode(';', $categoryNode->CategoryPath);
             $categoryPathNames = explode(';', $categoryNode->CategoryPathNames);
         } else {
             throw new Exception();
         }
     }
     foreach ($categoryPath as $n => $categoryId) {
         if ($categoryId == 0) {
             break;
         }
         $category[] = array('branchId' => $categoryId, 'name' => $categoryPathNames[$n]);
     }
     $this->plentyCategoryBranch = $category;
     $this->storeId = $storeId;
     $this->shopId = PlentymarketsMappingController::getShopByPlentyID($storeId);
     $this->categoryNodeLang = $categoryNodeLang;
     // get the main language of the shop set it temp
     $mainLang = array_values(PlentymarketsTranslation::getShopMainLanguage($this->shopId));
     $this->shopLang = PlentymarketsTranslation::getPlentyLocaleFormat($mainLang[0]['locale']);
     if (is_null(self::$CategoryRepository)) {
         self::$CategoryRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category');
     }
     if (is_null(self::$CategoryApi)) {
         self::$CategoryApi = Shopware\Components\Api\Manager::getResource('Category');
     }
 }
    /**
     * Performs the actual import
     *
     * @throws PlentymarketsExportException
     */
    public function run()
    {
        // Get the data from plentymarkets (for every mapped shop)
        $shopIds = Shopware()->Db()->fetchAll('
			SELECT * FROM plenty_mapping_shop
		');
        foreach ($shopIds as $shopId) {
            $mainShopId = PlentymarketsMappingController::getShopByPlentyID($shopId['plentyID']);
            $mainLang = array_values(PlentymarketsTranslation::getShopMainLanguage($mainShopId));
            $Request_GetItemCategoryTree = new PlentySoapRequest_GetItemCategoryTree();
            $Request_GetItemCategoryTree->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($mainLang[0]['locale']);
            $Request_GetItemCategoryTree->GetCategoryNames = true;
            $Request_GetItemCategoryTree->StoreID = $shopId['plentyID'];
            $Request_GetItemCategoryTree->GetAktivCategories = true;
            /** @var PlentySoapResponse_GetItemCategoryTree $Response_GetItemCategoryTree */
            $Response_GetItemCategoryTree = PlentymarketsSoapClient::getInstance()->GetItemCategoryTree($Request_GetItemCategoryTree);
            if (!$Response_GetItemCategoryTree->Success) {
                Shopware()->Db()->query('INSERT plenty_mapping_category SELECT * FROM plenty_mapping_category_old');
                throw new PlentymarketsImportException('The item category tree could not be retrieved', 2920);
            }
            /** @var PlentySoapObject_ItemCategoryTreeNode $Category */
            foreach ($Response_GetItemCategoryTree->MultishopTree->item[0]->CategoryTree->item as $Category) {
                $importEntityItemCategoryTree = new PlentymarketsImportEntityItemCategoryTree($Category, $shopId['plentyID'], $Request_GetItemCategoryTree->Lang);
                $importEntityItemCategoryTree->import();
            }
        }
        $this->rebuild();
    }
 /**
  * I am the constructor
  *
  * @param PlentySoapObject_ItemCategoryTreeNode|PlentySoapObject_ItemCategory $categoryNode
  * @param integer $storeId
  * @throws Exception
  */
 public function __construct($categoryNode, $storeId)
 {
     $category = array();
     if (property_exists($categoryNode, 'ItemCategoryPath')) {
         $categoryPath = explode(';', $categoryNode->ItemCategoryPath);
         $categoryPathNames = explode(';', $categoryNode->ItemCategoryPathNames);
     } else {
         if (property_exists($categoryNode, 'CategoryPath')) {
             $categoryPath = explode(';', $categoryNode->CategoryPath);
             $categoryPathNames = explode(';', $categoryNode->CategoryPathNames);
         } else {
             throw new Exception();
         }
     }
     foreach ($categoryPath as $n => $categoryId) {
         if ($categoryId == 0) {
             break;
         }
         $category[] = array('branchId' => $categoryId, 'name' => $categoryPathNames[$n]);
     }
     $this->plentyCategoryBranch = $category;
     $this->storeId = $storeId;
     $this->shopId = PlentymarketsMappingController::getShopByPlentyID($storeId);
     if (is_null(self::$CategoryRepository)) {
         self::$CategoryRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category');
     }
     if (is_null(self::$CategoryApi)) {
         self::$CategoryApi = Shopware\Components\Api\Manager::getResource('Category');
     }
 }