Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to duplicate pages #7

Merged
merged 7 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions assets/js/pages-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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,
der-On marked this conversation as resolved.
Show resolved Hide resolved
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
*/
Expand Down
40 changes: 40 additions & 0 deletions controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
der-On marked this conversation as resolved.
Show resolved Hide resolved
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
der-On marked this conversation as resolved.
Show resolved Hide resolved
])
];

return $result;
}

public function onDelete()
{
$this->validateRequestTheme();
Expand Down
9 changes: 8 additions & 1 deletion controllers/index/_page_toolbar.htm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

<?= $this->makePartial('~/modules/cms/controllers/index/_button_reset.htm'); ?>

<button
type="button"
class="btn btn-default empty oc-icon-clone <?php if (!$objectPath): ?>hide<?php endif ?>"
data-control="duplicate-object"
>
</button>

<button
type="button"
class="btn btn-default empty oc-icon-trash-o <?php if (!$objectPath): ?>hide<?php endif ?>"
Expand All @@ -33,4 +40,4 @@
</button>

<?= $this->makePartial('~/modules/cms/controllers/index/_button_lastmodified.htm'); ?>
</div>
</div>