-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputfieldRepeaterMatrixDuplicate.module
More file actions
346 lines (311 loc) · 17.5 KB
/
InputfieldRepeaterMatrixDuplicate.module
File metadata and controls
346 lines (311 loc) · 17.5 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php namespace ProcessWire;
/**
* @author flipzoom; Media - David Karich
* @contact David Karich <david.karich@flipzoom.de>
* @website www.flipzoom.de
* @create 2019-03-26
* @updated 2020-09-17
* @updated 2020-10-01
* @updated 2022-05-19
* @style Tab size: 4 / Soft tabs: YES
* ----------------------------------------------------------------------------------
* @licence
* Copyright (c) 2022 flipzoom; Media - David Karich
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions: The above copyright notice and
* this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ----------------------------------------------------------------------------------
* @contributors: The fundament for copying multiple items was created by @Autofahrn - THX!
* @contributors: A BugFix suggestion when additional repeater fields are present was contributed by @joshua - THX!
*/
class InputfieldRepeaterMatrixDuplicate extends WireData implements Module, ConfigurableModule {
/**
* ------------------------------------------------------------------------
* Module info
* ------------------------------------------------------------------------
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => __('Repeater Matrix Item Duplicator', __FILE__),
'summary' => __('With this module you can duplicate multible RepeaterMatrix-Items from one page to another which has the same matrix field. Please check the Read Me for restrictions.', __FILE__),
'author' => 'David Karich - flipzoom; Media',
'href' => 'https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate',
'version' => 202,
'autoload' => 'template=admin',
'singular' => true,
'icon' => 'clipboard',
'requires' => array('ProcessWire>=3.0.80', 'FieldtypeRepeaterMatrix')
);
}
/**
* ------------------------------------------------------------------------
* Default configuration values
* ------------------------------------------------------------------------
* @return array
*/
public static function getDefaultConfig() {
return array(
'excludeFields' => array(),
'excludeRoles' => array(),
'disableCopyDialog' => 0,
'disablePasteDialog' => 0
);
}
/**
* ------------------------------------------------------------------------
* Populate default configuration (will be overwritten after constructor
* with user's own configuration)
* ------------------------------------------------------------------------
* @return object
*/
public function __construct() {
foreach(self::getDefaultConfig() as $key => $value) {
$this->$key = $value;
}
}
/**
* ------------------------------------------------------------------------
* Build module configuration page
* ------------------------------------------------------------------------
* @param array $data
* @return mixed
*/
static public function getModuleConfigInputfields(array $data) {
// ------------------------------------------------------------------------
// Init vars
// ------------------------------------------------------------------------
$modules = wire('modules');
$input = wire('input');
$pwRoles = wire('roles');
$pwFields = wire('fields');
// ------------------------------------------------------------------------
// Build roles array
// ------------------------------------------------------------------------
foreach($pwRoles as $role) {
$roles[] = $role;
}
// ------------------------------------------------------------------------
// Build field array
// ------------------------------------------------------------------------
foreach($pwFields as $field) {
if(!$field->type instanceof FieldtypeRepeaterMatrix) continue;
$fields[] = $field;
}
// ------------------------------------------------------------------------
// Merge default config settings (custom values overwrite defaults)
// ------------------------------------------------------------------------
$defaults = self::getDefaultConfig();
$data = array_merge($defaults, $data);
// ------------------------------------------------------------------------
// On save actions
// ------------------------------------------------------------------------
if($input->post->submit_save_module) {
$excludeFields = (array) $input->post->excludeFields;
$excludeRoles = (array) $input->post->excludeRoles;
$disableCopyDialog = (int) $input->post->disableCopyDialog;
$disablePasteDialog = (int) $input->post->disablePasteDialog;
}
// ------------------------------------------------------------------------
// Build config screen form
// ------------------------------------------------------------------------
$form = new InputfieldWrapper();
// ------------------------------------------------------------------------
// Field select
// ------------------------------------------------------------------------
$field = $modules->get('InputfieldAsmSelect');
$field->name = 'excludeFields';
$field->label = __('Exclude Matrix Fields', __FILE__);
$field->icon = 'cubes';
$field->columnWidth = '100';
$field->description = __('Optionally select RepeaterMatrix fields to be excluded.', __FILE__);
foreach($fields as $_field) $field->addOption($_field->name);
$field->value = $data['excludeFields'];
$form->add($field);
// ------------------------------------------------------------------------
// Role select
// ------------------------------------------------------------------------
$field = $modules->get('InputfieldAsmSelect');
$field->name = 'excludeRoles';
$field->label = __('Exclude Roles', __FILE__);
$field->icon = 'user-circle-o';
$field->columnWidth = '100';
$field->description = __('Optionally select roles to be excluded.', __FILE__);
foreach($roles as $role) $field->addOption($role->name);
$field->value = $data['excludeRoles'];
$form->add($field);
// ------------------------------------------------------------------------
// Disable copy dialog
// ------------------------------------------------------------------------
$field = $modules->get('InputfieldCheckbox');
$field->name = 'disableCopyDialog';
$field->label = __('Disable copy dialog', __FILE__);
$field->icon = 'window-maximize';
$field->columnWidth = '50';
$field->description = __('Disable the dialog before copying an item.', __FILE__);
$field->checked = ($data['disableCopyDialog']) ? 'checked' : '';
$field->value = $data['disableCopyDialog'];
$form->add($field);
// ------------------------------------------------------------------------
// Disable paste dialog
// ------------------------------------------------------------------------
$field = $modules->get('InputfieldCheckbox');
$field->name = 'disablePasteDialog';
$field->label = __('Disable paste dialog', __FILE__);
$field->icon = 'window-maximize';
$field->columnWidth = '50';
$field->description = __('Disable the dialog before pasting an item.', __FILE__);
$field->checked = ($data['disablePasteDialog']) ? 'checked' : '';
$field->value = $data['disablePasteDialog'];
$form->add($field);
// ------------------------------------------------------------------------
// Build form
// ------------------------------------------------------------------------
return $form;
}
/**
* ------------------------------------------------------------------------
* Initialize the repeaters inputfield
* ------------------------------------------------------------------------
*/
public function init() {
// ------------------------------------------------------------------------
// Exclude certain roles
// ------------------------------------------------------------------------
$isAllowed = true;
if($this->user->isLoggedin()) {
foreach($this->user->roles as $role) {
if(in_array((string) $role->name, $this->excludeRoles)) {
$isAllowed = false;
break;
}
}
} else {
$isAllowed = false;
}
// ------------------------------------------------------------------------
// Bind hooks and scripts
// ------------------------------------------------------------------------
if($isAllowed) {
// ------------------------------------------------------------------------
// Load module info
// ------------------------------------------------------------------------
$info = $this->getModuleInfo();
$version = $info['version'];
// ------------------------------------------------------------------------
// Load scripts
// ------------------------------------------------------------------------
$this->config->scripts->add($this->config->urls->InputfieldRepeaterMatrixDuplicate . "InputfieldRepeaterMatrixDuplicate.js?v={$version}");
$this->config->styles->add($this->config->urls->InputfieldRepeaterMatrixDuplicate . "InputfieldRepeaterMatrixDuplicate.css?v={$version}");
// ------------------------------------------------------------------------
// Bind hooks
// ------------------------------------------------------------------------
$this->addHookAfter('Inputfield::render', $this, 'render');
$this->addHookAfter('Inputfield::processInput', $this, 'processInput');
// ------------------------------------------------------------------------
// Bind translations for js functions
// ------------------------------------------------------------------------
$this->config->js('InputfieldRepeaterMatrixDuplicate', array(
'labels' => array(
'duplicate' => $this->_x('Copy this item to the clipboard to paste into another page?', 'repeater-item-action'),
'paste_item' => $this->_x('Paste item', 'repeater-item-action'),
'paste_items' => $this->_x('Paste items', 'repeater-item-action'),
'confirm' => $this->_x('Are you sure you want to paste the item here? This will save the current page and inserts the item at the last position.', 'repeater-item-action'),
'clipboard' => $this->_x('The repeater item has been copied to the clipboard. Go to another page with the same repeater field and paste it. Note: the current version of the item can only be copied if the page has been saved.', 'repeater-item-action')
),
'config' => array(
'disableCopyDialog' => ((int) $this->disableCopyDialog === 0) ? false : true,
'disablePasteDialog' => ((int) $this->disablePasteDialog === 0) ? false : true
)
));
}
}
/**
* ------------------------------------------------------------------------
* Bind data attributes to repeater items
* ------------------------------------------------------------------------
* @param HookEvent $event
* @return HookEvent
*/
public function render(HookEvent $event) {
// ------------------------------------------------------------------------
// Init event vars
// ------------------------------------------------------------------------
$inputfield = $event->object;
$pageID = $this->sanitizer->int($this->input->id);
// ------------------------------------------------------------------------
// Only bind on repeater matrix field items and allowed fields
// ------------------------------------------------------------------------
if(!$inputfield instanceof InputfieldRepeaterMatrix OR in_array((string) $inputfield->name, $this->excludeFields)) return;
// ------------------------------------------------------------------------
// Render data attributes
// ------------------------------------------------------------------------
$event->return = str_replace("<li class='Inputfield", "<li data-rmd-target-field='{$inputfield->name}' data-rmd-current-page='{$pageID}' class='Inputfield", $event->return);
}
/**
* ------------------------------------------------------------------------
* Clone repeater item from on to another page
* ------------------------------------------------------------------------
* @param HookEvent $event
*/
public function processInput(HookEvent $event) {
// ------------------------------------------------------------------------
// Init vars
// ------------------------------------------------------------------------
$inputfield = $event->object;
$data = $event->arguments[0];
// ------------------------------------------------------------------------
// Check if target page and field is valid
// ------------------------------------------------------------------------
if($inputfield instanceof InputfieldRepeaterMatrix AND
!in_array((string) $inputfield->name, $this->excludeFields) AND
(string) $inputfield->name === (string) $data['rmdFieldName'] AND
$data['rmdTargetPage'] AND
$data['rmdSourceItems'] AND
(string) $data['rmdPasteTrigger'] === "1") {
// ------------------------------------------------------------------------
// Init vars and get pages
// ------------------------------------------------------------------------
$fieldName = $data['rmdFieldName'];
$targetPageID = (int) $data['rmdTargetPage'];
$targetPage = wire('pages')->get($targetPageID);
$sortOrder = wireCount($targetPage->$fieldName) + 1;
$sourcePages = explode(',', (string) $data['rmdSourceItems']);
$itemCounter = 0;
// ------------------------------------------------------------------------
// Check if items are present, then process each item individually and
// clone it to the target page
// ------------------------------------------------------------------------
if(is_array($sourcePages) AND wireCount($sourcePages) > 0) {
foreach($sourcePages as $_sourcePage) {
$sourcePage = wire('pages')->get((int) $_sourcePage);
$sourcePage->sort = ($sortOrder + $itemCounter);
$sourcePage->status = Page::statusUnpublished;
// ------------------------------------------------------------------------
// Clone item from source to target page
// ------------------------------------------------------------------------
if($sourcePage AND $targetPage) {
$targetPage->$fieldName->add($sourcePage);
$targetPage->$fieldName->save();
}
// ------------------------------------------------------------------------
// Increment sort order item counter
// ------------------------------------------------------------------------
$itemCounter++;
}
}
}
}
}