diff --git a/inc/abstracttarget.class.php b/inc/abstracttarget.class.php index 7d3d4732c..448209283 100644 --- a/inc/abstracttarget.class.php +++ b/inc/abstracttarget.class.php @@ -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); /** diff --git a/inc/condition.class.php b/inc/condition.class.php index aab435418..eacce562b 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -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; } diff --git a/inc/exportableinterface.class.php b/inc/exportableinterface.class.php index cd784d58e..afaf969c9 100644 --- a/inc/exportableinterface.class.php +++ b/inc/exportableinterface.class.php @@ -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 diff --git a/inc/form.class.php b/inc/form.class.php index 16fc11bb5..44d8037dd 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -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; } @@ -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); @@ -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); diff --git a/inc/form_language.class.php b/inc/form_language.class.php index bb1f41de1..158927ec6 100644 --- a/inc/form_language.class.php +++ b/inc/form_language.class.php @@ -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'; @@ -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; } @@ -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; } @@ -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; } } diff --git a/inc/form_profile.class.php b/inc/form_profile.class.php index b9d9be052..fe548781c 100644 --- a/inc/form_profile.class.php +++ b/inc/form_profile.class.php @@ -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; } diff --git a/inc/form_validator.class.php b/inc/form_validator.class.php index 876d4ad66..f4b213097 100644 --- a/inc/form_validator.class.php +++ b/inc/form_validator.class.php @@ -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; } diff --git a/inc/item_targetticket.class.php b/inc/item_targetticket.class.php index 16da83542..d9ad02abe 100644 --- a/inc/item_targetticket.class.php +++ b/inc/item_targetticket.class.php @@ -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; } diff --git a/inc/question.class.php b/inc/question.class.php index f05ea053e..662c9dfe0 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -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; } diff --git a/inc/questiondependency.class.php b/inc/questiondependency.class.php index 514840743..990c8aef0 100644 --- a/inc/questiondependency.class.php +++ b/inc/questiondependency.class.php @@ -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; } diff --git a/inc/questionrange.class.php b/inc/questionrange.class.php index 8538d537b..329500910 100644 --- a/inc/questionrange.class.php +++ b/inc/questionrange.class.php @@ -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; } diff --git a/inc/questionregex.class.php b/inc/questionregex.class.php index bccefe628..a97232649 100644 --- a/inc/questionregex.class.php +++ b/inc/questionregex.class.php @@ -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; } diff --git a/inc/section.class.php b/inc/section.class.php index 74e3ae52b..35ae70529 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -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; } diff --git a/inc/target_actor.class.php b/inc/target_actor.class.php index 7bae0e56c..17cc676b6 100644 --- a/inc/target_actor.class.php +++ b/inc/target_actor.class.php @@ -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; } diff --git a/inc/targetchange.class.php b/inc/targetchange.class.php index 197ef4710..464886bb2 100644 --- a/inc/targetchange.class.php +++ b/inc/targetchange.class.php @@ -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; } diff --git a/inc/targetticket.class.php b/inc/targetticket.class.php index 729cecdf6..22948a095 100644 --- a/inc/targetticket.class.php +++ b/inc/targetticket.class.php @@ -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; } diff --git a/install/mysql/plugin_formcreator_empty.sql b/install/mysql/plugin_formcreator_empty.sql index 6283e1f7f..8e767a6f2 100644 --- a/install/mysql/plugin_formcreator_empty.sql +++ b/install/mysql/plugin_formcreator_empty.sql @@ -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; \ No newline at end of file