From ff05c23221a3e783cae96b7f69f4dbc5dc70d7e5 Mon Sep 17 00:00:00 2001 From: Ondrej Brinkel Date: Mon, 14 Mar 2022 17:08:10 +0100 Subject: [PATCH 1/6] allows duplicating page --- assets/js/pages-page.js | 37 ++++++++++++++++++++++++++ controllers/Index.php | 40 +++++++++++++++++++++++++++++ controllers/index/_page_toolbar.htm | 9 ++++++- 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/assets/js/pages-page.js b/assets/js/pages-page.js index d892a23..6253732 100644 --- a/assets/js/pages-page.js +++ b/assets/js/pages-page.js @@ -58,6 +58,13 @@ this.proxy(this.onCreateObject) ) + // Duplicate object button click + $(document).on( + 'click', + '#pages-master-tabs form[data-object-type=page] [data-control=duplicate-object]', + this.proxy(this.onDuplicateObject) + ) + // Submenu item is clicked in the sidebar $(document).on('submenu.oc.treeview', 'form.layout[data-content-id=pages]', this.proxy(this.onSidebarSubmenuItemClick)) @@ -367,6 +374,36 @@ return false } + /* + * Triggered when the Duplicate button is clicked on the page form + */ + PagesPage.prototype.onDuplicateObject = function(e) { + var self = this, + $button = $(e.target), + $form = $button.closest('form'), + parent = $button.data('parent') !== undefined ? $button.data('parent') : null, + type = $form.data('object-type') ? $form.data('object-type') : $form.data('template-type'), + tabId = type + Math.random() + + $.wn.stripeLoadIndicator.show() + $form.request('onDuplicateObject', { + data: { + type: type, + parent: parent + } + }).done(function(data){ + self.$masterTabs.ocTab('addTab', data.tabTitle, data.tab, tabId, $form.data('type-icon') + ' new-template') + $('#layout-side-panel').trigger('close.oc.sidePanel') + self.setPageTitle(data.tabTitle) + }).always(function(){ + $.wn.stripeLoadIndicator.hide() + }) + + e.stopPropagation() + + return false + } + /* * Triggered when an item is clicked in the sidebar submenu */ diff --git a/controllers/Index.php b/controllers/Index.php index 519e61d..74d7711 100644 --- a/controllers/Index.php +++ b/controllers/Index.php @@ -190,6 +190,46 @@ public function onCreateObject() return $result; } + public function onDuplicateObject() + { + $this->validateRequestTheme(); + + $type = Request::input('objectType'); + + $object = $this->loadObject($type, trim(Request::input('objectPath'))); + $parent = Request::input('parent'); + $parentPage = null; + + if ($type == 'page') { + if (strlen($parent)) { + $parentPage = StaticPage::load($this->theme, $parent); + } + } + + $className = get_class($object); + $data = $object->toArray(); + $duplicatedObject = new $className($data); + + $widget = $this->makeObjectFormWidget($type, $duplicatedObject); + $this->vars['objectPath'] = ''; + $this->vars['canCommit'] = $this->canCommitObject($duplicatedObject); + $this->vars['canReset'] = $this->canResetObject($duplicatedObject); + + $result = [ + 'tabTitle' => $this->getTabTitle($type, $duplicatedObject), + 'tab' => $this->makePartial('form_page', [ + 'form' => $widget, + 'objectType' => $type, + 'objectTheme' => $this->theme->getDirName(), + 'objectMtime' => null, + 'objectParent' => $parent, + 'parentPage' => $parentPage + ]) + ]; + + return $result; + } + public function onDelete() { $this->validateRequestTheme(); diff --git a/controllers/index/_page_toolbar.htm b/controllers/index/_page_toolbar.htm index ccb7138..875eee5 100644 --- a/controllers/index/_page_toolbar.htm +++ b/controllers/index/_page_toolbar.htm @@ -23,6 +23,13 @@ makePartial('~/modules/cms/controllers/index/_button_reset.htm'); ?> + +