Skip to content

Commit

Permalink
feat(form): progressbar for form 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 Aug 17, 2020
1 parent c9b3aa7 commit deb1e20
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 1 deletion.
8 changes: 8 additions & 0 deletions front/form.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions inc/exportable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions inc/exportableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 20 additions & 1 deletion inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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([
Expand Down
4 changes: 4 additions & 0 deletions inc/form_profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 4 additions & 0 deletions inc/form_validator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions inc/item_targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
41 changes: 41 additions & 0 deletions inc/linker.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* ---------------------------------------------------------------------
*/

use GlpiPlugin\Formcreator\Exception\ImportFailureException;

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
Expand All @@ -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 "<div class='center'>";
echo "<table class='tab_cadrehov'><tr><th>".__('Importing', 'formcreator')."</th></tr>";
echo "<tr class='tab_bg_2'><td>";
Html::createProgressBar(__('Import in progress'));
echo "</td></tr></table></div>\n";
}
}

/**
* Store an object added in the DB
*
Expand All @@ -50,7 +79,14 @@ public function addObject($originalId, PluginFormcreatorExportableInterface $obj
if (!isset($this->imported[$object->getType()])) {
$this->imported[$object->getType()] = [];
}
if (isset($this->imported[$object->getType()][$originalId])) {
throw new ImportFailureException(sprintf('Attempt to create an already created item "%1$s" with original ID "%2$s"', $object->getType(), $originalId));
}
$this->imported[$object->getType()][$originalId] = $object;
$this->progress++;
if (!isCommandLine() && !isAPI()) {
Html::changeProgressBarPosition($this->getProgress(), $this->getTotalCount(), $this->getProgress() . ' / ' . $this->getTotalCount());
}
}

/**
Expand Down Expand Up @@ -139,4 +175,9 @@ public function linkPostponed() {

return true;
}

public function reset() {
$this->imported = [];
$this->postponed = [];
}
}
9 changes: 9 additions & 0 deletions inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,15 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
return $itemId;
}

public static function countItemsToImport($input) {
// TODO: need improvement to handle parameters
$subItems = [
'_conditions' => PluginFormcreatorCondition::class,
];

return 1 + self::countChildren($input, $subItems);
}

public function export($remove_uuid = false) {
if ($this->isNewItem()) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions inc/questiondependency.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
return $itemId;
}

public static function countItemsToImport($input) {
return 1;
}

public function export($remove_uuid = false) {
if ($this->isNewItem()) {
return false;
Expand Down
5 changes: 5 additions & 0 deletions inc/questionrange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con

return $itemId;
}

public static function countItemsToImport($input)
{
return 1;
}
}
5 changes: 5 additions & 0 deletions inc/questionregex.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,9 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con

return $itemId;
}

public static function countItemsToImport($input)
{
return 1;
}
}
8 changes: 8 additions & 0 deletions inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
return $itemId;
}

public static function countItemsToImport($input) {
$subItems = [
'_questions' => PluginFormcreatorQuestion::class,
'_conditions' => PluginFormcreatorCondition::class,
];
return 1 + self::countChildren($input, $subItems);
}

public function export($remove_uuid = false) {
if ($this->isNewItem()) {
return false;
Expand Down
5 changes: 5 additions & 0 deletions inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,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 actor
* @param boolean $remove_uuid remove the uuid key
Expand Down
10 changes: 10 additions & 0 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
return $itemId;
}

public static function countItemsToImport($input) {
$subItems = [
'_actors' => PluginFormcreatorTarget_Actor::class,
'_conditions' => PluginFormcreatorCondition::class,
];

return 1 + self::countChildren($subItems, $input);
}


public function showForm($ID, $options = []) {
if ($ID == 0) {
// Not used for now
Expand Down
10 changes: 10 additions & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,16 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
return $itemId;
}

public static function countItemsToImport($input) {
$subItems = [
'_actors' => PluginFormcreatorTarget_Actor::class,
'_ticket_relations' => PluginFormcreatorItem_TargetTicket::class,
'_conditions' => PluginFormcreatorCondition::class,
];

return 1 + self::countChildren($subItems, $input);
}

protected function getTaggableFields() {
return [
'target_name',
Expand Down

0 comments on commit deb1e20

Please sign in to comment.