diff --git a/inc/fields/checkboxesfield.class.php b/inc/fields/checkboxesfield.class.php index 4f756237c..66e857cef 100644 --- a/inc/fields/checkboxesfield.class.php +++ b/inc/fields/checkboxesfield.class.php @@ -140,7 +140,7 @@ public function serializeValue() { return ''; } - return Toolbox::addslashes_deep(json_encode($this->value, JSON_OBJECT_AS_ARRAY)); + return Toolbox::addslashes_deep(json_encode($this->value, JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE)); } public function deserializeValue($value) { diff --git a/inc/fields/radiosfield.class.php b/inc/fields/radiosfield.class.php index 59560731b..611d230d8 100644 --- a/inc/fields/radiosfield.class.php +++ b/inc/fields/radiosfield.class.php @@ -122,6 +122,7 @@ public function getRenderedHtml($canEdit = true) { $html .= ''; $html .= ''; $html .= ''; $html .= ''; } diff --git a/install/upgrade_to_2.11.php b/install/upgrade_to_2.11.php index cecd5e821..09341091a 100644 --- a/install/upgrade_to_2.11.php +++ b/install/upgrade_to_2.11.php @@ -81,5 +81,91 @@ public function upgrade(Migration $migration) { // Move uuid field at last position $table = 'glpi_plugin_formcreator_targettickets'; $migration->addPostQuery("ALTER TABLE `$table` MODIFY `uuid` varchar(255) DEFAULT NULL AFTER `show_rule`"); + + $this->migrateCheckboxesAndMultiselect(); + $this->migrateRadiosAndSelect(); + } + + /** + * Migrate checkboxes and multiselect data to JSON + * + * @return void + */ + public function migrateCheckboxesAndMultiselect() { + global $DB; + + // Migrate default value + $questionTable = 'glpi_plugin_formcreator_questions'; + $request = [ + 'SELECT' => ['id', 'default_values', 'values'], + 'FROM' => $questionTable, + 'WHERE' => ['fieldtype' => ['checkboxes', 'multiselect']], + ]; + foreach($DB->request($request) as $row) { + $newValues = $row['values']; + if (json_decode($row['values']) === null) { + // Seems already migrated, skipping + $newValues = json_encode(explode("\r\n", $row['values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE); + $newValues = Toolbox::addslashes_deep($newValues); + } + $newDefault = $row['default_values']; + if (json_decode($row['default_values']) === null) { + // Seems already migrated, skipping + $newDefault = json_encode(explode("\r\n", $row['default_values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE); + $newDefault = Toolbox::addslashes_deep($newDefault); + } + $DB->update($questionTable, ['values' => $newValues, 'default_values' => $newDefault], ['id' => $row['id']]); + } + + // Migrate answers + $answerTable = 'glpi_plugin_formcreator_answers'; + $request = [ + 'SELECT' => ["$answerTable.id", 'answer'], + 'FROM' => $answerTable, + 'INNER JOIN' => [ + $questionTable => [ + 'FKEY' => [ + $questionTable => 'id', + $answerTable => 'plugin_formcreator_questions_id', + ] + ] + ], + 'WHERE' => ['fieldtype' => 'checkboxes', 'multiselect'], + ]; + foreach ($DB->request($request) as $row) { + $newAnswer = $row['answer']; + if (json_decode($row['answer']) === null) { + // Seems already migrated, skipping + $newAnswer = json_encode(explode("\r\n", $row['answer']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE); + $newAnswer = Toolbox::addslashes_deep($newAnswer); + } + $DB->update($answerTable, ['answer' => $newAnswer], ['id' => $row['id']]); + } + } + + /** + * Migrate radios and select data to JSON + * + * @return void + */ + public function migrateRadiosAndSelect() { + global $DB; + + // Migrate default value + $questionTable = 'glpi_plugin_formcreator_questions'; + $request = [ + 'SELECT' => ['id', 'default_values', 'values'], + 'FROM' => $questionTable, + 'WHERE' => ['fieldtype' => ['radios', 'select']], + ]; + foreach($DB->request($request) as $row) { + $newValues = $row['values']; + if (json_decode($row['values']) === null) { + // Seems already migrated, skipping + $newValues = json_encode(explode("\r\n", $row['values']), JSON_OBJECT_AS_ARRAY+JSON_UNESCAPED_UNICODE); + $newValues = Toolbox::addslashes_deep($newValues); + } + $DB->update($questionTable, ['values' => $newValues], ['id' => $row['id']]); + } } }