From 78e733bec4ec1d6b6953bba9d232eb7addc5aa66 Mon Sep 17 00:00:00 2001 From: Toni Haryanto Date: Wed, 4 Feb 2015 18:07:52 +0700 Subject: [PATCH] fix saving and deleting page --- sites/default/content/pages/lipsum/index.md | 5 +- sites/default/content/pages/lipsum/subpage.md | 4 +- system/application/libraries/Pusaka.php | 84 +++++++++++++---- .../modules/pages/controllers/Panel.php | 92 +++++++++---------- .../modules/pages/views/page_form.php | 16 +--- .../modules/pages/views/page_list.php | 6 +- .../application/modules/pages/views/pages.php | 11 +++ 7 files changed, 134 insertions(+), 84 deletions(-) mode change 100755 => 100644 sites/default/content/pages/lipsum/index.md mode change 100755 => 100644 sites/default/content/pages/lipsum/subpage.md diff --git a/sites/default/content/pages/lipsum/index.md b/sites/default/content/pages/lipsum/index.md old mode 100755 new mode 100644 index f166322..f9bc7fb --- a/sites/default/content/pages/lipsum/index.md +++ b/sites/default/content/pages/lipsum/index.md @@ -1,5 +1,4 @@ +{: btnSaveExit :} 1 {: title :} Lipsum {: slug :} lipsum -{: content :} Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum. -{: meta_description :} Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. -{: meta_keywords :} lorem, ipsum, dolor, sit, amet +{: content :} dsf sdgdsgdfsg dfgdfsg diff --git a/sites/default/content/pages/lipsum/subpage.md b/sites/default/content/pages/lipsum/subpage.md old mode 100755 new mode 100644 index 26294b9..e3c7cbf --- a/sites/default/content/pages/lipsum/subpage.md +++ b/sites/default/content/pages/lipsum/subpage.md @@ -1,4 +1,4 @@ +{: btnSaveExit :} 1 {: title :} Subpage {: slug :} subpage -{: parent :} lipsum -{: content :} Sample Sub Page +{: content :} df dsf sad sdfasdfdsf diff --git a/system/application/libraries/Pusaka.php b/system/application/libraries/Pusaka.php index a2c0924..74d7d94 100755 --- a/system/application/libraries/Pusaka.php +++ b/system/application/libraries/Pusaka.php @@ -57,28 +57,74 @@ function scan_pages($map = false, $prefix = '', $depth = 5) $map = directory_map(PAGE_FOLDER, $depth); $new_map = array(); - foreach ($map as $folder => $file) - if($file != 'index.json' && $file != 'index.md' && $file != 'index.html'){ - if(is_string($file)){ - $slug = $this->remove_extension($file); - $content = array( - 'title' => $this->guess_name($file), - 'url' => $prefix.$slug - ); + foreach ($map as $folder => $file){ + if($file != 'index.json' && $file != 'index.md' && $file != 'index.html'){ + if(is_array($file)){ + $slug = $this->remove_extension($folder); + $content = array( + 'title' => $this->guess_name($folder), + 'url' => $prefix.$slug, + 'children' => $this->scan_pages($file, $prefix.$slug.'/') + ); + } else { + $slug = $this->remove_extension($file); + $content = array( + 'title' => $this->guess_name($file), + 'url' => $prefix.$slug + ); + } + + $new_map[$slug] = $content; } - else { - $slug = $this->remove_extension($folder); - $content = array( - 'title' => $this->guess_name($folder), - 'url' => $prefix.$slug, - 'children' => $this->scan_pages($file, $prefix.$slug.'/') - ); + } + + return $new_map; + } + + function move_page($prevslug, $slug, $source, $dest) + { + // page move to another folder + if($source != $dest) { + // if it is move to subpage, not to root + if(!empty($dest)) { + // if parent still as standalone file (not in folder) + if(file_exists(PAGE_FOLDER.$dest.'.md')) { + // create folder and move the parent inside + mkdir(PAGE_FOLDER.$dest, 0775); + rename(PAGE_FOLDER.$dest.'.md', PAGE_FOLDER.$dest.'/index.md'); + + // create index.html file + copy(PAGE_FOLDER.'index.html', PAGE_FOLDER.$dest.'/index.html'); + } } + } + + // move to new location + if(is_dir(PAGE_FOLDER.$prevslug)) + rename(PAGE_FOLDER.$prevslug, PAGE_FOLDER.$dest.'/'.$slug); + else + rename(PAGE_FOLDER.$prevslug.'.md', PAGE_FOLDER.$dest.'/'.$slug.'.md'); + - $new_map[$slug] = $content; + // if file left the empty folder, not from the root + if(!empty($source) && $filesleft = glob(PAGE_FOLDER.$source.'/*')){ + // if there are only index.html, index.md + if(count($filesleft) <= 2){ + // move to upper parent + $this->raise_page($source); + } } + } - return $new_map; + function raise_page($source) + { + $parent_subparent_arr = explode("/", $source); + $parent_name = array_pop($parent_subparent_arr); + $parent_subparent = implode("/", $parent_subparent_arr); + rename(PAGE_FOLDER.$source.'/index.md', PAGE_FOLDER.$parent_subparent.'/'.$parent_name.'.md'); + + unlink(PAGE_FOLDER.$source.'/index.html'); + rmdir(PAGE_FOLDER.$source); } // -------------------------------------------------------------------- @@ -724,7 +770,7 @@ public function guess_name($name, $prefix = null) if(! $prefix) $name = ucwords($name); - return $name; + return rtrim($name, '/'); } // -------------------------------------------------------------------------- @@ -748,7 +794,7 @@ public function remove_extension($file) } - return $file; + return rtrim($file, '/'); } // -------------------------------------------------------------------------- diff --git a/system/application/modules/pages/controllers/Panel.php b/system/application/modules/pages/controllers/Panel.php index 2e2f710..d8abef6 100755 --- a/system/application/modules/pages/controllers/Panel.php +++ b/system/application/modules/pages/controllers/Panel.php @@ -54,6 +54,12 @@ function __construct(){ * PAGES **********************************************/ + function coba() + { + print_r(directory_map(PAGE_FOLDER)); + print_r($this->pusaka->scan_pages()); + } + function index() { // $pages = $this->pusaka->get_pages_tree(); @@ -84,6 +90,13 @@ function sync($redirect = true) function create() { + $segs = $this->uri->uri_string(); + $seg_array = explode("/", $segs, 4); + + $parent = false; + if(isset($seg_array[3])) + $parent = $seg_array[3]; + $this->form_validation->set_rules($this->page_fields); if($this->form_validation->run()){ @@ -118,7 +131,10 @@ function create() // update page index $this->sync(false); - redirect('panel/pages'); + if($this->input->post('btnSaveExit')) + redirect('panel/pages'); + else + redirect('panel/pages/edit/'.$page['parent'].$page['slug']); } else { $this->template->set('error', 'Page failed to save. Make sure the folder '.PAGE_FOLDER.' is writable.'); @@ -129,6 +145,7 @@ function create() $this->template ->set('type', 'create') ->set('page', '') + ->set('parent', $parent) ->set('url', '') ->set('layouts', $this->pusaka->get_layouts($this->config->item('theme'))) ->set('pagelinks', $this->pusaka->get_flatnav()) @@ -137,7 +154,13 @@ function create() function edit() { - if(!$prevslug = $this->input->get('page')) show_404(); + $segs = $this->uri->uri_string(); + $seg_array = explode("/", $segs, 4); + + if(isset($seg_array[3])) + $prevslug = $seg_array[3]; + else + show_404(); $prevpage = $this->pusaka->get_page($prevslug, false); if(!isset($prevpage['slug'])) @@ -170,7 +193,10 @@ function edit() // update page index $this->sync(false); - redirect('panel/pages'); + if($this->input->post('btnSaveExit')) + redirect('panel/pages'); + else + redirect('panel/pages/edit/'.$page['parent'].$page['slug']); } @@ -186,10 +212,25 @@ function edit() function delete() { - if(!$prevslug = $this->input->get('page')) show_404(); + $segs = $this->uri->uri_string(); + $seg_array = explode("/", $segs, 4); + + if(isset($seg_array[3])) + $prevslug = $seg_array[3]; + else + show_404(); if(unlink(PAGE_FOLDER.'/'.$prevslug.'.md')){ $this->session->set_flashdata('success', 'Page '.$prevslug.' deleted.'); + + // check to raise parent page + $slug_array = explode("/", $prevslug); + if(count($slug_array) >= 2){ + array_pop($slug_array); + $parent = implode("/", $slug_array); + $this->pusaka->raise_page($parent); + } + // update page index $this->sync(false); } @@ -212,7 +253,7 @@ function move_page() $source = implode("/", $source_arr); $dest = $this->input->post('dest'); - $this->_move_page($source.'/'.$page, $page, $source, $dest); + $this->pusaka->move_page($source.'/'.$page, $page, $source, $dest); // update page index $msg = $this->sync(false); @@ -220,47 +261,6 @@ function move_page() echo json_encode(array('page' => $page, 'source' => $source, 'dest' => $dest) + $msg); } - function _move_page($prevslug, $slug, $source, $dest) - { - // page move to another folder - if($source != $dest) { - // if it is move to subpage, not to root - if(!empty($dest)) { - // if parent still as standalone file (not in folder) - if(file_exists(PAGE_FOLDER.$dest.'.md')) { - // create folder and move the parent inside - mkdir(PAGE_FOLDER.$dest, 0775); - rename(PAGE_FOLDER.$dest.'.md', PAGE_FOLDER.$dest.'/index.md'); - - // create index.html file - copy(PAGE_FOLDER.'index.html', PAGE_FOLDER.$dest.'/index.html'); - } - } - } - - // move to new location - if(is_dir(PAGE_FOLDER.$prevslug)) - rename(PAGE_FOLDER.$prevslug, PAGE_FOLDER.$dest.'/'.$slug); - else - rename(PAGE_FOLDER.$prevslug.'.md', PAGE_FOLDER.$dest.'/'.$slug.'.md'); - - - // if file left the empty folder, not from the root - if(!empty($source) && $filesleft = glob(PAGE_FOLDER.$source.'/*')){ - // if there are only index.html, index.md - if(count($filesleft) <= 2){ - // move to upper parent - $parent_subparent_arr = explode("/", $source); - $parent_name = array_pop($parent_subparent_arr); - $parent_subparent = implode("/", $parent_subparent_arr); - rename(PAGE_FOLDER.$source.'/index.md', PAGE_FOLDER.$parent_subparent.'/'.$parent_name.'.md'); - - unlink(PAGE_FOLDER.$source.'/index.html'); - rmdir(PAGE_FOLDER.$source); - } - } - } - function sort($arr = false) { if(! $arr) diff --git a/system/application/modules/pages/views/page_form.php b/system/application/modules/pages/views/page_form.php index 70022d7..6d70010 100644 --- a/system/application/modules/pages/views/page_form.php +++ b/system/application/modules/pages/views/page_form.php @@ -1,10 +1,11 @@ -
+

PAGES PAGE

- + +