Skip to content

Commit

Permalink
feat(entityconfig): distinguish ID and entity foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Feb 14, 2022
1 parent fc0dffc commit 542b414
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 18 deletions.
6 changes: 5 additions & 1 deletion front/entityconfig.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

if (isset($_POST['update'])) {
$entityConfig = new PluginFormcreatorEntityconfig();
$entityConfig->update($_POST);
if ($entityConfig->getFromDBByCrit(['entities_id' => (int) $_POST['entities_id']])) {
$_POST['id'] = $entityConfig->getID();
unset($_POST['entities_id']);
$entityConfig->update($_POST);
}
}
Html::back();
11 changes: 9 additions & 2 deletions inc/entityconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
}

public function prepareInputForAdd($input) {
if (!isset($input['entities_id'])) {
return false;
}
$entity = new Entity();
if (!$entity->getFromDB($input['entities_id'])) {
return false;
}
$input['header'] = $input['header'] ?? '';

$config = Toolbox::getHtmLawedSafeConfig();
Expand Down Expand Up @@ -285,7 +292,7 @@ public function showFormForEntity(Entity $entity) {
if ($canedit) {
echo "<tr>";
echo "<td class='tab_bg_2 center' colspan='4'>";
echo Html::hidden('id', ['value' => $entity->fields["id"]]);
echo Html::hidden('entities_id', ['value' => $entity->fields["entities_id"]]);
echo Html::submit(_x('button', 'Save'), ['name' => 'update']);
echo "</td></tr>";
Html::closeForm();
Expand Down Expand Up @@ -382,7 +389,7 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
// Search in entity data of the current entity
if ($entity->getFromDB($entities_id)) {
// Value is defined : use it
if ($entityConfig->getFromDB($entities_id)) {
if ($entityConfig->getFromDBByCrit(['entities_id' => $entities_id])) {
if (is_numeric($default_value)
&& ($entityConfig->fields[$fieldref] != self::CONFIG_PARENT)) {
return $entityConfig->fields[$fieldval];
Expand Down
2 changes: 1 addition & 1 deletion install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ protected function configureExistingEntities() {

/** Value -2 is "inheritance from parent" @see PluginFormcreatorEntityconfig::CONFIG_PARENT */
$query = "INSERT INTO glpi_plugin_formcreator_entityconfigs
(id, replace_helpdesk, sort_order, is_kb_separated, is_search_visible, is_dashboard_visible, is_header_visible)
(entities_id, replace_helpdesk, sort_order, is_kb_separated, is_search_visible, is_dashboard_visible, is_header_visible)
SELECT ent.id,
IF(ent.id = 0, 0, -2),
IF(ent.id = 0, 0, -2),
Expand Down
6 changes: 4 additions & 2 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_categories` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_entityconfigs` (
`id` int unsigned NOT NULL,
`id` int unsigned NOT NULL AUTO_INCREMENT,
`entities_id` int unsigned NOT NULL DEFAULT '0',
`replace_helpdesk` int(11) NOT NULL DEFAULT '-2',
`sort_order` int(11) NOT NULL DEFAULT '-2',
`is_kb_separated` int(11) NOT NULL DEFAULT '-2',
`is_search_visible` int(11) NOT NULL DEFAULT '-2',
`is_dashboard_visible` int(11) NOT NULL DEFAULT '-2',
`is_header_visible` int(11) NOT NULL DEFAULT '-2',
`header` text,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`entities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_forms` (
Expand Down
17 changes: 14 additions & 3 deletions install/upgrade_to_2.13.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PluginFormcreatorUpgradeTo2_13 {
*/
public function upgrade(Migration $migration) {
$this->migration = $migration;
$this->migrateEntityConfig();
$this->migrateFkToUnsignedInt();
$this->addFormAnswerTitle();
$this->defaultValuesForTargets();
Expand Down Expand Up @@ -132,7 +133,18 @@ protected function addDashboardVisibility() {
$table = 'glpi_plugin_formcreator_entityconfigs';
$this->migration->addField($table, 'is_dashboard_visible', 'integer', ['after' => 'is_search_visible', 'value' => '-2']);

$this->migration->addPostQuery("UPDATE glpi_plugin_formcreator_entityconfigs SET `is_dashboard_visible`=1 WHERE `id`=0");
$this->migration->addPostQuery("UPDATE `glpi_plugin_formcreator_entityconfigs` SET `is_dashboard_visible`=1 WHERE `id`=0");
}

protected function migrateEntityConfig() {
global $DB;

$table = 'glpi_plugin_formcreator_entityconfigs';

$this->migration->addField($table, 'entities_id', 'int unsigned not null default 0', ['after' => 'id']);
$this->migration->migrationOneTable($table);
$this->migration->addKey($table, 'entities_id', 'unicity', 'UNIQUE');
$DB->queryOrDie("UPDATE `$table` SET `entities_id`=`id`");
}

protected function migrateFkToUnsignedInt() {
Expand Down Expand Up @@ -280,8 +292,7 @@ protected function migrateFkToUnsignedInt() {
}
}

// Exception for ID key of glpi_plugin_formcreator_entityconfigs : it is not an autoincrement
$table = 'glpi_plugin_formcreator_entityconfigs';
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null');
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
}
}
8 changes: 5 additions & 3 deletions tests/3-unit/PluginFormcreatorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ public function testGetInterface() {

// test simplified interface
$entityConfig = new \PluginFormcreatorEntityConfig();
$entityConfig->getFromDbByCrit(['entities_id' => 0]);
$entityConfig->update([
'id' => '0',
'id' => $entityConfig->getID(),
'replace_helpdesk' => '0',
]);
$this->login('post-only', 'postonly');
Expand All @@ -430,8 +431,9 @@ public function testGetInterface() {

// test service catalog
$entityConfig = new \PluginFormcreatorEntityConfig();
$entityConfig->getFromDbByCrit(['entities_id' => 0]);
$entityConfig->update([
'id' => '0',
'id' => $entityConfig->getId(),
'replace_helpdesk' => \PluginFormcreatorEntityConfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
]);
$this->login('post-only', 'postonly');
Expand All @@ -440,7 +442,7 @@ public function testGetInterface() {

$entityConfig = new \PluginFormcreatorEntityConfig();
$entityConfig->update([
'id' => '0',
'id' => $entityConfig->getId(),
'replace_helpdesk' => \PluginFormcreatorEntityConfig::CONFIG_EXTENDED_SERVICE_CATALOG,
]);
$this->login('post-only', 'postonly');
Expand Down
11 changes: 6 additions & 5 deletions tests/3-unit/PluginFormcreatorEntityConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testGetUsedConfig() {
// Set configuration for the 2 sub entities
$instance = $this->newTestedInstance();
$instance->add([
'id' => $entityId,
'entities_id' => $entityId,
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_EXTENDED_SERVICE_CATALOG,
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_MERGED,
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL,
Expand All @@ -96,7 +96,7 @@ public function testGetUsedConfig() {

$instance = $this->newTestedInstance();
$instance->add([
'id' => $entityId1,
'entities_id' => $entityId1,
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_MERGED,
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL,
Expand All @@ -105,7 +105,7 @@ public function testGetUsedConfig() {

$instance = $this->newTestedInstance();
$instance->add([
'id' => $entityId2,
'entities_id' => $entityId2,
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_EXTENDED_SERVICE_CATALOG,
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_DISTINCT,
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY,
Expand All @@ -114,7 +114,7 @@ public function testGetUsedConfig() {

$instance = $this->newTestedInstance();
$instance->add([
'id' => $entityId3,
'entities_id' => $entityId3,
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
Expand Down Expand Up @@ -145,8 +145,9 @@ public function testGetUsedConfig() {

// Check change on parent entity propagates to child with inherited settings
$instance = $this->newTestedInstance();
$instance->getFromDBByCrit(['entities_id' => $entityId]);
$instance->update([
'id' => $entityId,
'id' => $instance->getID(),
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_DISTINCT,
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY,
Expand Down
2 changes: 1 addition & 1 deletion tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function testImport() {
'location_question' => '0',
'validation_followup' => '1',
'destination_entity' => '0',
'destination_entity_value' => 0,
'destination_entity_value' => '0',
'tag_type' => \PluginFormcreatorTargetTicket::TAG_TYPE_NONE,
'tag_questions' => '0',
'tag_specifics' => '',
Expand Down

0 comments on commit 542b414

Please sign in to comment.