From 4c158b82e22fdb51826e4c30be1053008f527e07 Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Tue, 14 Jul 2015 13:33:34 +0700 Subject: [PATCH] Add post draft Add post draft feature for the blog post. --- robots.txt | 1 - system/admin/admin.php | 114 ++++++++++++++++++++++++--------- system/admin/views/add-post.html.php | 2 +- system/admin/views/edit-post.html.php | 8 ++- system/admin/views/user-draft.html.php | 33 ++++++++++ system/admin/views/user-posts.html.php | 12 ++-- system/htmly.php | 78 ++++++++++++++++++++-- system/includes/functions.php | 86 +++++++++++++++++++++++++ 8 files changed, 290 insertions(+), 44 deletions(-) create mode 100644 system/admin/views/user-draft.html.php diff --git a/robots.txt b/robots.txt index ec09ff3..cb5adb2 100644 --- a/robots.txt +++ b/robots.txt @@ -17,7 +17,6 @@ # http://www.sxw.org.uk/computing/robots/check.html User-agent: * -Crawl-delay: 10 # Directories Disallow: /config/ Disallow: /system/ diff --git a/system/admin/admin.php b/system/admin/admin.php index 7e61521..c4aa07b 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -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 = '' . $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 = '' . $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)); } diff --git a/system/admin/views/add-post.html.php b/system/admin/views/add-post.html.php index d67e0ec..f43f1c7 100644 --- a/system/admin/views/add-post.html.php +++ b/system/admin/views/add-post.html.php @@ -51,7 +51,7 @@ echo $postContent; } ?>
- +
diff --git a/system/admin/views/edit-post.html.php b/system/admin/views/edit-post.html.php index 279c03e..3ec8da4 100644 --- a/system/admin/views/edit-post.html.php +++ b/system/admin/views/edit-post.html.php @@ -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">
- Delete + + Delete + + Delete +
diff --git a/system/admin/views/user-draft.html.php b/system/admin/views/user-draft.html.php new file mode 100644 index 0000000..379cb08 --- /dev/null +++ b/system/admin/views/user-draft.html.php @@ -0,0 +1,33 @@ +

+ + + + + + + + + + + + + + + + + + +
TitleCreatedTagOperations
title ?>date) ?>tag) ?>Edit Delete
+ \ No newline at end of file diff --git a/system/admin/views/user-posts.html.php b/system/admin/views/user-posts.html.php index f2312f2..b6623a6 100644 --- a/system/admin/views/user-posts.html.php +++ b/system/admin/views/user-posts.html.php @@ -3,8 +3,10 @@ - - + + + + @@ -25,10 +27,10 @@ - + + - +
TitlePublishedViewsPublishedViews Tag Operations
title ?> date) ?> views ?>views ?> tag ?>Edit DeleteEdit Delete
diff --git a/system/htmly.php b/system/htmly.php index 8405dab..298f2da 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -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' => '' . config('breadcrumb.home') . ' » 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' => '' . config('breadcrumb.home') . ' » 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']; diff --git a/system/includes/functions.php b/system/includes/functions.php index 9c334aa..b80f27f 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -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 '
  • Posts
  • '; } echo '
  • Mine
  • '; + echo '
  • Draft
  • '; echo '
  • Add post
  • '; echo '
  • Add page
  • '; echo '
  • Edit profile
  • ';