diff --git a/assets/js/pages-page.js b/assets/js/pages-page.js index 4b28c06..c79ccf6 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 e512d8c..3b9d65d 100644 --- a/controllers/Index.php +++ b/controllers/Index.php @@ -281,6 +281,55 @@ public function onCreateObject(): array return $result; } + public function onDuplicateObject(): array + { + $this->validateRequestTheme(); + $type = $this->getObjectType(); + + $object = ObjectHelper::loadObject( + $this->theme, + $type, + trim(Request::input('objectPath')) + ); + $parentPage = null; + $parent = null; + + if ($type === 'page') { + $parentPage = $object->getParent() ?? null; + + if ($parentPage) { + $fileName = $parentPage->fileName; + $ext = pathinfo($fileName, PATHINFO_EXTENSION); + $parent = substr( + $fileName, 0, -strlen('.' . $ext) + ); + } + } + + $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(): array { $this->validateRequestTheme(); diff --git a/controllers/index/_page_toolbar.htm b/controllers/index/_page_toolbar.htm index ccb7138..44d0498 100644 --- a/controllers/index/_page_toolbar.htm +++ b/controllers/index/_page_toolbar.htm @@ -23,6 +23,14 @@ makePartial('~/modules/cms/controllers/index/_button_reset.htm'); ?> + +