/**
  * Localize articles of product.
  *
  * @param int $productUid Product uid
  * @param int $localizedProductUid Localized product uid
  * @param int $value Value
  *
  * @return void
  */
 protected function translateArticlesOfProduct($productUid, $localizedProductUid, $value)
 {
     $database = $this->getDatabaseConnection();
     // get articles of localized Product
     $localizedProductArticles = $this->belib->getArticlesOfProduct($localizedProductUid);
     // get all related articles
     $articles = $this->belib->getArticlesOfProduct($productUid);
     if (empty($articles)) {
         // Error Output, no Articles
         $this->error('LLL:EXT:commerce/Resources/Private/Language/locallang_be.xml:product.localization_without_article');
     }
     // Check if product has articles and localized product has no articles
     if (!empty($articles) && empty($localizedProductArticles)) {
         // determine language identifier
         // this is needed for updating the XML of the new created articles
         $langIsoCode = BackendUtility::getRecord('sys_language', (int) $value, 'static_lang_isocode');
         $langIdent = BackendUtility::getRecord('static_languages', (int) $langIsoCode['static_lang_isocode'], 'lg_typo3');
         $langIdent = strtoupper($langIdent['lg_typo3']);
         if (empty($langIdent)) {
             $langIdent = 'DEF';
         }
         // process all existing articles and copy them
         if (is_array($articles)) {
             foreach ($articles as $origArticle) {
                 // make a localization version
                 $locArticle = $origArticle;
                 // unset some values
                 unset($locArticle['uid']);
                 // set new article values
                 $now = time();
                 $locArticle['tstamp'] = $now;
                 $locArticle['crdate'] = $now;
                 $locArticle['sys_language_uid'] = $value;
                 $locArticle['l18n_parent'] = $origArticle['uid'];
                 $locArticle['uid_product'] = $localizedProductUid;
                 // get XML for attributes
                 // this has only to be changed if the language is something else than default.
                 // The possibility that something else happens is very small but anyhow... ;-)
                 if ($langIdent != 'DEF' && $origArticle['attributesedit']) {
                     $locArticle['attributesedit'] = $this->belib->buildLocalisedAttributeValues($origArticle['attributesedit'], $langIdent);
                 }
                 // create new article in DB
                 $database->exec_INSERTquery('tx_commerce_articles', $locArticle);
                 // get the uid of the localized article
                 $locatedArticleUid = $database->sql_insert_id();
                 // get all relations to attributes from the old article
                 // and copy them to new article
                 $res = $database->exec_SELECTquery('*', 'tx_commerce_articles_article_attributes_mm', 'uid_local = ' . (int) $origArticle['uid'] . ' AND uid_valuelist = 0');
                 while ($origRelation = $database->sql_fetch_assoc($res)) {
                     $origRelation['uid_local'] = $locatedArticleUid;
                     $database->exec_INSERTquery('tx_commerce_articles_article_attributes_mm', $origRelation);
                 }
             }
         }
     }
 }
 /**
  * Creates an article in the database and all needed releations to attributes
  * and values. It also creates a new prices and assignes it to the new article.
  *
  * @param array $parameter Parameter
  * @param string $key The key in the POST var array
  *
  * @return int Returns the new articleUid if success
  */
 protected function createArticle(array $parameter, $key)
 {
     $database = $this->getDatabaseConnection();
     // get the create data
     $data = GeneralUtility::_GP('createData');
     $hash = '';
     if (is_array($data)) {
         $data = $data[$key];
         $hash = md5($data);
         $data = unserialize($data);
     }
     $database->debugOutput = 1;
     $database->store_lastBuiltQuery = true;
     // get the highest sorting
     $sorting = $database->exec_SELECTgetSingleRow('uid, sorting', 'tx_commerce_articles', 'uid_product = ' . $this->uid, '', 'sorting DESC', 1);
     $sorting = is_array($sorting) && isset($sorting['sorting']) ? $sorting['sorting'] + 20 : 0;
     // create article data array
     $articleData = array('pid' => $this->pid, 'crdate' => time(), 'title' => strip_tags($this->createArticleTitleFromAttributes($parameter, (array) $data)), 'uid_product' => (int) $this->uid, 'sorting' => $sorting, 'article_attributes' => count($this->attributes['rest']) + count($data), 'attribute_hash' => $hash, 'article_type_uid' => 1);
     $temp = CoreBackendUtility::getModTSconfig($this->pid, 'mod.commerce.category');
     if ($temp) {
         $moduleConfig = CoreBackendUtility::implodeTSParams($temp['properties']);
         $defaultTax = (int) $moduleConfig['defaultTaxValue'];
         if ($defaultTax > 0) {
             $articleData['tax'] = $defaultTax;
         }
     }
     $hookObject = \CommerceTeam\Commerce\Factory\HookFactory::getHook('Utility/ArticleCreatorUtility', 'createArticle');
     if (method_exists($hookObject, 'preinsert')) {
         $hookObject->preinsert($articleData);
     }
     // create the article
     $database->exec_INSERTquery('tx_commerce_articles', $articleData);
     $articleUid = $database->sql_insert_id();
     // create a new price that is assigned to the new article
     $database->exec_INSERTquery('tx_commerce_article_prices', array('pid' => $this->pid, 'crdate' => time(), 'tstamp' => time(), 'uid_article' => $articleUid));
     // now write all relations between article and attributes into the database
     $relationBaseData = array('uid_local' => $articleUid);
     $createdArticleRelations = array();
     $relationCreateData = $relationBaseData;
     $productsAttributesRes = $database->exec_SELECTquery('sorting, uid_local, uid_foreign', 'tx_commerce_products_attributes_mm', 'uid_local = ' . (int) $this->uid);
     $attributesSorting = array();
     while ($productsAttributes = $database->sql_fetch_assoc($productsAttributesRes)) {
         $attributesSorting[$productsAttributes['uid_foreign']] = $productsAttributes['sorting'];
     }
     if (is_array($data)) {
         foreach ($data as $selectAttributeUid => $selectAttributeValueUid) {
             $relationCreateData['uid_foreign'] = $selectAttributeUid;
             $relationCreateData['uid_valuelist'] = $selectAttributeValueUid;
             $relationCreateData['sorting'] = $attributesSorting[$selectAttributeUid];
             $createdArticleRelations[] = $relationCreateData;
             $database->exec_INSERTquery('tx_commerce_articles_article_attributes_mm', $relationCreateData);
         }
     }
     if (is_array($this->attributes['rest'])) {
         foreach ($this->attributes['rest'] as $attribute) {
             if (empty($attribute['attributeData']['uid'])) {
                 continue;
             }
             $relationCreateData = $relationBaseData;
             $relationCreateData['sorting'] = $attribute['sorting'];
             $relationCreateData['uid_foreign'] = $attribute['attributeData']['uid'];
             if ($attribute['uid_correlationtype'] == 4) {
                 $relationCreateData['uid_product'] = $this->uid;
             }
             $relationCreateData['default_value'] = '';
             $relationCreateData['value_char'] = '';
             $relationCreateData['uid_valuelist'] = $attribute['uid_valuelist'];
             if (!$this->belib->isNumber($attribute['default_value'])) {
                 $relationCreateData['default_value'] = $attribute['default_value'];
             } else {
                 $relationCreateData['value_char'] = $attribute['default_value'];
             }
             $createdArticleRelations[] = $relationCreateData;
             $database->exec_INSERTquery('tx_commerce_articles_article_attributes_mm', $relationCreateData);
         }
     }
     // update the article
     // we have to write the xml datastructure for this article. This is needed
     // to have the correct values inserted on first call of the article.
     $this->belib->updateArticleXML($createdArticleRelations, false, $articleUid);
     // Now check, if the parent Product is already lokalised, so creat Article in
     // the lokalised version Select from Database different localisations
     $resOricArticle = $database->exec_SELECTquery('*', 'tx_commerce_articles', 'uid = ' . (int) $articleUid . ' AND deleted = 0');
     $origArticle = $database->sql_fetch_assoc($resOricArticle);
     $result = $database->exec_SELECTquery('*', 'tx_commerce_products', 'l18n_parent = ' . (int) $this->uid . ' AND deleted = 0');
     if ($database->sql_num_rows($result)) {
         // Only if there are products
         while ($localizedProducts = $database->sql_fetch_assoc($result)) {
             // walk thru and create articles
             $destLanguage = $localizedProducts['sys_language_uid'];
             // get the highest sorting
             $langIsoCode = CoreBackendUtility::getRecord('sys_language', (int) $destLanguage, 'static_lang_isocode');
             $langIdent = CoreBackendUtility::getRecord('static_languages', (int) $langIsoCode['static_lang_isocode'], 'lg_typo3');
             $langIdent = strtoupper($langIdent['lg_typo3']);
             // create article data array
             $articleData = array('pid' => $this->pid, 'crdate' => time(), 'title' => $parameter['title'], 'uid_product' => $localizedProducts['uid'], 'sys_language_uid' => $localizedProducts['sys_language_uid'], 'l18n_parent' => $articleUid, 'sorting' => $sorting['sorting'] + 20, 'article_attributes' => count($this->attributes['rest']) + count($data), 'attribute_hash' => $hash, 'article_type_uid' => 1, 'attributesedit' => $this->belib->buildLocalisedAttributeValues($origArticle['attributesedit'], $langIdent));
             // create the article
             $database->exec_INSERTquery('tx_commerce_articles', $articleData);
             $localizedArticleUid = $database->sql_insert_id();
             // get all relations to attributes from the old article and copy them
             // to new article
             $res = $database->exec_SELECTquery('*', 'tx_commerce_articles_article_attributes_mm', 'uid_local = ' . (int) $origArticle['uid'] . ' AND uid_valuelist = 0');
             while ($origRelation = $database->sql_fetch_assoc($res)) {
                 $origRelation['uid_local'] = $localizedArticleUid;
                 $database->exec_INSERTquery('tx_commerce_articles_article_attributes_mm', $origRelation);
             }
             $this->belib->updateArticleXML($createdArticleRelations, false, $localizedArticleUid);
         }
     }
     return $articleUid;
 }