Система регионов

This commit is contained in:
Konstantin
2026-05-30 15:15:57 +03:00
parent 7cd87e901f
commit e843f3019d
18 changed files with 293 additions and 54 deletions
@@ -0,0 +1,10 @@
ALTER TABLE `oc_zone`
ADD `slug` VARCHAR(64) NOT NULL DEFAULT '' AFTER `code`;
UPDATE `oc_zone` SET `slug` = 'brest' WHERE `zone_id` = 337;
UPDATE `oc_zone` SET `slug` = 'gomel' WHERE `zone_id` = 338;
UPDATE `oc_zone` SET `slug` = 'minsk' WHERE `zone_id` = 339;
UPDATE `oc_zone` SET `slug` = 'grodno' WHERE `zone_id` = 340;
UPDATE `oc_zone` SET `slug` = 'moskva' WHERE `zone_id` = 2761;
UPDATE `oc_zone` SET `slug` = 'sankt-peterburg' WHERE `zone_id` = 4239;
UPDATE `oc_zone` SET `slug` = 'bryansk' WHERE `zone_id` = 4240;
@@ -177,6 +177,7 @@ class ControllerLocalisationZone extends Controller {
'country' => $result['country'],
'name' => $result['name'] . (($result['zone_id'] == $this->config->get('config_zone_id')) ? $this->language->get('text_default') : null),
'code' => $result['code'],
'slug' => $result['slug'],
'edit' => $this->url->link('localisation/zone/edit', 'user_token=' . $this->session->data['user_token'] . '&zone_id=' . $result['zone_id'] . $url, true)
);
}
@@ -216,6 +217,7 @@ class ControllerLocalisationZone extends Controller {
$data['sort_country'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=c.name' . $url, true);
$data['sort_name'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=z.name' . $url, true);
$data['sort_code'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=z.code' . $url, true);
$data['sort_slug'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . '&sort=z.slug' . $url, true);
$url = '';
@@ -262,6 +264,12 @@ class ControllerLocalisationZone extends Controller {
$data['error_name'] = '';
}
if (isset($this->error['slug'])) {
$data['error_slug'] = $this->error['slug'];
} else {
$data['error_slug'] = '';
}
$url = '';
if (isset($this->request->get['sort'])) {
@@ -324,6 +332,14 @@ class ControllerLocalisationZone extends Controller {
$data['code'] = '';
}
if (isset($this->request->post['slug'])) {
$data['slug'] = $this->request->post['slug'];
} elseif (!empty($zone_info)) {
$data['slug'] = $zone_info['slug'];
} else {
$data['slug'] = '';
}
if (isset($this->request->post['country_id'])) {
$data['country_id'] = $this->request->post['country_id'];
} elseif (!empty($zone_info)) {
@@ -352,6 +368,16 @@ class ControllerLocalisationZone extends Controller {
$this->error['name'] = $this->language->get('error_name');
}
$this->request->post['slug'] = strtolower(trim((string)$this->request->post['slug']));
$is_default = isset($this->request->get['zone_id']) && ((int)$this->request->get['zone_id'] == (int)$this->config->get('config_zone_id'));
if ((!$is_default && $this->request->post['slug'] === '') || ($this->request->post['slug'] !== '' && !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $this->request->post['slug']))) {
$this->error['slug'] = $this->language->get('error_slug');
} elseif ($this->request->post['slug'] !== '' && $this->model_localisation_zone->getTotalZonesBySlug($this->request->post['slug'], isset($this->request->get['zone_id']) ? $this->request->get['zone_id'] : 0)) {
$this->error['slug'] = $this->language->get('error_slug_exists');
}
return !$this->error;
}
@@ -608,6 +608,14 @@ class ControllerSettingStore extends Controller {
$data['config_logo'] = '';
}
if (isset($this->request->post['config_logo_text'])) {
$data['config_logo_text'] = $this->request->post['config_logo_text'];
} elseif (isset($store_info['config_logo_text'])) {
$data['config_logo_text'] = $store_info['config_logo_text'];
} else {
$data['config_logo_text'] = '';
}
if (isset($this->request->post['config_logo']) && is_file(DIR_IMAGE . $this->request->post['config_logo'])) {
$data['logo'] = $this->model_tool_image->resize($this->request->post['config_logo'], 100, 100);
} elseif (isset($store_info['config_logo']) && is_file(DIR_IMAGE . $store_info['config_logo'])) {
@@ -14,18 +14,22 @@ $_['text_edit'] = 'Редактирование';
// Column
$_['column_name'] = 'Название региона';
$_['column_code'] = 'Код региона';
$_['column_slug'] = 'Slug';
$_['column_country'] = 'Страна';
$_['column_action'] = 'Действие';
// Entry
$_['entry_name'] = 'Название региона';
$_['entry_code'] = 'Код региона';
$_['entry_slug'] = 'Slug для URL';
$_['entry_country'] = 'Страна';
$_['entry_status'] = 'Статус';
// Error
$_['error_permission'] = 'У вас недостаточно прав для внесения изменений!';
$_['error_name'] = 'Название должно содержать от 3 до 128 символов!';
$_['error_slug'] = 'Slug обязателен для всех регионов, кроме региона по умолчанию, и может содержать только строчные латинские буквы, цифры и дефисы!';
$_['error_slug_exists'] = 'Такой slug уже используется другим регионом!';
$_['error_default'] = 'Этот Регион нельзя удалить, поскольку он используется по умолчанию!';
$_['error_store'] = 'Этот Регион нельзя удалить, поскольку он используется в %s магазинах!';
$_['error_address'] = 'Этот Регион нельзя удалить, поскольку он используется в %s адресах!';
@@ -63,6 +63,7 @@ $_['entry_order_status'] = 'Статус заказа';
$_['entry_stock_display'] = 'Показывать остаток на складе';
$_['entry_stock_checkout'] = 'Заказ при нехватке на складе';
$_['entry_logo'] = 'Логотип';
$_['entry_logo_text'] = 'Текстовый логотип';
$_['entry_icon'] = 'Иконка';
$_['entry_image_category'] = 'Размер изображений категории';
$_['entry_image_thumb'] = 'Размер большого изображения товара';
+10 -3
View File
@@ -1,7 +1,7 @@
<?php
class ModelLocalisationZone extends Model {
public function addZone($data) {
$this->db->query("INSERT INTO " . DB_PREFIX . "zone SET status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', country_id = '" . (int)$data['country_id'] . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "zone SET status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', slug = '" . $this->db->escape($data['slug']) . "', country_id = '" . (int)$data['country_id'] . "'");
$this->cache->delete('zone');
@@ -9,7 +9,7 @@ class ModelLocalisationZone extends Model {
}
public function editZone($zone_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "zone SET status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', country_id = '" . (int)$data['country_id'] . "' WHERE zone_id = '" . (int)$zone_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "zone SET status = '" . (int)$data['status'] . "', name = '" . $this->db->escape($data['name']) . "', code = '" . $this->db->escape($data['code']) . "', slug = '" . $this->db->escape($data['slug']) . "', country_id = '" . (int)$data['country_id'] . "' WHERE zone_id = '" . (int)$zone_id . "'");
$this->cache->delete('zone');
}
@@ -32,7 +32,8 @@ class ModelLocalisationZone extends Model {
$sort_data = array(
'c.name',
'z.name',
'z.code'
'z.code',
'z.slug'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
@@ -89,4 +90,10 @@ class ModelLocalisationZone extends Model {
return $query->row['total'];
}
public function getTotalZonesBySlug($slug, $zone_id = 0) {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "zone WHERE slug = '" . $this->db->escape($slug) . "' AND zone_id != '" . (int)$zone_id . "'");
return $query->row['total'];
}
}
@@ -40,6 +40,15 @@
<input type="text" name="code" value="{{ code }}" placeholder="{{ entry_code }}" id="input-code" class="form-control" />
</div>
</div>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-slug">{{ entry_slug }}</label>
<div class="col-sm-10">
<input type="text" name="slug" value="{{ slug }}" placeholder="{{ entry_slug }}" id="input-slug" class="form-control" />
{% if error_slug %}
<div class="text-danger">{{ error_slug }}</div>
{% endif %}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-country">{{ entry_country }}</label>
<div class="col-sm-10">
@@ -50,6 +50,11 @@
{% else %}
<a href="{{ sort_code }}">{{ column_code }}</a>
{% endif %}</td>
<td class="text-left">{% if sort == 'z.slug' %}
<a href="{{ sort_slug }}" class="{{ order|lower }}">{{ column_slug }}</a>
{% else %}
<a href="{{ sort_slug }}">{{ column_slug }}</a>
{% endif %}</td>
<td class="text-right">{{ column_action }}</td>
</tr>
</thead>
@@ -65,12 +70,13 @@
<td class="text-left">{{ zone.country }}</td>
<td class="text-left">{{ zone.name }}</td>
<td class="text-left">{{ zone.code }}</td>
<td class="text-left">{{ zone.slug }}</td>
<td class="text-right"><a href="{{ zone.edit }}" data-toggle="tooltip" title="{{ button_edit }}" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td class="text-center" colspan="5">{{ text_no_results }}</td>
<td class="text-center" colspan="6">{{ text_no_results }}</td>
</tr>
{% endif %}
</tbody>
+17 -6
View File
@@ -19,7 +19,7 @@ class ControllerCommonCity extends Controller {
);
}
$data['action'] = $this->url->link('common/city/save');
$data['action'] = $this->url->link('common/city/save', '', true);
$this->response->setOutput($this->load->view('common/city_list', $data));
}
@@ -28,12 +28,23 @@ class ControllerCommonCity extends Controller {
$json = array();
if (isset($this->request->post['city_id'])) {
$this->session->data['city_id'] = (int)$this->request->post['city_id'];
$json['success'] = true;
$this->load->model('localisation/zone');
$zone = $this->model_localisation_zone->getZone($this->session->data['city_id']);
$json['name'] = $zone ? $zone['name'] : '';
$zone = $this->model_localisation_zone->getZone((int)$this->request->post['city_id']);
if (!$zone || (int)$zone['country_id'] !== (int)$this->config->get('config_country_id')) {
$json['success'] = false;
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
return;
}
$this->session->data['city_id'] = (int)$zone['zone_id'];
$json['success'] = true;
$json['name'] = $zone['name'];
$json['redirect'] = $this->model_localisation_zone->getZoneUrl($zone['zone_id'], isset($this->request->post['redirect']) ? $this->request->post['redirect'] : $this->url->link('common/home'));
} else {
$json['success'] = false;
}
+1 -1
View File
@@ -118,7 +118,7 @@ foreach(['name','email','telephone','address','open', 'comment'] as $item){
}
}
$data['city_list_url'] = $this->url->link('common/city');
$data['city_list_url'] = $this->url->link('common/city', '', true);
return $this->load->view('common/header', $data);
}
@@ -43,6 +43,7 @@ class ControllerProductManufacturer extends Controller {
}
$data['continue'] = $this->url->link('common/home');
$data['manufacturer'] = $this->url->link('product/manufacturer');
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
+105 -9
View File
@@ -3,6 +3,7 @@
// * @license GNU General Public License version 3; see LICENSE.txt
class ControllerStartupSeoUrl extends Controller {
private $region_slug = '';
//seopro start
private $seo_pro;
@@ -13,17 +14,12 @@ class ControllerStartupSeoUrl extends Controller {
//seopro end
public function index() {
// Add rewrite to url class
if ($this->config->get('config_seo_url')) {
$this->url->addRewrite($this);
}
// Decode URL
if (isset($this->request->get['_route_'])) {
$parts = explode('/', $this->request->get['_route_']);
$this->prepareRegion($parts);
//seopro prepare route
if($this->config->get('config_seo_pro')){
$parts = $this->seo_pro->prepareRoute($parts);
@@ -31,7 +27,7 @@ class ControllerStartupSeoUrl extends Controller {
//seopro prepare route end
// remove any empty arrays from trailing
if (utf8_strlen(end($parts)) == 0) {
if ($parts && utf8_strlen(end($parts)) == 0) {
array_pop($parts);
}
@@ -86,6 +82,15 @@ class ControllerStartupSeoUrl extends Controller {
}
}
if (!isset($this->request->get['_route_']) && parse_url($this->request->server['REQUEST_URI'], PHP_URL_PATH) === '/') {
$this->setRegion((int)$this->config->get('config_zone_id'));
} elseif ($this->region_slug === '') {
$this->loadRegionFromSession();
}
// Add rewrite to url class
$this->url->addRewrite($this);
//seopro validate
if($this->config->get('config_seo_pro')){
$this->seo_pro->validate();
@@ -95,6 +100,10 @@ class ControllerStartupSeoUrl extends Controller {
}
public function rewrite($link) {
if (!$this->config->get('config_seo_url')) {
return $this->addRegionPrefix($link);
}
$url_info = parse_url(str_replace('&amp;', '&', $link));
if($this->config->get('config_seo_pro')){
@@ -177,9 +186,96 @@ class ControllerStartupSeoUrl extends Controller {
}
}
return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
return $this->addRegionPrefix($url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query);
} else {
return $this->addRegionPrefix($link);
}
}
private function prepareRegion(&$parts) {
$parts = array_values(array_filter($parts, 'strlen'));
$zone = array();
if (isset($parts[0])) {
$this->load->model('localisation/zone');
$zone = $this->model_localisation_zone->getZoneBySlug($parts[0], $this->config->get('config_country_id'));
}
if ($zone) {
array_shift($parts);
$this->setRegion((int)$zone['zone_id'], $zone['slug']);
} else {
$this->setRegion((int)$this->config->get('config_zone_id'));
}
if ($parts) {
$this->request->get['_route_'] = implode('/', $parts);
} else {
unset($this->request->get['_route_']);
}
}
private function loadRegionFromSession() {
if (!isset($this->session->data['city_id'])) {
return;
}
$this->load->model('localisation/zone');
$zone = $this->model_localisation_zone->getZone($this->session->data['city_id']);
if ($zone && (int)$zone['country_id'] === (int)$this->config->get('config_country_id')) {
$this->setRegion((int)$zone['zone_id'], $zone['slug']);
}
}
private function setRegion($zone_id, $slug = '') {
$this->session->data['city_id'] = (int)$zone_id;
$this->region_slug = ((int)$zone_id === (int)$this->config->get('config_zone_id')) ? '' : $slug;
}
private function addRegionPrefix($link) {
if ($this->region_slug === '') {
return $link;
}
$url_info = parse_url(str_replace('&amp;', '&', $link));
if (!$url_info || !isset($url_info['scheme']) || !isset($url_info['host'])) {
return $link;
}
$path = isset($url_info['path']) ? preg_replace('#/index\.php$#', '', $url_info['path']) : '';
$base_path = parse_url($this->config->get('config_url'), PHP_URL_PATH);
$base_path = '/' . trim((string)$base_path, '/');
if ($base_path === '/') {
$base_path = '';
}
if ($base_path !== '' && ($path === $base_path || strpos($path, $base_path . '/') === 0)) {
$relative_path = substr($path, strlen($base_path));
} else {
$relative_path = $path;
}
$parts = array_values(array_filter(explode('/', trim($relative_path, '/')), 'strlen'));
if (!isset($parts[0]) || $parts[0] !== $this->region_slug) {
array_unshift($parts, $this->region_slug);
}
$link = $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . $base_path . '/' . implode('/', $parts);
if (isset($url_info['query'])) {
$link .= '?' . str_replace('&', '&amp;', $url_info['query']);
}
if (isset($url_info['fragment'])) {
$link .= '#' . $url_info['fragment'];
}
return $link;
}
}
+51
View File
@@ -19,4 +19,55 @@ class ModelLocalisationZone extends Model {
return $zone_data;
}
public function getZoneBySlug($slug, $country_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE slug = '" . $this->db->escape($slug) . "' AND country_id = '" . (int)$country_id . "' AND status = '1' LIMIT 1");
return $query->row;
}
public function getZoneUrl($zone_id, $url) {
$zone = $this->getZone($zone_id);
if (!$zone) {
return $url;
}
$url_info = parse_url(str_replace('&amp;', '&', $url));
$secure = isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] === 'on') || ($this->request->server['HTTPS'] === '1'));
$store_url = $secure ? $this->config->get('config_ssl') : $this->config->get('config_url');
$store_info = parse_url($store_url);
if (!$url_info || !$store_info || !isset($store_info['scheme']) || !isset($store_info['host'])) {
return $store_url;
}
$parts = array_values(array_filter(explode('/', trim(isset($url_info['path']) ? $url_info['path'] : '', '/')), 'strlen'));
$base_parts = array_values(array_filter(explode('/', trim(isset($store_info['path']) ? $store_info['path'] : '', '/')), 'strlen'));
if ($base_parts && array_slice($parts, 0, count($base_parts)) === $base_parts) {
$parts = array_slice($parts, count($base_parts));
}
$zones = $this->getZonesByCountryId($zone['country_id']);
foreach ($zones as $item) {
if (!empty($item['slug']) && isset($parts[0]) && $parts[0] === $item['slug']) {
array_shift($parts);
break;
}
}
if ((int)$zone['zone_id'] !== (int)$this->config->get('config_zone_id') && $zone['slug'] !== '') {
array_unshift($parts, $zone['slug']);
}
$redirect = $store_info['scheme'] . '://' . $store_info['host'] . (isset($store_info['port']) ? ':' . $store_info['port'] : '') . '/' . implode('/', array_merge($base_parts, $parts));
if (isset($url_info['query'])) {
$redirect .= '?' . $url_info['query'];
}
return $redirect;
}
}
@@ -46,6 +46,13 @@ a:hover path{
font-size: 30px;
}
.logo img {
display: block;
width: auto;
height: auto;
max-width: 100%;
max-height: 48px;
}
header .menu {
margin: 0;
@@ -106,6 +113,9 @@ header .menu>li:last-child {
font-size: 20px;
}
.logo img {
max-height: 32px;
}
}
.btn {
text-transform: uppercase;
@@ -457,11 +457,10 @@ function citySelect(el){
$('.city-item').click(function(e){
e.preventDefault();
var cityId = $(this).data('city-id');
var cityName = $(this).text();
send.post('index.php?route=common/city/save', {city_id: cityId}, function(json){
var saveUrl = $('.city-grid').data('save-url');
send.post(saveUrl, {city_id: cityId, redirect: window.location.href}, function(json){
if (json.success) {
$('.current-city-name').text(json.name);
$('.modal').modal('hide');
window.location.href = json.redirect;
}
});
});
@@ -20,7 +20,7 @@
</div>
{% else %}
<p>{{ text_empty }}</p>
<a href="/" class="btn btn-dark">{{ button_continue }}</a>
<a href="{{ continue }}" class="btn btn-dark">{{ button_continue }}</a>
{% endif %}
</div>
@@ -1,4 +1,4 @@
<div class="city-grid">
<div class="city-grid" data-save-url="{{ action }}">
{% for zone in zones %}
<button type="button" class="city-item{% if zone.active %} active{% endif %}" data-city-id="{{ zone.zone_id }}">{{ zone.name }}</button>
{% endfor %}
@@ -10,7 +10,7 @@
<h1>{{ heading_title }}</h1>
{% if categories %}
<p><strong>{{ text_index }}</strong> {% for category in categories %}
&nbsp;&nbsp;&nbsp;<a href="index.php?route=product/manufacturer#{{ category.name }}">{{ category.name }}</a> {% endfor %} </p>
&nbsp;&nbsp;&nbsp;<a href="{{ manufacturer }}#{{ category.name }}">{{ category.name }}</a> {% endfor %} </p>
{% for category in categories %}
<h2 id="{{ category.name }}">{{ category.name }}</h2>
{% if category.manufacturer %}
@@ -53,7 +53,7 @@
<h1>{{ heading_title }}</h1>
{% if categories %}
<p><strong>{{ text_index }}</strong> {% for category in categories %}
&nbsp;&nbsp;&nbsp;<a href="index.php?route=product/manufacturer#{{ category.name }}">{{ category.name }}</a> {% endfor %} </p>
&nbsp;&nbsp;&nbsp;<a href="{{ manufacturer }}#{{ category.name }}">{{ category.name }}</a> {% endfor %} </p>
{% for category in categories %}
<h2 id="{{ category.name }}">{{ category.name }}</h2>
{% if category.manufacturer %}