Merge branch 'feature/ordering-refactor' into develop

This commit is contained in:
Andy Miller
2017-03-22 12:09:47 -06:00
2 changed files with 60 additions and 54 deletions

View File

@@ -117,17 +117,6 @@ form:
title: PLUGIN_ADMIN.SETTINGS
underline: true
ordering:
type: toggle
label: PLUGIN_ADMIN.FOLDER_NUMERIC_PREFIX
help: PLUGIN_ADMIN.FOLDER_NUMERIC_PREFIX_HELP
highlight: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
folder:
type: text
label: PLUGIN_ADMIN.FOLDER_NAME
@@ -165,6 +154,17 @@ form:
title: PLUGIN_ADMIN.ORDERING
underline: true
ordering:
type: toggle
label: PLUGIN_ADMIN.FOLDER_NUMERIC_PREFIX
help: PLUGIN_ADMIN.FOLDER_NUMERIC_PREFIX_HELP
highlight: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
order:
type: order
label: PLUGIN_ADMIN.PAGE_ORDER

View File

@@ -804,6 +804,9 @@ class Page
if ($name == 'folder') {
return preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder);
}
if ($name == 'slug') {
return $this->slug();
}
if ($name == 'name') {
$language = $this->language() ? '.' . $this->language() : '';
$name_val = str_replace($language . '.md', '', $this->name());
@@ -887,12 +890,12 @@ class Page
/**
* Save page if there's a file assigned to it.
*
* @param bool $reorder Internal use.
* @param bool|mixed $reorder Internal use.
*/
public function save($reorder = true)
{
// Perform move, copy or reordering if needed.
$this->doRelocation($reorder);
// Perform move, copy [or reordering] if needed.
$this->doRelocation();
$file = $this->file();
if ($file) {
@@ -901,6 +904,13 @@ class Page
$file->markdown($this->raw_content);
$file->save();
}
// Perform reorder if required
if ($reorder && is_array($reorder)) {
$this->doReorder($reorder);
}
$this->_original = null;
}
/**
@@ -2606,65 +2616,62 @@ class Page
}
/**
* Moves or copies the page in filesystem.
* Reorders all siblings according to a defined order
*
* @internal
*
* @param bool $reorder
*
* @throws Exception
* @param $new_order
*/
protected function doRelocation($reorder)
protected function doReorder($new_order)
{
if (!$this->_original) {
return;
}
// Do reordering.
if ($reorder && $this->order() != $this->_original->order()) {
/** @var Pages $pages */
$pages = Grav::instance()['pages'];
$pages = Grav::instance()['pages'];
$pages->init();
$parent = $this->parent();
$this->_original->path($this->path());
// Extract visible children from the parent page.
$list = [];
/** @var Page $page */
foreach ($parent->children()->visible() as $page) {
if ($page->order()) {
$list[$page->slug] = $page->path();
}
}
$siblings = $this->parent()->children();
$siblings->order('slug', 'asc', $new_order);
// If page was moved, take it out of the list.
if ($this->_action == 'move') {
unset($list[$this->slug()]);
}
$counter = 0;
$list = array_values($list);
// Reorder all moved pages.
foreach ($siblings as $slug => $page) {
$order = intval(trim($page->order(),'.'));
$counter++;
// Then add it back to the new location (if needed).
if ($this->order()) {
array_splice($list, min($this->order() - 1, count($list)), 0, [$this->path()]);
}
// Reorder all moved pages.
foreach ($list as $order => $path) {
if ($path == $this->path()) {
if ($order) {
if ($page->path() == $this->path() && $this->folderExists()) {
// Handle current page; we do want to change ordering number, but nothing else.
$this->order($order + 1);
$this->order($counter);
$this->save(false);
} else {
// Handle all the other pages.
$page = $pages->get($path);
if ($page && $page->exists() && !$page->_action && $page->order() != $order + 1) {
$page = $page->move($parent);
$page->order($order + 1);
$page = $pages->get($page->path());
if ($page && $page->folderExists() && !$page->_action) {
$page = $page->move($this->parent());
$page->order($counter);
$page->save(false);
}
}
}
}
}
/**
* Moves or copies the page in filesystem.
*
* @internal
*
* @throws Exception
*/
protected function doRelocation()
{
if (!$this->_original) {
return;
}
if (is_dir($this->_original->path())) {
if ($this->_action == 'move') {
Folder::move($this->_original->path(), $this->path());
@@ -2680,7 +2687,6 @@ class Page
}
}
$this->_original = null;
}
protected function setPublishState()