public function postProcess() { $id_shops = array(); if (Tools::isSubmit('mass_csv_form_submit')) { if (Shop::getContext() == Shop::CONTEXT_ALL) { $shops = Shop::getShops(); if (!empty($shops)) { foreach ($shops as $s) { $id_shops[] = $s['id_shop']; } } } elseif (Shop::getContext() == Shop::CONTEXT_GROUP) { $shopid = Shop::getContextShopGroupID(true); $shops = ShopGroup::getShopsFromGroup($shopid); if (!empty($shops)) { foreach ($shops as $s) { $id_shops[] = $s['id_shop']; } } } if (empty($id_shops)) { $id_shops[] = Shop::getContextShopID(); } $id_shops = array_unique($id_shops); if (is_uploaded_file($_FILES['csv_file']['tmp_name'])) { $mimes = array('application/vnd.ms-excel', 'text/plain', 'text/csv', 'text/tsv'); if (!in_array($_FILES['csv_file']['type'], $mimes)) { $this->errors[] = Tools::displayError($this->l('Error : Problem with file upload. Please retry or check your file.')); return false; } if (move_uploaded_file($_FILES['csv_file']['tmp_name'], $this->mod->cache_dir . $_FILES['csv_file']['name'])) { $old_file_name = $_FILES['csv_file']['name']; $csv_content = Tools::file_get_contents($this->mod->cache_dir . $old_file_name); $array = $this->csvToArray($csv_content); if (empty($array)) { $this->errors[] = Tools::displayError($this->l('Error : Problem with file upload. Please retry or check your file.')); parent::postProcess(); return false; } foreach ($array as $value) { if (!empty($value[0])) { $new = !empty($value[1]) ? trim($value[1]) : '/'; $type = !empty($value[2]) ? trim($value[2]) : '301'; $regex = !empty($value[3]) ? (int) $value[3] : 0; $redirect = new Redirect(); $redirect->old = trim($value[0]); $redirect->new = $new; $redirect->type = $type; $redirect->regex = (bool) $regex; $redirect->active = true; $redirect->add(); /*foreach ($id_shops as $v) { $db->execute('INSERT INTO `'._DB_PREFIX_.'redirect_shop` (`id_redirect_shop`,`id_redirect`, `id_shop`) VALUES (NULL,'.(int)$redirect->id.','.(int)$v.')'); }*/ } } } Tools::redirectAdmin(self::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminRedirect') . '&conf=4'); } } parent::postProcess(); }
public function insertRedirects($redirects, $force_active = false) { $db = Db::getInstance(); $active = (bool) ($force_active === true || (bool) Configuration::get('MGRT_AUTOACTIVE') === true); foreach ($redirects as $redirect) { if (empty($redirect['shops'])) { continue; } // Checking potential conflicts foreach ($redirect['shops'] as $id_shop) { $old = '/' . ltrim($redirect['old'], '/'); $new = isset($redirect['new']) ? '/' . ltrim($redirect['new'], '/') : '/'; $this->checkConflict($old, $id_shop); $id_redirect = $db->getValue('SELECT * FROM ' . _DB_PREFIX_ . 'redirect r WHERE r.regex = 0 AND r.old = "' . pSQL($old) . '" AND r.new = "' . pSQL($new) . '"'); //Making new if don't already exist if (empty($id_redirect)) { // Let's log it $obj = new Redirect(); $obj->old = $old; $obj->new = $new; $obj->type = '301'; $obj->regex = false; $obj->active = $active; $obj->add(); $id_redirect = $obj->id; } $db->execute('INSERT INTO `' . _DB_PREFIX_ . 'redirect_shop` (`id_redirect_shop`, `id_redirect`, `id_shop`) VALUES (NULL, ' . (int) $id_redirect . ', ' . (int) $id_shop . ')'); } } }