Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added editing php configs #56

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 79 additions & 56 deletions assets/plugins/templatesedit/class/templateseditbuilder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,26 @@ public function __construct()
*/
public function renderTemplate(): string
{
return $this->view('tab', [
'name' => 'templatesEditBuilder',
'title' => 'template Builder',
'tabsObject' => 'tp',
'content' => $this->view('builder', [
if ($this->params['configFile']) {
$content = $this->view('builder_code', [
'config' => file_get_contents($this->basePath . $this->params['configFile']) ?: '<?php' . "\n",
'filename' => $this->params['configFile'],
]);
} else {
$content = $this->view('builder', [
'config' => $this->json_encode($this->config),
'data_fields' => $this->json_encode($this->defaultFields),
'data_tvars' => $this->json_encode($this->defaultTvars),
'data_categories' => $this->json_encode($this->defaultCategories),
'data_types' => $this->json_encode($this->fieldTypes)
])
]);
}

return $this->view('tab', [
'name' => 'templatesEditBuilder',
'title' => 'template Builder',
'tabsObject' => 'tp',
'content' => $content
]);
}

Expand All @@ -126,33 +135,37 @@ protected function setParams(): void
$this->params['check_this_config'] = false;
$this->params['check_base_config'] = false;
$this->params['check_default_config'] = 0;

$this->params['configFile'] = null;
}

/**
*
*/
protected function setDefaultParams(): void
{
$this->params['default.tab'] = 'General';
if (!$this->params['configFile']) {
$this->params['default.tab'] = 'General';

foreach ($this->config as $tabId => &$tab) {
if (!isset($tab['title'])) {
$tab['title'] = 'Tab';
}
if (!empty($tab['default'])) {
$this->params['default.tab'] = $tabId;
}
if (isset($tab['fields'])) {
$tab['col:0:12']['fields'] = $tab['fields'];
unset($tab['fields']);
foreach ($this->config as $tabId => &$tab) {
if (!isset($tab['title'])) {
$tab['title'] = 'Tab';
}
if (!empty($tab['default'])) {
$this->params['default.tab'] = $tabId;
}
if (isset($tab['fields'])) {
$tab['col:0:12']['fields'] = $tab['fields'];
unset($tab['fields']);
}
}
}

if (!isset($this->config['#Static'])) {
$this->config['#Static'] = [
'title' => 'Static',
'col:0:12' => []
];
if (!isset($this->config['#Static'])) {
$this->config['#Static'] = [
'title' => 'Static',
'col:0:12' => []
];
}
}
}

Expand Down Expand Up @@ -463,7 +476,7 @@ protected function getConfig(): array
$this->config = json_decode(file_get_contents($file), true);
} else {
if (file_exists($this->basePath . 'configs/template__' . $this->params['id'] . '.php')) {
$this->config = require_once $this->basePath . 'configs/template__' . $this->params['id'] . '.php';
$this->params['configFile'] = 'configs/template__' . $this->params['id'] . '.php';
} else {
$this->config = require_once $this->basePath . 'configs/template__default.php';
}
Expand Down Expand Up @@ -500,52 +513,62 @@ protected function setDefaultConfig(int $id = 0): void
*/
public function saveTemplate(): void
{
$data = !empty($_POST['templatesedit_builder_data']) ? $this->evo->removeSanitizeSeed($_POST['templatesedit_builder_data']) : '';
if (isset($_POST['templatesedit_builder_data'])) {
$data = $this->evo->removeSanitizeSeed($_POST['templatesedit_builder_data']);

if (!empty($data)) {
if ($this->params['check_default_config'] == $this->params['id'] || empty($this->params['config_default'][$this->params['templatesedit_builder_role']]) || (!empty($this->params['config_default'][$this->params['templatesedit_builder_role']]) && file_get_contents($this->params['config_default'][$this->params['templatesedit_builder_role']]) != $data)) {
file_put_contents($this->basePath . 'configs/template__' . $this->params['config'] . '.json', $data);
if (!empty($data)) {
if ($this->params['check_default_config'] == $this->params['id'] || empty($this->params['config_default'][$this->params['templatesedit_builder_role']]) || (!empty($this->params['config_default'][$this->params['templatesedit_builder_role']]) && file_get_contents($this->params['config_default'][$this->params['templatesedit_builder_role']]) != $data)) {
file_put_contents($this->basePath . 'configs/template__' . $this->params['config'] . '.json', $data);
} else {
if (is_file($this->basePath . 'configs/template__' . $this->params['config'] . '.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['config'] . '.json');
}
}
} else {
if (is_file($this->basePath . 'configs/template__' . $this->params['config'] . '.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['config'] . '.json');
}
if (is_file($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json');
}
}
} else {
if (is_file($this->basePath . 'configs/template__' . $this->params['config'] . '.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['config'] . '.json');
}
if (is_file($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json');
}
}

switch ($this->params['action']) {
case 'set_default':
$this->setDefaultConfig((int) $this->params['id']);
switch ($this->params['action']) {
case 'set_default':
$this->setDefaultConfig((int) $this->params['id']);

if (!empty($data)) {
if ($files = glob($this->basePath . 'configs/template__*.json')) {
foreach ($files as $file) {
if (!in_array($file, $this->params['config_default'])) {
unlink($file);
if (!empty($data)) {
if ($files = glob($this->basePath . 'configs/template__*.json')) {
foreach ($files as $file) {
if (!in_array($file, $this->params['config_default'])) {
unlink($file);
}
}
}
file_put_contents($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json', $data);
file_put_contents($this->basePath . 'configs/template__' . $this->params['id'] . '__1.json', $data);
}
file_put_contents($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json', $data);
file_put_contents($this->basePath . 'configs/template__' . $this->params['id'] . '__1.json', $data);
}
break;
break;

case 'del_default':
if (is_file($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json');
}
break;
case 'del_default':
if (is_file($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json')) {
unlink($this->basePath . 'configs/template__' . $this->params['id'] . '__default.json');
}
break;

case 'set_base':
file_put_contents($this->basePath . 'configs/template__' . $this->params['config'] . '.json', file_get_contents($this->params['check_base_config']));
break;

case 'set_base':
file_put_contents($this->basePath . 'configs/template__' . $this->params['config'] . '.json', file_get_contents($this->params['check_base_config']));
break;
}
} elseif (isset($_POST['templatesedit_builder_code'])) {
$data = $this->evo->removeSanitizeSeed($_POST['templatesedit_builder_code']);

if ($data == '') {
$data = '<?php ' . "\n" . 'return [];' . "\n";
}

file_put_contents($this->basePath . 'configs/template__' . $this->params['id'] . '.php', $data);
}

if (!empty($this->params['action'])) {
Expand Down
2 changes: 1 addition & 1 deletion assets/plugins/templatesedit/tpl/builder.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</div>
<textarea name="templatesedit_builder_data" id="templatesedit_builder_data" rows="15" style="display: none"><?= $data['config'] ?></textarea>
<script src="<?= MODX_BASE_URL ?>assets/plugins/templatesedit/js/Sortable.min.js"></script>
<script src="<?= MODX_BASE_URL ?>assets/plugins/templatesedit/js/TemplatesEditBuilder.js?v=3.1.9"></script>
<script src="<?= MODX_BASE_URL ?>assets/plugins/templatesedit/js/TemplatesEditBuilder.js?v=3.1.11"></script>
<script>
new TemplatesEditBuilder(document.getElementById('builder'), {
dataEl: document.getElementById('templatesedit_builder_data'),
Expand Down
29 changes: 29 additions & 0 deletions assets/plugins/templatesedit/tpl/builder_code.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="container container-body px-0">
<p class="px-3">Edit config: <strong><?= $data['filename'] ?? '' ?></strong></p>

<div class="section-editor clearfix">
<textarea name="templatesedit_builder_code" id="templatesedit_builder_code" rows="40"
onchange="documentDirty=true;"><?= $data['config'] ?? '' ?></textarea>
</div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
if (window['myCodeMirrors'] && window['myCodeMirrors']['post']) {
var config = window['myCodeMirrors']['post']['options']

window['myCodeMirrors']['templatesedit_builder_code'] = CodeMirror.fromTextArea(
document.getElementById('templatesedit_builder_code'),
{
mode: 'php',
theme: config['theme'],
defaulttheme: config['defaulttheme'],
darktheme: config['darktheme'],
indentUnit: config['indentUnit'],
tabSize: config['tabSize'],
lineNumbers: config['lineNumbers']
}
)
}
})
</script>
2 changes: 1 addition & 1 deletion install/assets/plugins/templatesedit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Кастомизация полей при редактировании документов
*
* @category plugin
* @version 3.1.10
* @version 3.1.11
* @package evo
* @internal @properties &showTvImage=Show TV thumbnail;list;yes,no;no &showTvName=Show TV titles;list;yes,no;no &excludeTvCategory=Exclude TV from categories;text;
* @internal @events OnDocFormTemplateRender,OnDocFormRender,OnDocFormSave,OnTempFormRender,OnTempFormSave,OnTempFormDelete
Expand Down