-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.driver.php
executable file
·216 lines (182 loc) · 7.79 KB
/
extension.driver.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
Class extension_parametrisator extends Extension{
public function about() {
return array('name' => __('Parametrisator'),
'version' => '1.4',
'release-date' => '2013-07-26',
'author' => array('name' => 'Marcin Konicki',
'website' => 'http://ahwayakchih.neoni.net',
'email' => '[email protected]'),
'description' => __('Allows to specify XPath(s) for any data-source. Each XPath will be used to select data from XML that was generated by that data-source. Selected data will be added to the parameter pool used by all data-sources.')
);
}
public function uninstall() {
}
public function update($previousVersion) {
return true;
}
public function install() {
return true;
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'addJavaScriptAndCSS'
),
array(
'page' => '/blueprints/datasources/',
'delegate' => 'DatasourcePreCreate',
'callback' => 'appendDataSourceParameters'
),
array(
'page' => '/blueprints/datasources/',
'delegate' => 'DatasourcePreEdit',
'callback' => 'appendDataSourceParameters'
),
array(
'page' => '/frontend/',
'delegate' => 'DataSourcePostExecute',
'callback' => 'generateParameters'
),
);
}
public function addJavaScriptAndCSS() {
$callback = Symphony::Engine()->getPageCallback();
if ($callback['driver'] != 'blueprintsdatasources' || !is_array($callback['context']) || empty($callback['context'])) return;
// Find data source handle.
$handle = NULL;
if ($callback['context'][0] == 'edit' && !empty($callback['context'][1])) {
$handle = $callback['context'][1];
}
// Find current XPath values.
$parametrisator = array('xslt' => false, 'xpaths' => array());
if (isset($_POST['parametrisator'])) {
$parametrisator['xslt'] = $_POST['parametrisator']['xslt'];
if (isset($_POST['parametrisator']['xpaths']) && isset($_POST['parametrisator']['xpaths']['name'])) {
$parametrisator['xpaths'] = array_combine($_POST['parametrisator']['xpaths']['name'], $_POST['parametrisator']['xpaths']['xpath']);
}
else {
$parametrisator['xpaths'] = array();
}
}
else if (!empty($handle)) {
$existing = DatasourceManager::create($handle, NULL, false);
if (!empty($existing)) {
if (is_array($existing->dsParamParametrisator)) {
$parametrisator = $existing->dsParamParametrisator;
}
if (!empty($existing->dsParamROOTELEMENT)) {
$handle = $existing->dsParamROOTELEMENT;
}
}
}
// Remove empty values
$parametrisator['xpaths'] = array_filter($parametrisator['xpaths']);
// Get list of utilities
$xsltfile = $parametrisator['xslt'];
$parametrisator['xslt'] = '<option value="">'.__('Disabled').'</option>';
$utilities = General::listStructure(UTILITIES, array('xsl'), false, 'asc', UTILITIES);
if (is_array($utilities) && is_array($utilities['filelist'])) {
foreach ($utilities['filelist'] as $utility) {
$parametrisator['xslt'] .= '<option value="'.$utility.'"'.($xsltfile == $utility ? ' selected="selected"' : '').'>'.$utility.'</option>';
}
}
// Let our script know sort and order values.
Administration::instance()->Page->addElementToHead(
new XMLElement(
'script',
"Symphony.Context.add('parametrisator', " . json_encode(array('xslt' => $parametrisator['xslt'], 'xpaths' => $parametrisator['xpaths'], 'handle' => $handle)) . ");",
array('type' => 'text/javascript')
), 100
);
// Append scripts and styles for field settings pane
Administration::instance()->Page->addScriptToHead(URL . '/extensions/parametrisator/assets/parametrisator.settings.js', 101, false);
}
public function appendDataSourceParameters(&$context) {
/*
'file' => $file,
'contents' => &$dsShell,
'params' => $params,
'elements' => $elements,
'filters' => $filters,
'dependencies' => $dependencies
*/
if (!isset($_POST['parametrisator']['xpaths']) || empty($_POST['parametrisator']['xpaths'])) return;
if (!empty($_POST['parametrisator']['xslt'])) {
$XSLT = UTILITIES . '/'. preg_replace(array('%/+%', '%(^|/)\.\./%'), '/', $_POST['parametrisator']['xslt']);
if (file_exists($XSLT)) $parametrisator['xslt'] = $_POST['parametrisator']['xslt'];
}
else {
$parametrisator['xslt'] = false;
}
$parametrisator['xpaths'] = array_filter(array_combine(array_map(array('Lang', 'createHandle'), $_POST['parametrisator']['xpaths']['name']), $_POST['parametrisator']['xpaths']['xpath']));
if (empty($parametrisator['xpaths'])) return;
$parametrisator = preg_replace(array("/\n /", "/\n\)\s*$/"), array("\n\t\t\t", "\n\t\t);"), var_export($parametrisator, true))."\n";
// Store XPaths in data-source
$context['contents'] = preg_replace('/public \$dsParamROOTELEMENT [^\n]+\n/', '$0 public \$dsParamParametrisator = '.$parametrisator, $context['contents']);
// Update dependencies
// From content.blueprintsdatasources.php
if (preg_match_all('@(\$ds-[0-9a-z_\.\-]+)@i', $parametrisator, $matches)) {
if (!is_array($context['dependencies'])) $context['dependencies'] = array();
$context['dependencies'] = General::array_remove_duplicates(array_merge($matches[1], $context['dependencies']));
$context['contents'] = preg_replace('/'.preg_quote('$this->_dependencies = array(').'[^\)]*\);/', '$this->_dependencies = array(\''.implode("', '", $context['dependencies']).'\');', $context['contents']);
}
}
public function generateParameters(&$context) {
/*
'datasource' => $ds,
'xml' => &$xml,
'param_pool' => &$this->_env['pool']
*/
if (empty($context) || !isset($context['xml']) || !isset($context['datasource'])
|| empty($context['xml'])
|| !isset($context['datasource']->dsParamParametrisator)
|| !isset($context['datasource']->dsParamParametrisator['xpaths'])
// || strpos($context['xml'], '<error>') === FALSE // We cannot depend on $this->_force_empty_result because we're not in the datasource scope (and that's a protected variable, and if it weren't, it could be changed by other code by now :(.
) return;
$xml = NULL;
if (is_object($context['xml'])) {
$xml = $context['xml']->generate(false);
}
if (!$xml) {
return;
}
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
if (!$dom->loadXML($xml)) {
// TODO: create some kind of an "parametrisator error" parameter?
return;
}
if (!empty($context['datasource']->dsParamParametrisator['xslt'])) {
$XSLTfilename = UTILITIES . '/'. preg_replace(array('%/+%', '%(^|/)../%'), '/', $context['datasource']->dsParamParametrisator['xslt']);
if (file_exists($XSLTfilename)) {
$XSLProc = new XsltProcessor;
$xslt = new DomDocument;
$xslt->load($XSLTfilename);
$XSLProc->importStyleSheet($xslt);
// Set some context
$XSLProc->setParameter('', array(
'rootelement' => $context['datasource']->dsParamROOTELEMENT,
'source' => $context['datasource']->getSource()
));
$temp = $XSLProc->transformToDoc($dom);
if ($temp instanceof DOMDocument) {
$dom = $temp;
}
}
}
$xpath = new DOMXPath($dom);
foreach($context['datasource']->dsParamParametrisator['xpaths'] as $handle => $query) {
$query = $context['datasource']->__processParametersInString($query, array('env' => Frontend::Page()->Env(), 'param' => Frontend::Page()->Params()));
$data = $xpath->query($query);
if ($data === FALSE) continue;
$key = "ds-" . $context['datasource']->dsParamROOTELEMENT . "-" . $handle;
if (!is_array($context['param_pool'][$key])) $context['param_pool'][$key] = array();
foreach ($data as $node) {
$context['param_pool'][$key][] = $node->nodeValue;
}
}
}
}