public function renderFormAddress()
 {
     // Change table and className for addresses
     $this->table = 'address';
     $this->className = 'Address';
     $id_address = Tools::getValue('id_address');
     // Create Object Address
     $address = new Address($id_address);
     $res = $address->getFieldsRequiredDatabase();
     $required_fields = array();
     foreach ($res as $row) {
         $required_fields[(int) $row['id_required_field']] = $row['field_name'];
     }
     $form = array('legend' => array('title' => $this->l('Addresses'), 'icon' => 'icon-building'));
     if (!$address->id_manufacturer || !Manufacturer::manufacturerExists($address->id_manufacturer)) {
         $form['input'][] = array('type' => 'select', 'label' => $this->l('Choose the manufacturer'), 'name' => 'id_manufacturer', 'options' => array('query' => Manufacturer::getManufacturers(), 'id' => 'id_manufacturer', 'name' => 'name'));
     } else {
         $form['input'][] = array('type' => 'text', 'label' => $this->l('Manufacturer'), 'name' => 'name', 'col' => 4, 'disabled' => true);
         $form['input'][] = array('type' => 'hidden', 'name' => 'id_manufacturer');
     }
     $form['input'][] = array('type' => 'hidden', 'name' => 'alias');
     $form['input'][] = array('type' => 'hidden', 'name' => 'id_address');
     if (in_array('company', $required_fields)) {
         $form['input'][] = array('type' => 'text', 'label' => $this->l('Company'), 'name' => 'company', 'display' => in_array('company', $required_fields), 'required' => in_array('company', $required_fields), 'maxlength' => 16, 'col' => 4, 'hint' => $this->l('Company name for this supplier'));
     }
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Last name'), 'name' => 'lastname', 'required' => true, 'col' => 4, 'hint' => $this->l('Invalid characters:') . ' 0-9!<>,;?=+()@#"�{}_$%:');
     $form['input'][] = array('type' => 'text', 'label' => $this->l('First name'), 'name' => 'firstname', 'required' => true, 'col' => 4, 'hint' => $this->l('Invalid characters:') . ' 0-9!<>,;?=+()@#"�{}_$%:');
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Address'), 'name' => 'address1', 'col' => 6, 'required' => true);
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Address (2)'), 'name' => 'address2', 'col' => 6, 'required' => in_array('address2', $required_fields));
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Zip/postal code'), 'name' => 'postcode', 'col' => 2, 'required' => in_array('postcode', $required_fields));
     $form['input'][] = array('type' => 'text', 'label' => $this->l('City'), 'name' => 'city', 'col' => 4, 'required' => true);
     $form['input'][] = array('type' => 'select', 'label' => $this->l('Country'), 'name' => 'id_country', 'required' => false, 'default_value' => (int) $this->context->country->id, 'col' => 4, 'options' => array('query' => Country::getCountries($this->context->language->id), 'id' => 'id_country', 'name' => 'name'));
     $form['input'][] = array('type' => 'select', 'label' => $this->l('State'), 'name' => 'id_state', 'required' => false, 'col' => 4, 'options' => array('query' => array(), 'id' => 'id_state', 'name' => 'name'));
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Home phone'), 'name' => 'phone', 'col' => 4, 'required' => in_array('phone', $required_fields));
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Mobile phone'), 'name' => 'phone_mobile', 'col' => 4, 'required' => in_array('phone_mobile', $required_fields));
     $form['input'][] = array('type' => 'textarea', 'label' => $this->l('Other'), 'name' => 'other', 'required' => false, 'hint' => $this->l('Forbidden characters:') . ' <>;=#{}', 'rows' => 2, 'cols' => 10, 'col' => 6);
     $form['submit'] = array('title' => $this->l('Save'));
     $this->fields_value = array('name' => Manufacturer::getNameById($address->id_manufacturer), 'alias' => 'manufacturer', 'id_country' => $address->id_country);
     $this->initToolbar();
     $this->fields_form[0]['form'] = $form;
     $this->getlanguages();
     $helper = new HelperForm();
     $helper->show_cancel_button = true;
     $back = Tools::safeOutput(Tools::getValue('back', ''));
     if (empty($back)) {
         $back = self::$currentIndex . '&token=' . $this->token;
     }
     if (!Validate::isCleanHtml($back)) {
         die(Tools::displayError());
     }
     $helper->back_url = $back;
     $helper->currentIndex = self::$currentIndex;
     $helper->token = $this->token;
     $helper->table = $this->table;
     $helper->identifier = $this->identifier;
     $helper->title = $this->l('Edit Addresses');
     $helper->id = $address->id;
     $helper->toolbar_scroll = true;
     $helper->languages = $this->_languages;
     $helper->default_form_language = $this->default_form_language;
     $helper->allow_employee_form_lang = $this->allow_employee_form_lang;
     $helper->fields_value = $this->getFieldsValue($address);
     $helper->toolbar_btn = $this->toolbar_btn;
     $this->content .= $helper->generateForm($this->fields_form);
 }
Exemple #2
0
 public function addressImport()
 {
     $this->receiveTab();
     $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT');
     $handle = $this->openCsvFile();
     self::setLocale();
     for ($current_line = 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);
         self::setDefaultValues($info);
         $address = new Address();
         self::array_walk($info, array('AdminImport', 'fillInfo'), $address);
         if (isset($address->country) and is_numeric($address->country)) {
             if (Country::getNameById(Configuration::get('PS_LANG_DEFAULT'), (int) $address->country)) {
                 $address->id_country = (int) $address->country;
             }
         } elseif (isset($address->country) and is_string($address->country) and !empty($address->country)) {
             if ($id_country = Country::getIdByName(NULL, $address->country)) {
                 $address->id_country = (int) $id_country;
             } else {
                 $country = new Country();
                 $country->active = 1;
                 $country->name = self::createMultiLangField($address->country);
                 $country->id_zone = 0;
                 // Default zone for country to create
                 $country->iso_code = strtoupper(substr($address->country, 0, 2));
                 // Default iso for country to create
                 $country->contains_states = 0;
                 // Default value for country to create
                 $langFieldError = $country->validateFieldsLang(UNFRIENDLY_ERROR, true);
                 if (($fieldError = $country->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $country->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $country->add()) {
                     $address->id_country = (int) $country->id;
                 } else {
                     $this->_errors[] = $country->name[$defaultLanguageId] . ' ' . Tools::displayError('Cannot be saved');
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
                 }
             }
         }
         if (isset($address->state) and is_numeric($address->state)) {
             if (State::getNameById((int) $address->state)) {
                 $address->id_state = (int) $address->state;
             }
         } elseif (isset($address->state) and is_string($address->state) and !empty($address->state)) {
             if ($id_state = State::getIdByName($address->state)) {
                 $address->id_state = (int) $id_state;
             } else {
                 $state = new State();
                 $state->active = 1;
                 $state->name = $address->state;
                 $state->id_country = isset($country->id) ? (int) $country->id : 0;
                 $state->id_zone = 0;
                 // Default zone for state to create
                 $state->iso_code = strtoupper(substr($address->state, 0, 2));
                 // Default iso for state to create
                 $state->tax_behavior = 0;
                 if (($fieldError = $state->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $state->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $state->add()) {
                     $address->id_state = (int) $state->id;
                 } else {
                     $this->_errors[] = $state->name . ' ' . Tools::displayError('Cannot be saved');
                     $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
                 }
             }
         }
         if (isset($address->customer_email) and !empty($address->customer_email)) {
             if (Validate::isEmail($address->customer_email)) {
                 $customer = Customer::customerExists($address->customer_email, true);
                 if ($customer) {
                     $address->id_customer = (int) $customer;
                 } else {
                     $this->_errors[] = mysql_error() . ' ' . $address->customer_email . ' ' . Tools::displayError('does not exist in database') . ' ' . (isset($info['id']) ? ' (ID ' . $info['id'] . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                 }
             } else {
                 $this->_errors[] = '"' . $address->customer_email . '" :' . Tools::displayError('Is not a valid Email');
             }
         }
         if (isset($address->manufacturer) and is_numeric($address->manufacturer) and Manufacturer::manufacturerExists((int) $address->manufacturer)) {
             $address->id_manufacturer = (int) $address->manufacturer;
         } elseif (isset($address->manufacturer) and is_string($address->manufacturer) and !empty($address->manufacturer)) {
             $manufacturer = new Manufacturer();
             $manufacturer->name = $address->manufacturer;
             if (($fieldError = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $manufacturer->add()) {
                 $address->id_manufacturer = (int) $manufacturer->id;
             } else {
                 $this->_errors[] = mysql_error() . ' ' . $manufacturer->name . (isset($manufacturer->id) ? ' (' . $manufacturer->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                 $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
             }
         }
         if (isset($address->supplier) and is_numeric($address->supplier) and Supplier::supplierExists((int) $address->supplier)) {
             $address->id_supplier = (int) $address->supplier;
         } elseif (isset($address->supplier) and is_string($address->supplier) and !empty($address->supplier)) {
             $supplier = new Supplier();
             $supplier->name = $address->supplier;
             if (($fieldError = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $supplier->add()) {
                 $address->id_supplier = (int) $supplier->id;
             } else {
                 $this->_errors[] = mysql_error() . ' ' . $supplier->name . (isset($supplier->id) ? ' (' . $supplier->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                 $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
             }
         }
         $res = false;
         if (($fieldError = $address->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $address->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
             if ($address->id and $address->addressExists($address->id)) {
                 $res = $address->update();
             }
             if (!$res) {
                 $res = $address->add();
             }
         }
         if (!$res) {
             $this->_errors[] = $info['alias'] . (isset($info['id']) ? ' (ID ' . $info['id'] . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
             $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
         }
     }
     $this->closeCsvFile($handle);
 }
 protected function addressImportOne($info, $force_ids, $validateOnly = false)
 {
     AdminImportController::setDefaultValues($info);
     if ($force_ids && isset($info['id']) && (int) $info['id']) {
         $address = new Address((int) $info['id']);
     } else {
         if (array_key_exists('id', $info) && (int) $info['id'] && Address::addressExists((int) $info['id'])) {
             $address = new Address((int) $info['id']);
         } else {
             $address = new Address();
         }
     }
     AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $address);
     if (isset($address->country) && is_numeric($address->country)) {
         if (Country::getNameById(Configuration::get('PS_LANG_DEFAULT'), (int) $address->country)) {
             $address->id_country = (int) $address->country;
         }
     } elseif (isset($address->country) && is_string($address->country) && !empty($address->country)) {
         if ($id_country = Country::getIdByName(null, $address->country)) {
             $address->id_country = (int) $id_country;
         } else {
             $country = new Country();
             $country->active = 1;
             $country->name = AdminImportController::createMultiLangField($address->country);
             $country->id_zone = 0;
             // Default zone for country to create
             $country->iso_code = Tools::strtoupper(Tools::substr($address->country, 0, 2));
             // Default iso for country to create
             $country->contains_states = 0;
             // Default value for country to create
             $lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true);
             if (($field_error = $country->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $country->add()) {
                 $address->id_country = (int) $country->id;
             } else {
                 if (!$validateOnly) {
                     $default_language_id = (int) Configuration::get('PS_LANG_DEFAULT');
                     $this->errors[] = sprintf($this->trans('%s cannot be saved', array(), 'Admin.Parameters.Notification'), $country->name[$default_language_id]);
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->state) && is_numeric($address->state)) {
         if (State::getNameById((int) $address->state)) {
             $address->id_state = (int) $address->state;
         }
     } elseif (isset($address->state) && is_string($address->state) && !empty($address->state)) {
         if ($id_state = State::getIdByName($address->state)) {
             $address->id_state = (int) $id_state;
         } else {
             $state = new State();
             $state->active = 1;
             $state->name = $address->state;
             $state->id_country = isset($country->id) ? (int) $country->id : 0;
             $state->id_zone = 0;
             // Default zone for state to create
             $state->iso_code = Tools::strtoupper(Tools::substr($address->state, 0, 2));
             // Default iso for state to create
             $state->tax_behavior = 0;
             if (($field_error = $state->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $state->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $state->add()) {
                 $address->id_state = (int) $state->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = sprintf($this->trans('%s cannot be saved', array(), 'Admin.Parameters.Notification'), $state->name);
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->customer_email) && !empty($address->customer_email)) {
         if (Validate::isEmail($address->customer_email)) {
             // a customer could exists in different shop
             $customer_list = Customer::getCustomersByEmail($address->customer_email);
             if (count($customer_list) == 0) {
                 $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), Db::getInstance()->getMsgError(), $address->customer_email, isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
             }
         } else {
             $this->errors[] = sprintf($this->trans('"%s" is not a valid email address.', array(), 'Admin.Parameters.Notification'), $address->customer_email);
             return;
         }
     } elseif (isset($address->id_customer) && !empty($address->id_customer)) {
         if (Customer::customerIdExistsStatic((int) $address->id_customer)) {
             $customer = new Customer((int) $address->id_customer);
             // a customer could exists in different shop
             $customer_list = Customer::getCustomersByEmail($customer->email);
             if (count($customer_list) == 0) {
                 $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), Db::getInstance()->getMsgError(), $customer->email, (int) $address->id_customer);
             }
         } else {
             $this->errors[] = sprintf(Tools::displayError('The customer ID #%d does not exist in the database, and therefore cannot be ' . ($validateOnly ? 'validated' : 'saved')), $address->id_customer);
         }
     } else {
         $customer_list = array();
         $address->id_customer = 0;
     }
     if (isset($address->manufacturer) && is_numeric($address->manufacturer) && Manufacturer::manufacturerExists((int) $address->manufacturer)) {
         $address->id_manufacturer = (int) $address->manufacturer;
     } elseif (isset($address->manufacturer) && is_string($address->manufacturer) && !empty($address->manufacturer)) {
         if ($manufacturerId = Manufacturer::getIdByName($address->manufacturer)) {
             $address->id_manufacturer = $manufacturerId;
         } else {
             $manufacturer = new Manufacturer();
             $manufacturer->name = $address->manufacturer;
             if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $manufacturer->add()) {
                 $address->id_manufacturer = (int) $manufacturer->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null');
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     if (isset($address->supplier) && is_numeric($address->supplier) && Supplier::supplierExists((int) $address->supplier)) {
         $address->id_supplier = (int) $address->supplier;
     } elseif (isset($address->supplier) && is_string($address->supplier) && !empty($address->supplier)) {
         if ($supplierId = Supplier::getIdByName($address->supplier)) {
             $address->id_supplier = $supplierId;
         } else {
             $supplier = new Supplier();
             $supplier->name = $address->supplier;
             if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && !$validateOnly && $supplier->add()) {
                 $address->id_supplier = (int) $supplier->id;
             } else {
                 if (!$validateOnly) {
                     $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null');
                 }
                 if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
     }
     $res = false;
     if (($field_error = $address->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $address->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
         $address->force_id = (bool) $force_ids;
         if (isset($customer_list) && count($customer_list) > 0) {
             $filter_list = array();
             foreach ($customer_list as $customer) {
                 if (in_array($customer['id_customer'], $filter_list)) {
                     continue;
                 }
                 $filter_list[] = $customer['id_customer'];
                 $address->id_customer = $customer['id_customer'];
             }
         }
         if ($address->id && $address->addressExists($address->id)) {
             $res = $validateOnly || $address->update();
         }
         if (!$res) {
             $res = $validateOnly || $address->add();
         }
     }
     if (!$res) {
         if (!$validateOnly) {
             $this->errors[] = sprintf($this->trans('%1$s (ID: %2$s) cannot be saved', array(), 'Admin.Parameters.Notification'), $info['alias'], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
         }
         if ($field_error !== true || isset($lang_field_error) && $lang_field_error !== true) {
             $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
         }
     }
 }
 public function handleConfirm($update = false)
 {
     global $currentIndex, $cookie, $smarty;
     $products_to_import = array();
     $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT');
     $file_path = Tools::getValue('current_file');
     $overwrite_imgs = Tools::getValue("overwrite_imgs");
     //$file_path = '/Users/rohit/webroot/indusdiva/admin12/product-uploads/upload_sheet_1.csv';
     $f = fopen($file_path, 'r');
     $file_error = false;
     if ($f) {
         //discard header
         $line = fgetcsv($f);
         while ($line = fgetcsv($f)) {
             //ignore empty lines
             if (empty($line)) {
                 continue;
             }
             //trim data
             foreach ($line as $key => $value) {
                 $line[$key] = trim($value);
             }
             $id_product = $line[0];
             $images = $line[1];
             $product_name = $line[2];
             $fabric = $line[3];
             $color = $line[4];
             $mrp = $line[5];
             $supplier_code = $line[6];
             $reference = $line[7];
             $location = $line[8];
             $length = $line[9];
             $width = $line[10];
             $blouse_length = $line[11];
             $garment_type = $line[12];
             $work_type = $line[13];
             $weight = $line[14];
             $description = $line[15];
             $other_info = $line[16];
             $wash_care = $line[17];
             $shipping_estimate = $line[18];
             $supplier_price = $line[19];
             $manufacturer = $line[20];
             $categories = explode(",", $line[21]);
             $tax_rule = $line[22];
             $quantity = $line[23];
             $active = $line[24];
             $discount = $line[25];
             $tags = $line[26];
             $kameez_style = $line[27];
             $salwar_style = $line[28];
             $sleeves = $line[29];
             $customizable = $line[30];
             $generic_color = $line[31];
             $skirt_length = $line[32];
             $dupatta_length = $line[33];
             $stone = $line[34];
             $plating = $line[35];
             $material = $line[36];
             $dimensions = $line[37];
             $look = $line[38];
             $as_shown = isset($line[39]) && !empty($line[39]) ? intval($line[39]) : 0;
             $id_sizechart = isset($line[40]) && !empty($line[40]) ? intval($line[40]) : 0;
             $is_exclusive = isset($line[41]) && !empty($line[41]) ? intval($line[41]) : 0;
             $handbag_occasion = isset($line[42]) && !empty($line[42]) ? $line[42] : null;
             $handbag_style = isset($line[43]) && !empty($line[43]) ? $line[43] : null;
             $handbag_material = isset($line[44]) && !empty($line[44]) ? $line[44] : null;
             $images = explode(",", $images);
             $error = false;
             //validate fields
             if (!Validate::isFloat($mrp)) {
                 $error = 'MRP should be a number: ' . trim($reference);
             } elseif (!Validate::isFloat($supplier_price)) {
                 $error = 'Supplier Price should be a number: ' . trim($reference);
             }
             $importCategories = array();
             if (is_array($categories)) {
                 $categories = array_unique($categories);
                 foreach ($categories as $category) {
                     $category = intval(trim($category));
                     if (empty($category)) {
                         continue;
                     }
                     if (!is_numeric($category) || !Category::categoryExists($category)) {
                         $error = 'Category does not exist: ' . $category;
                     }
                     $importCategories[] = $category;
                 }
             } else {
                 $error = 'Atleast one category required: ' . trim($reference);
             }
             if (!Validate::isFloat($weight)) {
                 $error = 'Weight has to be a number: ' . trim($reference);
             }
             if (!empty($manufacturer) && (!is_numeric($manufacturer) || !Manufacturer::manufacturerExists((int) $manufacturer))) {
                 $error = 'Manufacturer does not exist';
             }
             if ($quantity && !is_numeric($quantity) || $discount && !is_numeric($discount)) {
                 $error = 'Quantity and discount should be numbers: ' . trim($reference);
             }
             if (!Validate::isLoadedObject(new TaxRulesGroup($tax_rule))) {
                 $error = 'Tax rate invalid: ' . trim($reference);
             }
             if (!$update) {
                 $sql = "SELECT `reference`\n\t\t\t\t\t\t\tFROM " . _DB_PREFIX_ . "product p\n\t\t\t\t\t\t\tWHERE p.`reference` = '" . $reference . "'";
                 $row = Db::getInstance()->getRow($sql);
                 if (isset($row['reference'])) {
                     $error = "Duplicate indusdiva code : " . trim($reference);
                 }
             }
             //check for souring price
             if ($supplier_price > $mrp / 1.2) {
                 $error = "MRP too low : " . trim($reference);
             }
             //check for images
             if (!$update || $overwrite_imgs == "on") {
                 foreach ($images as $image_name) {
                     $image_name = trim($image_name);
                     $image_path = IMAGE_UPLOAD_PATH . $image_name;
                     if (!empty($image_name) && !file_exists($image_path)) {
                         $error = "Image not found for: " . trim($reference) . ", Image Name: " . $image_name;
                         break;
                     }
                 }
             }
             $vendor_code = substr($reference, 0, 6);
             $sql = "select id_supplier from ps_supplier where code = '{$vendor_code}'";
             $row = Db::getInstance()->getRow($sql);
             if (!isset($row['id_supplier'])) {
                 $error = "Vendor Details not found for : " . trim($reference);
             } else {
                 $id_supplier = $row['id_supplier'];
             }
             //For sudarshan, supplier_code (vendor product code) is mandatory
             if (false) {
                 //(int) $id_supplier === 2 ) {
                 if (empty($supplier_code)) {
                     $error = "Reference: {$reference} -- Supplier Code is Mandatory for Vendor {$vendor_code}";
                 } else {
                     if (strpos("::", ${$supplier_code}) === false) {
                         $error = "Reference: {$reference} -- Supplier Code:{$supplier_code} is not in DESIGN_NO::ITEM_CODE format for Vendor {$vendor_code}";
                     }
                 }
             }
             if (!$error) {
                 if ($update && !empty($id_product)) {
                     $product = new Product((int) $id_product);
                     if (!Validate::isLoadedObject($product)) {
                         $error = "Error loading the product: " . $id_product;
                         return;
                     }
                 } elseif (!$update) {
                     $product = new Product();
                 }
                 $product->id_tax_rules_group = $tax_rule;
                 $product->reference = $reference;
                 $product->id_supplier = $id_supplier;
                 $product->location = $location;
                 $product->tax_rate = TaxRulesGroup::getTaxesRate((int) $product->id_tax_rules_group, Configuration::get('PS_COUNTRY_DEFAULT'), 0, 0);
                 if (isset($manufacturer) and is_numeric($manufacturer) and Manufacturer::manufacturerExists((int) $manufacturer)) {
                     $product->id_manufacturer = $manufacturer;
                 }
                 $product->price = (double) $mrp;
                 $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', '');
                 $product->id_category = $importCategories;
                 $product->id_category_default = 1;
                 $product->name = array();
                 $product->name[$defaultLanguageId] = $product_name;
                 $product->description_short = array();
                 $product->description_short[$defaultLanguageId] = $style_tips;
                 $product->description = array();
                 $product->description[$defaultLanguageId] = $description;
                 $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]);
                 $product->link_rewrite = array();
                 $product->link_rewrite[$defaultLanguageId] = $link_rewrite;
                 $product->quantity = $quantity ? intval($quantity) : 0;
                 if ($discount && is_numeric($discount)) {
                     $product->discount = $discount;
                 }
                 if (!empty($tags)) {
                     $product->tags = $tags;
                 }
                 $product->weight = is_numeric($weight) ? $weight : 0;
                 $product->width = is_numeric($width) ? $width : 0;
                 $product->height = is_numeric($length) ? $length : 0;
                 $product->supplier_reference = $supplier_code;
                 $product->wholesale_price = $supplier_price ? (double) $supplier_price : 0;
                 $product->active = $active == 1 ? 1 : 0;
                 $product->images = $images;
                 $product->fabric = $fabric;
                 $product->color = $color;
                 $product->generic_color = $generic_color;
                 $product->garment_type = $garment_type;
                 $product->work_type = $work_type;
                 $product->blouse_length = $blouse_length ? $blouse_length : ' ';
                 $product->wash_care = $wash_care ? $wash_care : ' ';
                 $product->other_info = $other_info ? $other_info : ' ';
                 $product->shipping_estimate = $shipping_estimate ? $shipping_estimate : ' ';
                 $product->is_customizable = $customizable == 1 ? 1 : 0;
                 $product->kameez_style = $kameez_style;
                 $product->salwar_style = $salwar_style;
                 $product->sleeves = $sleeves;
                 $product->skirt_length = $skirt_length;
                 $product->dupatta_length = $dupatta_length;
                 $product->stone = $stone;
                 $product->plating = $plating;
                 $product->material = $material;
                 $product->dimensions = $dimensions;
                 $product->look = $look;
                 $product->as_shown = $as_shown;
                 $product->id_sizechart = $id_sizechart;
                 $product->is_exclusive = $is_exclusive;
                 $product->handbag_occasion = $handbag_occasion;
                 $product->handbag_style = $handbag_style;
                 $product->handbag_material = $handbag_material;
                 $product->indexed = 0;
                 $products_to_import[] = $product;
             } else {
                 $smarty->assign('error', $error);
                 return;
                 $file_error = true;
             }
         }
         if (!$file_error) {
             $added_product_ids = array();
             foreach ($products_to_import as $product) {
                 $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true);
                 $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true);
                 if ($fieldError === true and $langFieldError === true) {
                     // check quantity
                     if ($product->quantity == NULL) {
                         $product->quantity = 0;
                     }
                     // If no id_product or update failed
                     if ($update && $product->id) {
                         $res = $product->update();
                     } else {
                         $res = $product->add();
                     }
                     $added_product_ids[] = $product->id;
                 }
                 if (isset($product->discount) && $product->discount > 0) {
                     SpecificPrice::deleteByProductId((int) $product->id);
                     $specificPrice = new SpecificPrice();
                     $specificPrice->id_product = (int) $product->id;
                     $specificPrice->id_shop = (int) Shop::getCurrentShop();
                     $specificPrice->id_currency = 0;
                     $specificPrice->id_country = 0;
                     $specificPrice->id_group = 0;
                     $specificPrice->from_quantity = 1;
                     $specificPrice->reduction = $product->discount / 100;
                     $specificPrice->reduction_type = 'percentage';
                     $specificPrice->from = '2012-01-01 00:00:00';
                     $specificPrice->to = '2016-01-01 00:00:00';
                     $specificPrice->price = $product->price;
                     $specificPrice->add();
                 }
                 if (isset($product->tags) and !empty($product->tags)) {
                     // Delete tags for this id product, for no duplicating error
                     Tag::deleteTagsForProduct($product->id);
                     $tag = new Tag();
                     $tag->addTags($defaultLanguageId, $product->id, $tags);
                 }
                 if (isset($product->images) and is_array($product->images) and sizeof($product->images) and !$update || $overwrite_imgs == "on") {
                     $product->deleteImages();
                     $first_image = true;
                     foreach ($product->images as $image_name) {
                         $image_name = trim($image_name);
                         $image_path = IMAGE_UPLOAD_PATH . $image_name;
                         if (!empty($image_name)) {
                             $image = new Image();
                             $image->id_product = (int) $product->id;
                             $image->position = Image::getHighestPosition($product->id) + 1;
                             $image->cover = $first_image;
                             $image->legend[$defaultLanguageId] = $product->name[$defaultLanguageId];
                             if (($fieldError = $image->validateFields(false, true)) === true and ($langFieldError = $image->validateFieldsLang(false, true)) === true and $image->add()) {
                                 if (!self::copyImg($product->id, $image->id, $image_path)) {
                                     $_warnings[] = Tools::displayError('Error copying image: ') . $image_path;
                                 } else {
                                     //delete the original image
                                     @unlink($image_path);
                                 }
                             } else {
                                 $_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved');
                                 $_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error();
                             }
                         }
                         $first_image = false;
                     }
                 }
                 if (isset($product->id_category)) {
                     $product->updateCategories(array_map('intval', $product->id_category));
                 }
                 $this->addFeature($product->id, 'fabric', $product->fabric);
                 $this->addFeature($product->id, 'color', $product->color);
                 $this->addFeature($product->id, 'garment_type', $product->garment_type);
                 $this->addFeature($product->id, 'work_type', $product->work_type);
                 $this->addFeature($product->id, 'blouse_length', $product->blouse_length);
                 $this->addFeature($product->id, 'wash_care', $product->wash_care);
                 $this->addFeature($product->id, 'other_info', $product->other_info);
                 // to avoid type errors in the catalog sheet - construct the string here again
                 $shipping_sla = (int) preg_replace('/\\D/', '', $product->shipping_estimate);
                 $shipping_estimate_str = "";
                 if ($shipping_sla > 0) {
                     $shipping_estimate_str = $shipping_sla === 1 ? "Ready to be shipped in 1 day" : "Ready to be shipped in {$shipping_sla} days";
                 }
                 $this->addFeature($product->id, 'shipping_estimate', $shipping_estimate_str);
                 $this->addFeature($product->id, 'kameez_style', $product->kameez_style);
                 $this->addFeature($product->id, 'salwar_style', $product->salwar_style);
                 $this->addFeature($product->id, 'sleeves', $product->sleeves);
                 $this->addFeature($product->id, 'generic_color', $product->generic_color);
                 $this->addFeature($product->id, 'skirt_length', $product->skirt_length);
                 $this->addFeature($product->id, 'dupatta_length', $product->dupatta_length);
                 $this->addFeature($product->id, 'stone', $product->stone);
                 $this->addFeature($product->id, 'plating', $product->plating);
                 $this->addFeature($product->id, 'material', $product->material);
                 $this->addFeature($product->id, 'dimensions', $product->dimensions);
                 $this->addFeature($product->id, 'look', $product->look);
                 $this->addFeature($product->id, 'handbag_occasion', $product->handbag_occasion);
                 $this->addFeature($product->id, 'handbag_style', $product->handbag_style);
                 $this->addFeature($product->id, 'handbag_material', $product->handbag_material);
             }
             $smarty->assign("products_affected", $products_to_import);
             //reindex the products
             SolrSearch::updateProducts($added_product_ids);
             $smarty->assign("is_update", $update);
         } else {
             $smarty->assign('file_error', 1);
         }
     } else {
         $smarty->assign('error_reading', 1);
     }
 }
 public function addressImport()
 {
     $this->receiveTab();
     $default_language_id = (int) Configuration::get('PS_LANG_DEFAULT');
     $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);
         $address = new Address();
         AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $address);
         if (isset($address->country) && is_numeric($address->country)) {
             if (Country::getNameById(Configuration::get('PS_LANG_DEFAULT'), (int) $address->country)) {
                 $address->id_country = (int) $address->country;
             }
         } elseif (isset($address->country) && is_string($address->country) && !empty($address->country)) {
             if ($id_country = Country::getIdByName(null, $address->country)) {
                 $address->id_country = (int) $id_country;
             } else {
                 $country = new Country();
                 $country->active = 1;
                 $country->name = AdminImportController::createMultiLangField($address->country);
                 $country->id_zone = 0;
                 // Default zone for country to create
                 $country->iso_code = Tools::strtoupper(Tools::substr($address->country, 0, 2));
                 // Default iso for country to create
                 $country->contains_states = 0;
                 // Default value for country to create
                 $lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true);
                 if (($field_error = $country->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $country->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $country->add()) {
                     $address->id_country = (int) $country->id;
                 } else {
                     $this->errors[] = sprintf(Tools::displayError('%s cannot be saved'), $country->name[$default_language_id]);
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
         if (isset($address->state) && is_numeric($address->state)) {
             if (State::getNameById((int) $address->state)) {
                 $address->id_state = (int) $address->state;
             }
         } elseif (isset($address->state) && is_string($address->state) && !empty($address->state)) {
             if ($id_state = State::getIdByName($address->state)) {
                 $address->id_state = (int) $id_state;
             } else {
                 $state = new State();
                 $state->active = 1;
                 $state->name = $address->state;
                 $state->id_country = isset($country->id) ? (int) $country->id : 0;
                 $state->id_zone = 0;
                 // Default zone for state to create
                 $state->iso_code = Tools::strtoupper(Tools::substr($address->state, 0, 2));
                 // Default iso for state to create
                 $state->tax_behavior = 0;
                 if (($field_error = $state->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $state->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $state->add()) {
                     $address->id_state = (int) $state->id;
                 } else {
                     $this->errors[] = sprintf(Tools::displayError('%s cannot be saved'), $state->name);
                     $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                 }
             }
         }
         if (isset($address->customer_email) && !empty($address->customer_email)) {
             if (Validate::isEmail($address->customer_email)) {
                 // a customer could exists in different shop
                 $customer_list = Customer::getCustomersByEmail($address->customer_email);
                 if (count($customer_list) == 0) {
                     $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be saved.'), Db::getInstance()->getMsgError(), $address->customer_email, isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
                 }
             } else {
                 $this->errors[] = sprintf(Tools::displayError('"%s" is not a valid email address.'), $address->customer_email);
                 continue;
             }
         } elseif (isset($address->id_customer) && !empty($address->id_customer)) {
             if (Customer::customerIdExistsStatic((int) $address->id_customer)) {
                 $customer = new Customer((int) $address->id_customer);
                 // a customer could exists in different shop
                 $customer_list = Customer::getCustomersByEmail($customer->email);
                 if (count($customer_list) == 0) {
                     $this->errors[] = sprintf(Tools::displayError('%1$s does not exist in database %2$s (ID: %3$s), and therefore cannot be saved.'), Db::getInstance()->getMsgError(), $customer->email, (int) $address->id_customer);
                 }
             } else {
                 $this->errors[] = sprintf(Tools::displayError('The customer ID #%d does not exist in the database, and therefore cannot be saved.'), $address->id_customer);
             }
         } else {
             $customer_list = array();
             $address->id_customer = 0;
         }
         if (isset($address->manufacturer) && is_numeric($address->manufacturer) && Manufacturer::manufacturerExists((int) $address->manufacturer)) {
             $address->id_manufacturer = (int) $address->manufacturer;
         } elseif (isset($address->manufacturer) && is_string($address->manufacturer) && !empty($address->manufacturer)) {
             $manufacturer = new Manufacturer();
             $manufacturer->name = $address->manufacturer;
             if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $manufacturer->add()) {
                 $address->id_manufacturer = (int) $manufacturer->id;
             } else {
                 $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null');
                 $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
             }
         }
         if (isset($address->supplier) && is_numeric($address->supplier) && Supplier::supplierExists((int) $address->supplier)) {
             $address->id_supplier = (int) $address->supplier;
         } elseif (isset($address->supplier) && is_string($address->supplier) && !empty($address->supplier)) {
             $supplier = new Supplier();
             $supplier->name = $address->supplier;
             if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $supplier->add()) {
                 $address->id_supplier = (int) $supplier->id;
             } else {
                 $this->errors[] = Db::getInstance()->getMsgError() . ' ' . sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null');
                 $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
             }
         }
         $res = false;
         if (($field_error = $address->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $address->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) {
             if (isset($customer_list) && count($customer_list) > 0) {
                 $filter_list = array();
                 foreach ($customer_list as $customer) {
                     if (in_array($customer['id_customer'], $filter_list)) {
                         continue;
                     }
                     $filter_list[] = $customer['id_customer'];
                     unset($address->id);
                     $address->id_customer = $customer['id_customer'];
                     $res = $address->add();
                     if (!$res) {
                         $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $info['alias'], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
                     }
                 }
             } else {
                 $address->force_id = (bool) Tools::getValue('forceIDs');
                 if ($address->id && $address->addressExists($address->id)) {
                     $res = $address->update();
                 }
                 if (!$res) {
                     $res = $address->add();
                 }
             }
         }
         if (!$res) {
             $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $info['alias'], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null');
             $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
         }
     }
     $this->closeCsvFile($handle);
 }
    public function productImport()
    {
        $this->receiveTab();
        $handle = $this->openCsvFile();
        $default_language_id = (int) Configuration::get('PS_LANG_DEFAULT');
        $id_lang = Language::getIdByIso(Tools::getValue('iso_lang'));
        if (!Validate::isUnsignedId($id_lang)) {
            $id_lang = $default_language_id;
        }
        AdminImportController::setLocale();
        $shop_ids = Shop::getCompleteListOfShopsID();
        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);
            if (self::ignoreRow($info)) {
                continue;
            }
            if (Tools::getValue('forceIDs') && isset($info['id']) && (int) $info['id']) {
                $product = new Product((int) $info['id']);
            } elseif (Tools::getValue('match_ref') && array_key_exists('reference', $info)) {
                $datas = Db::getInstance()->getRow('
						SELECT p.`id_product`
						FROM `' . _DB_PREFIX_ . 'product` p
						' . Shop::addSqlAssociation('product', 'p') . '
						WHERE p.`reference` = "' . pSQL($info['reference']) . '"
					');
                if (isset($datas['id_product']) && $datas['id_product']) {
                    $product = new Product((int) $datas['id_product']);
                } else {
                    $product = new Product();
                }
            } else {
                if (array_key_exists('id', $info) && is_string($info['id'])) {
                    $prod = self::findProductByName($default_language_id, $info['id'], $info['name']);
                    if ($prod['id_product']) {
                        $info['id'] = (int) $prod['id_product'];
                    }
                }
                if (array_key_exists('id', $info) && (int) $info['id'] && Product::existsInDatabase((int) $info['id'], 'product')) {
                    $product = new Product((int) $info['id']);
                    $product->loadStockData();
                    $category_data = Product::getProductCategories((int) $product->id);
                    if (is_array($category_data)) {
                        foreach ($category_data as $tmp) {
                            if (!isset($product->category) || !$product->category || is_array($product->category)) {
                                $product->category[] = $tmp;
                            }
                        }
                    }
                } else {
                    $product = new Product();
                }
            }
            AdminImportController::setEntityDefaultValues($product);
            AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $product);
            if (!Shop::isFeatureActive()) {
                $product->shop = 1;
            } elseif (!isset($product->shop) || empty($product->shop)) {
                $product->shop = implode($this->multiple_value_separator, Shop::getContextListShopID());
            }
            if (!Shop::isFeatureActive()) {
                $product->id_shop_default = 1;
            } else {
                $product->id_shop_default = (int) Context::getContext()->shop->id;
            }
            // link product to shops
            $product->id_shop_list = array();
            foreach (explode($this->multiple_value_separator, $product->shop) as $shop) {
                if (!empty($shop) && !is_numeric($shop)) {
                    $product->id_shop_list[] = Shop::getIdByName($shop);
                } elseif (!empty($shop)) {
                    $product->id_shop_list[] = $shop;
                }
            }
            if ((int) $product->id_tax_rules_group != 0) {
                if (Validate::isLoadedObject(new TaxRulesGroup($product->id_tax_rules_group))) {
                    $address = $this->context->shop->getAddress();
                    $tax_manager = TaxManagerFactory::getManager($address, $product->id_tax_rules_group);
                    $product_tax_calculator = $tax_manager->getTaxCalculator();
                    $product->tax_rate = $product_tax_calculator->getTotalRate();
                } else {
                    $this->addProductWarning('id_tax_rules_group', $product->id_tax_rules_group, Tools::displayError('Invalid tax rule group ID. You first need to create a group with this ID.'));
                }
            }
            if (isset($product->manufacturer) && is_numeric($product->manufacturer) && Manufacturer::manufacturerExists((int) $product->manufacturer)) {
                $product->id_manufacturer = (int) $product->manufacturer;
            } elseif (isset($product->manufacturer) && is_string($product->manufacturer) && !empty($product->manufacturer)) {
                if ($manufacturer = Manufacturer::getIdByName($product->manufacturer)) {
                    $product->id_manufacturer = (int) $manufacturer;
                } else {
                    $manufacturer = new Manufacturer();
                    $manufacturer->name = $product->manufacturer;
                    if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $manufacturer->add()) {
                        $product->id_manufacturer = (int) $manufacturer->id;
                    } else {
                        $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null');
                        $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                    }
                }
            }
            if (isset($product->supplier) && is_numeric($product->supplier) && Supplier::supplierExists((int) $product->supplier)) {
                $product->id_supplier = (int) $product->supplier;
            } elseif (isset($product->supplier) && is_string($product->supplier) && !empty($product->supplier)) {
                if ($supplier = Supplier::getIdByName($product->supplier)) {
                    $product->id_supplier = (int) $supplier;
                } else {
                    $supplier = new Supplier();
                    $supplier->name = $product->supplier;
                    $supplier->active = true;
                    if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $supplier->add()) {
                        $product->id_supplier = (int) $supplier->id;
                        $supplier->associateTo($product->id_shop_list);
                    } else {
                        $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null');
                        $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                    }
                }
            }
            if (isset($product->price_tex) && !isset($product->price_tin)) {
                $product->price = $product->price_tex;
            } elseif (isset($product->price_tin) && !isset($product->price_tex)) {
                $product->price = $product->price_tin;
                // If a tax is already included in price, withdraw it from price
                if ($product->tax_rate) {
                    $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', '');
                }
            } elseif (isset($product->price_tin) && isset($product->price_tex)) {
                $product->price = $product->price_tex;
            }
            if (!Configuration::get('PS_USE_ECOTAX')) {
                $product->ecotax = 0;
            }
            $properties = $product->productProperties();
            if ((double) $properties['pp_unit_price_ratio'] > 0) {
                $product->unit_price = (double) $product->price / (double) $properties['pp_unit_price_ratio'];
            }
            if (isset($product->category) && is_array($product->category) && count($product->category)) {
                $product->id_category = array();
                // Reset default values array
                foreach ($product->category as $value) {
                    if (is_numeric($value)) {
                        if (Category::categoryExists((int) $value)) {
                            $product->id_category[] = (int) $value;
                        } else {
                            $category_to_create = new Category();
                            $category_to_create->id = (int) $value;
                            $category_to_create->name = AdminImportController::createMultiLangField($value);
                            $category_to_create->active = 1;
                            $category_to_create->id_parent = Configuration::get('PS_HOME_CATEGORY');
                            // Default parent is home for unknown category to create
                            $category_link_rewrite = Tools::link_rewrite($category_to_create->name[$default_language_id]);
                            $category_to_create->link_rewrite = AdminImportController::createMultiLangField($category_link_rewrite);
                            if (($field_error = $category_to_create->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $category_to_create->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $category_to_create->add()) {
                                $product->id_category[] = (int) $category_to_create->id;
                            } else {
                                $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $category_to_create->name[$default_language_id], isset($category_to_create->id) && !empty($category_to_create->id) ? $category_to_create->id : 'null');
                                $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
                            }
                        }
                    } elseif (is_string($value) && !empty($value)) {
                        $category = Category::searchByPath($default_language_id, trim($value), $this, 'productImportCreateCat');
                        if ($category['id_category']) {
                            $product->id_category[] = (int) $category['id_category'];
                        } else {
                            $this->errors[] = sprintf(Tools::displayError('%1$s cannot be saved'), trim($value));
                        }
                    }
                }
                $product->id_category = array_values(array_unique($product->id_category));
            }
            if (!isset($product->id_category_default) || !$product->id_category_default) {
                $product->id_category_default = isset($product->id_category[0]) ? (int) $product->id_category[0] : (int) Configuration::get('PS_HOME_CATEGORY');
            }
            $link_rewrite = is_array($product->link_rewrite) && isset($product->link_rewrite[$id_lang]) ? trim($product->link_rewrite[$id_lang]) : '';
            $valid_link = Validate::isLinkRewrite($link_rewrite);
            if (isset($product->link_rewrite[$id_lang]) && empty($product->link_rewrite[$id_lang]) || !$valid_link) {
                $link_rewrite = Tools::link_rewrite($product->name[$id_lang]);
                if ($link_rewrite == '') {
                    $link_rewrite = 'friendly-url-autogeneration-failed';
                }
            }
            if (!$valid_link) {
                $this->warnings[] = sprintf(Tools::displayError('Rewrite link for %1$s (ID: %2$s) was re-written as %3$s.'), $product->name[$id_lang], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null', $link_rewrite);
            }
            if (!(Tools::getValue('match_ref') || Tools::getValue('forceIDs')) || !(is_array($product->link_rewrite) && count($product->link_rewrite) && !empty($product->link_rewrite[$id_lang]))) {
                $product->link_rewrite = AdminImportController::createMultiLangField($link_rewrite);
            }
            // replace the value of separator by coma
            if ($this->multiple_value_separator != ',') {
                if (is_array($product->meta_keywords)) {
                    foreach ($product->meta_keywords as &$meta_keyword) {
                        if (!empty($meta_keyword)) {
                            $meta_keyword = str_replace($this->multiple_value_separator, ',', $meta_keyword);
                        }
                    }
                }
            }
            // Convert comma into dot for all floating values
            foreach (Product::$definition['fields'] as $key => $array) {
                if ($array['type'] == Product::TYPE_FLOAT) {
                    $product->{$key} = str_replace(',', '.', $product->{$key});
                }
            }
            // Indexation is already 0 if it's a new product, but not if it's an update
            $product->indexed = 0;
            $res = false;
            $field_error = $product->validateFields(UNFRIENDLY_ERROR, true);
            $lang_field_error = $product->validateFieldsLang(UNFRIENDLY_ERROR, true);
            if ($field_error === true && $lang_field_error === true) {
                // check quantity
                if ($product->quantity == null) {
                    $product->quantity = 0;
                }
                // If match ref is specified && ref product && ref product already in base, trying to update
                if (Tools::getValue('match_ref') && $product->reference && $product->existsRefInDatabase($product->reference)) {
                    $datas = Db::getInstance()->getRow('
						SELECT product_shop.`date_add`, p.`id_product`
						FROM `' . _DB_PREFIX_ . 'product` p
						' . Shop::addSqlAssociation('product', 'p') . '
						WHERE p.`reference` = "' . pSQL($product->reference) . '"
					');
                    $product->id = (int) $datas['id_product'];
                    $product->date_add = pSQL($datas['date_add']);
                    $res = $product->update();
                } elseif ($product->id && Product::existsInDatabase((int) $product->id, 'product')) {
                    $datas = Db::getInstance()->getRow('
						SELECT product_shop.`date_add`
						FROM `' . _DB_PREFIX_ . 'product` p
						' . Shop::addSqlAssociation('product', 'p') . '
						WHERE p.`id_product` = ' . (int) $product->id);
                    $product->date_add = pSQL($datas['date_add']);
                    $res = $product->update();
                }
                // If no id_product or update failed
                $product->force_id = (bool) Tools::getValue('forceIDs');
                if (!$res) {
                    if (isset($product->date_add) && $product->date_add != '') {
                        $res = $product->add(false);
                    } else {
                        $res = $product->add();
                    }
                }
                if ($product->getType() == Product::PTYPE_VIRTUAL) {
                    StockAvailable::setProductOutOfStock((int) $product->id, 1);
                } else {
                    StockAvailable::setProductOutOfStock((int) $product->id, (int) $product->out_of_stock);
                }
            }
            $shops = array();
            $product_shop = explode($this->multiple_value_separator, $product->shop);
            foreach ($product_shop as $shop) {
                if (empty($shop)) {
                    continue;
                }
                $shop = trim($shop);
                if (!empty($shop) && !is_numeric($shop)) {
                    $shop = Shop::getIdByName($shop);
                }
                if (in_array($shop, $shop_ids)) {
                    $shops[] = $shop;
                } else {
                    $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Shop is not valid'));
                }
            }
            if (empty($shops)) {
                $shops = Shop::getContextListShopID();
            }
            // If both failed, mysql error
            if (!$res) {
                $this->errors[] = 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');
                $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError();
            } else {
                // Product supplier
                if (isset($product->id) && $product->id && isset($product->id_supplier) && property_exists($product, 'supplier_reference')) {
                    $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier((int) $product->id, 0, (int) $product->id_supplier);
                    if ($id_product_supplier) {
                        $product_supplier = new ProductSupplier($id_product_supplier);
                    } else {
                        $product_supplier = new ProductSupplier();
                    }
                    $product_supplier->id_product = (int) $product->id;
                    $product_supplier->id_product_attribute = 0;
                    $product_supplier->id_supplier = (int) $product->id_supplier;
                    $product_supplier->product_supplier_price_te = $product->wholesale_price;
                    $product_supplier->product_supplier_reference = $product->supplier_reference;
                    $product_supplier->save();
                }
                // SpecificPrice (only the basic reduction feature is supported by the import)
                if (!Shop::isFeatureActive()) {
                    $info['shop'] = 1;
                } elseif (!isset($info['shop']) || empty($info['shop'])) {
                    $info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID());
                }
                // Get shops for each attributes
                $info['shop'] = explode($this->multiple_value_separator, $info['shop']);
                $id_shop_list = array();
                foreach ($info['shop'] as $shop) {
                    if (!empty($shop) && !is_numeric($shop)) {
                        $id_shop_list[] = (int) Shop::getIdByName($shop);
                    } elseif (!empty($shop)) {
                        $id_shop_list[] = $shop;
                    }
                }
                if (isset($info['reduction_price']) && $info['reduction_price'] > 0 || isset($info['reduction_percent']) && $info['reduction_percent'] > 0) {
                    foreach ($id_shop_list as $id_shop) {
                        $specific_price = SpecificPrice::getSpecificPrice($product->id, $id_shop, 0, 0, 0, 1, 0, 0, 0, 0);
                        if (is_array($specific_price) && isset($specific_price['id_specific_price'])) {
                            $specific_price = new SpecificPrice((int) $specific_price['id_specific_price']);
                        } else {
                            $specific_price = new SpecificPrice();
                        }
                        $specific_price->id_product = (int) $product->id;
                        $specific_price->id_specific_price_rule = 0;
                        $specific_price->id_shop = $id_shop;
                        $specific_price->id_currency = 0;
                        $specific_price->id_country = 0;
                        $specific_price->id_group = 0;
                        $specific_price->price = -1;
                        $specific_price->id_customer = 0;
                        $specific_price->from_quantity = 1;
                        $specific_price->reduction = isset($info['reduction_price']) && $info['reduction_price'] ? $info['reduction_price'] : $info['reduction_percent'] / 100;
                        $specific_price->reduction_type = isset($info['reduction_price']) && $info['reduction_price'] ? 'amount' : 'percentage';
                        $specific_price->from = isset($info['reduction_from']) && Validate::isDate($info['reduction_from']) ? $info['reduction_from'] : '0000-00-00 00:00:00';
                        $specific_price->to = isset($info['reduction_to']) && Validate::isDate($info['reduction_to']) ? $info['reduction_to'] : '0000-00-00 00:00:00';
                        if (!$specific_price->save()) {
                            $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Discount is invalid'));
                        }
                    }
                }
                if (isset($product->tags) && !empty($product->tags)) {
                    if (isset($product->id) && $product->id) {
                        $tags = Tag::getProductTags($product->id);
                        if (is_array($tags) && count($tags)) {
                            if (!empty($product->tags)) {
                                $product->tags = explode($this->multiple_value_separator, $product->tags);
                            }
                            if (is_array($product->tags) && count($product->tags)) {
                                foreach ($product->tags as $key => $tag) {
                                    if (!empty($tag)) {
                                        $product->tags[$key] = trim($tag);
                                    }
                                }
                                $tags[$id_lang] = $product->tags;
                                $product->tags = $tags;
                            }
                        }
                    }
                    // Delete tags for this id product, for no duplicating error
                    Tag::deleteTagsForProduct($product->id);
                    if (!is_array($product->tags) && !empty($product->tags)) {
                        $product->tags = AdminImportController::createMultiLangField($product->tags);
                        foreach ($product->tags as $key => $tags) {
                            $is_tag_added = Tag::addTags($key, $product->id, $tags, $this->multiple_value_separator);
                            if (!$is_tag_added) {
                                $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Tags list is invalid'));
                                break;
                            }
                        }
                    } else {
                        foreach ($product->tags as $key => $tags) {
                            $str = '';
                            foreach ($tags as $one_tag) {
                                $str .= $one_tag . $this->multiple_value_separator;
                            }
                            $str = rtrim($str, $this->multiple_value_separator);
                            $is_tag_added = Tag::addTags($key, $product->id, $str, $this->multiple_value_separator);
                            if (!$is_tag_added) {
                                $this->addProductWarning(Tools::safeOutput($info['name']), (int) $product->id, 'Invalid tag(s) (' . $str . ')');
                                break;
                            }
                        }
                    }
                }
                //delete existing images if "delete_existing_images" is set to 1
                if (isset($product->delete_existing_images)) {
                    if ((bool) $product->delete_existing_images) {
                        $product->deleteImages();
                    }
                }
                if (isset($product->image) && is_array($product->image) && count($product->image)) {
                    $product_has_images = (bool) Image::getImages($this->context->language->id, (int) $product->id);
                    foreach ($product->image as $key => $url) {
                        $url = trim($url);
                        $error = false;
                        if (!empty($url)) {
                            $url = str_replace(' ', '%20', $url);
                            $image = new Image();
                            $image->id_product = (int) $product->id;
                            $image->position = Image::getHighestPosition($product->id) + 1;
                            $image->cover = !$key && !$product_has_images ? true : false;
                            // file_exists doesn't work with HTTP protocol
                            if (($field_error = $image->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $image->add()) {
                                // associate image to selected shops
                                $image->associateTo($shops);
                                if (!AdminImportController::copyImg($product->id, $image->id, $url, 'products', !Tools::getValue('regenerate'))) {
                                    $image->delete();
                                    $this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
                                }
                            } else {
                                $error = true;
                            }
                        } else {
                            $error = true;
                        }
                        if ($error) {
                            $this->warnings[] = sprintf(Tools::displayError('Product #%1$d: the picture (%2$s) cannot be saved.'), $image->id_product, $url);
                        }
                    }
                }
                if (isset($product->id_category) && is_array($product->id_category)) {
                    $product->updateCategories(array_map('intval', $product->id_category));
                }
                $product->checkDefaultAttributes();
                if (!$product->cache_default_attribute) {
                    Product::updateDefaultAttribute($product->id);
                }
                // Features import
                $features = get_object_vars($product);
                if (isset($features['features']) && !empty($features['features'])) {
                    foreach (explode($this->multiple_value_separator, $features['features']) as $single_feature) {
                        if (empty($single_feature)) {
                            continue;
                        }
                        $tab_feature = explode(':', $single_feature);
                        $feature_name = isset($tab_feature[0]) ? trim($tab_feature[0]) : '';
                        $feature_value = isset($tab_feature[1]) ? trim($tab_feature[1]) : '';
                        $position = isset($tab_feature[2]) ? (int) $tab_feature[2] - 1 : false;
                        $custom = isset($tab_feature[3]) ? (int) $tab_feature[3] : false;
                        if (!empty($feature_name) && !empty($feature_value)) {
                            $id_feature = (int) Feature::addFeatureImport($feature_name, $position);
                            $id_product = null;
                            if (Tools::getValue('forceIDs') || Tools::getValue('match_ref')) {
                                $id_product = (int) $product->id;
                            }
                            $id_feature_value = (int) FeatureValue::addFeatureValueImport($id_feature, $feature_value, $id_product, $id_lang, $custom);
                            Product::addFeatureProductImport($product->id, $id_feature, $id_feature_value);
                        }
                    }
                }
                // clean feature positions to avoid conflict
                Feature::cleanPositions();
                // set advanced stock managment
                if (isset($product->advanced_stock_management)) {
                    if ($product->advanced_stock_management != 1 && $product->advanced_stock_management != 0) {
                        $this->warnings[] = sprintf(Tools::displayError('Advanced stock management has incorrect value. Not set for product %1$s '), $product->name[$default_language_id]);
                    } elseif (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $product->advanced_stock_management == 1) {
                        $this->warnings[] = sprintf(Tools::displayError('Advanced stock management is not enabled, cannot enable on product %1$s '), $product->name[$default_language_id]);
                    } else {
                        $product->setAdvancedStockManagement($product->advanced_stock_management);
                    }
                    // automaticly disable depends on stock, if a_s_m set to disabled
                    if (StockAvailable::dependsOnStock($product->id) == 1 && $product->advanced_stock_management == 0) {
                        StockAvailable::setProductDependsOnStock($product->id, 0);
                    }
                }
                // Check if warehouse exists
                if (isset($product->warehouse) && $product->warehouse) {
                    if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
                        $this->warnings[] = sprintf(Tools::displayError('Advanced stock management is not enabled, warehouse not set on product %1$s '), $product->name[$default_language_id]);
                    } else {
                        if (Warehouse::exists($product->warehouse)) {
                            // Get already associated warehouses
                            $associated_warehouses_collection = WarehouseProductLocation::getCollection($product->id);
                            // Delete any entry in warehouse for this product
                            foreach ($associated_warehouses_collection as $awc) {
                                $awc->delete();
                            }
                            $warehouse_location_entity = new WarehouseProductLocation();
                            $warehouse_location_entity->id_product = $product->id;
                            $warehouse_location_entity->id_product_attribute = 0;
                            $warehouse_location_entity->id_warehouse = $product->warehouse;
                            if (WarehouseProductLocation::getProductLocation($product->id, 0, $product->warehouse) !== false) {
                                $warehouse_location_entity->update();
                            } else {
                                $warehouse_location_entity->save();
                            }
                            StockAvailable::synchronize($product->id);
                        } else {
                            $this->warnings[] = sprintf(Tools::displayError('Warehouse did not exist, cannot set on product %1$s.'), $product->name[$default_language_id]);
                        }
                    }
                }
                // stock available
                if (isset($product->depends_on_stock)) {
                    if ($product->depends_on_stock != 0 && $product->depends_on_stock != 1) {
                        $this->warnings[] = sprintf(Tools::displayError('Incorrect value for "depends on stock" for product %1$s '), $product->name[$default_language_id]);
                    } elseif ((!$product->advanced_stock_management || $product->advanced_stock_management == 0) && $product->depends_on_stock == 1) {
                        $this->warnings[] = sprintf(Tools::displayError('Advanced stock management not enabled, cannot set "depends on stock" for product %1$s '), $product->name[$default_language_id]);
                    } else {
                        StockAvailable::setProductDependsOnStock($product->id, $product->depends_on_stock);
                    }
                    // This code allows us to set qty and disable depends on stock
                    if (isset($product->quantity) && $product->quantity) {
                        // if depends on stock and quantity, add quantity to stock
                        if ($product->depends_on_stock == 1) {
                            $stock_manager = StockManagerFactory::getManager();
                            $price = str_replace(',', '.', $product->wholesale_price);
                            if ($price == 0) {
                                $price = 1.0E-6;
                            }
                            $price = round((double) $price, 6);
                            $warehouse = new Warehouse($product->warehouse);
                            if ($stock_manager->addProduct((int) $product->id, 0, $warehouse, $product->quantity, 1, $price, true)) {
                                StockAvailable::synchronize((int) $product->id);
                            }
                        } else {
                            if (Shop::isFeatureActive()) {
                                foreach ($shops as $shop) {
                                    StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $shop);
                                }
                            } else {
                                StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $this->context->shop->id);
                            }
                        }
                    }
                } else {
                    if (Shop::isFeatureActive()) {
                        foreach ($shops as $shop) {
                            StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $shop);
                        }
                    } else {
                        StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $this->context->shop->id);
                    }
                }
            }
        }
        $this->closeCsvFile($handle);
    }
 public function renderFormAddress()
 {
     // Change table and className for addresses
     $this->table = 'address';
     $this->className = 'Address';
     $id_address = Tools::getValue('id_address');
     // Create Object Address
     $address = new Address($id_address);
     $form = array('legend' => array('title' => $this->l('Addresses'), 'image' => '../img/admin/contact.gif'));
     if (!$address->id_manufacturer || !Manufacturer::manufacturerExists($address->id_manufacturer)) {
         $form['input'][] = array('type' => 'select', 'label' => $this->l('Choose the manufacturer:'), 'name' => 'id_manufacturer', 'options' => array('query' => Manufacturer::getManufacturers(), 'id' => 'id_manufacturer', 'name' => 'name'));
     } else {
         $form['input'][] = array('type' => 'text', 'label' => $this->l('Manufacturer:'), 'name' => 'name', 'disabled' => true);
         $form['input'][] = array('type' => 'hidden', 'name' => 'id_manufacturer');
     }
     $form['input'][] = array('type' => 'hidden', 'name' => 'alias');
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Last name:'), 'name' => 'lastname', 'size' => 33, 'required' => true, 'hint' => $this->l('Invalid characters:') . ' 0-9!<>,;?=+()@#"�{}_$%:');
     $form['input'][] = array('type' => 'text', 'label' => $this->l('First name:'), 'name' => 'firstname', 'size' => 33, 'required' => true, 'hint' => $this->l('Invalid characters:') . ' 0-9!<>,;?=+()@#"�{}_$%:');
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Address:'), 'name' => 'address1', 'size' => 33, 'required' => true);
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Address (2):'), 'name' => 'address2', 'size' => 33, 'required' => false);
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Postcode / Zip Code:'), 'name' => 'postcode', 'size' => 33, 'required' => false);
     $form['input'][] = array('type' => 'text', 'label' => $this->l('City:'), 'name' => 'city', 'size' => 33, 'required' => true);
     $form['input'][] = array('type' => 'select', 'label' => $this->l('Country:'), 'name' => 'id_country', 'required' => false, 'default_value' => (int) $this->context->country->id, 'options' => array('query' => Country::getCountries($this->context->language->id), 'id' => 'id_country', 'name' => 'name'));
     $form['input'][] = array('type' => 'select', 'label' => $this->l('State:'), 'name' => 'id_state', 'required' => false, 'options' => array('query' => array(), 'id' => 'id_state', 'name' => 'name'));
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Home phone:'), 'name' => 'phone', 'size' => 33, 'required' => false);
     $form['input'][] = array('type' => 'text', 'label' => $this->l('Mobile phone:'), 'name' => 'phone_mobile', 'size' => 33, 'required' => false);
     $form['input'][] = array('type' => 'textarea', 'label' => $this->l('Other:'), 'name' => 'other', 'cols' => 36, 'rows' => 4, 'required' => false, 'hint' => $this->l('Forbidden characters:') . ' <>;=#{}');
     $form['submit'] = array('title' => $this->l('   Save   '), 'class' => 'button');
     $this->fields_value = array('name' => Manufacturer::getNameById($address->id_manufacturer), 'alias' => 'manufacturer', 'id_country' => Configuration::get('PS_COUNTRY_DEFAULT'));
     $this->initToolbar();
     $this->fields_form[0]['form'] = $form;
     $this->getlanguages();
     $helper = new HelperForm();
     $helper->currentIndex = self::$currentIndex;
     $helper->token = $this->token;
     $helper->table = $this->table;
     $helper->identifier = $this->identifier;
     $helper->title = $this->l('Edit Addresses');
     $helper->id = $address->id;
     $helper->toolbar_scroll = true;
     $helper->languages = $this->_languages;
     $helper->default_form_language = $this->default_form_language;
     $helper->allow_employee_form_lang = $this->allow_employee_form_lang;
     $helper->fields_value = $this->getFieldsValue($address);
     $helper->toolbar_btn = $this->toolbar_btn;
     $this->content .= $helper->generateForm($this->fields_form);
 }