Convert old templates title as well as body (#9034)

* Convert old templates title as well as body

* Update convert-template.inc.php
This commit is contained in:
Neil Lathwood
2018-08-19 09:28:57 +01:00
committed by GitHub
parent 460d95e1d6
commit 2d72d8144e
2 changed files with 17 additions and 10 deletions

View File

@@ -31,7 +31,7 @@ header('Content-type: application/json');
if (!Auth::user()->hasGlobalAdmin()) { if (!Auth::user()->hasGlobalAdmin()) {
die(json_encode([ die(json_encode([
'status' => 'error', 'status' => 'error',
'message' => 'ERROR: You need to be admin', 'message' => 'You need to be admin',
])); ]));
} }
@@ -42,10 +42,16 @@ if (empty($vars['template'])) {
])); ]));
} }
$new = ''; $new_body = '';
foreach (explode(PHP_EOL, $vars['template']) as $line) { foreach (explode(PHP_EOL, $vars['template']) as $line) {
$new_body .= convert_template($line) . PHP_EOL;
}
$new_title = convert_template($vars['title']);
function convert_template($line)
{
if (str_contains($line, '{calc')) { if (str_contains($line, '{calc')) {
$new .= preg_replace( return preg_replace(
[ [
'/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/',// Replaces {calc (something*100)} '/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/',// Replaces {calc (something*100)}
'/%([\w\d]+)\.([\w\d]+)/',// Replaces %something.anything '/%([\w\d]+)\.([\w\d]+)/',// Replaces %something.anything
@@ -93,13 +99,13 @@ foreach (explode(PHP_EOL, $vars['template']) as $line) {
'$key', '$key',
'$value', '$value',
]; ];
$new .= preg_replace($find, $replace, $old1); return preg_replace($find, $replace, $old1);
} }
$new .= PHP_EOL;
} }
die(json_encode([ die(json_encode([
'status' => 'ok', 'status' => 'ok',
'message' => 'Template converted, review and save to update', 'message' => 'Template converted, review and save to update',
'template' => $new, 'template' => $new_body,
'title' => $new_title,
])); ]));

View File

@@ -112,17 +112,18 @@ $('#create-template').click('', function(e) {
$('#convert-template').click('', function(e) { $('#convert-template').click('', function(e) {
e.preventDefault(); e.preventDefault();
var template = $("#template").val(); var template = $("#template").val();
var title = $("#title").val();
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "ajax_form.php", url: "ajax_form.php",
data: {type: "convert-template", template: template}, data: {type: "convert-template", template: template, title: title},
dataType: "json", dataType: "json",
success: function(output) { success: function(output) {
console.log(output);
if(output.status === 'ok') { if(output.status === 'ok') {
toastr.success(output.message); toastr.success(output.message);
$("#convert-template").hide(); $("#convert-template").hide();
$("#template").val(output.template) $("#template").val(output.template);
$("#title").val(output.title);
} else { } else {
toastr.error(output.message); toastr.error(output.message);
} }