Skip to content

Commit

Permalink
Merge branch 'bugfix/json_checkbox' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 25, 2020
2 parents 050f538 + 3276b9a commit 0a0441a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions inc/fields/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function getRenderedHtml($canEdit = true) {
$html .= '</label>';
$html .= '</span>';
$html .= '<label class="label-radio" title="' . $value . '" for="' . $domId . '_' . $i . '">';
$html .= $value;
$html .= '</label>';
$html .= '</div>';
}
Expand Down
86 changes: 86 additions & 0 deletions install/upgrade_to_2.11.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
}
}
}

0 comments on commit 0a0441a

Please sign in to comment.