Example #1
0
 public function attributeImport()
 {
     global $cookie;
     $defaultLanguage = _PS_LANG_DEFAULT_;
     $groups = array();
     foreach (AttributeGroup::getAttributesGroups($defaultLanguage) as $group) {
         $groups[$group['name']] = (int) $group['id_attribute_group'];
     }
     $attributes = array();
     foreach (Attribute::getAttributes($defaultLanguage) as $attribute) {
         $attributes[$attribute['attribute_group'] . '_' . $attribute['name']] = (int) $attribute['id_attribute'];
     }
     $this->receiveTab();
     $handle = $this->openCsvFile();
     $fsep = (is_null(Tools::getValue('multiple_value_separator')) or trim(Tools::getValue('multiple_value_separator')) == '') ? ',' : Tools::getValue('multiple_value_separator');
     self::setLocale();
     for ($current_line = 0, $lines_ok = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++) {
         if (Tools::getValue('convert')) {
             $line = $this->utf8_encode_array($line);
         }
         $info = self::getMaskedRow($line);
         $info = array_map('trim', $info);
         self::setDefaultValues($info);
         $product = new Product((int) $info['id_product'], false, $defaultLanguage);
         $id_image = null;
         if (isset($info['image_url']) && $info['image_url']) {
             $productHasImages = (bool) Image::getImages((int) $cookie->id_lang, (int) $product->id);
             $url = $info['image_url'];
             $image = new Image();
             $image->id_product = (int) $product->id;
             $image->position = Image::getHighestPosition($product->id) + 1;
             $image->cover = !$productHasImages ? true : false;
             $image->legend = self::createMultiLangField($product->name);
             if (($fieldError = $image->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $image->add()) {
                 if (!self::copyImg($product->id, $image->id, $url)) {
                     $this->_warnings[] = Tools::displayError('Error copying image: ') . $url;
                 } else {
                     $id_image = array($image->id);
                 }
             } else {
                 $this->_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                 $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . Db::getInstance()->getMsgError();
             }
         } elseif (isset($info['image_position']) && $info['image_position']) {
             $images = $product->getImages($defaultLanguage);
             if ($images) {
                 foreach ($images as $row) {
                     if ($row['position'] == (int) $info['image_position']) {
                         $id_image = array($row['id_image']);
                         break;
                     }
                 }
             }
             if (!$id_image) {
                 $this->_warnings[] = sprintf(Tools::displayError('No image found for combination with id_product = %s and image position = %s.'), $product->id, (int) $info['image_position']);
             }
         }
         $info['ecotax'] = str_replace(',', '.', $info['ecotax']);
         $info['weight'] = str_replace(',', '.', $info['weight']);
         $id_product_attribute = Combination::getCombinationbyRef($info['reference']);
         if (Validate::isUnsignedId($id_product_attribute) && (int) $id_product_attribute > 0) {
             $product->updateCombinationEntity($id_product_attribute, (double) $info['wholesale_price'], (double) $info['price'], (double) $info['weight'], 0, (double) $info['ecotax'], (int) $info['quantity'], $id_image, $info['reference'], 0, $info['ean13'], (int) $info['default_on'], 0, $info['upc'], (int) $info['minimal_quantity']);
         } else {
             $id_product_attribute = $product->addCombinationEntity((double) $info['wholesale_price'], (double) $info['price'], (double) $info['weight'], 0, (double) $info['ecotax'], (int) $info['quantity'], $id_image, $info['reference'], 0, $info['ean13'], (int) $info['default_on'], 0, $info['upc'], (int) $info['minimal_quantity']);
         }
         if ($id_product_attribute) {
             $lines_ok++;
         }
         foreach (explode($fsep, $info['options']) as $option) {
             list($group, $attribute) = array_map('trim', explode(':', $option));
             if (!isset($groups[$group])) {
                 $obj = new AttributeGroup();
                 $obj->is_color_group = false;
                 $obj->name[$defaultLanguage] = $group;
                 $obj->public_name[$defaultLanguage] = $group;
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $groups[$group] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             if (!isset($attributes[$group . '_' . $attribute])) {
                 $obj = new Attribute();
                 $obj->id_attribute_group = $groups[$group];
                 $obj->name[$defaultLanguage] = str_replace(array('\\n', '\\r'), '', $attribute);
                 if (($fieldError = $obj->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $obj->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                     $obj->add();
                     $attributes[$group . '_' . $attribute] = $obj->id;
                 } else {
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '');
                 }
             }
             Db::getInstance()->Execute('INSERT INTO ' . _DB_PREFIX_ . 'product_attribute_combination (id_attribute, id_product_attribute) VALUES (' . (int) $attributes[$group . '_' . $attribute] . ',' . (int) $id_product_attribute . ')');
         }
     }
     $this->closeCsvFile($handle);
     return (int) $lines_ok;
 }