]> git.joonet.de Git - adminer.git/commitdiff
Add plugin pretty-json-column
authorChristopher CHEN <fishdrowned@gmail.com>
Wed, 18 Jul 2018 06:35:45 +0000 (14:35 +0800)
committerJakub Vrana <jakub@vrana.cz>
Tue, 18 Sep 2018 16:59:24 +0000 (18:59 +0200)
(cherry picked from commit 9b8612a49c43ff170837dbf84f8c08a0e85386a8)

plugins/pretty-json-column.php [new file with mode: 0644]

diff --git a/plugins/pretty-json-column.php b/plugins/pretty-json-column.php
new file mode 100644 (file)
index 0000000..01d6307
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+/** Pretty print JSON values in edit
+*/
+class AdminerPrettyJsonColumn {
+       /** @var AdminerPlugin */
+       protected $adminer;
+
+       public function __construct($adminer) {
+               $this->adminer = $adminer;
+       }
+
+       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) {
+               $json = $this->_testJson($value);
+               if ($json !== $value) {
+                       $jsonText = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+                       return <<<HTML
+<textarea $attrs cols="50" rows="20">$jsonText</textarea>
+HTML;
+               }
+               return '';
+       }
+
+       function processInput($field, $value, $function = '') {
+               if ($function === '') {
+                       $json = $this->_testJson($value);
+                       if ($json !== $value) {
+                               $value = json_encode($json);
+                       }
+               }
+               return $this->adminer->_callParent('processInput', array($field, $value, $function));
+       }
+}