Files
adminer/plugins/pretty-json-column.php

35 lines
1.0 KiB
PHP
Raw Normal View History

<?php
/** Pretty print JSON values in edit
2025-03-12 06:01:29 +01:00
* @link https://www.adminer.org/plugins/#use
* @author Christopher Chen
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerPrettyJsonColumn {
2025-03-11 07:56:28 +01:00
private function testJson($value) {
if ((substr($value, 0, 1) == '{' || substr($value, 0, 1) == '[') && ($json = json_decode($value, true))) {
return $json;
}
return $value;
}
function editInput($table, $field, $attrs, $value) {
2025-03-11 07:56:28 +01:00
$json = $this->testJson($value);
if ($json !== $value) {
$jsonText = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
2025-03-11 12:21:07 +01:00
return "<textarea$attrs cols='50' rows='20'>" . h($jsonText) . "</textarea>";
}
return '';
}
function processInput($field, $value, $function = '') {
if ($function === '') {
2025-03-11 07:56:28 +01:00
$json = $this->testJson($value);
if ($json !== $value) {
$value = json_encode($json);
}
}
}
}