Skip to content

Commit

Permalink
feat(abstractitiltarget): duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 12, 2023
1 parent 06b1e07 commit 3c09301
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 4 deletions.
48 changes: 48 additions & 0 deletions ajax/form_duplicate_target.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

include ('../../../inc/includes.php');

// Check if plugin is activated...
if (!(new Plugin())->isActivated('formcreator')) {
http_response_code(404);
die();
}

if (!isset($_REQUEST['itemtype']) || !isset($_REQUEST['items_id']) || !isset($_REQUEST['action'])) {
http_response_code(500);
die();
}

Session::checkRight('entity', UPDATE);
if (!PluginFormcreatorCommon::getForm()->duplicateTarget($_REQUEST)) {
http_response_code(500);
}
13 changes: 13 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2493,4 +2493,17 @@ public static function getMailImage() {
public static function getNoMailImage() {
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
}

public function getCloneRelations(): array {
return [
PluginFormcreatorTarget_Actor::class,
PluginFormcreatorCondition::class,
];
}

public function prepareInputForClone($input) {
$input = parent::prepareInputForClone($input);
$input['_skip_create_actors'] = true;
return $input;
}
}
8 changes: 7 additions & 1 deletion inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/

use Glpi\Application\View\TemplateRenderer;
use Glpi\Toolbox\Sanitizer;
use \Glpi\Features\Clonable;

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
Expand All @@ -43,6 +43,7 @@ abstract class PluginFormcreatorAbstractTarget extends CommonDBChild implements
PluginFormcreatorConditionnableInterface,
PluginFormcreatorTranslatableInterface
{
use Clonable;
use PluginFormcreatorConditionnableTrait;
use PluginFormcreatorExportableTrait;
use PluginFormcreatorTranslatable;
Expand Down Expand Up @@ -553,4 +554,9 @@ protected function showDestinationEntitySetings($rand) {
echo '</td>';
echo '</tr>';
}

public function prepareInputForClone($input) {
unset($input['uuid']);
return $input;
}
}
5 changes: 5 additions & 0 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,9 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
}
return $this->deleteByCriteria($keepCriteria);
}

public function prepareInputForClone($input) {
unset($input['uuid']);
return $input;
}
}
2 changes: 2 additions & 0 deletions inc/fieldinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,6 @@ public function show(string $domain, bool $canEdit = true): string;
* @return void
*/
public function setFormAnswer(PluginFormcreatorFormAnswer $form_answer): void;

public function getRawValue();
}
36 changes: 34 additions & 2 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,17 @@ public function showTargets($ID, $options = []) {

echo '<td align="center" width="32">';
echo '<i
class="far fa-trash-alt formcreator_delete_target"
class="far fa-clone plugin_formcreator_duplicate_target"
alt="*"
title="' . __('Duplicate', 'formcreator') . '"
data-itemtype="' . get_class($target) . '"
data-items-id="' . $targetId . '"
align="absmiddle"
style="cursor: pointer"
></i>';
echo '&nbsp;';
echo '<i
class="far fa-trash-alt plugin_formcreator_delete_target"
alt="*"
title="' . __('Delete', 'formcreator') . '"
data-itemtype="' . get_class($target) . '"
Expand Down Expand Up @@ -2267,7 +2277,29 @@ public function addTarget($input) {
}

/**
* Delete a target fromfor the form
* Duplicate a target for the form
*
* @param aray $input
* @return boolean
*/
public function duplicateTarget($input) {
$itemtype = $input['itemtype'];
if (!in_array($itemtype, PluginFormcreatorForm::getTargetTypes())) {
Session::addMessageAfterRedirect(
__('Unsupported target type.', 'formcreator'),
false,
ERROR
);
return false;
}

$item = $itemtype::getById($input['items_id']);
$item->clone();
return true;
}

/**
* Delete a target for the form
*
* @param aray $input
* @return boolean
Expand Down
8 changes: 8 additions & 0 deletions inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* ---------------------------------------------------------------------
*/

use Glpi\Features\Clonable;
use GlpiPlugin\Formcreator\Exception\ImportFailureException;
use GlpiPlugin\Formcreator\Exception\ExportFailureException;

Expand All @@ -38,6 +39,7 @@

class PluginFormcreatorTarget_Actor extends CommonDBChild implements PluginFormcreatorExportableInterface
{
use Clonable;
use PluginFormcreatorExportableTrait;

static public $itemtype = 'itemtype';
Expand Down Expand Up @@ -311,4 +313,10 @@ public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : boo
}
return $this->deleteByCriteria($keepCriteria);
}

public function prepareInputForClone($input) {
$input['actor_value_' . $input['actor_type']] = $input['actor_value'];
unset($input['uuid']);
return $input;
}
}
1 change: 1 addition & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Glpi\Application\View\TemplateRenderer;
use Glpi\Toolbox\Sanitizer;


if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
Expand Down
19 changes: 18 additions & 1 deletion js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,24 @@ function plugin_formcreator_addTarget(items_id) {
});
}

$(document).on('click', '.formcreator_delete_target', function() {
$(document).on('click', '.plugin_formcreator_duplicate_target', function() {
if(confirm(i18n.textdomain('formcreator').__('Are you sure you want to duplicate this target:', 'formcreator'))) {
$.post({
url: formcreatorRootDoc + '/ajax/form_duplicate_target.php',
data: {
action: 'duplicate_target',
itemtype: $(this).data('itemtype'),
items_id: $(this).data('items-id'),
}
}).done(function () {
reloadTab();
}).fail(function () {
displayAjaxMessageAfterRedirect();
});
}
});

$(document).on('click', '.plugin_formcreator_delete_target', function() {
if(confirm(i18n.textdomain('formcreator').__('Are you sure you want to delete this target:', 'formcreator'))) {
$.post({
url: formcreatorRootDoc + '/ajax/form_delete_target.php',
Expand Down

0 comments on commit 3c09301

Please sign in to comment.