Browse Source

Add post draft

Add post draft feature for the blog post.
pull/189/head
Danang Probo Sayekti 10 years ago
parent
commit
4c158b82e2
8 changed files with 290 additions and 44 deletions
  1. +0
    -1
      robots.txt
  2. +84
    -30
      system/admin/admin.php
  3. +1
    -1
      system/admin/views/add-post.html.php
  4. +6
    -2
      system/admin/views/edit-post.html.php
  5. +33
    -0
      system/admin/views/user-draft.html.php
  6. +7
    -5
      system/admin/views/user-posts.html.php
  7. +73
    -5
      system/htmly.php
  8. +86
    -0
      system/includes/functions.php

+ 0
- 1
robots.txt View File

@ -17,7 +17,6 @@
# http://www.sxw.org.uk/computing/robots/check.html
User-agent: *
Crawl-delay: 10
# Directories
Disallow: /config/
Disallow: /system/


+ 84
- 30
system/admin/admin.php View File

@ -86,11 +86,14 @@ function remove_accent($str)
}
// Edit blog posts
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null, $description = null, $date = null, $img, $vid)
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null, $description = null, $date = null, $img, $vid, $revertPost, $publishDraft)
{
$oldurl = explode('_', $oldfile);
$dir = explode('/', $oldurl[0]);
$olddate = date('Y-m-d-h-i-s', strtotime($date));
if ($date !== null) {
$oldurl[0] = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/' . date('Y-m-d-h-i-s', strtotime($date));
$oldurl[0] = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/' . $olddate;
}
$post_title = $title;
@ -117,37 +120,74 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null,
$post_content = '<!--t ' . $post_title . ' t-->' . $post_description . $post_img . $post_vid ."\n\n" . $content;
if (!empty($post_title) && !empty($post_tag) && !empty($post_url) && !empty($post_content)) {
if (get_magic_quotes_gpc()) {
$post_content = stripslashes($post_content);
}
$newfile = $oldurl[0] . '_' . $post_tag . '_' . $post_url . '.md';
if ($oldfile === $newfile) {
file_put_contents($oldfile, print_r($post_content, true));
if(!empty($revertPost) || !empty($publishDraft)) {
if($dir[2] == 'draft') {
$filename = $dir[0] . '/' . $dir[1] . '/blog/' . $olddate . '_' . $post_tag . '_' . $post_url . '.md';
} else {
$filename = $dir[0] . '/' . $dir[1] . '/draft/' . $olddate . '_' . $post_tag . '_' . $post_url . '.md';
}
file_put_contents($filename, print_r($post_content, true));
unlink($oldfile);
$newfile = $olddate . '_' . $post_tag . '_' . $post_url . '.md';
} else {
$newfile = $oldurl[0] . '_' . $post_tag . '_' . $post_url . '.md';
if ($oldfile === $newfile) {
file_put_contents($oldfile, print_r($post_content, true));
} else {
rename($oldfile, $newfile);
file_put_contents($newfile, print_r($post_content, true));
}
}
if(!empty($publishDraft)) {
$dt = $olddate;
$t = str_replace('-', '', $dt);
$time = new DateTime($t);
$timestamp = $time->format("Y-m-d");
} else {
rename($oldfile, $newfile);
file_put_contents($newfile, print_r($post_content, true));
}
$replaced = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/';
$dt = str_replace($replaced, '', $oldurl[0]);
$t = str_replace('-', '', $dt);
$time = new DateTime($t);
$timestamp = $time->format("Y-m-d");
$replaced = substr($oldurl[0], 0, strrpos($oldurl[0], '/')) . '/';
$dt = str_replace($replaced, '', $oldurl[0]);
$t = str_replace('-', '', $dt);
$time = new DateTime($t);
$timestamp = $time->format("Y-m-d");
}
// The post date
$postdate = strtotime($timestamp);
// The post URL
$posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url;
rebuilt_cache('all');
clear_post_cache($dt, $post_tag, $post_url, $newfile);
if ($destination == 'post') {
header("Location: $posturl");
if(!empty($revertPost)) {
$drafturl = site_url() . 'admin/draft';
header("Location: $drafturl");
} else {
header("Location: $posturl");
}
} else {
$redirect = site_url() . $destination;
header("Location: $redirect");
if(!empty($publishDraft)) {
header("Location: $posturl");
} elseif (!empty($revertPost)) {
$drafturl = site_url() . 'admin/draft';
header("Location: $drafturl");
} else {
$redirect = site_url() . $destination;
header("Location: $redirect");
}
}
}
}
@ -192,7 +232,7 @@ function edit_page($title, $url, $content, $oldfile, $destination = null, $descr
}
// Add blog post
function add_post($title, $tag, $url, $content, $user, $description = null, $img, $vid)
function add_post($title, $tag, $url, $content, $user, $description = null, $img, $vid, $draft)
{
$post_date = date('Y-m-d-H-i-s');
@ -220,21 +260,35 @@ function add_post($title, $tag, $url, $content, $user, $description = null, $img
$post_content = '<!--t ' . $post_title . ' t-->' . $post_description . $post_img . $post_vid ."\n\n" . $content;
if (!empty($post_title) && !empty($post_tag) && !empty($post_url) && !empty($post_content)) {
if (get_magic_quotes_gpc()) {
if (get_magic_quotes_gpc()) {
$post_content = stripslashes($post_content);
}
$filename = $post_date . '_' . $post_tag . '_' . $post_url . '.md';
$dir = 'content/' . $user . '/blog/';
if (empty($draft)) {
$dir = 'content/' . $user . '/blog/';
} else {
$dir = 'content/' . $user . '/draft/';
}
if (is_dir($dir)) {
file_put_contents($dir . $filename, print_r($post_content, true));
} else {
mkdir($dir, 0777, true);
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true));
}
rebuilt_cache('all');
clear_post_cache($post_date, $post_tag, $post_url, $dir . $filename);
$redirect = site_url() . 'admin/mine';
if (empty($draft)) {
$redirect = site_url() . 'admin/mine';
} else {
$redirect = site_url() . 'admin/draft';
}
header("Location: $redirect");
}
}
@ -261,7 +315,7 @@ function add_page($title, $url, $content, $description = null)
if (is_dir($dir)) {
file_put_contents($dir . $filename, print_r($post_content, true));
} else {
mkdir($dir, 0777, true);
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true));
}
@ -294,7 +348,7 @@ function add_sub_page($title, $url, $content, $static, $description = null)
if (is_dir($dir)) {
file_put_contents($dir . $filename, print_r($post_content, true));
} else {
mkdir($dir, 0777, true);
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true));
}
@ -376,7 +430,7 @@ function edit_profile($title, $content, $user)
if (is_dir($dir)) {
file_put_contents($filename, print_r($user_content, true));
} else {
mkdir($dir, 0777, true);
mkdir($dir, 0775, true);
file_put_contents($filename, print_r($user_content, true));
}
rebuilt_cache('all');
@ -406,7 +460,7 @@ function migrate($title, $time, $tags, $content, $url, $user, $source)
if (is_dir($dir)) {
file_put_contents($dir . $filename, print_r($post_content, true));
} else {
mkdir($dir, 0777, true);
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true));
}


+ 1
- 1
system/admin/views/add-post.html.php View File

@ -51,7 +51,7 @@
echo $postContent;
} ?></textarea><br/>
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
<input type="submit" name="submit" class="submit" value="Publish"/>
<input type="submit" name="publish" class="submit" value="Publish"/> <input type="submit" name="draft" class="draft" value="Save as draft"/>
</form>
</div>
<div id="insertImageDialog" title="Insert Image">


+ 6
- 2
system/admin/views/edit-post.html.php View File

@ -12,7 +12,7 @@ $oldvid = get_content_tag('vid', $content);
$oldcontent = remove_html_comments($content);
$dir = substr($url, 0, strrpos($url, '/'));
$isdraft = explode('/', $dir);
$oldurl = explode('_', $url);
$oldtag = $oldurl[1];
@ -81,7 +81,11 @@ $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destinat
} ?>" name="content" cols="20" rows="10"><?php echo $oldcontent ?></textarea><br>
<input type="hidden" name="oldfile" class="text" value="<?php echo $url ?>"/>
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
<input type="submit" name="submit" class="submit" value="Save"/> <a href="<?php echo $delete ?>">Delete</a>
<?php if ($isdraft[2] == 'draft') { ?>
<input type="submit" name="publishdraft" class="submit" value="Publish draft"/> <input type="submit" name="updatedraft" class="draft" value="Update draft"/> <a href="<?php echo $delete ?>">Delete</a>
<?php } else { ?>
<input type="submit" name="updatepost" class="submit" value="Update post"/> <input type="submit" name="revertpost" class="revert" value="Revert to draft"/> <a href="<?php echo $delete ?>">Delete</a>
<?php }?>
</form>
</div>
<div id="insertImageDialog" title="Insert Image">


+ 33
- 0
system/admin/views/user-draft.html.php View File

@ -0,0 +1,33 @@
<h2 class="post-index"><?php echo $heading ?></h2>
<?php if (!empty($posts)) { ?>
<table class="post-list">
<tr class="head">
<th>Title</th>
<th>Created</th>
<th>Tag</th>
<th>Operations</th>
</tr>
<?php $i = 0;
$len = count($posts); ?>
<?php foreach ($posts as $p): ?>
<?php
if ($i == 0) {
$class = 'item first';
} elseif ($i == $len - 1) {
$class = 'item last';
} else {
$class = 'item';
}
$i++;
?>
<tr class="<?php echo $class ?>">
<td><?php echo $p->title ?></td>
<td><?php echo date('d F Y', $p->date) ?></td>
<td><?php echo strip_tags($p->tag) ?></td>
<td><a href="<?php echo $p->url ?>/edit?destination=admin/draft">Edit</a> <a href="<?php echo $p->url ?>/delete?destination=admin/draft">Delete</a></td>
</tr>
<?php endforeach; ?>
</table>
<?php } else {
echo 'No draft found!';
} ?>

+ 7
- 5
system/admin/views/user-posts.html.php View File

@ -3,8 +3,10 @@
<table class="post-list">
<tr class="head">
<th>Title</th>
<th>Published</th><?php if (config("views.counter") == "true"): ?>
<th>Views</th><?php endif; ?>
<th>Published</th>
<?php if (config("views.counter") == "true"): ?>
<th>Views</th>
<?php endif; ?>
<th>Tag</th>
<th>Operations</th>
</tr>
@ -25,10 +27,10 @@
<td><a target="_blank" href="<?php echo $p->url ?>"><?php echo $p->title ?></a></td>
<td><?php echo date('d F Y', $p->date) ?></td>
<?php if (config("views.counter") == "true"): ?>
<td><?php echo $p->views ?></td><?php endif; ?>
<td><?php echo $p->views ?></td>
<?php endif; ?>
<td><?php echo $p->tag ?></td>
<td><a href="<?php echo $p->url ?>/edit?destination=admin/mine">Edit</a> <a
href="<?php echo $p->url ?>/delete?destination=admin/mine">Delete</a></td>
<td><a href="<?php echo $p->url ?>/edit?destination=admin/mine">Edit</a> <a href="<?php echo $p->url ?>/delete?destination=admin/mine">Delete</a></td>
</tr>
<?php endforeach; ?>
</table>


+ 73
- 5
system/htmly.php View File

@ -255,12 +255,13 @@ post('/add/post', function () {
$content = from($_REQUEST, 'content');
$description = from($_REQUEST, 'description');
$user = $_SESSION[config("site.url")]['user'];
$draft = from($_REQUEST, 'draft');
if ($proper && !empty($title) && !empty($tag) && !empty($content)) {
if (!empty($url)) {
add_post($title, $tag, $url, $content, $user, $description, $img, $vid);
add_post($title, $tag, $url, $content, $user, $description, $img, $vid, $draft);
} else {
$url = $title;
add_post($title, $tag, $url, $content, $user, $description, $img, $vid);
add_post($title, $tag, $url, $content, $user, $description, $img, $vid, $draft);
}
} else {
$message['error'] = '';
@ -483,6 +484,65 @@ get('/admin/mine', function () {
}
});
// Show admin/draft
get('/admin/draft', function () {
if (login()) {
config('views.root', 'system/admin/views');
$profile = $_SESSION[config("site.url")]['user'];
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('profile.perpage');
$posts = get_draft($profile, $page, $perpage);
$total = get_count($profile, 'dirname');
$bio = get_bio($profile);
if (isset($bio[0])) {
$bio = $bio[0];
} else {
$bio = default_profile($profile);
}
if (empty($posts) || $page < 1) {
render('user-draft', array(
'title' => 'My draft - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'page' => $page,
'heading' => 'My draft',
'posts' => null,
'bio' => $bio->body,
'name' => $bio->title,
'bodyclass' => 'userdraft',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; Draft for: ' . $bio->title,
));
die;
}
render('user-draft', array(
'title' => 'My draft - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'heading' => 'My draft',
'page' => $page,
'posts' => $posts,
'bio' => $bio->body,
'name' => $bio->title,
'bodyclass' => 'userdraft',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; Draft for: ' . $bio->title,
));
} else {
$login = site_url() . 'login';
header("location: $login");
}
});
// Show import page
get('/admin/import', function () {
if (login()) {
@ -1367,7 +1427,10 @@ get('/:year/:month/:name/edit', function ($year, $month, $name) {
$post = find_post($year, $month, $name);
if (!$post) {
not_found();
$post = find_draft($year, $month, $name);
if (!$post) {
not_found();
}
}
$current = $post['current'];
@ -1414,6 +1477,8 @@ post('/:year/:month/:name/edit', function () {
$date = from($_REQUEST, 'date');
$time = from($_REQUEST, 'time');
$dateTime = null;
$revertPost = from($_REQUEST, 'revertpost');
$publishDraft = from($_REQUEST, 'publishdraft');
if ($date !== null && $time !== null) {
$dateTime = $date . ' ' . $time;
}
@ -1422,7 +1487,7 @@ post('/:year/:month/:name/edit', function () {
if (empty($url)) {
$url = $title;
}
edit_post($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $img, $vid);
edit_post($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $img, $vid, $revertPost, $publishDraft);
} else {
$message['error'] = '';
if (empty($title)) {
@ -1469,7 +1534,10 @@ get('/:year/:month/:name/delete', function ($year, $month, $name) {
$post = find_post($year, $month, $name);
if (!$post) {
not_found();
$post = find_draft($year, $month, $name);
if (!$post) {
not_found();
}
}
$current = $post['current'];


+ 86
- 0
system/includes/functions.php View File

@ -108,6 +108,24 @@ function get_zip_files()
return $_zip;
}
// Get user draft.
function get_draft_posts()
{
static $_draft = array();
if (empty($_draft)) {
$tmp = array();
$tmp = glob('content/*/draft/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_draft[] = pathinfo($file);
}
}
usort($_draft, "sortfile");
}
return $_draft;
}
// usort function. Sort by filename.
function sortfile($a, $b)
{
@ -304,6 +322,50 @@ function find_post($year, $month, $name)
}
}
// Find draft.
function find_draft($year, $month, $name)
{
$posts = get_draft_posts();
foreach ($posts as $index => $v) {
$url = $v['basename'];
if (strpos($url, "$year-$month") !== false && strpos($url, $name . '.md') !== false) {
// Use the get_posts method to return
// a properly parsed object
$ar = get_posts($posts, $index + 1, 1);
$nx = get_posts($posts, $index, 1);
$pr = get_posts($posts, $index + 2, 1);
if ($index == 0) {
if (isset($pr[0])) {
return array(
'current' => $ar[0],
'prev' => $pr[0]
);
} else {
return array(
'current' => $ar[0],
'prev' => null
);
}
} elseif (count($posts) == $index + 1) {
return array(
'current' => $ar[0],
'next' => $nx[0]
);
} else {
return array(
'current' => $ar[0],
'next' => $nx[0],
'prev' => $pr[0]
);
}
}
}
}
// Return tag page.
function get_tag($tag, $page, $perpage, $random)
{
@ -384,6 +446,29 @@ function get_profile($profile, $page, $perpage)
return $tmp = get_posts($tmp, $page, $perpage);
}
// Return draft list
function get_draft($profile, $page, $perpage)
{
$posts = get_draft_posts();
$tmp = array();
foreach ($posts as $index => $v) {
$url = $v['dirname'];
$str = explode('/', $url);
$author = $str[count($str) - 2];
if ($profile === $author) {
$tmp[] = $v;
}
}
if (empty($tmp)) {
return;
}
return $tmp = get_posts($tmp, $page, $perpage);
}
// Return author bio.
function get_bio($author)
{
@ -1655,6 +1740,7 @@ EOF;
echo '<li><a href="' . $base . 'admin/posts">Posts</a></li>';
}
echo '<li><a href="' . $base . 'admin/mine">Mine</a></li>';
echo '<li><a href="' . $base . 'admin/draft">Draft</a></li>';
echo '<li><a href="' . $base . 'add/post">Add post</a></li>';
echo '<li><a href="' . $base . 'add/page">Add page</a></li>';
echo '<li><a href="' . $base . 'edit/profile">Edit profile</a></li>';


Loading…
Cancel
Save