Skip to content

Commit

Permalink
возможность смены URL компонентов из админки,
Browse files Browse the repository at this point in the history
опция конфигурации controllers_without_widgets,
close #618 close #616
  • Loading branch information
fuzegit committed Sep 25, 2017
1 parent 0684518 commit 78e3d09
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 36 deletions.
5 changes: 2 additions & 3 deletions install/languages/en/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ CREATE TABLE `{#}controllers` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL COMMENT 'System name',
`slug` varchar(64) DEFAULT NULL,
`is_enabled` tinyint(1) unsigned DEFAULT '1' COMMENT 'Enabled?',
`options` text COMMENT 'Settings array',
`author` varchar(128) NOT NULL COMMENT 'Author name',
Expand All @@ -239,9 +240,7 @@ CREATE TABLE `{#}controllers` (
`files` varchar(10000) DEFAULT NULL,
`addon_id` int(11) UNSIGNED DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `enabled` (`is_enabled`),
KEY `is_backend` (`is_backend`)
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Components';

INSERT INTO `{#}controllers` (`id`, `title`, `name`, `is_enabled`, `options`, `author`, `url`, `version`, `is_backend`) VALUES
Expand Down
5 changes: 2 additions & 3 deletions install/languages/ru/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ CREATE TABLE `{#}controllers` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL COMMENT 'Системное имя',
`slug` varchar(64) DEFAULT NULL,
`is_enabled` tinyint(1) unsigned DEFAULT '1' COMMENT 'Включен?',
`options` text COMMENT 'Массив настроек',
`author` varchar(128) NOT NULL COMMENT 'Имя автора',
Expand All @@ -239,9 +240,7 @@ CREATE TABLE `{#}controllers` (
`files` varchar(10000) DEFAULT NULL COMMENT 'Список файлов контроллера (для стороних компонентов)',
`addon_id` int(11) UNSIGNED DEFAULT NULL COMMENT 'ID дополнения в официальном каталоге',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `enabled` (`is_enabled`),
KEY `is_backend` (`is_backend`)
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Компоненты';

INSERT INTO `{#}controllers` (`id`, `title`, `name`, `is_enabled`, `options`, `author`, `url`, `version`, `is_backend`) VALUES
Expand Down
3 changes: 2 additions & 1 deletion install/steps/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ function create_config($path, $file){
'allow_ips' => '',
'default_editor' => 'redactor',
'show_breadcrumbs' => 1,
'check_spoofing_type' => 0
'check_spoofing_type' => 0,
'controllers_without_widgets' => array('admin')
);

write_config($file, $config);
Expand Down
7 changes: 6 additions & 1 deletion system/controllers/admin/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class admin extends cmsFrontend {
const addons_api_key = '8e13cb202f8bdc27dc765e0448e50d11';
const addons_api_point = 'http://addons.instantcms.ru/api/method/';

public $disallow_mapping_redirect = true;

protected $useOptions = true;

const perpage = 30;
Expand Down Expand Up @@ -275,7 +277,10 @@ public function loadControllerBackend($controller_name, $request){
$backend = new $controller_class($request);

// Устанавливаем корень для URL внутри бакенда
$backend->setRootURL($this->name.'/controllers/edit/'.$controller_name);
$admin_controller_url = $this->name;
$controller_alias = cmsCore::getControllerAliasByName($admin_controller_url);
if ($controller_alias) { $admin_controller_url = $controller_alias; }
$backend->setRootURL($admin_controller_url.'/controllers/edit/'.$controller_name);

return $backend;

Expand Down
14 changes: 14 additions & 0 deletions system/controllers/admin/grids/grid_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ function grid_controllers($controller){
return $item['is_backend'];
}
),
'slug' => array(
'title' => LANG_ADMIN_CONTROLLER_SLUG,
'width' => 300,
'editable' => array(
'table' => 'controllers',
'attributes' => array('placeholder' => '{name}')
),
'handler' => function ($v, $row){
if(!$v){
return $row['name'];
}
return $v;
}
),
'is_enabled' => array(
'title' => LANG_IS_ENABLED,
'flag' => true,
Expand Down
3 changes: 2 additions & 1 deletion system/controllers/content/actions/item_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public function run(){
}

$item['ctype_name'] = $ctype['name'];
$item['ctype_id'] = $ctype['id'];
$item['ctype_id'] = $ctype['id'];
$item['ctype_data'] = $ctype;

if ($is_submitted){

Expand Down
7 changes: 1 addition & 6 deletions system/controllers/content/actions/item_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function run(){
if (!$item) { cmsCore::error404(); }

$item['ctype_id'] = $ctype['id'];
$item['ctype_name'] = $ctype['name'];

// проверяем наличие доступа
if (!cmsUser::isAllowed($ctype['name'], 'edit')) { cmsCore::error404(); }
Expand Down Expand Up @@ -94,9 +95,6 @@ public function run(){
// Форма отправлена?
$is_submitted = $this->request->has('submit');

$item['ctype_name'] = $ctype['name'];
$item['ctype_id'] = $ctype['id'];

if ($ctype['props']){

$category_id = !$is_submitted ? $item['category_id'] :
Expand Down Expand Up @@ -144,9 +142,6 @@ public function run(){

if (!$errors){

unset($item['ctype_name']);
unset($item['ctype_id']);

$item['is_approved'] = $item['is_approved'] && (!$ctype['is_premod_edit'] || $is_moderator);
$item['approved_by'] = null;

Expand Down
2 changes: 1 addition & 1 deletion system/controllers/content/widgets/slider/options.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ public function init($options=false) {

}

}
}
2 changes: 1 addition & 1 deletion system/controllers/images/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getPresetsList($with_params = false){
orderBy('width')->
get('images_presets', function($item, $model) use($with_params){
if($with_params){
return $item['title'].', '.$item['name'].', '.($item['width'] ? $item['width'] : LANG_AUTO).'X'.($item['height'] ? $item['height'] : LANG_AUTO).'px';
return $item['title'].' ('.$item['name'].', '.($item['width'] ? $item['width'] : LANG_AUTO).' x '.($item['height'] ? $item['height'] : LANG_AUTO).')';
}
return $item['title'];
}, 'name');
Expand Down
10 changes: 7 additions & 3 deletions system/core/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public static function getControllersMapping(){
$map_file = 'system/config/remap.php';
$map_function = 'remap_controllers';

if (!cmsCore::includeFile($map_file)) { return false; }
if (!cmsCore::includeFile($map_file)) { return self::$mapping; }

if (!function_exists($map_function)){ return false; }
if (!function_exists($map_function)){ return self::$mapping; }

self::$mapping = call_user_func($map_function);

if (!is_array(self::$mapping)){ return false; }
if (!is_array(self::$mapping)){ return array(); }

return self::$mapping;

Expand Down Expand Up @@ -95,6 +95,10 @@ public function setData($cfg_file = 'config.php') {
$this->data['detect_ip_key'] = 'REMOTE_ADDR';
}

if(!isset($this->data['controllers_without_widgets'])){
$this->data['controllers_without_widgets'] = array('admin');
}

$this->upload_host_abs = $this->upload_host;

if (mb_strpos($this->upload_host, $this->host) === 0){
Expand Down
42 changes: 37 additions & 5 deletions system/core/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class cmsController {

private static $controllers;
private static $mapping;

public $name;
public $title;
Expand All @@ -13,6 +14,14 @@ class cmsController {
public $root_url;
public $root_path;

/**
* Если для контроллера задан ремап
* и это свойство установлено в true
* редиректа со старого адреса не будет
*
* @var boolean
*/
public $disallow_mapping_redirect = false;
/**
* Флаг, что контроллер должен работать только после
* регистрации в БД
Expand Down Expand Up @@ -164,6 +173,10 @@ public function isControllerInstalled($name) {
return isset(self::$controllers[$name]);
}

public function hasSlug() {
return !empty(self::$controllers[$this->name]['slug']) ? self::$controllers[$this->name]['slug'] : false;
}

public function isControllerEnabled($name) {

// проверяем только те, которые зарегистрированы в базе
Expand All @@ -183,6 +196,24 @@ public static function enabled($name) {
return true;
}

public static function getControllersMapping() {

if (self::$mapping !== null) { return self::$mapping; }

self::$mapping = array();

self::loadControllers();

foreach (self::$controllers as $controller) {
if($controller['slug']){
self::$mapping[$controller['name']] = $controller['slug'];
}
}

return self::$mapping;

}

private static function loadControllers() {

if(!isset(self::$controllers)){
Expand All @@ -193,7 +224,8 @@ private static function loadControllers() {
'i.id' => 'id',
'i.is_enabled' => 'is_enabled',
'i.options' => 'options',
'i.name' => 'name'
'i.name' => 'name',
'i.slug' => 'slug'
), true);

self::$controllers = $model->useCache('controllers')->get('controllers', function ($item, $model) {
Expand Down Expand Up @@ -767,16 +799,16 @@ public function redirectToHome(){
* @param array $params
* @param array $query
*/
public function redirectTo($controller, $action='', $params=array(), $query=array()){
public function redirectTo($controller, $action='', $params=array(), $query=array(), $code=303){

$href_lang = cmsCore::getLanguageHrefPrefix();

$location = $this->cms_config->root .($href_lang ? $href_lang.'/' : ''). $controller . '/' . $action;
$location = $this->cms_config->root .($href_lang ? $href_lang.'/' : ''). $controller . ($action ? '/'.$action : '');

if ($params){ $location .= '/' . implode('/', $params); }
if ($query){ $location .= '?' . http_build_query($query); }
if ($query){ $location .= '?' . http_build_query($query, '', '&'); }

$this->redirect($location);
$this->redirect($location, $code);

}

Expand Down
30 changes: 24 additions & 6 deletions system/core/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@ public static function getController($controller_name, $request=null){

public static function getControllerNameByAlias($controller_alias){

$mapping = cmsConfig::getControllersMapping();
$config_mapping = cmsConfig::getControllersMapping();
$controllers_mapping = cmsController::getControllersMapping();

$mapping = array_merge($controllers_mapping, $config_mapping);
if (!$mapping) { return false; }

foreach($mapping as $name=>$alias){
Expand All @@ -364,9 +366,13 @@ public static function getControllerAliasByName($controller_name){

$mapping = cmsConfig::getControllersMapping();

if (!$mapping || !isset($mapping[$controller_name])){ return false; }
if (!empty($mapping[$controller_name])){ return $mapping[$controller_name]; }

$cmapping = cmsController::getControllersMapping();

if (!empty($cmapping[$controller_name])){ return $cmapping[$controller_name]; }

return $mapping[$controller_name];
return false;

}

Expand Down Expand Up @@ -654,7 +660,18 @@ public function runController(){

// контроллер включен?
if(!$controller->isEnabled()){
self::error404();
return self::error404();
}

// редирект 301, если настроен ремап
if(!$remap_to && $slug = $controller->hasSlug()){

// если контроллер запрещает редирект, то 404
if($controller->disallow_mapping_redirect){
return self::error404();
}

$controller->redirectTo($slug, $this->uri_action, $this->uri_params, $this->uri_query, 301);
}

// сохраняем в контроллере название текущего экшена
Expand Down Expand Up @@ -701,8 +718,9 @@ public function setMatchedPages($matched_pages) {
*/
public function runWidgets(){

// в админке нам виджеты не нужны
if ($this->controller == 'admin') { return; }
$controllers_without_widgets = cmsConfig::get('controllers_without_widgets');

if ($controllers_without_widgets && in_array($this->controller, $controllers_without_widgets)) { return; }

$matched_pages = $this->loadMatchedPages()->getMatchedPages();
if (!$matched_pages) { return; }
Expand Down
26 changes: 25 additions & 1 deletion system/core/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class cmsTemplate {
protected $head_css = array();
protected $head_main_js = array();
protected $head_js = array();
protected $insert_js = array();
protected $insert_css = array();
protected $head_js_no_merge = array();
protected $head_css_no_merge = array();
protected $title;
Expand Down Expand Up @@ -831,18 +833,30 @@ public function addControllerCSS($path, $cname = '', $allow_merge = true){

public function insertJS($file, $comment=''){

$hash = md5($file);
if (isset($this->insert_js[$hash])) { return false; }
$this->insert_js[$hash] = $file;

$file = (strpos($file, '://') !== false) ? $file : $this->site_config->root . $file;
$comment = $comment ? "<!-- {$comment} !-->" : '';
// атрибут rel="forceLoad" добавлен для nyroModal
echo '<script type="text/javascript" rel="forceLoad" src="'.$file.'">'.$comment.'</script>';

return true;

}

public function insertCSS($file){

$hash = md5($file);
if (isset($this->insert_css[$hash])) { return false; }
$this->insert_css[$hash] = $file;

$file = (strpos($file, '://') !== false) ? $file : $this->site_config->root . $file;
echo '<link rel="stylesheet" type="text/css" href="'.$file.'">';

return true;

}

/**
Expand Down Expand Up @@ -1499,9 +1513,19 @@ public function renderGridRowsJSON($grid, $dataset, $total=1, $pages_count=1){
if(!empty($column['editable']['save_action'])){
$save_action = string_replace_keys_values($column['editable']['save_action'], $row);
}
$attributes = array();
if(!empty($column['editable']['attributes'])){
foreach ($column['editable']['attributes'] as $akey => $avalue) {
if(is_string($avalue)){
$attributes[$akey] = string_replace_keys_values($avalue, $row);
} else {
$attributes[$akey] = $avalue;
}
}
}
if(!empty($save_action)){
$value = '<div class="grid_field_value '.$field.'_grid_value '.((isset($column['href']) ? 'edit_by_click' : '')).'">'.$value.'</div>';
$value .= '<div class="grid_field_edit '.((isset($column['href']) ? 'edit_by_click' : '')).'">'.html_input('text', $field, $row[$field]);
$value .= '<div class="grid_field_edit '.((isset($column['href']) ? 'edit_by_click' : '')).'">'.html_input('text', $field, $row[$field], $attributes);
if($editable_index == $editable_count){
$value .= html_button(LANG_SAVE, '', '', array('data-action'=>$save_action, 'class'=>'inline_submit'));
}
Expand Down
3 changes: 2 additions & 1 deletion system/languages/en/controllers/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,5 @@
define('LANG_CONFIRM_LOGIN', 'Security Check');
define('LANG_CONFIRM_LOGIN_HINT', 'You are trying to log in under the name of "%s" from an unusual location');
define('LANG_CONFIRM_LOGIN_HINT1', 'In order to confirm ownership of the accaunt, we ask you to enter the password');
define('LANG_ADMIN_IS_DELETED', 'Deleted?');
define('LANG_ADMIN_IS_DELETED', 'Deleted?');
define('LANG_ADMIN_CONTROLLER_SLUG', 'Controller Url');
3 changes: 2 additions & 1 deletion system/languages/ru/controllers/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,5 @@
define('LANG_CONFIRM_LOGIN', 'Проверка безопасности');
define('LANG_CONFIRM_LOGIN_HINT', 'Вы пытаетесь зайти под именем "%s" из необычного места.');
define('LANG_CONFIRM_LOGIN_HINT1', 'Чтобы подтвердить, что Вы действительно являетесь владельцем аккаунта, пожалуйста, введите пароль.');
define('LANG_ADMIN_IS_DELETED', 'Удалён?');
define('LANG_ADMIN_IS_DELETED', 'Удалён?');
define('LANG_ADMIN_CONTROLLER_SLUG', 'Url компонента');
Loading

0 comments on commit 78e3d09

Please sign in to comment.