Skip to content

Commit

Permalink
Merge pull request #56 from 64j/3.1.x
Browse files Browse the repository at this point in the history
Added editing php configs
  • Loading branch information
64j authored Jan 29, 2024
2 parents ab0811d + ad5e92d commit 665d245
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 58 deletions.
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

0 comments on commit 665d245

Please sign in to comment.