/**
  * Performs the actual import
  *
  * @param integer $lastUpdateTimestamp
  */
 public function run($lastUpdateTimestamp)
 {
     $Request_GetItemAttributes = new PlentySoapRequest_GetItemAttributes();
     $Request_GetItemAttributes->GetValues = true;
     $Request_GetItemAttributes->LastUpdateFrom = $lastUpdateTimestamp;
     /** @var PlentySoapResponse_GetItemAttributes $Response_GetItemAttributes */
     $Response_GetItemAttributes = PlentymarketsSoapClient::getInstance()->GetItemAttributes($Request_GetItemAttributes);
     if (!$Response_GetItemAttributes->Success) {
         return;
     }
     foreach ($Response_GetItemAttributes->Attributes->item as $Attribute) {
         $PlentymarketsImportEntityItemAttribute = new PlentymarketsImportEntityItemAttribute($Attribute);
         $PlentymarketsImportEntityItemAttribute->import();
     }
     // run import of attributes and attributes value translations
     $mainShops = PlentymarketsUtils::getShopwareMainShops();
     /** @var $mainShop Shopware\Models\Shop\Shop */
     foreach ($mainShops as $mainShop) {
         // get all active languages of the main shop
         $activeLanguages = PlentymarketsTranslation::getShopActiveLanguages($mainShop->getId());
         foreach ($activeLanguages as $key => $language) {
             $Request_GetItemAttributes = new PlentySoapRequest_GetItemAttributes();
             $Request_GetItemAttributes->GetValues = true;
             $Request_GetItemAttributes->LastUpdateFrom = $lastUpdateTimestamp;
             $Request_GetItemAttributes->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']);
             /** @var PlentySoapResponse_GetItemAttributes $Response_GetItemAttributes */
             $Response_GetItemAttributes = PlentymarketsSoapClient::getInstance()->GetItemAttributes($Request_GetItemAttributes);
             if ($Response_GetItemAttributes->Success) {
                 foreach ($Response_GetItemAttributes->Attributes->item as $Attribute) {
                     $PlentymarketsImportEntityItemAttribute = new PlentymarketsImportEntityItemAttribute($Attribute);
                     // set the atrribute translations from plenty for the language shops
                     if (!is_null($language['mainShopId'])) {
                         $languageShopID = PlentymarketsTranslation::getLanguageShopID($key, $language['mainShopId']);
                         $PlentymarketsImportEntityItemAttribute->importTranslation($languageShopID);
                     } else {
                         // import translations for the main shop languages
                         $PlentymarketsImportEntityItemAttribute->importTranslation($mainShop->getId());
                     }
                 }
             }
         }
     }
 }
    /**
     * Import all translation of the PropertyValue
     */
    public function importItemPropertyValueTranslations()
    {
        // array with item properties only in German
        $german_itemProperties = array_filter($this->ItemBase->ItemProperties->item, function ($property) {
            return $property->PropertyValueLang == 'de';
        });
        // array with item properties only in german
        $otherLang_itemProperties = array_filter($this->ItemBase->ItemProperties->item, function ($property) {
            return !($property->PropertyValueLang == 'de');
        });
        // Properties in other languages as German
        /** @var PlentySoapObject_ItemProperty $ItemProperty */
        foreach ($otherLang_itemProperties as $ItemProperty) {
            // search for the german property value to get afterwards the shopware property value id from tb: s_filter_values
            /** @var PlentySoapObject_ItemProperty $germanProperty */
            foreach ($german_itemProperties as $germanProperty) {
                if ($germanProperty->PropertyID == $ItemProperty->PropertyID) {
                    // the german Property is found
                    break;
                }
            }
            // search for the shopware language shop
            $shopId = null;
            // get all active languages of the main shop
            $activeLanguages = PlentymarketsTranslation::getShopActiveLanguages($this->Shop->getId());
            // search the language shop with the language equal with the property language
            foreach ($activeLanguages as $localeId => $language) {
                if (PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']) == $ItemProperty->PropertyValueLang) {
                    // if the founded shop is a language shop
                    if (!is_null($language['mainShopId'])) {
                        $shopId = PlentymarketsTranslation::getLanguageShopID($localeId, $language['mainShopId']);
                    } else {
                        // the main shop has the same language as the property
                        $shopId = $this->Shop->getId();
                    }
                }
            }
            // if the language shop was found, save the property value for this language shop
            if (!is_null($shopId)) {
                // try to get the property value Id from TB : s_filter_values
                // !! in TB: s_filter_values the values are saved in the German language = $germanProperty->PropertyValue
                try {
                    $shopware_property = PlentymarketsMappingController::getPropertyByPlentyID($ItemProperty->PropertyID);
                    $parts = explode(';', $shopware_property);
                    $shopware_propertyID = $parts[1];
                    $sql = 'SELECT id
						FROM s_filter_values
						WHERE optionID =' . $shopware_propertyID . ' AND value LIKE "%' . $germanProperty->PropertyValue . '%"';
                    $response = Shopware()->Db()->query($sql)->fetchAll();
                    $shopware_propertyValueID = $response[0]['id'];
                    if (!is_null($shopware_propertyValueID)) {
                        // save the translation of the property
                        $property_data = array('optionValue' => $ItemProperty->PropertyValue);
                        PlentymarketsTranslation::setShopwareTranslation('propertyvalue', $shopware_propertyValueID, $shopId, $property_data);
                    }
                } catch (Exception $e) {
                    // throw exception
                }
            }
        }
    }
 /**
  * @param $shopware_ImageID int
  * @param $plenty_ImageNames PlentySoapResponse_ObjectGetItemImageName[]
  * @param $shopware_storeID int
  */
 private function importImageTitleTranslation($shopware_ImageID, $plenty_ImageNames, $shopware_storeID)
 {
     // get all active languages of the main shop
     $activeLanguages = PlentymarketsTranslation::getShopActiveLanguages($shopware_storeID);
     foreach ($activeLanguages as $localeId => $language) {
         /**
          * @var $plentyImageName  PlentySoapResponse_ObjectGetItemImageName
          */
         foreach ($plenty_ImageNames as $plentyImageName) {
             if (!is_null($plentyImageName->Name) && strlen($plentyImageName->Name) > 0) {
                 // search the language shop with the language equal as the image name language
                 if (PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']) == $plentyImageName->Lang) {
                     $shopId = null;
                     // if the founded shop is a language shop
                     if (!is_null($language['mainShopId'])) {
                         $shopId = PlentymarketsTranslation::getLanguageShopID($localeId, $language['mainShopId']);
                     } elseif (PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']) != 'de') {
                         // set the imagae title translation for the main shop that has the main language other as German
                         $shopId = $shopware_storeID;
                     }
                     // if the language shop was found / set , save the image title translation for this language shop
                     if (!is_null($shopId)) {
                         // save the translation of the plenty image title
                         $image_data = array('description' => $plentyImageName->Name);
                         PlentymarketsTranslation::setShopwareTranslation('articleimage', $shopware_ImageID, $shopId, $image_data);
                     }
                 }
             }
         }
     }
 }
 /**
  * imports the item for the given shop
  *
  * @param integer $itemId
  * @param integer $storeId
  * @throws PlentymarketsImportException
  */
 public function importItem($itemId, $storeId)
 {
     // Check whether the item has already been imported
     $full = !isset($this->itemIdsDone[$itemId]);
     // Build the request
     $Request_GetItemsBase = new PlentySoapRequest_GetItemsBase();
     $Request_GetItemsBase->GetAttributeValueSets = $full;
     $Request_GetItemsBase->GetCategories = true;
     $Request_GetItemsBase->GetCategoryNames = true;
     $Request_GetItemsBase->GetItemAttributeMarkup = $full;
     $Request_GetItemsBase->GetItemOthers = $full;
     //$Request_GetItemsBase->GetItemProperties = $full;
     $Request_GetItemsBase->GetItemProperties = true;
     $Request_GetItemsBase->GetItemSuppliers = false;
     $Request_GetItemsBase->GetItemURL = 0;
     $Request_GetItemsBase->GetLongDescription = $full;
     $Request_GetItemsBase->GetMetaDescription = false;
     $Request_GetItemsBase->GetShortDescription = $full;
     $Request_GetItemsBase->GetTechnicalData = false;
     $Request_GetItemsBase->StoreID = $storeId;
     $Request_GetItemsBase->ItemID = $itemId;
     // get the main language of the shop
     //$mainLang = array_values(PlentymarketsTranslation::getInstance()->getShopMainLanguage(PlentymarketsMappingController::getShopByPlentyID($storeId)));
     // set the main language of the shop in soap request
     //$Request_GetItemsBase->Lang = PlentymarketsTranslation::getInstance()->getPlentyLocaleFormat($mainLang[0]['locale']);
     $Request_GetItemsBase->Lang = 'de';
     // Do the request
     $Response_GetItemsBase = PlentymarketsSoapClient::getInstance()->GetItemsBase($Request_GetItemsBase);
     // On error
     if ($Response_GetItemsBase->Success == false) {
         // Re-add the item to the stack and quit
         PlentymarketsImportStackItem::getInstance()->addItem($itemId, $storeId);
         return;
     }
     // Item not found
     if (!isset($Response_GetItemsBase->ItemsBase->item[0])) {
         return;
     }
     //
     $ItemBase = $Response_GetItemsBase->ItemsBase->item[0];
     // Skip bundles
     if ($ItemBase->BundleType == 'bundle' && PlentymarketsConfig::getInstance()->getItemBundleHeadActionID(IMPORT_ITEM_BUNDLE_HEAD_NO) == IMPORT_ITEM_BUNDLE_HEAD_NO) {
         PlentymarketsLogger::getInstance()->message('Sync:Item', 'The item »' . $ItemBase->Texts->Name . '« will be skipped (bundle)');
         return;
     }
     // get the item texts in all active languages
     $itemTexts = array();
     $shopId = PlentymarketsMappingController::getShopByPlentyID($storeId);
     //if this is a main shop , get the item texts translation for its main language and its shop languages
     if (PlentymarketsTranslation::isMainShop($shopId)) {
         // get all active languages of the shop (from shopware)
         $activeLanguages = PlentymarketsTranslation::getShopActiveLanguages($shopId);
         foreach ($activeLanguages as $localeId => $language) {
             $Request_GetItemsTexts = new PlentySoapRequest_GetItemsTexts();
             $Request_GetItemsTexts->ItemsList = array();
             $Object_RequestItems = new PlentySoapObject_RequestItems();
             $Object_RequestItems->ExternalItemNumer = null;
             // string
             $Object_RequestItems->ItemId = $itemId;
             // string
             $Object_RequestItems->ItemNumber = null;
             // string
             $Object_RequestItems->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']);
             // string
             $Request_GetItemsTexts->ItemsList[] = $Object_RequestItems;
             $Response_GetItemsTexts = PlentymarketsSoapClient::getInstance()->GetItemsTexts($Request_GetItemsTexts);
             if (isset($Response_GetItemsTexts->ItemTexts->item[0])) {
                 $itemText = array();
                 // save the language infos for the item texts
                 $itemText['locale'] = $language['locale'];
                 // if mainShopId == null, then it is the main shop and no language shop
                 // each language shop has a mainShopId
                 if (!is_null($language['mainShopId'])) {
                     $itemText['languageShopId'] = PlentymarketsTranslation::getLanguageShopID($localeId, $language['mainShopId']);
                 } elseif (PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']) != 'de') {
                     // set the language for the main shop if the main language is not German
                     $itemText['languageShopId'] = $shopId;
                 }
                 $itemText['texts'] = $Response_GetItemsTexts->ItemTexts->item[0];
                 $itemTexts[] = $itemText;
             }
         }
     }
     try {
         $shopId = PlentymarketsMappingController::getShopByPlentyID($storeId);
         $Shop = Shopware()->Models()->find('Shopware\\Models\\Shop\\Shop', $shopId);
         $Importuer = new PlentymarketsImportEntityItem($ItemBase, $Shop);
         // The item has already been updated
         if (!$full) {
             // so we just need to do the categories
             $Importuer->importCategories();
             //if this is a main shop , import the translation for its main language and its shop languages
             if (PlentymarketsTranslation::isMainShop($shopId)) {
                 if (!empty($itemTexts)) {
                     // Do the import for item texts translation
                     $Importuer->saveItemTextsTranslation($itemTexts);
                 }
                 // Do the import for the property value translations
                 $Importuer->importItemPropertyValueTranslations();
             }
         } else {
             // Do a full import
             $Importuer->import();
             //if this is a main shop , import the translation for its main language and its shop languages
             if (PlentymarketsTranslation::isMainShop($shopId)) {
                 if (!empty($itemTexts)) {
                     // Do the import for item texts translation
                     $Importuer->saveItemTextsTranslation($itemTexts);
                 }
                 // Do the import for the property value translations
                 $Importuer->importItemPropertyValueTranslations();
             }
             // Add it to the link controller
             PlentymarketsImportControllerItemLinked::getInstance()->addItem($ItemBase->ItemID);
             // Mark this item as done
             $this->itemIdsDone[$ItemBase->ItemID] = true;
         }
         // Log the usage data
         PyLog()->usage();
     } catch (Shopware\Components\Api\Exception\ValidationException $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item:Validation', 'The item »' . $ItemBase->Texts->Name . '« with the id »' . $ItemBase->ItemID . '« could not be imported', 3010);
         foreach ($E->getViolations() as $ConstraintViolation) {
             PlentymarketsLogger::getInstance()->error('Sync:Item:Validation', $ConstraintViolation->getMessage());
             PlentymarketsLogger::getInstance()->error('Sync:Item:Validation', $ConstraintViolation->getPropertyPath() . ': ' . $ConstraintViolation->getInvalidValue());
         }
     } catch (Shopware\Components\Api\Exception\OrmException $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item:Orm', 'The item »' . $ItemBase->Texts->Name . '« with the id »' . $ItemBase->ItemID . '« could not be imported (' . $E->getMessage() . ')', 3020);
         PlentymarketsLogger::getInstance()->error('Sync:Item:Orm', $E->getTraceAsString(), 1000);
         throw new PlentymarketsImportException('The item import will be stopped (internal database error)', 3021);
     } catch (PlentymarketsImportItemNumberException $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item:Number', $E->getMessage(), $E->getCode());
     } catch (PlentymarketsImportItemException $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item:Number', $E->getMessage(), $E->getCode());
     } catch (Exception $E) {
         PlentymarketsLogger::getInstance()->error('Sync:Item', 'The item »' . $ItemBase->Texts->Name . '« with the id »' . $ItemBase->ItemID . '« could not be imported', 3000);
         PlentymarketsLogger::getInstance()->error('Sync:Item', $E->getTraceAsString(), 1000);
         PlentymarketsLogger::getInstance()->error('Sync:Item', get_class($E));
         PlentymarketsLogger::getInstance()->error('Sync:Item', $E->getMessage());
     }
 }
 /**
  * Run import of property groups and property translations
  * @param int $lastUpdateTimestamp
  */
 private function runImportTranslations($lastUpdateTimestamp)
 {
     $mainShops = PlentymarketsUtils::getShopwareMainShops();
     /** @var $mainShop Shopware\Models\Shop\Shop */
     foreach ($mainShops as $mainShop) {
         // get all active languages of the main shop
         $activeLanguages = PlentymarketsTranslation::getShopActiveLanguages($mainShop->getId());
         foreach ($activeLanguages as $key => $language) {
             // import tanslation for property group
             $Request_GetPropertyGroups = new PlentySoapRequest_GetPropertyGroups();
             $Request_GetPropertyGroups->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']);
             $Request_GetPropertyGroups->LastUpdateFrom = $lastUpdateTimestamp;
             $Request_GetPropertyGroups->Page = 0;
             do {
                 /** @var PlentySoapResponse_GetPropertyGroups $Response_GetPropertyGroups */
                 $Response_GetPropertyGroups = PlentymarketsSoapClient::getInstance()->GetPropertyGroups($Request_GetPropertyGroups);
                 foreach ($Response_GetPropertyGroups->PropertyGroups->item as $group) {
                     $PlentymarketsImportEntityItemPropertyGroup = new PlentymarketsImportEntityItemPropertyGroup($group);
                     // set the property group translations from plenty for the language shops
                     if (!is_null($language['mainShopId'])) {
                         $languageShopID = PlentymarketsTranslation::getLanguageShopID($key, $language['mainShopId']);
                         $PlentymarketsImportEntityItemPropertyGroup->importPropertyGroupTranslation($languageShopID);
                     } else {
                         $PlentymarketsImportEntityItemPropertyGroup->importPropertyGroupTranslation($mainShop->getId());
                     }
                 }
             } while (++$Request_GetPropertyGroups->Page < $Response_GetPropertyGroups->Pages);
             // import translation for properties
             $Request_GetProperties = new PlentySoapRequest_GetProperties();
             $Request_GetProperties->Lang = PlentymarketsTranslation::getPlentyLocaleFormat($language['locale']);
             $Request_GetProperties->LastUpdateFrom = $lastUpdateTimestamp;
             $Request_GetProperties->Page = 0;
             do {
                 /** @var PlentySoapResponse_GetProperties $Response_GetProperties */
                 $Response_GetProperties = PlentymarketsSoapClient::getInstance()->GetProperties($Request_GetProperties);
                 foreach ($Response_GetProperties->Properties->item as $Option) {
                     $PlentymarketsImportEntityItemPropertyOption = new PlentymarketsImportEntityItemPropertyOption($Option);
                     // set the property translations from plenty for the language shops
                     if (!is_null($language['mainShopId'])) {
                         $languageShopID = PlentymarketsTranslation::getLanguageShopID($key, $language['mainShopId']);
                         $PlentymarketsImportEntityItemPropertyOption->importPropertyTranslation($languageShopID);
                     } else {
                         // set the property translation for the main shop
                         $PlentymarketsImportEntityItemPropertyOption->importPropertyTranslation($mainShop->getId());
                     }
                 }
             } while (++$Request_GetProperties->Page < $Response_GetProperties->Pages);
         }
     }
 }