-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathProcessPageClone.module
More file actions
266 lines (227 loc) · 8.79 KB
/
ProcessPageClone.module
File metadata and controls
266 lines (227 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* ProcessWire Page Clone Process
*
* For more details about how Process modules work, please see:
* /wire/core/Process.php
*
* ProcessWire 2.x
* Copyright (C) 2015 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* https://processwire.com
*
*/
class ProcessPageClone extends Process {
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' => 103,
'autoload' => "process=ProcessPageList", // Note: most Process modules should not be 'autoload', this is an exception.
'permission' => 'page-clone',
'permissions' => array(
'page-clone' => 'Clone a page',
'page-clone-tree' => 'Clone a tree of pages'
),
'page' => array(
'name' => 'clone',
'title' => 'Clone',
'parent' => 'page',
'status' => Page::statusHidden,
)
);
}
/**
* The page being cloned
*
*/
protected $page;
/**
* The action link label used in the PageList
*
* Redefined for mult-language support in the ready method.
*
*/
protected $pageListActionLabel = 'Copy';
/**
* URL to the admin, cached here to avoid repeat $config calls
*
*/
protected $adminUrl = '';
/**
* Called when the API and $page loaded are ready
*
* We use this to determine whether we should add a hook or not.
* If we're in ProcessPageList, we add the hook.
*
*/
public function ready() {
$process = wire('page')->process;
if($process == 'ProcessPageList') {
$this->adminUrl = $this->wire('config')->urls->admin;
$this->pageListActionLabel = $this->_('Copy'); // Action label that appears in PageList
$this->addHookAfter("ProcessPageListRender::getPageActions", $this, 'hookPageListActions');
}
}
/**
* Hook into ProcessPageListRender::getPageActions to return a 'copy' action when appropriate
*
*/
public function hookPageListActions(HookEvent $event) {
$page = $event->arguments[0];
$actions = $event->return;
if($this->hasPermission($page)) $actions[] = array(
'cn' => 'Copy',
'name' => $this->pageListActionLabel,
'url' => "{$this->adminUrl}page/clone/?id={$page->id}"
);
$event->return = $actions;
}
/**
* Main execution: Display Copy Page form or process it
*
*/
public function ___execute() {
$this->headline($this->_('Copy Page')); // Headline
$error = $this->_("Unable to load page");
$id = (int) $this->wire('input')->get->id;
if(!$id) throw new WireException($error);
$this->page = $this->wire('pages')->get($id);
if($this->page->id < 2) throw new WireException($error);
if(!$this->hasPermission($this->page)) throw new WirePermissionException($error);
if($this->wire('input')->post->submit_clone) $this->process();
return $this->render();
}
/**
* Check if the current user has permission to clone the given page
*
* @param Page $page
* @return bool
*
*/
public function hasPermission(Page $page) {
$user = $this->user;
if($page->parent->template->noChildren) return false;
if($page->template->noParents) return false;
if(count($page->parent->template->childTemplates) && !in_array($page->template->id, $page->parent->template->childTemplates)) return false;
if(count($page->template->parentTemplates) && !in_array($page->parent->template->id, $page->template->parentTemplates)) return false;
if($user->isSuperuser()) return true;
if($user->hasPermission('page-create', $page) && $user->hasPermission('page-clone', $page) && $page->parent->addable()) return true;
return false;
}
/**
* Render a form asking for information to be used for the new cloned page.
*
*/
protected function ___buildForm() {
$form = $this->modules->get("InputfieldForm");
$form->attr('action', './?id=' . $this->page->id);
$form->attr('method', 'post');
$form->description = sprintf($this->_("This will make a copy of %s"), $this->page->path); // Form description/headline
$form->addClass('InputfieldFormFocusFirst');
$n = 1;
while(count($this->page->parent->children("include=all, name={$this->page->name}-$n"))) $n++;
$titleField = $this->modules->get("InputfieldPageTitle");
$titleField->attr('name', 'clone_page_title');
$titleField->attr('value', $this->page->title . ' ' . sprintf($this->_n("(copy)", "(copy %d)", $n), $n)); // Phrase added to page title to make it unique
$titleField->label = $this->_("Title of new page"); // Label for title field
$nameField = $this->modules->get("InputfieldPageName");
$nameField->attr('name', 'clone_page_name');
$nameField->attr('value', $this->page->name . '-' . $n);
$nameField->parentPage = $this->page->parent;
$languages = $this->wire('languages');
$useLanguages = $languages;
if($useLanguages) {
$titleUseLanguages = $this->page->template->fieldgroup->hasField('title') && $this->wire('fields')->get('title')->getInputfield($this->page)->useLanguages;
$nameUseLanguages = $this->wire('modules')->isInstalled('LanguageSupportPageNames');
if($titleUseLanguages) $titleField->useLanguages = true;
if($nameUseLanguages) $nameField->useLanguages = true;
foreach($languages as $language) {
if($language->isDefault()) continue;
if($titleUseLanguages) {
$value = $this->page->title->getLanguageValue($language);
$titleField->set("value$language->id", $value);
}
if($nameUseLanguages) {
$value = $this->page->get("name$language->id");
if(strlen($value)) $nameField->set("value$language->id", $value . '-' . $n);
}
}
}
if($this->page->template->fieldgroup->hasField('title')) $form->add($titleField);
$form->add($nameField);
$field = $this->modules->get("InputfieldCheckbox");
$field->attr('name', 'clone_page_unpublished');
$field->attr('value', 1);
$field->label = $this->_("Make the new page unpublished?");
$field->collapsed = Inputfield::collapsedYes;
$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($this->page->numChildren && $this->user->hasPermission('page-clone-tree', $this->page)) {
$field = $this->modules->get("InputfieldCheckbox");
$field->attr('name', 'clone_page_tree');
$field->attr('value', 1);
$field->label = $this->_("Copy children too?");
$field->description = $this->_("If checked, all children, grandchildren, etc., will also be cloned with this page.");
$field->notes = $this->_("Warning: if there is a very large structure of pages below this, it may be time consuming or impossible to complete.");
$field->collapsed = Inputfield::collapsedYes;
$form->add($field);
}
$field = $this->modules->get("InputfieldSubmit");
$field->attr('name', 'submit_clone');
$form->add($field);
return $form;
}
/**
* Render a form asking for information to be used for the new cloned page.
*
*/
protected function ___render() {
$form = $this->buildForm();
return $form->render();
}
/**
* User has clicked submit, so we create the clone, then redirect to it in PageList
*
*/
protected function ___process() {
$page = clone $this->page;
$input = $this->input;
$originalName = $page->name;
$this->session->CSRF->validate();
$form = $this->buildForm();
$form->processInput($this->wire('input')->post);
if($input->post->clone_page_unpublished) $page->addStatus(Page::statusUnpublished);
$cloneTree = $input->post->clone_page_tree && $this->user->hasPermission('page-clone-tree', $this->page);
$titleField = $form->get('clone_page_title');
$nameField = $form->get('clone_page_name');
if($nameField->useLanguages) {
foreach($this->wire('languages') as $language) {
$valueAttr = $language->isDefault() ? "value" : "value$language->id";
$nameAttr = $language->isDefault() ? "name" : "name$language->id";
$value = $nameField->get($valueAttr);
$page->set($nameAttr, $value);
}
} else {
$page->name = $nameField->value;
}
$clone = $this->pages->clone($page, $page->parent, $cloneTree);
if(!$clone->id) throw new WireException(sprintf($this->_("Unable to clone page %s"), $page->path));
if($titleField->useLanguages && is_object($clone->title)) {
foreach($this->wire('languages') as $language) {
$valueAttr = $language->isDefault() ? "value" : "value$language->id";
$value = $titleField->get($valueAttr);
$clone->title->setLanguageValue($language, $value);
}
} else {
$clone->title = $titleField->value;
}
$clone->save();
$this->message(sprintf($this->_('Cloned page "%1$s" to "%2$s"'), $originalName, $clone->name));
$this->session->redirect($this->config->urls->admin . 'page/list/?open=' . $clone->id);
}
public function getPage() {
return $this->page;
}
}