File: pretty-json-column.php

package info (click to toggle)
adminer 4.7.1-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,608 kB
  • sloc: php: 25,013; javascript: 2,236; makefile: 21; sh: 3
file content (40 lines) | stat: -rw-r--r-- 996 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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));
	}
}