Ejemplo n.º 1
0
    protected function supplierImportOne($info, $shop_is_feature_active, $regenerate, $force_ids, $validateOnly = false)
    {
        AdminImportController::setDefaultValues($info);
        if ($force_ids && isset($info['id']) && (int) $info['id']) {
            $supplier = new Supplier((int) $info['id']);
        } else {
            if (array_key_exists('id', $info) && (int) $info['id'] && Supplier::existsInDatabase((int) $info['id'], 'supplier')) {
                $supplier = new Supplier((int) $info['id']);
            } else {
                $supplier = new Supplier();
            }
        }
        AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supplier);
        if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
            $res = false;
            if ($supplier->id && $supplier->supplierExists($supplier->id)) {
                $res = $validateOnly || $supplier->update();
            }
            $supplier->force_id = (bool) $force_ids;
            if (!$res) {
                $res = $validateOnly || $supplier->add();
            }
            //copying images of suppliers
            if (!$validateOnly && isset($supplier->image) && !empty($supplier->image)) {
                if (!AdminImportController::copyImg($supplier->id, null, $supplier->image, 'suppliers', !$regenerate)) {
                    $this->warnings[] = $supplier->image . ' ' . $this->trans('cannot be copied.', array(), 'Admin.Parameters.Notification');
                }
            }
            if (!$res) {
                $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), isset($info['name']) && !empty($info['name']) ? Tools::safeOutput($info['name']) : 'No Name', isset($info['id']) && !empty($info['id']) ? Tools::safeOutput($info['id']) : 'No ID');
            } elseif (!$validateOnly) {
                // Associate supplier to group shop
                if ($shop_is_feature_active && $supplier->shop) {
                    Db::getInstance()->execute('
						DELETE FROM ' . _DB_PREFIX_ . 'supplier_shop
						WHERE id_supplier = ' . (int) $supplier->id);
                    $supplier->shop = explode($this->multiple_value_separator, $supplier->shop);
                    $shops = array();
                    foreach ($supplier->shop as $shop) {
                        if (empty($shop)) {
                            continue;
                        }
                        $shop = trim($shop);
                        if (!is_numeric($shop)) {
                            $shop = ShopGroup::getIdByName($shop);
                        }
                        $shops[] = $shop;
                    }
                    $supplier->associateTo($shops);
                }
            }
        } else {
            $this->errors[] = $this->l('Supplier is invalid') . ' (' . $supplier->name . ')';
            $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '');
        }
    }
    public function supplierImport()
    {
        $this->receiveTab();
        $handle = $this->openCsvFile();
        AdminImportController::setLocale();
        for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++) {
            if (Tools::getValue('convert')) {
                $line = $this->utf8EncodeArray($line);
            }
            $info = AdminImportController::getMaskedRow($line);
            AdminImportController::setDefaultValues($info);
            if (Tools::getValue('forceIDs') && isset($info['id']) && (int) $info['id']) {
                $supplier = new Supplier((int) $info['id']);
            } else {
                if (array_key_exists('id', $info) && (int) $info['id'] && Supplier::existsInDatabase((int) $info['id'], 'supplier')) {
                    $supplier = new Supplier((int) $info['id']);
                } else {
                    $supplier = new Supplier();
                }
            }
            AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supplier);
            if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
                $res = false;
                if ($supplier->id && $supplier->supplierExists($supplier->id)) {
                    $res = $supplier->update();
                }
                $supplier->force_id = (bool) Tools::getValue('forceIDs');
                if (!$res) {
                    $res = $supplier->add();
                }
                //copying images of suppliers
                if (isset($supplier->image) && !empty($supplier->image)) {
                    if (!AdminImportController::copyImg($supplier->id, null, $supplier->image, 'suppliers', !Tools::getValue('regenerate'))) {
                        $this->warnings[] = $supplier->image . ' ' . Tools::displayError('cannot be copied.');
                    }
                }
                if (!$res) {
                    $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), isset($info['name']) && !empty($info['name']) ? Tools::safeOutput($info['name']) : 'No Name', isset($info['id']) && !empty($info['id']) ? Tools::safeOutput($info['id']) : 'No ID');
                } else {
                    // Associate supplier to group shop
                    if (Shop::isFeatureActive() && $supplier->shop) {
                        Db::getInstance()->execute('
							DELETE FROM ' . _DB_PREFIX_ . 'supplier_shop
							WHERE id_supplier = ' . (int) $supplier->id);
                        $supplier->shop = explode($this->multiple_value_separator, $supplier->shop);
                        $shops = array();
                        foreach ($supplier->shop as $shop) {
                            if (empty($shop)) {
                                continue;
                            }
                            $shop = trim($shop);
                            if (!is_numeric($shop)) {
                                $shop = ShopGroup::getIdByName($shop);
                            }
                            $shops[] = $shop;
                        }
                        $supplier->associateTo($shops);
                    }
                }
            } else {
                $this->errors[] = $this->l('Supplier is invalid') . ' (' . $supplier->name . ')';
                $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '');
            }
        }
        $this->closeCsvFile($handle);
    }