Skip to content

Commit 3c09301

Browse files
committed
feat(abstractitiltarget): duplicate
1 parent 06b1e07 commit 3c09301

9 files changed

+136
-4
lines changed

ajax/form_duplicate_target.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
include ('../../../inc/includes.php');
33+
34+
// Check if plugin is activated...
35+
if (!(new Plugin())->isActivated('formcreator')) {
36+
http_response_code(404);
37+
die();
38+
}
39+
40+
if (!isset($_REQUEST['itemtype']) || !isset($_REQUEST['items_id']) || !isset($_REQUEST['action'])) {
41+
http_response_code(500);
42+
die();
43+
}
44+
45+
Session::checkRight('entity', UPDATE);
46+
if (!PluginFormcreatorCommon::getForm()->duplicateTarget($_REQUEST)) {
47+
http_response_code(500);
48+
}

inc/abstractitiltarget.class.php

+13
Original file line numberDiff line numberDiff line change
@@ -2493,4 +2493,17 @@ public static function getMailImage() {
24932493
public static function getNoMailImage() {
24942494
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
24952495
}
2496+
2497+
public function getCloneRelations(): array {
2498+
return [
2499+
PluginFormcreatorTarget_Actor::class,
2500+
PluginFormcreatorCondition::class,
2501+
];
2502+
}
2503+
2504+
public function prepareInputForClone($input) {
2505+
$input = parent::prepareInputForClone($input);
2506+
$input['_skip_create_actors'] = true;
2507+
return $input;
2508+
}
24962509
}

inc/abstracttarget.class.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232

3333
use Glpi\Application\View\TemplateRenderer;
34-
use Glpi\Toolbox\Sanitizer;
34+
use \Glpi\Features\Clonable;
3535

3636
if (!defined('GLPI_ROOT')) {
3737
die("Sorry. You can't access this file directly");
@@ -43,6 +43,7 @@ abstract class PluginFormcreatorAbstractTarget extends CommonDBChild implements
4343
PluginFormcreatorConditionnableInterface,
4444
PluginFormcreatorTranslatableInterface
4545
{
46+
use Clonable;
4647
use PluginFormcreatorConditionnableTrait;
4748
use PluginFormcreatorExportableTrait;
4849
use PluginFormcreatorTranslatable;
@@ -553,4 +554,9 @@ protected function showDestinationEntitySetings($rand) {
553554
echo '</td>';
554555
echo '</tr>';
555556
}
557+
558+
public function prepareInputForClone($input) {
559+
unset($input['uuid']);
560+
return $input;
561+
}
556562
}

inc/condition.class.php

+5
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,9 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
374374
}
375375
return $this->deleteByCriteria($keepCriteria);
376376
}
377+
378+
public function prepareInputForClone($input) {
379+
unset($input['uuid']);
380+
return $input;
381+
}
377382
}

inc/fieldinterface.class.php

+2
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,6 @@ public function show(string $domain, bool $canEdit = true): string;
311311
* @return void
312312
*/
313313
public function setFormAnswer(PluginFormcreatorFormAnswer $form_answer): void;
314+
315+
public function getRawValue();
314316
}

inc/form.class.php

+34-2
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,17 @@ public function showTargets($ID, $options = []) {
522522

523523
echo '<td align="center" width="32">';
524524
echo '<i
525-
class="far fa-trash-alt formcreator_delete_target"
525+
class="far fa-clone plugin_formcreator_duplicate_target"
526+
alt="*"
527+
title="' . __('Duplicate', 'formcreator') . '"
528+
data-itemtype="' . get_class($target) . '"
529+
data-items-id="' . $targetId . '"
530+
align="absmiddle"
531+
style="cursor: pointer"
532+
></i>';
533+
echo '&nbsp;';
534+
echo '<i
535+
class="far fa-trash-alt plugin_formcreator_delete_target"
526536
alt="*"
527537
title="' . __('Delete', 'formcreator') . '"
528538
data-itemtype="' . get_class($target) . '"
@@ -2267,7 +2277,29 @@ public function addTarget($input) {
22672277
}
22682278

22692279
/**
2270-
* Delete a target fromfor the form
2280+
* Duplicate a target for the form
2281+
*
2282+
* @param aray $input
2283+
* @return boolean
2284+
*/
2285+
public function duplicateTarget($input) {
2286+
$itemtype = $input['itemtype'];
2287+
if (!in_array($itemtype, PluginFormcreatorForm::getTargetTypes())) {
2288+
Session::addMessageAfterRedirect(
2289+
__('Unsupported target type.', 'formcreator'),
2290+
false,
2291+
ERROR
2292+
);
2293+
return false;
2294+
}
2295+
2296+
$item = $itemtype::getById($input['items_id']);
2297+
$item->clone();
2298+
return true;
2299+
}
2300+
2301+
/**
2302+
* Delete a target for the form
22712303
*
22722304
* @param aray $input
22732305
* @return boolean

inc/target_actor.class.php

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131

32+
use Glpi\Features\Clonable;
3233
use GlpiPlugin\Formcreator\Exception\ImportFailureException;
3334
use GlpiPlugin\Formcreator\Exception\ExportFailureException;
3435

@@ -38,6 +39,7 @@
3839

3940
class PluginFormcreatorTarget_Actor extends CommonDBChild implements PluginFormcreatorExportableInterface
4041
{
42+
use Clonable;
4143
use PluginFormcreatorExportableTrait;
4244

4345
static public $itemtype = 'itemtype';
@@ -311,4 +313,10 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
311313
}
312314
return $this->deleteByCriteria($keepCriteria);
313315
}
316+
317+
public function prepareInputForClone($input) {
318+
$input['actor_value_' . $input['actor_type']] = $input['actor_value'];
319+
unset($input['uuid']);
320+
return $input;
321+
}
314322
}

inc/targetticket.class.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Glpi\Application\View\TemplateRenderer;
3535
use Glpi\Toolbox\Sanitizer;
3636

37+
3738
if (!defined('GLPI_ROOT')) {
3839
die("Sorry. You can't access this file directly");
3940
}

js/scripts.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,24 @@ function plugin_formcreator_addTarget(items_id) {
14421442
});
14431443
}
14441444

1445-
$(document).on('click', '.formcreator_delete_target', function() {
1445+
$(document).on('click', '.plugin_formcreator_duplicate_target', function() {
1446+
if(confirm(i18n.textdomain('formcreator').__('Are you sure you want to duplicate this target:', 'formcreator'))) {
1447+
$.post({
1448+
url: formcreatorRootDoc + '/ajax/form_duplicate_target.php',
1449+
data: {
1450+
action: 'duplicate_target',
1451+
itemtype: $(this).data('itemtype'),
1452+
items_id: $(this).data('items-id'),
1453+
}
1454+
}).done(function () {
1455+
reloadTab();
1456+
}).fail(function () {
1457+
displayAjaxMessageAfterRedirect();
1458+
});
1459+
}
1460+
});
1461+
1462+
$(document).on('click', '.plugin_formcreator_delete_target', function() {
14461463
if(confirm(i18n.textdomain('formcreator').__('Are you sure you want to delete this target:', 'formcreator'))) {
14471464
$.post({
14481465
url: formcreatorRootDoc + '/ajax/form_delete_target.php',

0 commit comments

Comments
 (0)