add pretty XML field like JSON

This commit is contained in:
Pau Capó 2019-07-20 11:48:30 +02:00
parent 0521832bad
commit a86b1606b1
2 changed files with 75 additions and 55 deletions

View file

@ -1,80 +1,47 @@
<?php
/** Display JSON values as table in edit
* @link https://www.adminer.org/plugins/#use
* @author Jakub Vrana, https://www.vrana.cz/
* @author Martin Zeman (Zemistr), http://www.zemistr.eu/
* @author Pau Capó, https://www.paucapo.com/
* @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 AdminerJsonColumn
{
private function _testJson($value)
{
if ((substr($value, 0, 1) == '{' || substr($value, 0, 1) == '[') && ($json = json_decode($value, true)))
{
return $json;
return htmlentities(json_encode($json, JSON_PRETTY_PRINT));
}
return $value;
}
private function _buildTable($json)
{
echo '<table cellspacing="0" style="margin:2px;">';
foreach ($json as $key => $val)
{
echo '<tr>';
echo '<th>' . h($key) . '</th>';
echo '<td>';
if (is_scalar($val) || $val === null)
{
if (is_bool($val))
{
$val = $val ? 'true' : 'false';
}
elseif ($val === null)
{
$val = 'null';
}
elseif (!is_numeric($val))
{
$val = '"' . h(addcslashes($val, "\r\n\"")) . '"';
}
echo '<code class="jush-js">' . $val . '</code>';
}
else
{
$this->_buildTable($val);
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
return false;
}
function editInput($table, $field, $attrs, $value)
{
$json = $this->_testJson($value);
if ($json !== $value)
if (!$json)
{
return;
}
$name = $field['field'];
?>
<a href="#" id="toggle_json_<?= $name ?>">show as table</a><br>
<pre id="show_json_<?= $name ?>" style="display: none"><?= htmlentities(json_encode($json, JSON_PRETTY_PRINT)); ?></pre>
<pre id="show_json_<?= $name ?>" style="display: none"><code><?= $json; ?></code></pre>
<script <?= nonce() ?>>
document.getElementById('toggle_json_<?= $name ?>').addEventListener('click', function (e) {
e.preventDefault();
var show = document.getElementById('show_json_<?= $name ?>');
if (show.style.display === 'none') {
show.style.display = '';
}
else {
} else {
show.style.display = 'none';
}
});
</script>
<?php
}
}
}

View file

@ -0,0 +1,53 @@
<?php
/** Display XML values as table in edit
* @link https://www.adminer.org/plugins/#use
* @author Pau Capó, https://www.paucapo.com/
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
class AdminerXMLColumn
{
function _testXML($value)
{
$xml = simplexml_load_string($value);
if (is_null($xml) || $xml === false)
{
return false;
}
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
return htmlentities($dom->saveXML());
}
function editInput($table, $field, $attrs, $value)
{
$xml = $this->_testXML($value);
if (!$xml)
{
return;
}
$name = $field['field'];
?>
<a href="#" id="toggle_xml_<?= $name ?>">show as table</a><br>
<pre id="show_xml_<?= $name ?>" style="display: none"><code><?= $xml ?></code></pre>
<script <?= nonce() ?>>
document.getElementById('toggle_xml_<?= $name ?>').addEventListener('click', function (e) {
e.preventDefault();
var show = document.getElementById('show_xml_<?= $name ?>');
if (show.style.display === 'none') {
show.style.display = '';
} else {
show.style.display = 'none';
}
});
</script>
<?php
}
}