diff --git a/front/form.form.php b/front/form.form.php index 850f6fd8f..9d7eb344c 100644 --- a/front/form.form.php +++ b/front/form.form.php @@ -113,6 +113,14 @@ Html::footer(); } else if (isset($_POST['import_send'])) { + Html::header( + PluginFormcreatorForm::getTypeName(2), + $_SERVER['PHP_SELF'], + 'admin', + 'PluginFormcreatorForm', + 'option' + ); + // Import form Session::checkRight('entity', UPDATE); $form->importJson($_REQUEST); diff --git a/inc/condition.class.php b/inc/condition.class.php index 0308c813b..f148026c0 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -165,6 +165,11 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con return $itemId; } + + public static function countItemsToImport($input) { + return 1; + } + /** * Export in an array all the data of the current instanciated condition * @param boolean $remove_uuid remove the uuid key diff --git a/inc/exportable.class.php b/inc/exportable.class.php index 1362d6971..53ca77aae 100644 --- a/inc/exportable.class.php +++ b/inc/exportable.class.php @@ -75,6 +75,7 @@ public function exportChildrenObjects($subItems, $export, $remove_uuid = false) * * @param array PluginFormcreatorExportableInterface $item * @param PluginFormcreatorLinker $linker + * @param $subItems * @param array $input * @return void */ @@ -111,4 +112,42 @@ public function importChildrenObjects($item, $linker, $subItems, $input) { } } } + + /** + * Count sub items + * + * @param array $input + * @param $subItems + * @return integer + */ + public static function countChildren($input, $subItems = []) { + if (count($subItems) < 1) { + return 1; + } + + $count = 0; + foreach($subItems as $key => $itemtypes) { + if (isset($input[$key])) { + // force array of itemetypes + if (!is_array($itemtypes)) { + if (!isset($input[$key])) { + $input[$key] = []; + } + $input[$key] = [$itemtypes => $input[$key]]; + $itemtypes = [$itemtypes]; + } + + foreach ($itemtypes as $itemtype) { + if (!isset($input[$key][$itemtype])) { + continue; + } + foreach ($input[$key][$itemtype] as $subInput) { + $count += $itemtype::countItemsToImport($subInput); + } + } + } + } + + return $count; + } } \ No newline at end of file diff --git a/inc/exportableinterface.class.php b/inc/exportableinterface.class.php index 50d281b47..dc828befc 100644 --- a/inc/exportableinterface.class.php +++ b/inc/exportableinterface.class.php @@ -64,4 +64,11 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con * @return boolean */ public function deleteObsoleteItems(CommonDBTM $container, array $exclude); + + /** get the count of inner objects to import + * @param array $input data to import + * + * return integer + */ + public static function countItemsToImport($input); } diff --git a/inc/form.class.php b/inc/form.class.php index f912e7749..cb87c803e 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -1841,10 +1841,17 @@ public function importJson($params = []) { continue; } + // Get the total count of objects to import, for the progressbar + $linker = new PluginFormcreatorLinker(); + foreach($forms_toimport['forms'] as $form) { + $linker->countItems($form, self::class); + } + $linker->initProgressBar(); + $success = true; foreach ($forms_toimport['forms'] as $form) { + $linker->reset(); set_time_limit(30); - $linker = new PluginFormcreatorLinker(); try { self::import($linker, $form); } catch (ImportFailureException $e) { @@ -1979,6 +1986,18 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con return $itemId; } + public static function countItemsToImport($input) { + // Code similar to ImportChildrenObjects + $subItems = [ + '_profiles' => PluginFormcreatorForm_Profile::class, + '_sections' => PluginFormcreatorSection::class, + '_conditions' => PluginFormcreatorCondition::class, + '_targets' => (new self())->getTargetTypes(), + '_validators' => PluginFormcreatorForm_Validator::class, + ]; + return 1 + self::countChildren($input, $subItems); + } + public function createDocumentType() { $documentType = new DocumentType(); $success = $documentType->add([ diff --git a/inc/form_profile.class.php b/inc/form_profile.class.php index 550ad7404..2ab50211a 100644 --- a/inc/form_profile.class.php +++ b/inc/form_profile.class.php @@ -251,6 +251,10 @@ public function export($remove_uuid = false) { return $form_profile; } + public static function countItemsToImport($input) { + return 1; + } + public function deleteObsoleteItems(CommonDBTM $container, array $exclude) { $keepCriteria = [ diff --git a/inc/form_validator.class.php b/inc/form_validator.class.php index bed08ab2b..9b5849cea 100644 --- a/inc/form_validator.class.php +++ b/inc/form_validator.class.php @@ -118,6 +118,10 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $for return $itemId; } + public static function countItemsToImport($input) { + return 1; + } + /** * Export in an array all the data of the current instanciated validator * @param boolean $remove_uuid remove the uuid key diff --git a/inc/item_targetticket.class.php b/inc/item_targetticket.class.php index ab596169a..e7cb8050c 100644 --- a/inc/item_targetticket.class.php +++ b/inc/item_targetticket.class.php @@ -154,6 +154,10 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con return $itemId; } + public static function countItemsToImport($input) { + return 1; + } + public function prepareInputForAdd($input) { // generate a unique id if (!isset($input['uuid']) diff --git a/inc/linker.class.php b/inc/linker.class.php index 29df7447c..2aa6718ee 100644 --- a/inc/linker.class.php +++ b/inc/linker.class.php @@ -29,6 +29,8 @@ * --------------------------------------------------------------------- */ +use GlpiPlugin\Formcreator\Exception\ImportFailureException; + if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } @@ -39,6 +41,33 @@ class PluginFormcreatorLinker private $postponed = []; + private $progress = 0; + + private $totalCount = 0; + + public function countItems($input, $itemtype) { + // Get the total count of objects to import, for the progressbar + $this->totalCount += $itemtype::countItemsToImport($input); + } + + public function getProgress() { + return $this->progress; + } + + public function getTotalCount() { + return $this->totalCount; + } + + public function initProgressBar() { + if (!isCommandLine() && !isAPI()) { + echo "
".__('Importing', 'formcreator')." |
---|
"; + Html::createProgressBar(__('Import in progress')); + echo " |