This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
TableImporterShortcode.php
186 lines (167 loc) · 6.32 KB
/
TableImporterShortcode.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Grav\Plugin\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Grav\Common\Utils;
use Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\File\File;
// use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use League\Csv\Reader;
class TableImporterShortcode extends Shortcode
{
protected $outerEscape = null;
public function init()
{
$this->shortcode->getHandlers()->add('ti', array($this, 'process'));
}
public function process(ShortcodeInterface $sc) {
$fn = $sc->getParameter('file', null);
if ($fn === null) {
$fn = $sc->getShortcodeText();
$fn = str_replace('[ti=', '', $fn);
$fn = str_replace('/]', '', $fn);
$fn = trim($fn);
}
if ( ($fn === null) && ($fn === '') ) {
return "<p>Table Importer: Malformed shortcode (<tt>".htmlspecialchars($sc->getShortcodeText())."</tt>).</p>";
}
$raw = $sc->getParameter('raw', null);
if ($raw === null) {
$raw = false;
} else {
$raw = true;
}
$type = $sc->getParameter('type', null);
$delim = $sc->getParameter('delimiter', ',');
$encl = $sc->getParameter('enclosure', '"');
$esc = $sc->getParameter('escape', '\\');
$class = $sc->getParameter('class', null);
$caption = $sc->getParameter('caption', null);
$header = $sc->getParameter('header', null);
if ($header === null) {
$header = true;
} else {
$header = false;
}
// Get absolute file name
$abspath = null;
if ($fn !== null) {
$abspath = $this->getPath(static::sanitize($fn));
}
if ($abspath === null) {
return "<p>Table Importer: Could not resolve file name '$fn'.</p>";
}
if (! file_exists($abspath)) {
return "<p>Table Importer: Could not find the requested data file '$fn'.</p>";
}
// Determine what type of file it is
if ($type === null) {
if ( (Utils::endswith(strtolower($fn), '.yaml')) || ((Utils::endswith(strtolower($fn), '.yml'))) ) {
$type = 'yaml';
} elseif (Utils::endswith(strtolower($fn), '.json')) {
$type = 'json';
} elseif (Utils::endswith(strtolower($fn), '.csv')) {
$type = 'csv';
} else {
return "<p>Table Importer: Could not determine the type of the requested data file '$fn'. This plugin only supports YAML, JSON, and CSV.</p>";
}
}
// Load the data
$data = null;
switch ($type) {
case 'yaml':
$data = Yaml::parse(file_get_contents($abspath));
break;
case 'json':
$data = json_decode(file_get_contents($abspath));
break;
case 'csv':
$reader = Reader::createFromPath($abspath, 'r');
$reader->setDelimiter($delim);
$reader->setEnclosure($encl);
$this->outerEscape = $esc;
$reader->setEscape($esc);
// This func is to compensate for a bug in PHP's `SplFileObject` class.
// https://bugs.php.net/bug.php?id=55413
// This func strips out the extraneous escape character.
$func = function ($row) {
$e = preg_quote($this->outerEscape);
foreach ($row as &$cell) {
$cell = preg_replace("/$e(?!$e)/", '', $cell);
}
unset($cell);
return $row;
};
$data = $reader->fetchAll($func);
break;
}
try {
// Build the table
if ($data === null) {
return "<p>Table Importer: Something went wrong loading '$type' data from the requested file '$fn'.</p>";
}
$output = '';
// Table's id can be specified by adding an `id` attribute to the shortcode
$id = $sc->getParameter('id', null);
if ($id === null)
$id = $this->shortcode->getId($sc);
$output .= '<table id="'.$id.'"';
if ($class !== null) {
$output .= ' class="'.htmlspecialchars($class).'"';
}
$output .= '>';
// Insert caption if given
if ( ($caption !== null) && (strlen($caption) > 0) ) {
$output .= '<caption>'.htmlspecialchars($caption).'</caption>';
}
if ($header) {
$row = array_shift($data);
$output .= '<thead><tr>';
foreach ($row as $cell) {
$output .= '<th>'.$cell.'</th>';
}
$output .= '</tr></thead>';
}
$output .= '<tbody>';
foreach ($data as $row) {
$output .= '<tr>';
foreach ($row as $cell) {
if ($raw) {
$output .= '<td>'.$cell.'</td>';
} else {
$output .= '<td>'.htmlspecialchars($cell).'</td>';
}
}
$output .= '</tr>';
}
$output .= '</tbody>';
$output .= '</table>';
return $output;
} catch (\Exception $e) {
return '<p>The data in "'.$fn.'" appears to be malformed. Please review the documentation.';
}
}
private function getPath($fn) {
if (Utils::startswith($fn, 'data:')) {
$path = $this->grav['locator']->findResource('user://data', true);
$fn = str_replace('data:', '', $fn);
} else {
$path = $this->grav['shortcode']->getPage()->path();
}
if ( (Utils::endswith($path, DS)) || (Utils::startswith($fn, DS)) ) {
$path = $path . $fn;
} else {
$path = $path . DS . $fn;
}
if (file_exists($path)) {
return $path;
}
return null;
}
private static function sanitize($fn) {
$fn = trim($fn);
$fn = str_replace('..', '', $fn);
$fn = ltrim($fn, DS);
$fn = str_replace(DS.DS, DS, $fn);
return $fn;
}
}