From 0d471599d7d25f5892540b8f8e1a8ee5cd4d942d Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 21 Mar 2017 15:35:16 -0600 Subject: [PATCH 1/2] Changed logic for order handling --- system/blueprints/pages/default.yaml | 22 +++++------ system/src/Grav/Common/Page/Page.php | 56 +++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/system/blueprints/pages/default.yaml b/system/blueprints/pages/default.yaml index 60f298fcc..70823a988 100644 --- a/system/blueprints/pages/default.yaml +++ b/system/blueprints/pages/default.yaml @@ -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 diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index fdcfe1f36..384032847 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -887,12 +887,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 +901,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; } /** @@ -2605,6 +2612,46 @@ class Page return $path; } + protected function doReorder($new_order) + { + if (!$this->_original) { + return; + } + + $siblings = $this->parent()->children(); + $siblings->order('slug', 'asc', $new_order); + + $counter = 1; + + // Reorder all moved pages. + foreach ($siblings as $slug => $page) { + $order = intval(trim($page->order(),'.')); + + if ($order) { + + if ($page->path() == $this->path()) { + // Handle current page; we do want to change ordering number, but nothing else. + $this->order($counter++); + $this->save(false); + } else { + // Handle all the other pages. + $page = Grav::instance()['pages']->get($page->path()); + + if ($page && $page->exists() && !$page->_action) { + + $page = $page->move($this->parent()); + $page->order($counter++); + $page->save(false); + + } + } + + + } + + } + } + /** * Moves or copies the page in filesystem. * @@ -2614,7 +2661,7 @@ class Page * * @throws Exception */ - protected function doRelocation($reorder) + protected function doRelocation($reorder = false) { if (!$this->_original) { return; @@ -2680,7 +2727,6 @@ class Page } } - $this->_original = null; } protected function setPublishState() From ef7ff9ec4ed54511b26ae35f01feb05b79812b2d Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 21 Mar 2017 20:20:08 -0600 Subject: [PATCH 2/2] Improved reorder logic and removed old logic --- system/src/Grav/Common/Page/Page.php | 82 +++++++--------------------- 1 file changed, 21 insertions(+), 61 deletions(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 384032847..b2abf120f 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -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()); @@ -2612,43 +2615,47 @@ class Page return $path; } + /** + * Reorders all siblings according to a defined order + * + * @param $new_order + */ protected function doReorder($new_order) { if (!$this->_original) { return; } + $pages = Grav::instance()['pages']; + $pages->init(); + + $this->_original->path($this->path()); + $siblings = $this->parent()->children(); $siblings->order('slug', 'asc', $new_order); - $counter = 1; + $counter = 0; // Reorder all moved pages. foreach ($siblings as $slug => $page) { $order = intval(trim($page->order(),'.')); + $counter++; if ($order) { - - if ($page->path() == $this->path()) { + if ($page->path() == $this->path() && $this->folderExists()) { // Handle current page; we do want to change ordering number, but nothing else. - $this->order($counter++); + $this->order($counter); $this->save(false); } else { // Handle all the other pages. - $page = Grav::instance()['pages']->get($page->path()); - - if ($page && $page->exists() && !$page->_action) { - + $page = $pages->get($page->path()); + if ($page && $page->folderExists() && !$page->_action) { $page = $page->move($this->parent()); - $page->order($counter++); + $page->order($counter); $page->save(false); - } } - - } - } } @@ -2657,61 +2664,14 @@ class Page * * @internal * - * @param bool $reorder - * * @throws Exception */ - protected function doRelocation($reorder = false) + protected function doRelocation() { if (!$this->_original) { return; } - // Do reordering. - if ($reorder && $this->order() != $this->_original->order()) { - /** @var Pages $pages */ - $pages = Grav::instance()['pages']; - - $parent = $this->parent(); - - // 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(); - } - } - - // If page was moved, take it out of the list. - if ($this->_action == 'move') { - unset($list[$this->slug()]); - } - - $list = array_values($list); - - // 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()) { - // Handle current page; we do want to change ordering number, but nothing else. - $this->order($order + 1); - } 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->save(false); - } - } - } - } if (is_dir($this->_original->path())) { if ($this->_action == 'move') { Folder::move($this->_original->path(), $this->path());