Skip to content

Commit

Permalink
Новые возможности для редактирования файлов
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsite committed Jan 22, 2019
1 parent a86b9ce commit acbbb31
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 98 deletions.
216 changes: 120 additions & 96 deletions application/maxsite/admin/plugins/editor_files/admin.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,16 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

$content_file = '';
$file_path = '';
$curfile = '';

function _getFiles($rdi, $depth=0, $dir)
{
$out = array();

if (!is_object($rdi)) return $out;
$AJAX1 = getinfo('ajax') . base64_encode('admin/plugins/editor_files/load-file-ajax.php');
$AJAX2 = getinfo('ajax') . base64_encode('admin/plugins/editor_files/save-file-ajax.php');
$A_LINK = getinfo('site_admin_url') .'editor_files/';

for ($rdi->rewind(); $rdi->valid(); $rdi->next())
{
if ($rdi->isDot()) continue;
$t1 = t('Файл загружен!');
$t2 = t('Текущий файл: ');

if ($rdi->isDir() || $rdi->isFile())
{
$cur = $rdi->current();
$cur = str_replace('\\', '/', $cur);
if (_is_exclude($cur)) continue;

$cur = str_replace($dir, '', $cur);

if ($rdi->isDir())
{
if ($depth == 0)
{
$out[] = '<optgroup class="bg-gray100" label="' . $cur . '"></optgroup>';
}
}

if ($rdi->isFile())
{
$file_ext = strtolower(str_replace('.', '', strrchr($cur, '.')));

// php', 'txt', 'css', 'less', 'js', 'html', 'htm', 'ini', 'sass', 'scss'
if (in_array($file_ext, array('php', 'txt', 'css', 'js', 'html', 'htm', 'ini')))
{
$pn = $rdi->getPathname();
// $pn = str_replace('\\', '/', $pn); // замена из-за windows

if (is_writable($pn)) $out[] = $cur;

}
}

if ($rdi->hasChildren())
{
$out1 = _getFiles($rdi->getChildren(), 1 + $depth, $dir);
$out = array_merge($out, $out1);
}
}
}

return $out;
}

// проверка части вхождения каждого элемента массива $a в строку $str
// если вхождение есть, то отдаем true если нет, то false
function _is_exclude($str)
{
// исключаемые из списка элементы задаются в custom/set_val_admin.php
// mso_set_val('editor_files_exclude', array('/node_modules/'));

$a = mso_get_val('editor_files_exclude', array());

foreach ($a as $find)
{
if (stripos($str, $find) !== false) return true; // найдено вхождение
}

return false;
}

$directory = getinfo('template_dir');
$directory = str_replace('\\', '/', $directory);
Expand All @@ -78,29 +20,28 @@ function _is_exclude($str)
$files = _getFiles($r, 0, $directory);

// в третьем сегменте можно указать адрес файла в base64
$content_file = '';
$file_path = '';

if (mso_segment(3))
{
$f = base64_decode(mso_segment(3));
$f = str_replace('~', '-', $f);
$f = str_replace('\\', '-', $f);
$f = getinfo('template_dir') . $f;
$f = htmlentities(base64_decode(mso_segment(3)));

// есть такой файл
if (file_exists($f))
if ($f)
{
$content_file = file_get_contents($f);
$file_path = mso_segment(3);
$f = str_replace('~', '-', $f);
$f = str_replace('\\', '-', $f);

$ff = getinfo('template_dir') . $f;

// есть такой файл
if (file_exists($ff))
{
$curfile = $t2 . '<b>' . $f . '</b>';
$content_file = file_get_contents($ff);
$file_path = mso_segment(3);
}
}
}


// pr(getinfo('template'));
// pr($directory);
// pr($files);

$select = '<option value="" selected>-</option>';

foreach ($files as $file)
Expand All @@ -112,48 +53,65 @@ function _is_exclude($str)
$select .= '<option value="' . base64_encode($file) . '"' . $opt_selected . '>' . $file . '</option>';
}
else
{
$select .= $file;
}
}

?>

<h1><?= t('Файлы для редактирования шаблона') . ' «' . getinfo('template') . '»' ?></h1>
<h1><?= t('Файлы шаблона') . ' «' . getinfo('template') . '»' ?></h1>

<p class="mar30-t"><?= t('Выберите файл:') ?> <select id="select_file" class="w-auto"><?= $select ?></select> <span id="success"></span></p>
<div class="flex flex-vcenter">
<div class="flex-grow0 pad5-r"><?= t('Файл:') ?></div>
<div class="flex-grow3"> <select id="select_file" class="w100"><?= $select ?></select></div>

<?php
// если есть custom/my-editor-files.php (в нём только <option>), то подключаем его
if ($f1 = mso_fe('custom/my-editor-files.php'))
{
echo '<div class="flex-grow0 pad5-rl">' . t('или') . '</div><div class="flex-grow3"><select id="select_file1" class="w100">';
require($f1);
echo '</select></div>';
}
?>
</div>

<div class="t90 mar10-t"><span id="curfile"><?= $curfile ?></span></div>
<div id="success"></div>

<?php

echo '<form method="post" id="edit_form" action=""><textarea name="content" id="content" class="w100 h500px bg-gray50">' . $content_file . '</textarea><input type="hidden" id="file_path" name="file_path" value="' . $file_path . '"><p><button id="b-save" class="button i-save" type="submit">Сохранить</button></p></form>';

$AJAX1 = getinfo('ajax') . base64_encode('admin/plugins/editor_files/load-file-ajax.php');
$AJAX2 = getinfo('ajax') . base64_encode('admin/plugins/editor_files/save-file-ajax.php');
echo '<form class="mar10-t" method="post" id="edit_form"><textarea name="content" id="content" class="w100 h500px bg-gray50">' . $content_file . '</textarea><input type="hidden" id="file_path" name="file_path" value="' . $file_path . '"><p><button id="b-save" class="button i-save" type="submit">Сохранить</button></p></form>';

echo <<<EOF
<script>
jQuery(function($) {
$(document).ready(function() {
$('#b-save').fadeOut(0);
$('#select_file').change(function(){
$('#select_file, #select_file1').change(function(){
var f = $("#select_file :selected").val();
var f = $("option:selected", this).val();
if (f)
{
{
$('#success').hide();
$.post("{$AJAX1}", {file:f}, function(response) {
$('#file_path').val(f);
// $('#content').html(response);
$('#content').val(response);
// это для отладки
$('#success').html('<span class="i-check mar10-l t-green t130"></span>Файл загружен');
$('#success').html('<div class="update pos-fixed w200px pad10 pos20-r pos0-t t-center">{$t1}</div>');
$('#success').show();
$('#success').fadeOut(5000);
$('#b-save').fadeOut(1000);
});
$('#curfile').html('{$t2} <a class="bold" href="{$A_LINK}' + f + '">' + $("option:selected", this).text() + '</a>');
}
})
Expand All @@ -162,7 +120,6 @@ function _is_exclude($str)
$('#success').html(response);
$('#success').show();
$('#success').fadeOut(5000);
$('#b-save').fadeOut(1000);
});
Expand All @@ -175,7 +132,74 @@ function _is_exclude($str)
});
</script>
EOF;



function _getFiles($rdi, $depth=0, $dir)
{
$out = array();

if (!is_object($rdi)) return $out;

for ($rdi->rewind(); $rdi->valid(); $rdi->next())
{
if ($rdi->isDot()) continue;

if ($rdi->isDir() || $rdi->isFile())
{
$cur = $rdi->current();
$cur = str_replace('\\', '/', $cur);
if (_is_exclude($cur)) continue;

$cur = str_replace($dir, '', $cur);

if ($rdi->isDir())
{
if ($depth == 0)
{
$out[] = '<optgroup class="bg-gray100" label="' . $cur . '"></optgroup>';
}
}

if ($rdi->isFile())
{
$file_ext = strtolower(str_replace('.', '', strrchr($cur, '.')));

// php', 'txt', 'css', 'less', 'js', 'html', 'htm', 'ini', 'sass', 'scss'
if (in_array($file_ext, array('php', 'txt', 'css', 'js', 'html', 'htm', 'ini')))
{
$pn = $rdi->getPathname();
if (is_writable($pn)) $out[] = $cur;
}
}

if ($rdi->hasChildren())
{
$out1 = _getFiles($rdi->getChildren(), 1 + $depth, $dir);
$out = array_merge($out, $out1);
}
}
}

return $out;
}

// проверка части вхождения каждого элемента массива $a в строку $str
// если вхождение есть, то отдаем true если нет, то false
function _is_exclude($str)
{
// исключаемые из списка элементы задаются в custom/set_val_admin.php
// mso_set_val('editor_files_exclude', array('/node_modules/'));

$a = mso_get_val('editor_files_exclude', array());

foreach ($a as $find)
{
if (stripos($str, $find) !== false) return true; // найдено вхождение
}

return false;
}

# end of file
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
{
file_put_contents($file, $post['content']);

echo '<span class="i-check mar10-l t-green t130"></span>Сохранено';
echo '<div class="update pos-fixed w200px pad10 pos20-r pos0-t t-center">' . t('Сохранено!') . '</div>';
}
else
{
echo '<span class="i-check mar10-l t-red t130"></span>Файл не найден';
echo '<div class="error pos-fixed w200px pad10 pos20-r pos0-t t-center">' . t('Файл не найден!') . '</div>';
}
}

Expand Down

0 comments on commit acbbb31

Please sign in to comment.