Skip to content

Commit

Permalink
feat(form_language): export / import
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 15, 2021
1 parent b765943 commit 1c74196
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 29 deletions.
2 changes: 0 additions & 2 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ abstract class PluginFormcreatorAbstractTarget extends CommonDBChild implements
/** @var boolean $skipCreateActors Flag to disable creation of actors after creation of the item */
protected $skipCreateActors = false;

abstract public function export(bool $remove_uuid = false);

abstract public function save(PluginFormcreatorFormAnswer $formanswer);

/**
Expand Down
2 changes: 1 addition & 1 deletion inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static function countItemsToImport(array $input) : int {
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/exportableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface PluginFormcreatorExportableInterface
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false);
public function export(bool $remove_uuid = false) : array;

/**
* Import an itemtype into the db
Expand Down
24 changes: 13 additions & 11 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ public static function countAvailableForm() {
return $nb;
}

function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand All @@ -1773,11 +1773,12 @@ function export(bool $remove_uuid = false) {
$export['usage_count']);

$subItems = [
'_profiles' => PluginFormcreatorForm_Profile::class,
'_sections' => PluginFormcreatorSection::class,
'_conditions' => PluginFormcreatorCondition::class,
'_targets' => (new self())->getTargetTypes(),
'_validators' => PluginFormcreatorForm_Validator::class,
'_profiles' => PluginFormcreatorForm_Profile::class,
'_sections' => PluginFormcreatorSection::class,
'_conditions' => PluginFormcreatorCondition::class,
'_targets' => (new self())->getTargetTypes(),
'_validators' => PluginFormcreatorForm_Validator::class,
'_translations' => PluginFormcreatorForm_Language::class,
];
$export = $this->exportChildrenObjects($subItems, $export, $remove_uuid);

Expand Down Expand Up @@ -2035,11 +2036,12 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
$linker->addObject($originalId, $item);

$subItems = [
'_profiles' => PluginFormcreatorForm_Profile::class,
'_sections' => PluginFormcreatorSection::class,
'_conditions' => PluginFormcreatorCondition::class,
'_targets' => (new self())->getTargetTypes(),
'_validators' => PluginFormcreatorForm_Validator::class,
'_profiles' => PluginFormcreatorForm_Profile::class,
'_sections' => PluginFormcreatorSection::class,
'_conditions' => PluginFormcreatorCondition::class,
'_targets' => (new self())->getTargetTypes(),
'_validators' => PluginFormcreatorForm_Validator::class,
'_translations' => PluginFormcreatorForm_Language::class,
];
$item->importChildrenObjects($item, $linker, $subItems, $input);

Expand Down
112 changes: 109 additions & 3 deletions inc/form_language.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
* ---------------------------------------------------------------------
*/

class PluginFormcreatorForm_Language extends CommonDBTM
use GlpiPlugin\Formcreator\Exception\ImportFailureException;

class PluginFormcreatorForm_Language extends CommonDBChild
implements PluginFormcreatorExportableInterface
{
static public $itemtype = PluginFormcreatorForm::class;
static public $items_id = 'plugin_formcreator_forms_id';

static $rightname = 'entity';

Expand Down Expand Up @@ -101,6 +106,12 @@ public function prepareInputForAdd($input) {
return false;
}

// generate a unique id
if (!isset($input['uuid'])
|| empty($input['uuid'])) {
$input['uuid'] = plugin_formcreator_getUuid();
}

return $input;
}

Expand All @@ -109,6 +120,12 @@ public function prepareInputForUpdate($input) {
unset($input[$formFk]);
unset($input['name']);

// generate a uniq id
if (!isset($input['uuid'])
|| empty($input['uuid'])) {
$input['uuid'] = plugin_formcreator_getUuid();
}

return $input;
}

Expand Down Expand Up @@ -447,7 +464,96 @@ public function getForbiddenStandardMassiveAction() {
];
}

public function updateTranslation($input) {
$a = null;
public static function countItemsToImport(array $input) : int {
return 1;
}

public function deleteObsoleteItems(CommonDBTM $container, array $exclude) : bool {
$keepCriteria = [
self::$items_id => $container->getID(),
];
if (count($exclude) > 0) {
$keepCriteria[] = ['NOT' => ['id' => $exclude]];
}
return $this->deleteByCriteria($keepCriteria);
}

public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
global $DB;

if (!isset($input['uuid']) && !isset($input['id'])) {
throw new ImportFailureException(sprintf('UUID or ID is mandatory for %1$s', static::getTypeName(1)));
}

// restore key and FK
$formFk = PluginFormcreatorForm::getForeignKeyField();
$input[$formFk] = $containerId;

$item = new self();
// Find an existing section to update, only if an UUID is available
$itemId = false;
/** @var string $idKey key to use as ID (id or uuid) */
$idKey = 'id';
if (isset($input['uuid'])) {
// Try to find an existing item to update
$idKey = 'uuid';
$itemId = plugin_formcreator_getFromDBByField(
$item,
'uuid',
$input['uuid']
);
}

// Escape text fields
foreach (['name'] as $key) {
$input[$key] = $DB->escape($input[$key]);
}

// Add or update form language
$originalId = $input[$idKey];
if ($itemId !== false) {
$input['id'] = $itemId;
$item->update($input);
} else {
unset($input['id']);
$item->useAutomaticOrdering = false;
$itemId = $item->add($input);
}
if ($itemId === false) {
$typeName = strtolower(self::getTypeName());
throw new ImportFailureException(sprintf(__('Failed to add or update the %1$s %2$s', 'formceator'), $typeName, $input['name']));
}

// add the form language to the linker
$linker->addObject($originalId, $item);

$form = new PluginFormcreatorForm();
$form->getFromDB($input[$formFk]);
$translations = $input['_strings'] ?? [];
$form->setTranslations($input['name'], $translations);

return $itemId;
}

public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}

$export = $this->fields;

// remove ID or UUID
$idToRemove = 'id';
if ($remove_uuid) {
$idToRemove = 'uuid';
}
unset($export[$idToRemove]);

$formFk = PluginFormcreatorForm::getForeignKeyField();
$form = new PluginFormcreatorForm();
$form->getFromDB($this->fields[$formFk]);
$export['_strings'] = $form->getTranslations($this->fields['name']);

return $export;
}
}
2 changes: 1 addition & 1 deletion inc/form_profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/form_validator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function countItemsToImport(array $input) : int {
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/item_targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function getTypeName($nb = 0) {
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ public static function countItemsToImport(array $input) : int {
return 1 + self::countChildren($input, $subItems);
}

public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/questiondependency.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public static function countItemsToImport($input) : int {
return 1;
}

public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/questionrange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function getFieldName() {
return $this->fieldName;
}

public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/questionregex.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getFieldName() {
return $this->fieldName;
}

public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public static function countItemsToImport(array $input) : int {
return 1 + self::countChildren($input, $subItems);
}

public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function countItemsToImport($input) : int {
*
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function getTaggableFields() {
* Export in an array all the data of the current instanciated target ticket
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ protected function getTaggableFields() {
* Export in an array all the data of the current instanciated targetticket
* @return array the array with all data (with sub tables)
*/
public function export(bool $remove_uuid = false) {
public function export(bool $remove_uuid = false) : array {
if ($this->isNewItem()) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,6 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_forms_languages` (
`plugin_formcreator_forms_id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`comment` text,
`uuid` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

0 comments on commit 1c74196

Please sign in to comment.