Skip to content

Commit 542b414

Browse files
committed
feat(entityconfig): distinguish ID and entity foreign key
1 parent fc0dffc commit 542b414

File tree

8 files changed

+45
-18
lines changed

8 files changed

+45
-18
lines changed

front/entityconfig.form.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
if (isset($_POST['update'])) {
4242
$entityConfig = new PluginFormcreatorEntityconfig();
43-
$entityConfig->update($_POST);
43+
if ($entityConfig->getFromDBByCrit(['entities_id' => (int) $_POST['entities_id']])) {
44+
$_POST['id'] = $entityConfig->getID();
45+
unset($_POST['entities_id']);
46+
$entityConfig->update($_POST);
47+
}
4448
}
4549
Html::back();

inc/entityconfig.class.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
132132
}
133133

134134
public function prepareInputForAdd($input) {
135+
if (!isset($input['entities_id'])) {
136+
return false;
137+
}
138+
$entity = new Entity();
139+
if (!$entity->getFromDB($input['entities_id'])) {
140+
return false;
141+
}
135142
$input['header'] = $input['header'] ?? '';
136143

137144
$config = Toolbox::getHtmLawedSafeConfig();
@@ -285,7 +292,7 @@ public function showFormForEntity(Entity $entity) {
285292
if ($canedit) {
286293
echo "<tr>";
287294
echo "<td class='tab_bg_2 center' colspan='4'>";
288-
echo Html::hidden('id', ['value' => $entity->fields["id"]]);
295+
echo Html::hidden('entities_id', ['value' => $entity->fields["entities_id"]]);
289296
echo Html::submit(_x('button', 'Save'), ['name' => 'update']);
290297
echo "</td></tr>";
291298
Html::closeForm();
@@ -382,7 +389,7 @@ static function getUsedConfig($fieldref, $entities_id, $fieldval = '', $default_
382389
// Search in entity data of the current entity
383390
if ($entity->getFromDB($entities_id)) {
384391
// Value is defined : use it
385-
if ($entityConfig->getFromDB($entities_id)) {
392+
if ($entityConfig->getFromDBByCrit(['entities_id' => $entities_id])) {
386393
if (is_numeric($default_value)
387394
&& ($entityConfig->fields[$fieldref] != self::CONFIG_PARENT)) {
388395
return $entityConfig->fields[$fieldval];

install/install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected function configureExistingEntities() {
263263

264264
/** Value -2 is "inheritance from parent" @see PluginFormcreatorEntityconfig::CONFIG_PARENT */
265265
$query = "INSERT INTO glpi_plugin_formcreator_entityconfigs
266-
(id, replace_helpdesk, sort_order, is_kb_separated, is_search_visible, is_dashboard_visible, is_header_visible)
266+
(entities_id, replace_helpdesk, sort_order, is_kb_separated, is_search_visible, is_dashboard_visible, is_header_visible)
267267
SELECT ent.id,
268268
IF(ent.id = 0, 0, -2),
269269
IF(ent.id = 0, 0, -2),

install/mysql/plugin_formcreator_empty.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_categories` (
2828
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2929

3030
CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_entityconfigs` (
31-
`id` int unsigned NOT NULL,
31+
`id` int unsigned NOT NULL AUTO_INCREMENT,
32+
`entities_id` int unsigned NOT NULL DEFAULT '0',
3233
`replace_helpdesk` int(11) NOT NULL DEFAULT '-2',
3334
`sort_order` int(11) NOT NULL DEFAULT '-2',
3435
`is_kb_separated` int(11) NOT NULL DEFAULT '-2',
3536
`is_search_visible` int(11) NOT NULL DEFAULT '-2',
3637
`is_dashboard_visible` int(11) NOT NULL DEFAULT '-2',
3738
`is_header_visible` int(11) NOT NULL DEFAULT '-2',
3839
`header` text,
39-
PRIMARY KEY (`id`)
40+
PRIMARY KEY (`id`),
41+
UNIQUE KEY `unicity` (`entities_id`)
4042
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
4143

4244
CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_forms` (

install/upgrade_to_2.13.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class PluginFormcreatorUpgradeTo2_13 {
3737
*/
3838
public function upgrade(Migration $migration) {
3939
$this->migration = $migration;
40+
$this->migrateEntityConfig();
4041
$this->migrateFkToUnsignedInt();
4142
$this->addFormAnswerTitle();
4243
$this->defaultValuesForTargets();
@@ -132,7 +133,18 @@ protected function addDashboardVisibility() {
132133
$table = 'glpi_plugin_formcreator_entityconfigs';
133134
$this->migration->addField($table, 'is_dashboard_visible', 'integer', ['after' => 'is_search_visible', 'value' => '-2']);
134135

135-
$this->migration->addPostQuery("UPDATE glpi_plugin_formcreator_entityconfigs SET `is_dashboard_visible`=1 WHERE `id`=0");
136+
$this->migration->addPostQuery("UPDATE `glpi_plugin_formcreator_entityconfigs` SET `is_dashboard_visible`=1 WHERE `id`=0");
137+
}
138+
139+
protected function migrateEntityConfig() {
140+
global $DB;
141+
142+
$table = 'glpi_plugin_formcreator_entityconfigs';
143+
144+
$this->migration->addField($table, 'entities_id', 'int unsigned not null default 0', ['after' => 'id']);
145+
$this->migration->migrationOneTable($table);
146+
$this->migration->addKey($table, 'entities_id', 'unicity', 'UNIQUE');
147+
$DB->queryOrDie("UPDATE `$table` SET `entities_id`=`id`");
136148
}
137149

138150
protected function migrateFkToUnsignedInt() {
@@ -280,8 +292,7 @@ protected function migrateFkToUnsignedInt() {
280292
}
281293
}
282294

283-
// Exception for ID key of glpi_plugin_formcreator_entityconfigs : it is not an autoincrement
284295
$table = 'glpi_plugin_formcreator_entityconfigs';
285-
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null');
296+
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
286297
}
287298
}

tests/3-unit/PluginFormcreatorCommon.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,9 @@ public function testGetInterface() {
420420

421421
// test simplified interface
422422
$entityConfig = new \PluginFormcreatorEntityConfig();
423+
$entityConfig->getFromDbByCrit(['entities_id' => 0]);
423424
$entityConfig->update([
424-
'id' => '0',
425+
'id' => $entityConfig->getID(),
425426
'replace_helpdesk' => '0',
426427
]);
427428
$this->login('post-only', 'postonly');
@@ -430,8 +431,9 @@ public function testGetInterface() {
430431

431432
// test service catalog
432433
$entityConfig = new \PluginFormcreatorEntityConfig();
434+
$entityConfig->getFromDbByCrit(['entities_id' => 0]);
433435
$entityConfig->update([
434-
'id' => '0',
436+
'id' => $entityConfig->getId(),
435437
'replace_helpdesk' => \PluginFormcreatorEntityConfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
436438
]);
437439
$this->login('post-only', 'postonly');
@@ -440,7 +442,7 @@ public function testGetInterface() {
440442

441443
$entityConfig = new \PluginFormcreatorEntityConfig();
442444
$entityConfig->update([
443-
'id' => '0',
445+
'id' => $entityConfig->getId(),
444446
'replace_helpdesk' => \PluginFormcreatorEntityConfig::CONFIG_EXTENDED_SERVICE_CATALOG,
445447
]);
446448
$this->login('post-only', 'postonly');

tests/3-unit/PluginFormcreatorEntityConfig.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testGetUsedConfig() {
8787
// Set configuration for the 2 sub entities
8888
$instance = $this->newTestedInstance();
8989
$instance->add([
90-
'id' => $entityId,
90+
'entities_id' => $entityId,
9191
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_EXTENDED_SERVICE_CATALOG,
9292
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_MERGED,
9393
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL,
@@ -96,7 +96,7 @@ public function testGetUsedConfig() {
9696

9797
$instance = $this->newTestedInstance();
9898
$instance->add([
99-
'id' => $entityId1,
99+
'entities_id' => $entityId1,
100100
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
101101
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_MERGED,
102102
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_ALPHABETICAL,
@@ -105,7 +105,7 @@ public function testGetUsedConfig() {
105105

106106
$instance = $this->newTestedInstance();
107107
$instance->add([
108-
'id' => $entityId2,
108+
'entities_id' => $entityId2,
109109
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_EXTENDED_SERVICE_CATALOG,
110110
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_DISTINCT,
111111
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY,
@@ -114,7 +114,7 @@ public function testGetUsedConfig() {
114114

115115
$instance = $this->newTestedInstance();
116116
$instance->add([
117-
'id' => $entityId3,
117+
'entities_id' => $entityId3,
118118
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
119119
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
120120
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_PARENT,
@@ -145,8 +145,9 @@ public function testGetUsedConfig() {
145145

146146
// Check change on parent entity propagates to child with inherited settings
147147
$instance = $this->newTestedInstance();
148+
$instance->getFromDBByCrit(['entities_id' => $entityId]);
148149
$instance->update([
149-
'id' => $entityId,
150+
'id' => $instance->getID(),
150151
'replace_helpdesk' => \PluginFormcreatorEntityconfig::CONFIG_SIMPLIFIED_SERVICE_CATALOG,
151152
'is_kb_separated' => \PluginFormcreatorEntityconfig::CONFIG_KB_DISTINCT,
152153
'sort_order' => \PluginFormcreatorEntityconfig::CONFIG_SORT_POPULARITY,

tests/3-unit/PluginFormcreatorTargetTicket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public function testImport() {
702702
'location_question' => '0',
703703
'validation_followup' => '1',
704704
'destination_entity' => '0',
705-
'destination_entity_value' => 0,
705+
'destination_entity_value' => '0',
706706
'tag_type' => \PluginFormcreatorTargetTicket::TAG_TYPE_NONE,
707707
'tag_questions' => '0',
708708
'tag_specifics' => '',

0 commit comments

Comments
 (0)