From c0a2817accea23837ab85b8ba31aabc7fbb20fc3 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Sat, 8 Feb 2020 15:18:54 +0100 Subject: new feature : duplicate close #14 --- MANUAL.md | 10 ++++++++++ app/class/Controllerpage.php | 42 ++++++++++++++++++++++++++++++++++++++++++ app/class/Page.php | 9 +++++++++ app/class/Routes.php | 2 ++ 4 files changed, 63 insertions(+) diff --git a/MANUAL.md b/MANUAL.md index 2bda948..575b2da 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -491,6 +491,12 @@ Type thoses commands after a __page_id__ Command used to add a page in the database. +##### /add:id + + /add: + +Will create a new page, as a copy of ``. + ##### /edit Command used to edit a page. If you're not logged in, it will ask for your credentials. @@ -511,6 +517,10 @@ Simply download the page as a JSON object file. Reserved to users that can edit Show a `var_dump` of the page object. This could be usefull for debbuging. +##### /duplicate + + /duplicate: + #### Home commands diff --git a/app/class/Controllerpage.php b/app/class/Controllerpage.php index 585db0a..92632fc 100644 --- a/app/class/Controllerpage.php +++ b/app/class/Controllerpage.php @@ -230,6 +230,16 @@ class Controllerpage extends Controller } } + public function addascopy(string $id, string $copy) + { + $id = idclean($id); + if($this->copy($copy, $id)) { + $this->routedirect('pageedit', ['page' => $this->page->id()]); + } else { + $this->routedirect('pageread/', ['page' => $id]); + } + } + public function confirmdelete($id) { $this->setpage($id, 'pageconfirmdelete'); @@ -304,6 +314,38 @@ class Controllerpage extends Controller $this->routedirect('home'); } + public function duplicate(string $srcid, string $targetid) + { + $targetid = idclean($targetid); + if ($this->copy($srcid, $targetid)) { + $this->routedirect('pageread/', ['page' => $targetid]); + } else { + $this->routedirect('pageread/', ['page' => idclean($srcid)]); + + } + } + + /** + * Copy a page to a new ID + * + * @param string $srcid Source page ID + * @param string $targetid Target page ID + */ + public function copy(string $srcid, string $targetid) + { + if ($this->user->iseditor()) { + $this->page = $this->pagemanager->get($srcid); + if($this->page !== false && $this->canedit() && $this->pagemanager->get($targetid) === false) { + $this->page->setid($targetid); + $this->page->setdatecreation(true); // Reset date of creation + $this->page->addauthor($this->user->id()); + $this->pagemanager->add($this->page); + return true; + } + } + return false; + } + public function update($id) { $this->setpage($id, 'pageupdate'); diff --git a/app/class/Page.php b/app/class/Page.php index e4692e3..c30352c 100644 --- a/app/class/Page.php +++ b/app/class/Page.php @@ -444,10 +444,19 @@ class Page extends Dbitem } } + /** + * DateTimeImmutable : set date + * string ISO8601 : set date + * true : reset to now + * + * @param string|DateTimeImmutable|true $datecreation Set or reset date of creation + */ public function setdatecreation($datecreation) { if ($datecreation instanceof DateTimeImmutable) { $this->datecreation = $datecreation; + } elseif ($datecreation === true) { + $this->datecreation = new DateTimeImmutable(null, timezone_open("Europe/Paris")); } else { $this->datecreation = DateTimeImmutable::createFromFormat(DateTime::ISO8601, $datecreation, new DateTimeZone('Europe/Paris')); } diff --git a/app/class/Routes.php b/app/class/Routes.php index 277d478..e71bf59 100644 --- a/app/class/Routes.php +++ b/app/class/Routes.php @@ -50,6 +50,7 @@ class Routes ['GET', '/[cid:page]/', 'Controllerpage#read', 'pageread/'], ['GET', '/[cid:page]', 'Controllerpage#read', 'pageread'], ['GET', '/[cid:page]/add', 'Controllerpage#add', 'pageadd'], + ['GET', '/[cid:page]/add:[cid:copy]', 'Controllerpage#addascopy', 'pageaddascopy'], ['GET', '/[cid:page]/edit', 'Controllerpage#edit', 'pageedit'], ['GET', '/[cid:page]/render', 'Controllerpage#render', 'pagerender'], ['GET', '/[cid:page]/log', 'Controllerpage#log', 'pagelog'], @@ -59,6 +60,7 @@ class Routes ['POST', '/[cid:page]/removeeditby', 'Controllerpage#removeeditby', 'pageremoveeditby'], ['GET', '/[cid:page]/delete', 'Controllerpage#confirmdelete', 'pageconfirmdelete'], ['POST', '/[cid:page]/delete', 'Controllerpage#delete', 'pagedelete'], + ['GET', '/[cid:page]/duplicate:[cid:duplicate]', 'Controllerpage#duplicate', 'pageduplicate'], ['GET', '/[cid:page]/[*]', 'Controllerpage#pagedirect', 'pageread/etoile'], ]); -- cgit v1.2.3