Skip to content

Commit

Permalink
fix saving and deleting page
Browse files Browse the repository at this point in the history
  • Loading branch information
yllumi committed Feb 4, 2015
1 parent 960ddbf commit 78e733b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 84 deletions.
5 changes: 2 additions & 3 deletions sites/default/content/pages/lipsum/index.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions sites/default/content/pages/lipsum/subpage.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{: btnSaveExit :} 1
{: title :} Subpage
{: slug :} subpage
{: parent :} lipsum
{: content :} Sample Sub Page
{: content :} df dsf sad sdfasdfdsf
84 changes: 65 additions & 19 deletions system/application/libraries/Pusaka.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -724,7 +770,7 @@ public function guess_name($name, $prefix = null)
if(! $prefix)
$name = ucwords($name);

return $name;
return rtrim($name, '/');
}

// --------------------------------------------------------------------------
Expand All @@ -748,7 +794,7 @@ public function remove_extension($file)
}


return $file;
return rtrim($file, '/');
}

// --------------------------------------------------------------------------
Expand Down
92 changes: 46 additions & 46 deletions system/application/modules/pages/controllers/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()){
Expand Down Expand Up @@ -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.');
Expand All @@ -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())
Expand All @@ -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']))
Expand Down Expand Up @@ -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']);
}


Expand All @@ -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);
}
Expand All @@ -212,55 +253,14 @@ 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);

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)
Expand Down
16 changes: 5 additions & 11 deletions system/application/modules/pages/views/page_form.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<form action="<?php echo site_url('panel/pages/'.$type.'?page='.$url); ?>" method="POST" class="panel-form">
<form action="<?php echo site_url('panel/pages/'.$type.'/'.$url); ?>" method="POST" data-page="<?php echo $url; ?>" data-mode="<?php echo $type; ?>" class="panel-form">
<div class="row heading">
<div class="col-md-6">
<h1><a href="<?php echo site_url('panel/pages'); ?>">PAGES</a> &bull; <?php echo strtoupper($type); ?> PAGE</h1>
</div>
<div class="col-md-6 align-right">
<button type="submit" class="btn btn-success"><span class="fa fa-save"></span> Save changes</button>
<button type="submit" name="btnSave" class="btn btn-save btn-info"><span class="fa fa-save"></span> Save</button>
<button type="submit" name="btnSaveExit" value="1" class="btn btn-save btn-success"><span class="fa fa-save"></span> Save and exit</button>
</div>
</div>
<ul class="nav nav-tabs" role="tablist">
Expand Down Expand Up @@ -36,7 +37,7 @@
<select class="form-control" name="parent" id="parent">
<option value="">—</option>
<?php foreach ($pagelinks as $pagelink => $caption): ?>
<option value="<?php echo $pagelink; ?>" <?php echo ($pagelink==$this->input->get('parent') || $pagelink==validate_value($page, 'parent'))? "selected":''; ?>><?php echo $caption; ?></option>
<option value="<?php echo $pagelink; ?>" <?php echo ($pagelink==$parent || $pagelink==validate_value($page, 'parent'))? "selected":''; ?>><?php echo $caption; ?></option>
<?php endforeach; ?>
</select>
</div>
Expand Down Expand Up @@ -76,11 +77,4 @@
</div>
</div>

</form>

<script>
// $(function() {
// $("textarea#pagedownMe").pagedownBootstrap();
// $('.wmd-preview').addClass('well').css('display', 'none');
// });
</script>
</form>
6 changes: 3 additions & 3 deletions system/application/modules/pages/views/page_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<small><a href="<?php echo site_url($content['url']); ?>" target="_blank" class="page-url"><?php echo $content['url']; ?></a></small>
<div class="align-right pull-right">
<div class="option">
<a href="<?php echo site_url('panel/pages/edit?page='.$content['url']); ?>" class="edit" title="Edit"><span class="fa fa-edit"></span></a>
<a href="<?php echo site_url('panel/pages/create?parent='.$content['url']); ?>" class="add" title="Add Subpage"><span class="fa fa-plus"></span></a>
<a href="<?php echo site_url('panel/pages/delete?page='.$content['url']); ?>" class="remove" title="Delete"><span class="fa fa-times"></span></a>
<a href="<?php echo site_url('panel/pages/edit/'.$content['url']); ?>" class="edit" title="Edit"><span class="fa fa-edit"></span></a>
<a href="<?php echo site_url('panel/pages/create/'.$content['url']); ?>" class="add" title="Add Subpage"><span class="fa fa-plus"></span></a>
<a href="<?php echo site_url('panel/pages/delete/'.$content['url']); ?>" class="remove" title="Delete"><span class="fa fa-times"></span></a>
</div>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions system/application/modules/pages/views/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,23 @@
var change_attributes = function(e, d){
if(d.dest){
e.data('url', d.dest+'/'+d.page);

e.children('.dd3-content').children('small').children('.page-url')
.attr('href', BASE_URL+d.dest+'/'+d.page).html(d.dest+'/'+d.page);

var opts = e.children('.dd3-content').children('div').children('.option');
opts.children('.edit').attr('href', BASE_URL+'panel/pages/edit/'+d.dest+'/'+d.page);
opts.children('.add').attr('href', BASE_URL+'panel/pages/create/'+d.dest+'/'+d.page);
opts.children('.remove').attr('href', BASE_URL+'panel/pages/delete/'+d.dest+'/'+d.page);
} else {
e.data('url', d.page);
e.children('.dd3-content').children('small').children('.page-url')
.attr('href', BASE_URL+d.page).html(d.page);

var opts = e.children('.dd3-content').children('div').children('.option');
opts.children('.edit').attr('href', BASE_URL+'panel/pages/edit/'+d.page);
opts.children('.add').attr('href', BASE_URL+'panel/pages/create/'+d.page);
opts.children('.remove').attr('href', BASE_URL+'panel/pages/delete/'+d.page);
}

// if element has children, do the same thing
Expand Down

0 comments on commit 78e733b

Please sign in to comment.