diff --git a/core/metamodel.class.php b/core/metamodel.class.php index feeefe7c2..3701bc1bb 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -2595,30 +2595,67 @@ if (!array_key_exists($sAttCode, self::$m_aAttribDefs[$sClass])) CMDBSource::Query($sSQL); } + /** + * Check (and updates if needed) the hierarchical keys + * @param $bForceComputation boolean If true, the _left and _right parameters will be recomputed even if some values already exist in the DB + */ + public static function CheckHKeys($bForceComputation = false) + { + foreach (self::GetClasses() as $sClass) + { + if (!self::HasTable($sClass)) continue; + + foreach(self::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef) + { + // Check (once) all the attributes that are hierarchical keys + if((self::GetAttributeOrigin($sClass, $sAttCode) == $sClass) && $oAttDef->IsHierarchicalKey()) + { + self::HKInit($sClass, $sAttCode, $bForceComputation); + } + } + } + } + /** * Initializes (i.e converts) a hierarchy stored using a 'parent_id' external key * into a hierarchy stored with a HierarchicalKey, by initializing the _left and _right values * to correspond to the existing hierarchy in the database * @param $sClass string Name of the class to process * @param $sAttCode string Code of the attribute to process + * @param $bForceComputation boolean If true, the _left and _right parameters will be recomputed even if some values already exist in the DB */ - public static function HKInit($sClass, $sAttCode) + public static function HKInit($sClass, $sAttCode, $bForceComputation = false) { $idx = 1; + $bUpdateNeeded = $bForceComputation; $oAttDef = self::GetAttributeDef($sClass, $sAttCode); $sTable = self::DBGetTable($sClass, $sAttCode); if ($oAttDef->IsHierarchicalKey()) { - try + if (!$bForceComputation) { - CMDBSource::Query('START TRANSACTION'); - self::HKInitChildren($sTable, $sAttCode, $oAttDef, 0, $idx); - CMDBSource::Query('COMMIT'); + // Check if some values already exist in the table for the _right value, if so, do nothing + $sRight = $oAttDef->GetSQLRight(); + $sSQL = "SELECT MAX(`$sRight`) AS MaxRight FROM `$sTable`"; + $iMaxRight = CMDBSource::QueryToScalar($sSQL); + if ($iMaxRight == 0) + { + $bUpdateNeeded = true; + } } - catch(Exception $e) + if ($bUpdateNeeded) { - CMDBSource::Query('ROLLBACK'); - throw new Exception("An error occured (".$e->getMessage().") while initializing the hierarchy for ($sClass, $sAttCode). The database was not modified."); + try + { + CMDBSource::Query('START TRANSACTION'); + self::HKInitChildren($sTable, $sAttCode, $oAttDef, 0, $idx); + CMDBSource::Query('COMMIT'); + } + catch(Exception $e) + { + CMDBSource::Query('ROLLBACK'); + throw new Exception("An error occured (".$e->getMessage().") while initializing the hierarchy for ($sClass, $sAttCode). The database was not modified."); + } } } } diff --git a/setup/index.php b/setup/index.php index ecafd39c3..7e09e2d90 100644 --- a/setup/index.php +++ b/setup/index.php @@ -1229,12 +1229,12 @@ function DisplaySummary(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $ $oP->add(''); $oP->add("
\n"); - $oP->add("\n"); + $oP->add("\n"); AddParamsToForm($oP, $aParamValues); $oP->add("\n"); $oP->add("\n"); // Note: the Next >> button is NOT a submit, since moving to the next page is triggered asynchronously - $oP->add("\n"); + $oP->add("\n"); $oP->add("
\n"); $oP->add("
\n"); break; @@ -1342,7 +1342,7 @@ function DisplaySummary(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $ $oP->add("\n"); $oP->add("\n"); // Note: the Next >> button is NOT a submit, since moving to the next page is triggered asynchronously - $oP->add("\n"); + $oP->add("\n"); $oP->add("
\n"); $oP->add("\n"); break; @@ -1354,7 +1354,7 @@ function DisplaySummary(SetupWebPage $oP, $aParamValues, $iCurrentStep, Config $ // have been processed $oP->add("
\n"); AddParamsToForm($oP, $aParamValues); - $oP->add("\n"); + $oP->add("\n"); $oP->add("
\n"); $oP->add("
\n"); diff --git a/setup/setup.js b/setup/setup.js index 1373fef0c..9939a5248 100644 --- a/setup/setup.js +++ b/setup/setup.js @@ -289,7 +289,7 @@ function LoadNextDataFile(response, status, xhr) { // We're done $("#progress").progression({ Current: 100, Maximum: 100, aBackgroundImg: 'orange-progress.gif', aTextColor: '#000000' }); - $('#setup').unblock(); + //$('#setup').unblock(); $('#GoToNextStep').submit(); // Use the hidden form to navigate to the next step } } diff --git a/setup/setuppage.class.inc.php b/setup/setuppage.class.inc.php index 89f6486e6..105847597 100644 --- a/setup/setuppage.class.inc.php +++ b/setup/setuppage.class.inc.php @@ -676,6 +676,8 @@ function CreateDatabaseStructure(Config $oConfig, $aSelectedModules, $sMode) { MetaModel::DBCreate(); SetupWebPage::log_ok("Database structure successfully created."); + // Check (and update only if it seems needed) the hierarchical keys + MetaModel::CheckHKeys(false /* bForceUpdate */); } else {