Esempio n. 1
0
 /**
  * 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 (!count($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 (count($articles) && !count($localizedProductArticles)) {
         // determine language identifier
         // this is needed for updating the XML of the new created articles
         $langIsoCode = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('sys_language', (int) $value, 'static_lang_isocode');
         $langIdent = \TYPO3\CMS\Backend\Utility\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 = BackendUtility::getModTSconfig($this->pid, 'mod.commerce.category');
        if ($temp) {
            $moduleConfig = BackendUtility::implodeTSParams($temp['properties']);
            $defaultTax = (int) $moduleConfig['defaultTaxValue'];
            if ($defaultTax > 0) {
                $articleData['tax'] = $defaultTax;
            }
        }
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/class.tx_commerce_articlecreator.php']['preinsert'])) {
            GeneralUtility::deprecationLog('
				hook
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/class.tx_commerce_articlecreator.php\'][\'preinsert\']
				is deprecated since commerce 1.0.0, it will be removed in commerce 1.4.0, please use instead
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/lib/class.tx_commerce_basket.php\'][\'createArticlePreInsert\']
			');
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/class.tx_commerce_articlecreator.php']['preinsert'] as $classRef) {
                $hookObj =& GeneralUtility::getUserObj($classRef);
                if (method_exists($hookObj, 'preinsert')) {
                    $hookObj->preinsert($articleData);
                }
            }
        }
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Utility/ArticleCreatorUtility.php']['createArticlePreInsert'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Utility/ArticleCreatorUtility.php']['createArticlePreInsert'] as $classRef) {
                $hookObj =& GeneralUtility::getUserObj($classRef);
                if (method_exists($hookObj, 'preinsert')) {
                    $hookObj->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);
        $resLocalizedProducts = $database->exec_SELECTquery('*', 'tx_commerce_products', 'l18n_parent = ' . (int) $this->uid . ' AND deleted = 0');
        if ($resLocalizedProducts && $database->sql_num_rows($resLocalizedProducts) > 0) {
            // Only if there are products
            while ($localizedProducts = $database->sql_fetch_assoc($resLocalizedProducts)) {
                // walk thru and create articles
                $destLanguage = $localizedProducts['sys_language_uid'];
                // get the highest sorting
                $langIsoCode = BackendUtility::getRecord('sys_language', (int) $destLanguage, 'static_lang_isocode');
                $langIdent = BackendUtility::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;
    }