/** * Add sql params for shops fields - specific to StockAvailable * * @param array $params Reference to the params array * @param int $id_shop Optional : The shop ID * */ public static function addSqlShopParams(&$params, $id_shop = null) { $context = Context::getContext(); $group_ok = false; // if there is no $id_shop, gets the context one // get shop group too if ($id_shop === null) { if (Shop::getContext() == Shop::CONTEXT_GROUP) { $shop_group = Shop::getContextShopGroup(); } else { $shop_group = $context->shop->getGroup(); $id_shop = $context->shop->id; } } else { $shop = new Shop($id_shop); $shop_group = $shop->getGroup(); } // if quantities are shared between shops of the group if ($shop_group->share_stock) { $params['id_shop_group'] = (int) $shop_group->id; $params['id_shop'] = 0; $group_ok = true; } else { $params['id_shop_group'] = 0; } // if no group specific restriction, set simple shop restriction if (!$group_ok) { $params['id_shop'] = (int) $id_shop; } }
public function customerImport() { $customer_exist = false; $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(); 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']) { $customer = new Customer((int) $info['id']); } else { if (array_key_exists('id', $info) && (int) $info['id'] && Customer::customerIdExistsStatic((int) $info['id'])) { $customer = new Customer((int) $info['id']); } else { $customer = new Customer(); } } if (array_key_exists('id', $info) && (int) $info['id'] && Customer::customerIdExistsStatic((int) $info['id'])) { $current_id_customer = $customer->id; $current_id_shop = $customer->id_shop; $current_id_shop_group = $customer->id_shop_group; $customer_exist = true; $customer_groups = $customer->getGroups(); $addresses = $customer->getAddresses((int) Configuration::get('PS_LANG_DEFAULT')); } // Group Importation if (isset($info['group']) && !empty($info['group'])) { foreach (explode($this->multiple_value_separator, $info['group']) as $key => $group) { $group = trim($group); if (empty($group)) { continue; } $id_group = false; if (is_numeric($group) && $group) { $my_group = new Group((int) $group); if (Validate::isLoadedObject($my_group)) { $customer_groups[] = (int) $group; } continue; } $my_group = Group::searchByName($group); if (isset($my_group['id_group']) && $my_group['id_group']) { $id_group = (int) $my_group['id_group']; } if (!$id_group) { $my_group = new Group(); $my_group->name = array($id_lang => $group); if ($id_lang != $default_language_id) { $my_group->name = $my_group->name + array($default_language_id => $group); } $my_group->price_display_method = 1; $my_group->add(); if (Validate::isLoadedObject($my_group)) { $id_group = (int) $my_group->id; } } if ($id_group) { $customer_groups[] = (int) $id_group; } } } elseif (empty($info['group']) && isset($customer->id) && $customer->id) { $customer_groups = array(0 => Configuration::get('PS_CUSTOMER_GROUP')); } AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $customer); if ($customer->passwd) { $customer->passwd = Tools::encrypt($customer->passwd); } $id_shop_list = explode($this->multiple_value_separator, $customer->id_shop); $customers_shop = array(); $customers_shop['shared'] = array(); $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); if (Shop::isFeatureActive() && $id_shop_list) { foreach ($id_shop_list as $id_shop) { if (empty($id_shop)) { continue; } $shop = new Shop((int) $id_shop); $group_shop = $shop->getGroup(); if ($group_shop->share_customer) { if (!in_array($group_shop->id, $customers_shop['shared'])) { $customers_shop['shared'][(int) $id_shop] = $group_shop->id; } } else { $customers_shop[(int) $id_shop] = $group_shop->id; } } } else { $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); $default_shop->getGroup(); $customers_shop[$default_shop->id] = $default_shop->getGroup()->id; } //set temporally for validate field $customer->id_shop = $default_shop->id; $customer->id_shop_group = $default_shop->getGroup()->id; if (isset($info['id_default_group']) && !empty($info['id_default_group']) && !is_numeric($info['id_default_group'])) { $info['id_default_group'] = trim($info['id_default_group']); $my_group = Group::searchByName($info['id_default_group']); if (isset($my_group['id_group']) && $my_group['id_group']) { $info['id_default_group'] = (int) $my_group['id_group']; } } $my_group = new Group($customer->id_default_group); if (!Validate::isLoadedObject($my_group)) { $customer->id_default_group = (int) Configuration::get('PS_CUSTOMER_GROUP'); } $customer_groups[] = (int) $customer->id_default_group; $customer_groups = array_flip(array_flip($customer_groups)); $res = true; if (($field_error = $customer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $customer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) { foreach ($customers_shop as $id_shop => $id_group) { $customer->force_id = (bool) Tools::getValue('forceIDs'); if ($id_shop == 'shared') { foreach ($id_group as $key => $id) { $customer->id_shop = (int) $key; $customer->id_shop_group = (int) $id; if ($customer_exist && ($current_id_shop_group == $id || in_array($current_id_shop, ShopGroup::getShopsFromGroup($id)))) { $customer->id = $current_id_customer; $res &= $customer->update(); } else { $res &= $customer->add(); if (isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address); } } } if ($res && isset($customer_groups)) { $customer->updateGroup($customer_groups); } } } else { $customer->id_shop = $id_shop; $customer->id_shop_group = $id_group; if ($customer_exist && $id_shop == $current_id_shop) { $customer->id = $current_id_customer; $res &= $customer->update(); } else { $res &= $customer->add(); if (isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address); } } } if ($res && isset($customer_groups)) { $customer->updateGroup($customer_groups); } } } } unset($customer_groups); $customer_exist = false; if (!$res) { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $info['email'], 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); }
protected function customerImportOne($info, $default_language_id, $id_lang, $shop_is_feature_active, $force_ids, $validateOnly = false) { AdminImportController::setDefaultValues($info); if ($force_ids && isset($info['id']) && (int) $info['id']) { $customer = new Customer((int) $info['id']); } else { if (array_key_exists('id', $info) && (int) $info['id'] && Customer::customerIdExistsStatic((int) $info['id'])) { $customer = new Customer((int) $info['id']); } else { $customer = new Customer(); } } $customer_exist = false; $autodate = true; if (array_key_exists('id', $info) && (int) $info['id'] && Customer::customerIdExistsStatic((int) $info['id']) && Validate::isLoadedObject($customer)) { $current_id_customer = (int) $customer->id; $current_id_shop = (int) $customer->id_shop; $current_id_shop_group = (int) $customer->id_shop_group; $customer_exist = true; $customer_groups = $customer->getGroups(); $addresses = $customer->getAddresses((int) Configuration::get('PS_LANG_DEFAULT')); } // Group Importation if (isset($info['group']) && !empty($info['group'])) { foreach (explode($this->multiple_value_separator, $info['group']) as $key => $group) { $group = trim($group); if (empty($group)) { continue; } $id_group = false; if (is_numeric($group) && $group) { $my_group = new Group((int) $group); if (Validate::isLoadedObject($my_group)) { $customer_groups[] = (int) $group; } continue; } $my_group = Group::searchByName($group); if (isset($my_group['id_group']) && $my_group['id_group']) { $id_group = (int) $my_group['id_group']; } if (!$id_group) { $my_group = new Group(); $my_group->name = array($id_lang => $group); if ($id_lang != $default_language_id) { $my_group->name = $my_group->name + array($default_language_id => $group); } $my_group->price_display_method = 1; if (!$validateOnly) { $my_group->add(); if (Validate::isLoadedObject($my_group)) { $id_group = (int) $my_group->id; } } } if ($id_group) { $customer_groups[] = (int) $id_group; } } } elseif (empty($info['group']) && isset($customer->id) && $customer->id) { $customer_groups = array(0 => Configuration::get('PS_CUSTOMER_GROUP')); } if (isset($info['date_add']) && !empty($info['date_add'])) { $autodate = false; } AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $customer); if ($customer->passwd) { $customer->passwd = Tools::encrypt($customer->passwd); } $id_shop_list = explode($this->multiple_value_separator, $customer->id_shop); $customers_shop = array(); $customers_shop['shared'] = array(); $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); if ($shop_is_feature_active && $id_shop_list) { foreach ($id_shop_list as $id_shop) { if (empty($id_shop)) { continue; } $shop = new Shop((int) $id_shop); $group_shop = $shop->getGroup(); if ($group_shop->share_customer) { if (!in_array($group_shop->id, $customers_shop['shared'])) { $customers_shop['shared'][(int) $id_shop] = $group_shop->id; } } else { $customers_shop[(int) $id_shop] = $group_shop->id; } } } else { $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); $default_shop->getGroup(); $customers_shop[$default_shop->id] = $default_shop->getGroup()->id; } //set temporary for validate field $customer->id_shop = $default_shop->id; $customer->id_shop_group = $default_shop->getGroup()->id; if (isset($info['id_default_group']) && !empty($info['id_default_group']) && !is_numeric($info['id_default_group'])) { $info['id_default_group'] = trim($info['id_default_group']); $my_group = Group::searchByName($info['id_default_group']); if (isset($my_group['id_group']) && $my_group['id_group']) { $info['id_default_group'] = (int) $my_group['id_group']; } } $my_group = new Group($customer->id_default_group); if (!Validate::isLoadedObject($my_group)) { $customer->id_default_group = (int) Configuration::get('PS_CUSTOMER_GROUP'); } $customer_groups[] = (int) $customer->id_default_group; $customer_groups = array_flip(array_flip($customer_groups)); // Bug when updating existing user that were csv-imported before... if (isset($customer->date_upd) && $customer->date_upd == '0000-00-00 00:00:00') { $customer->date_upd = date('Y-m-d H:i:s'); } $res = false; if (($field_error = $customer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $customer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) { $res = true; foreach ($customers_shop as $id_shop => $id_group) { $customer->force_id = (bool) $force_ids; if ($id_shop == 'shared') { foreach ($id_group as $key => $id) { $customer->id_shop = (int) $key; $customer->id_shop_group = (int) $id; if ($customer_exist && ((int) $current_id_shop_group == (int) $id || in_array($current_id_shop, ShopGroup::getShopsFromGroup($id)))) { $customer->id = (int) $current_id_customer; $res &= $validateOnly || $customer->update(); } else { $res &= $validateOnly || $customer->add($autodate); if (!$validateOnly && isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address, false, false); } } } if ($res && !$validateOnly && isset($customer_groups)) { $customer->updateGroup($customer_groups); } } } else { $customer->id_shop = $id_shop; $customer->id_shop_group = $id_group; if ($customer_exist && (int) $id_shop == (int) $current_id_shop) { $customer->id = (int) $current_id_customer; $res &= $validateOnly || $customer->update(); } else { $res &= $validateOnly || $customer->add($autodate); if (!$validateOnly && isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address, false, false); } } } if ($res && !$validateOnly && isset($customer_groups)) { $customer->updateGroup($customer_groups); } } } } if (isset($customer_groups)) { unset($customer_groups); } if (isset($current_id_customer)) { unset($current_id_customer); } if (isset($current_id_shop)) { unset($current_id_shop); } if (isset($current_id_shop_group)) { unset($current_id_shop_group); } if (isset($addresses)) { unset($addresses); } if (!$res) { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be ' . ($validateOnly ? 'validated' : 'saved')), $info['email'], 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(); } }
public function customerImport() { $customer_exist = false; $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 (array_key_exists('id', $info) && (int) $info['id'] && Customer::customerIdExistsStatic((int) $info['id'])) { $customer = new Customer((int) $info['id']); $current_id_customer = $customer->id; $current_id_shop = $customer->id_shop; $current_id_shop_group = $customer->id_shop_group; $customer_exist = true; $customer_groups = $customer->getGroups(); $addresses = $customer->getAddresses((int) Configuration::get('PS_LANG_DEFAULT')); foreach ($customer_groups as $key => $group) { if ($group == $customer->id_default_group) { unset($customer_groups[$key]); } } } else { $customer = new Customer(); } AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $customer); if ($customer->passwd) { $customer->passwd = Tools::encrypt($customer->passwd); } $id_shop_list = explode($this->multiple_value_separator, $customer->id_shop); $customers_shop = array(); $customers_shop['shared'] = array(); $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); if (Shop::isFeatureActive() && $id_shop_list) { foreach ($id_shop_list as $id_shop) { $shop = new Shop((int) $id_shop); $group_shop = $shop->getGroup(); if ($group_shop->share_customer) { if (!in_array($group_shop->id, $customers_shop['shared'])) { $customers_shop['shared'][(int) $id_shop] = $group_shop->id; } } else { $customers_shop[(int) $id_shop] = $group_shop->id; } } } else { $default_shop = new Shop((int) Configuration::get('PS_SHOP_DEFAULT')); $default_shop->getGroup(); $customers_shop[$default_shop->id] = $default_shop->getGroup()->id; } //set temporally for validate field $customer->id_shop = $default_shop->id; $customer->id_shop_group = $default_shop->getGroup()->id; $res = true; if (($field_error = $customer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $customer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true) { foreach ($customers_shop as $id_shop => $id_group) { if ($id_shop == 'shared') { foreach ($id_group as $key => $id) { $customer->id_shop = (int) $key; $customer->id_shop_group = (int) $id; if ($customer_exist && ($current_id_shop_group == $id || in_array($current_id_shop, ShopGroup::getShopsFromGroup($id)))) { $customer->id = $current_id_customer; $res &= $customer->update(); } else { unset($customer->id); $res &= $customer->add(); if (isset($customer_groups)) { $customer->addGroups($customer_groups); } if (isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address); } } } } } else { $customer->id_shop = $id_shop; $customer->id_shop_group = $id_group; if ($customer_exist && $id_shop == $current_id_shop) { $customer->id = $current_id_customer; $res &= $customer->update(); } else { unset($customer->id); $res &= $customer->add(); if (isset($customer_groups)) { $customer->addGroups($customer_groups); } if (isset($addresses)) { foreach ($addresses as $address) { $address['id_customer'] = $customer->id; unset($address['country'], $address['state'], $address['state_iso'], $address['id_address']); Db::getInstance()->insert('address', $address); } } } } } } $customer_exist = false; if (!$res) { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $info['email'], isset($info['id']) ? $info['id'] : 'null'); $this->errors[] = ($field_error !== true ? $field_error : '') . ($lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError(); } } $this->closeCsvFile($handle); }