mirror of
https://github.com/danpros/htmly.git
synced 2026-04-17 11:16:00 +05:30
Fix autosave #789
Track last saved draft than compare the new filename against the old filename when saving.
This commit is contained in:
parent
fcb013948c
commit
9beafa63bb
7 changed files with 59 additions and 31 deletions
|
|
@ -120,7 +120,7 @@ function remove_accent($str)
|
|||
}
|
||||
|
||||
// Add content
|
||||
function add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description = null, $media = null, $dateTime = null, $autoSave = null)
|
||||
function add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description = null, $media = null, $dateTime = null, $autoSave = null, $oldfile = null)
|
||||
{
|
||||
if (!is_null($autoSave)) {
|
||||
$draft = 'draft';
|
||||
|
|
@ -234,12 +234,20 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty
|
|||
$dir = 'content/' . $user . '/blog/' . $category. '/draft/';
|
||||
}
|
||||
|
||||
if (is_dir($dir)) {
|
||||
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
|
||||
} else {
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0775, true);
|
||||
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
|
||||
|
||||
}
|
||||
|
||||
$oldfile = $oldfile;
|
||||
$newfile = $dir . $filename;
|
||||
if ($oldfile !== $newfile) {
|
||||
if (file_exists($oldfile)) {
|
||||
rename($oldfile, $newfile);
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($newfile, print_r($post_content, true), LOCK_EX);
|
||||
|
||||
if (empty($draft)) {
|
||||
$draftFile = 'content/' . $user . '/blog/' . $category. '/draft/' . $filename;
|
||||
|
|
@ -255,7 +263,7 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty
|
|||
clear_post_cache($post_date, $post_tag, $post_url, $dir . $filename, $category, $type);
|
||||
|
||||
if (!is_null($autoSave)) {
|
||||
return "Auto Saved";
|
||||
return json_encode(array('message' => 'Auto Saved', 'file' => $newfile));
|
||||
}
|
||||
|
||||
if (empty($draft)) {
|
||||
|
|
@ -482,7 +490,7 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
|
|||
}
|
||||
|
||||
if (!is_null($autoSave)) {
|
||||
return "Auto Saved";
|
||||
return json_encode(array('message' => 'Auto Saved', 'file' => $newfile));
|
||||
}
|
||||
|
||||
if ($destination == 'post') {
|
||||
|
|
@ -517,12 +525,13 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
|
|||
}
|
||||
|
||||
// Add static page
|
||||
function add_page($title, $url, $content, $draft, $description = null, $autoSave = null)
|
||||
function add_page($title, $url, $content, $draft, $description = null, $autoSave = null, $oldfile = null)
|
||||
{
|
||||
if (!is_null($autoSave)) {
|
||||
$draft = 'draft';
|
||||
}
|
||||
$post_title = safe_html($title);
|
||||
$newfile = '';
|
||||
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
|
||||
$description = safe_html($description);
|
||||
if ($description !== null) {
|
||||
|
|
@ -565,7 +574,15 @@ function add_page($title, $url, $content, $draft, $description = null, $autoSave
|
|||
} else {
|
||||
if (!is_dir($dirDraft)) {
|
||||
mkdir($dirDraft, 0775, true);
|
||||
}
|
||||
}
|
||||
|
||||
$oldfile = $oldfile;
|
||||
$newfile = $dirDraft . $filename;
|
||||
if ($oldfile !== $newfile) {
|
||||
if (file_exists($oldfile)) {
|
||||
rename($oldfile, $newfile);
|
||||
}
|
||||
}
|
||||
file_put_contents($dirDraft . $filename, print_r($post_content, true), LOCK_EX);
|
||||
}
|
||||
|
||||
|
|
@ -573,7 +590,7 @@ function add_page($title, $url, $content, $draft, $description = null, $autoSave
|
|||
clear_page_cache($post_url);
|
||||
|
||||
if (!is_null($autoSave)) {
|
||||
return "Auto Saved";
|
||||
return json_encode(array('message' => 'Auto Saved', 'file' => $newfile));
|
||||
}
|
||||
|
||||
if (empty($draft)) {
|
||||
|
|
@ -587,12 +604,13 @@ function add_page($title, $url, $content, $draft, $description = null, $autoSave
|
|||
}
|
||||
|
||||
// Add static sub page
|
||||
function add_sub_page($title, $url, $content, $static, $draft, $description = null, $autoSave = null)
|
||||
function add_sub_page($title, $url, $content, $static, $draft, $description = null, $autoSave = null, $oldfile = null)
|
||||
{
|
||||
if (!is_null($autoSave)) {
|
||||
$draft = 'draft';
|
||||
}
|
||||
$post = find_page($static);
|
||||
$newfile = '';
|
||||
$static = pathinfo($post['current']->md, PATHINFO_FILENAME);
|
||||
$post_title = safe_html($title);
|
||||
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
|
||||
|
|
@ -638,11 +656,19 @@ function add_sub_page($title, $url, $content, $static, $draft, $description = nu
|
|||
if (!is_dir($dirDraft)) {
|
||||
mkdir($dirDraft, 0775, true);
|
||||
}
|
||||
|
||||
$oldfile = $oldfile;
|
||||
$newfile = $dirDraft . $filename;
|
||||
if ($oldfile !== $newfile) {
|
||||
if (file_exists($oldfile)) {
|
||||
rename($oldfile, $newfile);
|
||||
}
|
||||
}
|
||||
file_put_contents($dirDraft . $filename, print_r($post_content, true), LOCK_EX);
|
||||
}
|
||||
|
||||
if (!is_null($autoSave)) {
|
||||
return "Auto Saved";
|
||||
return json_encode(array('message' => 'Auto Saved', 'file' => $newfile));
|
||||
}
|
||||
|
||||
rebuilt_cache('all');
|
||||
|
|
@ -662,6 +688,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
|
|||
} else {
|
||||
$num = null;
|
||||
}
|
||||
$newfile = '';
|
||||
$views = array();
|
||||
$viewsFile = "content/data/views.json";
|
||||
$post_title = safe_html($title);
|
||||
|
|
@ -788,7 +815,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
|
|||
}
|
||||
|
||||
if (!is_null($autoSave)) {
|
||||
return "Auto Saved";
|
||||
return json_encode(array('message' => 'Auto Saved', 'file' => $newfile));
|
||||
}
|
||||
|
||||
if ($destination == 'post') {
|
||||
|
|
|
|||
|
|
@ -172,6 +172,7 @@ $( function() {
|
|||
<?php if ($type == 'is_post'):?>
|
||||
<input type="hidden" name="is_post" value="is_post">
|
||||
<?php endif;?>
|
||||
<input id="oldfile" type="hidden" name="oldfile" class="text"/>
|
||||
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -300,7 +301,6 @@ $( function() {
|
|||
var base_path = '<?php echo site_url() ?>';
|
||||
var initial_image = '<?php echo $images;?>';
|
||||
var parent_page = '';
|
||||
var oldfile = '';
|
||||
var addEdit = 'add';
|
||||
var saveInterval = 60000;
|
||||
</script>
|
||||
|
|
@ -341,7 +341,7 @@ $('.img-container').on("click", ".the-img", function(e) {
|
|||
document.getElementById('hideButton').addEventListener('click', toggleDivs);
|
||||
</script>
|
||||
<?php if (config('autosave.enable') == 'true' ):?>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js"></script>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js?v=1"></script>
|
||||
<?php endif;?>
|
||||
<script>
|
||||
if (localStorage.getItem("preview-state") === "open") {
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@
|
|||
<textarea id="wmd-input" class="form-control wmd-input <?php if (isset($postContent)) {if (empty($postContent)) {echo 'error';}} ?>" name="content" cols="20" rows="10"><?php if (isset($postContent)) {echo $postContent;} ?></textarea>
|
||||
<br>
|
||||
<input type="hidden" id="pType" name="posttype" value="<?php echo $type; ?>">
|
||||
<input id="oldfile" type="hidden" name="oldfile" class="text"/>
|
||||
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
|
||||
<?php if ($type == 'is_page' || $type == 'is_subpage') :?>
|
||||
<input type="submit" name="submit" class="btn btn-primary submit" value="<?php echo i18n('Publish');?>"/> <input type="submit" name="draft" class="btn btn-primary draft" value="<?php echo i18n('Save_as_draft');?>"/>
|
||||
|
|
@ -126,7 +127,6 @@
|
|||
var base_path = '<?php echo site_url() ?>';
|
||||
var initial_image = '<?php echo $images;?>';
|
||||
var parent_page = '<?php echo isset($parent) ? $parent : '';?>';
|
||||
var oldfile = '';
|
||||
var addEdit = 'add';
|
||||
var saveInterval = 60000;
|
||||
</script>
|
||||
|
|
@ -167,7 +167,7 @@ $('.img-container').on("click", ".the-img", function(e) {
|
|||
</script>
|
||||
<?php if (config('autosave.enable') == 'true' ):?>
|
||||
<?php if ($type == 'is_page' || $type == 'is_subpage') :?>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js"></script>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js?v=1"></script>
|
||||
<?php endif;?>
|
||||
<?php endif;?>
|
||||
<script>
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ $( function() {
|
|||
<?php if ($type == 'is_post'):?>
|
||||
<input type="hidden" name="is_post" value="is_post">
|
||||
<?php endif;?>
|
||||
<input type="hidden" name="oldfile" class="text" value="<?php echo $filename; ?>"/>
|
||||
<input id="oldfile" type="hidden" name="oldfile" class="text" value="<?php echo $filename; ?>"/>
|
||||
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -350,7 +350,6 @@ $( function() {
|
|||
var base_path = '<?php echo site_url() ?>';
|
||||
var initial_image = '<?php echo $images;?>';
|
||||
var parent_page = '';
|
||||
var oldfile = '<?php echo $filename;?>';
|
||||
var addEdit = 'edit';
|
||||
var saveInterval = 60000;
|
||||
</script>
|
||||
|
|
@ -392,7 +391,7 @@ $('.img-container').on("click", ".the-img", function(e) {
|
|||
</script>
|
||||
<?php if (config('autosave.enable') == 'true' ):?>
|
||||
<?php if ($isdraft[4] == 'draft') : ?>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js"></script>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js?v=1"></script>
|
||||
<?php endif;?>
|
||||
<?php endif;?>
|
||||
<script>
|
||||
|
|
|
|||
|
|
@ -151,10 +151,10 @@ $images = image_gallery(null, 1, 40);
|
|||
<?php if($type == 'is_frontpage' || $type == 'is_profile') { ?>
|
||||
<input type="submit" name="submit" class="btn btn-primary submit" value="<?php echo i18n('Save');?>"/>
|
||||
<?php } elseif ($type == 'is_category') {?>
|
||||
<input type="hidden" name="oldfile" class="text" value="<?php echo $url ?>"/>
|
||||
<input id="oldfile" type="hidden" name="oldfile" class="text" value="<?php echo $url ?>"/>
|
||||
<input type="submit" name="submit" class="btn btn-primary submit" value="<?php echo i18n('Save_category');?>"/>
|
||||
<?php } else {?>
|
||||
<input type="hidden" name="oldfile" class="text" value="<?php echo $url ?>"/>
|
||||
<input id="oldfile" type="hidden" name="oldfile" class="text" value="<?php echo $url ?>"/>
|
||||
<?php $dd = find_subpage($oldmd); ?>
|
||||
<?php $dr = find_draft_subpage($oldmd);?>
|
||||
<?php if (stripos($dir . '/', '/draft/') !== false) { ?>
|
||||
|
|
@ -274,7 +274,6 @@ $images = image_gallery(null, 1, 40);
|
|||
var base_path = '<?php echo site_url() ?>';
|
||||
var initial_image = '<?php echo $images;?>';
|
||||
var parent_page = '<?php echo isset($parent) ? $parent : '';?>';
|
||||
var oldfile = '<?php echo isset($url) ? $url : '';?>';
|
||||
var addEdit = 'edit';
|
||||
var saveInterval = 60000;
|
||||
</script>
|
||||
|
|
@ -319,7 +318,7 @@ $('.img-container').on("click", ".the-img", function(e) {
|
|||
<?php if (config('autosave.enable') == 'true'):?>
|
||||
<?php if ($type !== 'is_category' && $type !== 'is_profile' && $type !== 'is_frontpage') :?>
|
||||
<?php if (stripos($dir . '/', '/draft/') !== false): ?>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js"></script>
|
||||
<script src="<?php echo site_url();?>system/resources/js/save_draft.js?v=1"></script>
|
||||
<?php endif;?>
|
||||
<?php endif;?>
|
||||
<?php endif;?>
|
||||
|
|
|
|||
|
|
@ -911,6 +911,7 @@ post('/admin/autosave', function () {
|
|||
$draft = 'draft';
|
||||
$posttype = $_REQUEST['posttype'];
|
||||
$autoSave = $_REQUEST['autoSave'];
|
||||
$oldfile = $_REQUEST['oldfile'];
|
||||
$addEdit = $_REQUEST['addEdit'];
|
||||
$user = $_SESSION[site_url()]['user'];
|
||||
$role = user('role', $user);
|
||||
|
|
@ -923,7 +924,6 @@ post('/admin/autosave', function () {
|
|||
$revertPage = '';
|
||||
$revertPost = '';
|
||||
$publishDraft = '';
|
||||
$oldfile = $_REQUEST['oldfile'];
|
||||
$destination = null;
|
||||
}
|
||||
|
||||
|
|
@ -931,7 +931,7 @@ post('/admin/autosave', function () {
|
|||
if ($posttype == 'is_page') {
|
||||
if ($role === 'editor' || $role === 'admin') {
|
||||
if ($addEdit == 'add') {
|
||||
$response = add_page($title, $url, $content, $draft, $description, $autoSave);
|
||||
$response = add_page($title, $url, $content, $draft, $description, $autoSave, $oldfile);
|
||||
} else {
|
||||
$response = edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description, null, $autoSave);
|
||||
}
|
||||
|
|
@ -940,7 +940,7 @@ post('/admin/autosave', function () {
|
|||
if ($role === 'editor' || $role === 'admin') {
|
||||
$static = $_REQUEST['parent_page'];
|
||||
if ($addEdit == 'add') {
|
||||
$response = add_sub_page($title, $url, $content, $static, $draft, $description, $autoSave);
|
||||
$response = add_sub_page($title, $url, $content, $static, $draft, $description, $autoSave, $oldfile);
|
||||
} else {
|
||||
$response = edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description, $static, $autoSave);
|
||||
}
|
||||
|
|
@ -971,7 +971,7 @@ post('/admin/autosave', function () {
|
|||
|
||||
if (!empty($title) && !empty($tag) && !empty($content)) {
|
||||
if ($addEdit == 'add') {
|
||||
$response = add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description, $media, $dateTime, $autoSave);
|
||||
$response = add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description, $media, $dateTime, $autoSave, $oldfile);
|
||||
} else {
|
||||
$arr = explode('/', $oldfile);
|
||||
if ($user === $arr[1] || $role === 'editor' || $role === 'admin') {
|
||||
|
|
@ -981,8 +981,9 @@ post('/admin/autosave', function () {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
$response = "No content to save.";
|
||||
$response = json_encode(array('message' => 'No content to save.', 'file' => ''));
|
||||
}
|
||||
header('Content-Type: application/json');
|
||||
echo $response;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ function updateData() {
|
|||
var plink = $("#pLink").val();
|
||||
var pDate = $("#pDate").val();
|
||||
var pTime = $("#pTime").val();
|
||||
var oldfile = $("#oldfile").val();
|
||||
var dateTime = pDate + " " + pTime;
|
||||
var autoSave = 'autoSave';
|
||||
|
||||
|
|
@ -44,7 +45,8 @@ function updateData() {
|
|||
type: "POST",
|
||||
data: data,
|
||||
success: function(response) {
|
||||
$("#response").html(response);
|
||||
$("#response").html(response.message);
|
||||
$("#oldfile").val(response.file);
|
||||
$("#response").fadeIn(600, function() {
|
||||
$("#response").css("display", "block");
|
||||
});
|
||||
|
|
@ -59,4 +61,4 @@ function updateData() {
|
|||
|
||||
$(document).ready(function() {
|
||||
setInterval(updateData, saveInterval);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue