Skip to content

Commit

Permalink
Add button to duplicate pages (#7)
Browse files Browse the repository at this point in the history
Credit to Ondrej Brinkel <[email protected]>
  • Loading branch information
der-On authored Dec 13, 2023
1 parent c793c12 commit fbd2713
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 2 deletions.
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,
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
49 changes: 49 additions & 0 deletions controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion controllers/index/_page_toolbar.htm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@

<?= $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 ?>"
title="<?= e(trans('winter.pages::lang.editor.duplicate')) ?>"
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 +41,4 @@
</button>

<?= $this->makePartial('~/modules/cms/controllers/index/_button_lastmodified.htm'); ?>
</div>
</div>
1 change: 1 addition & 0 deletions lang/de/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
'layout' => 'Layout',
'description' => 'Beschreibung',
'preview' => 'Vorschau',
'duplicate' => 'Duplizieren',
'enter_fullscreen' => 'Vollbildmodus einschalten',
'exit_fullscreen' => 'Vollbildmodus verlassen',
'hidden' => 'Verstecken',
Expand Down
1 change: 1 addition & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
'layout' => 'Layout',
'description' => 'Description',
'preview' => 'Preview',
'duplicate' => 'Duplicate',
'enter_fullscreen' => 'Enter fullscreen mode',
'exit_fullscreen' => 'Exit fullscreen mode',
'hidden' => 'Hidden',
Expand Down
1 change: 1 addition & 0 deletions lang/fr/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
'layout' => 'Maquette',
'description' => 'Description',
'preview' => 'Aperçu',
'duplicate' => 'Dupliquer',
'enter_fullscreen' => 'Activer le mode plein écran',
'exit_fullscreen' => 'Annuler le mode plein écran',
'hidden' => 'Caché',
Expand Down
1 change: 1 addition & 0 deletions lang/nl/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
'layout' => 'Layout',
'description' => 'Beschrijving',
'preview' => 'Voorbeeld',
'duplicate' => 'Dupliceren',
'enter_fullscreen' => 'Volledig scherm openen',
'exit_fullscreen' => 'Volledig scherm afsluiten',
'hidden' => 'Verborgen',
Expand Down
2 changes: 1 addition & 1 deletion lang/pl/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@
'child_pages_description' => 'Wyświetla listę stron podrzędnych dla aktualnej strony',
'static_menu_menu_code' => 'Podaj kod systemowy menu',
],
];
];
1 change: 1 addition & 0 deletions lang/sk/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
'layout' => 'Layout',
'description' => 'Popis',
'preview' => 'Náhľad',
'duplicate' => 'Duplikovať',
'enter_fullscreen' => 'Zapnúť režim celej obrazovky',
'exit_fullscreen' => 'Vypnúť režim celej obrazovky',
'hidden' => 'Skrytá',
Expand Down

0 comments on commit fbd2713

Please sign in to comment.