@@ -1734,12 +1734,12 @@ function export($remove_uuid = false) {
17341734 // replace dropdown ids
17351735 if ($ form ['plugin_formcreator_categories_id ' ] > 0 ) {
17361736 $ form ['_plugin_formcreator_category ' ]
1737- = Dropdown::getDropdownName (' glpi_plugin_formcreator_categories ' ,
1737+ = Dropdown::getDropdownName (PluginFormcreatorCategory:: getTable () ,
17381738 $ form ['plugin_formcreator_categories_id ' ]);
17391739 }
17401740 if ($ form ['entities_id ' ] > 0 ) {
17411741 $ form ['_entity ' ]
1742- = Dropdown::getDropdownName (' glpi_entities ' ,
1742+ = Dropdown::getDropdownName (Entity:: getTable () ,
17431743 $ form ['entities_id ' ]);
17441744 }
17451745
@@ -2028,34 +2028,57 @@ public static function import(PluginFormcreatorImportLinker $importLinker, $form
20282028 set_time_limit (30 );
20292029
20302030 $ form_obj = new self ;
2031- $ entity = new Entity ;
2032- $ form_cat = new PluginFormcreatorCategory ;
2031+
2032+ $ forms_id = plugin_formcreator_getFromDBByField (
2033+ $ form_obj ,
2034+ 'uuid ' ,
2035+ $ form ['uuid ' ]
2036+ );
20332037
20342038 // retrieve foreign keys
2035- if (!isset ($ form ['_entity ' ])
2036- || !$ form ['entities_id ' ]
2037- = plugin_formcreator_getFromDBByField ($ entity ,
2038- 'completename ' ,
2039- $ form ['_entity ' ])) {
2040- $ form ['entities_id ' ] = $ _SESSION ['glpiactive_entity ' ];
2039+ $ entity = new Entity ();
2040+ $ entityFk = Entity::getForeignKeyField ();
2041+ $ entityId = $ _SESSION ['glpiactive_entity ' ];
2042+ if (isset ($ form ['_entity ' ])) {
2043+ plugin_formcreator_getFromDBByField (
2044+ $ entity ,
2045+ 'completename ' ,
2046+ $ form ['_entity ' ]
2047+ );
2048+ // Check rights on the destination entity of the form
2049+ if (!$ entity ->isNewItem () && $ entity ->canUpdateItem ()) {
2050+ $ entityId = $ entity ->getID ();
2051+ } else {
2052+ if ($ forms_id !== false ) {
2053+ // The form is in an entity where we don't have UPDATE right
2054+ Session::addMessageAfterRedirect (
2055+ sprintf (__ ('The form %1$s already exists and is in an unmodifiable entity. ' , 'formcreator ' ), $ form ['name ' ]),
2056+ false ,
2057+ WARNING
2058+ );
2059+ return false ;
2060+ }
2061+ }
20412062 }
2042- if (!isset ($ form ['_plugin_formcreator_categories_id ' ])
2043- || !$ form ['_plugin_formcreator_categories_id ' ]
2044- = plugin_formcreator_getFromDBByField ($ form_cat ,
2045- 'completename ' ,
2046- $ form ['_plugin_formcreator_category ' ])) {
2047- $ form ['plugin_formcreator_categories_id ' ] = 0 ;
2063+ $ form [$ entityFk ] = $ entityId ;
2064+
2065+ $ formCategory = new PluginFormcreatorCategory ();
2066+ $ formCategoryFk = PluginFormcreatorCategory::getForeignKeyField ();
2067+ $ formCategoryId = 0 ;
2068+ if (isset ($ form ['_plugin_formcreator_category ' ])) {
2069+ $ formCategoryId = $ formCategory ->import ([
2070+ 'completename ' => $ form ['_plugin_formcreator_category ' ],
2071+ ]);
20482072 }
2073+ $ form [$ formCategoryFk ] = $ formCategoryId ;
20492074
20502075 // escape text fields
20512076 foreach (['name ' , 'description ' , 'content ' ] as $ key ) {
20522077 $ form [$ key ] = $ DB ->escape ($ form [$ key ]);
20532078 }
20542079
20552080 // retrieve form by its uuid
2056- if ($ forms_id = plugin_formcreator_getFromDBByField ($ form_obj ,
2057- 'uuid ' ,
2058- $ form ['uuid ' ])) {
2081+ if (!$ form_obj ->isNewItem ()) {
20592082 // add id key
20602083 $ form ['id ' ] = $ forms_id ;
20612084
0 commit comments