Skip to content

Commit

Permalink
Merge branch 'feature/import_refactor' into support/2.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 28, 2020
2 parents 23447b4 + de2d2aa commit baa224d
Show file tree
Hide file tree
Showing 18 changed files with 393 additions and 457 deletions.
7 changes: 6 additions & 1 deletion inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
die("Sorry. You can't access this file directly");
}

class PluginFormcreatorCondition extends CommonDBTM implements PluginFormcreatorExportableInterface
class PluginFormcreatorCondition extends CommonDBChild implements PluginFormcreatorExportableInterface
{
use PluginFormcreatorExportable;

static public $itemtype = 'itemtype';
static public $items_id = 'items_id';

const SHOW_RULE_ALWAYS = 1;
const SHOW_RULE_HIDDEN = 2;
const SHOW_RULE_SHOWN = 3;
Expand Down
4 changes: 4 additions & 0 deletions inc/conditionnableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
* ---------------------------------------------------------------------
*/

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

interface PluginFormcreatorConditionnableInterface
{
/**
Expand Down
4 changes: 4 additions & 0 deletions inc/duplicatableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
* ---------------------------------------------------------------------
*/

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

interface PluginFormcreatorDuplicatableInterface
{
/**
Expand Down
149 changes: 149 additions & 0 deletions inc/exportable.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?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 - 2019 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
* ---------------------------------------------------------------------
*/

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

trait PluginFormcreatorExportable
{
/**
* Insert the export of sub items in the export
*
* @param array $subItems key/value pair list of sub items
* @param array $export the export of the object
* @param boolean $remove_uuid
* @return array
*/
public function exportChildrenObjects($subItems, $export, $remove_uuid = false) {
global $DB;

foreach ($subItems as $key => $itemtypes) {
if (!is_array($itemtypes)) {
$itemtypes = [$itemtypes];
}
$export[$key] = [];
foreach ($itemtypes as $itemtype) {
// $itemtype may be a CommonDBRelation type
// In such case it is still to the itemtype to build the export data
// because it may contain additinal data
// @see PluginFormcreatorItem_Ticket and its attribute 'link'
$list = [];
$allSubItems = $itemtype::getSQLCriteriaToSearchForItem($this->getType(), $this->getID());
foreach ($DB->request($allSubItems) as $row) {
/** @var CommonDBConnexity $subItem */
$subItem = new $itemtype();
$subItem->getFromDB($row['id']);
if (is_subclass_of($subItem, CommonDBRelation::class)) {
if ($row['itemtype_1'] == $row['itemtype_2'] && $row['is_1']) {
// the linked object is the same itemtype as the parent's itemtype
// this relation will be also exported in the reverse order
// Let's ignore it
// TODO: if someday an CommonDBRelation itemtype links 2 objects
// belonging to different forms, then we must ignore the relation
// only when the 2 linked objects belong to the same form.
// This will needs an extra check here.
continue;
}
}
$list[] = $subItem->export($remove_uuid);
}
if (!is_array($subItems[$key])) {
$export[$key] = $list;
} else {
$export[$key][$itemtype] = $list;
}
}
}

return $export;
}

/**
* Import children objects
*
* @param array PluginFormcreatorExportableInterface $item
* @param PluginFormcreatorLinker $linker
* @param array $input
* @return void
*/
public function importChildrenObjects($item, $linker, $subItems, $input) {
$itemId = $item->getID();
foreach ($subItems as $key => $itemtypes) {
if (!is_array($itemtypes)) {
if (!isset($input[$key])) {
$input[$key] = [];
}
$input[$key] = [$itemtypes => $input[$key]];
$itemtypes = [$itemtypes];
}
foreach ($itemtypes as $itemtype) {
$importedItems = [];
if (!isset($input[$key][$itemtype])) {
continue;
}
foreach ($input[$key][$itemtype] as $subInput) {
$importedItem = $itemtype::import(
$linker,
$subInput,
$itemId
);

// If $importedItem === false the item import is postponed
if ($importedItem !== false) {
$importedItems[] = $importedItem;
}
}
// Delete all other restrictions
$subItem = new $itemtype();
$subItem->deleteObsoleteItems($item, $importedItems);
}
}
}

public function deleteObsoleteItems(CommonDBTM $container, array $exclude)
{
if ($this instanceof CommonDBChild) {
$keepCriteria = [
'itemtype' => $container->getType(),
'items_id' => $container->getID(),
];
} else {
$keepCriteria = [
$container::getForeignKeyField() => $container->getID(),
];
}
if (count($exclude) > 0) {
$keepCriteria[] = ['NOT' => ['id' => $exclude]];
}
return $this->deleteByCriteria($keepCriteria);
}
}
11 changes: 11 additions & 0 deletions inc/exportableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public function export($remove_uuid = false);
* @return integer|false the id of the imported item or false on error
*/
public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0);

/**
* Delete all items belonging to a container and not in the list of items to keep
*
* Used when importing objects. Items not matching imported objects are deleted
* @param CommonDBTM $container instance of the object containing items of
* @param array $exclude list of ID to keep
*
* @return boolean
*/
public function deleteObsoleteItems(CommonDBTM $container, array $exclude);
}
Loading

0 comments on commit baa224d

Please sign in to comment.