1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 07:47:00 +02:00

Add feature request processwire/processwire-requests#186 which adds the configurable option to always use the full clone form. Also updated it to show a count of how many pages would be cloned when cloning children, and added dropdown options to the submit button so that you can optionally edit a page after cloning. Added icons to all inputs as well.

This commit is contained in:
Ryan Cramer
2024-04-18 11:33:24 -04:00
parent 76388b48e6
commit 9a6963a644

View File

@@ -12,6 +12,7 @@
* Optional GET variables:
* - redirect_page (int): Contains ID of page to redirect to after clone.
*
* @property bool|int $alwaysUseForm Disable the ajax shortcut for cloning pages and always use full form? 3.0.238+
* @method InputfieldForm buildForm()
* @method string render()
* @method void process()
@@ -19,13 +20,13 @@
*
*/
class ProcessPageClone extends Process {
class ProcessPageClone extends Process implements ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => __('Page Clone', __FILE__),
'summary' => __('Provides ability to clone/copy/duplicate pages in the admin. Adds a "copy" option to all applicable pages in the PageList.', __FILE__),
'version' => 105,
'version' => 106,
'autoload' => "template=admin", // Note: most Process modules should not be 'autoload', this is an exception.
'permission' => 'page-clone',
'permissions' => array(
@@ -63,6 +64,15 @@ class ProcessPageClone extends Process {
*/
protected $adminUrl = '';
/**
* Construct
*
*/
public function __construct() {
$this->set('alwaysUseForm', false);
parent::__construct();
}
/**
* Called when the API and $page loaded are ready
*
@@ -92,7 +102,7 @@ class ProcessPageClone extends Process {
'name' => $this->pageListActionLabel,
'url' => "{$this->adminUrl}page/clone/?id={$page->id}",
);
if(!$page->numChildren) {
if(!$page->numChildren && !$this->alwaysUseForm) {
$actions['copy']['url'] = "{$this->adminUrl}page/?action=clone&id={$page->id}";
$actions['copy']['ajax'] = true;
}
@@ -140,7 +150,7 @@ class ProcessPageClone extends Process {
*
*/
public function hasPermission(Page $page) {
$user = $this->user;
$user = $this->wire()->user;
$parent = $page->parent();
$parentTemplate = $parent->template;
$pageTemplate = $page->template;
@@ -235,11 +245,13 @@ class ProcessPageClone extends Process {
$titleField->attr('name', 'clone_page_title');
$titleField->attr('value', $suggested['title']);
$titleField->label = $this->_("Title of new page"); // Label for title field
$titleField->icon = 'file-text-o';
/** @var InputfieldPageName $nameField */
$nameField = $modules->get("InputfieldPageName");
$nameField->attr('name', 'clone_page_name');
$nameField->attr('value', $suggested['name']);
$nameField->icon = 'bookmark-o';
$nameField->parentPage = $page->parent;
$languages = $this->wire()->languages;
@@ -283,19 +295,26 @@ class ProcessPageClone extends Process {
$field = $modules->get("InputfieldCheckbox");
$field->attr('name', 'clone_page_unpublished');
$field->attr('value', 1);
$field->icon = 'eye-slash';
$field->label = $this->_("Make the new page unpublished?");
if($this->wire()->input->requestMethod('GET')) $field->attr('checked', 'checked');
$field->description = $this->_("If checked, the cloned page will be given an unpublished status so that it can't yet be seen on the front-end of your site.");
$form->add($field);
if($page->numChildren && $this->user->hasPermission('page-clone-tree', $page)) {
$numDescendents = $this->wire()->pages->count("has_parent=$page");
/** @var InputfieldCheckbox $field */
$field = $modules->get("InputfieldCheckbox");
$field->attr('name', 'clone_page_tree');
$field->attr('value', 1);
$field->label = $this->_("Copy children too?");
$field->icon = 'child';
$label = $this->_("Copy children too?");
$field->label = $label . ' ' . sprintf($this->_n('(~%d page)', '(~%d pages)', $numDescendents), $numDescendents);
$field->label2 = $label;
$field->description = $this->_("If checked, all children, grandchildren, etc., will also be cloned with this page.");
if($numDescendents > 20) {
$field->notes = $this->_("Warning: if there is a very large structure of pages below this, it may be time consuming or impossible to complete.");
}
$form->add($field);
/** @var InputfieldCheckbox $field */
@@ -303,14 +322,20 @@ class ProcessPageClone extends Process {
$field->attr('name', 'clone_tree_unpublished');
$field->attr('value', 1);
$field->label = $this->_("Make cloned children unpublished?");
$field->icon = 'eye-slash';
$field->description = $this->_("If checked, all cloned children, grandchildren, etc., will also be unpublished.");
$field->showIf = 'clone_page_tree=1';
$form->add($field);
}
/** @var InputfieldSubmit $field */
$submitLabel = $this->wire('processHeadline');
$field = $modules->get("InputfieldSubmit");
$field->attr('name', 'submit_clone');
$field->icon = 'copy';
$field->value = $submitLabel;
$field->addActionValue('list', sprintf($this->_('%s + List'), $submitLabel), 'tree');
$field->addActionValue('edit', sprintf($this->_('%s + Edit'), $submitLabel), 'edit');
$form->add($field);
$redirectPageID = (int) $this->wire()->input->get('redirect_page');
@@ -406,20 +431,21 @@ class ProcessPageClone extends Process {
$redirectURL = null;
$redirectID = (int) $input->post('redirect_page');
if($redirectID) {
if($input->post('submit_clone') === 'edit' && $clone->id && $clone->editable()) {
$redirectURL = $clone->editUrl();
} else if($redirectID) {
$redirectPage = $this->wire()->pages->get($redirectID);
if($redirectPage->viewable()) {
$redirectURL = $redirectPage->url();
}
$redirectURL .= "?open=$clone->id";
}
if(!$redirectURL) {
$redirectURL = $this->adminUrl . "page/list/";
$redirectURL = $this->adminUrl . "page/list/?open=$clone->id";
}
$redirectURL .= "?open=$clone->id";
$this->session->redirect($redirectURL);
$this->wire()->session->location($redirectURL);
}
/**
@@ -525,4 +551,23 @@ class ProcessPageClone extends Process {
return $this->page;
}
/**
* Module configuration
*
* @param InputfieldWrapper $inputfields
* @since 3.0.238
*
*/
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
$f = $inputfields->InputfieldCheckbox;
$f->attr('name', 'alwaysUseForm');
$f->label = $this->_('Always use full clone form?');
$f->description =
$this->_('When a page has no children, this module will use a time-saving shortcut to clone the page without leaving the page tree.') . ' ' .
$this->_('If you always want it to use the full clone options form (so that you can enter name, title, etc. before clone) then check this box.');
$f->themeOffset = 1;
if($this->alwaysUseForm) $f->attr('checked', 'checked');
$inputfields->add($f);
}
}