forked from sigbertklinke/R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRform.php
More file actions
executable file
·364 lines (312 loc) · 13.8 KB
/
Copy pathRform.php
File metadata and controls
executable file
·364 lines (312 loc) · 13.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/*
(C) 2006- Sigbert Klinke (sigbert@wiwi.hu-berlin.de)
This file is part of R/Octave Extension for Mediawiki.
The R/Octave Extension is free software: you can
redistribute it and/or modify it under the terms of
the GNU General Public License as published by the
Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The R/Octave Extension is distributed in the hope that
It will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General
Public License along with the R/Octave Extension.
If not, see http://www.gnu.org/licenses/.
*/
class Rform {
static function renderRform($input, $params, $parser, $frame) {
global $extr;
try {
if (!property_exists($parser, 'extR')) {
$parser->extR = array('depth' => 1,
'id' => array());
} else {
$parser->extR['depth'] = $parser->extR['depth']+1;
}
if ($parser->extR['depth']>1) throw new Exception ('Rform.php: Rform can not be nested');
$name = null;
if (array_key_exists('label', $params)) $name = $params['label'];
if (array_key_exists('name', $params)) $name = $params['name'];
if (is_null($name)) throw new Exception('Rform.php: Attribute "name" in Rform required');
$id = Rform::getID($params, $parser, $name);
$title = (array_key_exists('title', $params) ? $params['title'] : 'Submit form "' . $name . '" ?');
$submit = (array_key_exists('submit', $params) ? $params['submit'] : 'submit');
$inp = new HTMLtag('input',
array('type'=> 'hidden',
'name'=> 'R',
'value'=> rawurlencode($parser->getTitle() . '_' .$name)));
$inp->check();
$form = new HTMLtag('form',
array('id' => $id,
'action' => $extr->R_CGI,
'method' => 'post',
'target' => $name,
'class' => 'extrform',
'data-submit' => $submit));
if (array_key_exists('debug', $params)) $form->attr('class', 'extrdebug');
$form->body = (string) $inp;
$parser->extR['form'] = $name;
$parser->extR['idcount'] = 1;
$parser->extR['usejquery'] = $extr->JQ_CSS && (array_key_exists('jquery', $params) ? $params['jquery'] : $extr->options['usejquery']);
$parser->extR['submit'] = $submit;
// $extr->JQ_CSS: is the use of jQuery possible at all?
// $params['jquery']: has the user forced the use or disuse of jQuery
// $$extr->options['usejquery']): what has been the general setting for jQuery
if ($parser->extR['usejquery']) {
$parser->mOutput->addModules('ext.R.jQueryUI');
$form->attr('class', 'ui-widget');
$form->attr('class', $extr->JQ_THEME);
if (array_key_exists('debug', $params)) $form->attr('class', 'RextDebug');
$dlg = new HTMLtag('div',
array('class' => 'extrdialog',
'data-form' => $name));
$dlg->body = '<p class="ui-dialog-content">' . $title . '</p>';
$dlg->check();
$form->body .= (string) $dlg;
}
$form->body .= $parser->recursiveTagParse($input, $frame);
$parser->extR['depth'] = 0;
$form->check();
$div = new HTMLtag('div');
$div->attr('class', 'sunny');
$div->body = (string) $form;
$div->check();
return ((string) $div);
} catch (Exception $e) {
$extr->exception_log($e);
return ('<pre style="color:red">' . htmlentities($e->getMessage()) . '</pre>in<pre>' . htmlentities(new HTMLtag('Rform', $params)) . '</pre>');
}
}
static function addjQueryClasses ($params, $other='') {
$class = '';
if (array_key_exists('disabled', $params)) {
$class = 'ui-state-disabled';
} elseif (array_key_exists('readonly', $params)) {
$class = 'ui-state-default ui-state-disabled';
} else {
$class = 'ui-state-default';
}
$class .= ' ui-corner-all ' . $other;
return(trim($class));
}
static function addExtRAttributes ($params, $parser) {
$attr = array('data-form' => $parser->extR['form']);
if (array_key_exists('name', $params)) {
if (preg_match('/^(.*)\_([0-9]+)$/', $params['name'], $match)) {
$attr['data-name'] = $match[1];
$attr['data-item'] = $match[2];
} else {
$attr['data-name'] = $params['name'];
}
}
$attr['data-submit'] = (array_key_exists('submit', $params) ? $params['submit'] : $parser->extR['submit']);
return($attr);
}
static function getID ($params, $parser, $givenid=NULL) {
if (isset($givenid)) {
$id = 'extr_' . $givenid;
if (in_array($id, $parser->extR['id'])) throw new Exception('Rform.php: Attribute "id" can not be used twice');
} elseif(isset($params['id'])) {
$id = $params['id'];
if (in_array($id, $parser->extR['id'])) throw new Exception('Rform.php: Attribute "id" can not be used twice');
} else {
if (isset($params['name'])) {
$id = 'extr_' . $parser->extR['form'] . '_' . $params['name'];
} else {
$id = 'extr_' . $parser->extR['form'] . '_' . $parser->extR['idcount'];
$parser->extR['idcount']++;
}
while (in_array($id, $parser->extR['id'])) {
$id = $parser->extR['form'] . '_' . $params['name'] . $parser->extR['idcount'];
$parser->extR['idcount']++;
}
}
if (isset($id)) $parser->extR['id'][] = $id;
return ($id);
}
static function renderArea ($input, $params, $parser, $frame) {
if (!array_key_exists('name', $params)) throw new Exception('Rform.php: Attribute "name" is required');
$tag = new HTMLtag('textarea',
array('id' => Rform::getID($params, $parser),
'class' => 'extrtextarea'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['area'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->body = htmlentities($input);
$tag->check();
return((string) $tag);
}
static function renderText ($input, $params, $parser, $frame) {
if (!array_key_exists('name', $params)) throw new Exception('Rform.php: Attribute "name" is required');
$tag = new HTMLtag('input',
array('id' => Rform::getID($params, $parser),
'type' => 'text',
'class' => 'extrtext'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['text'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->check();
return((string) $tag);
}
static function renderCheckbox ($input, $params, $parser, $frame) {
if (!array_key_exists('name', $params)) throw new Exception('Rform.php: Attribute "name" is required');
$tag = new HTMLtag('input',
array('id' => Rform::getID($params, $parser),
'type' => 'checkbox',
'class' => 'extrcheckbox'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['checkbox'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->check();
return((string) $tag);
}
static function renderRadio ($input, $params, $parser, $frame) {
if (!array_key_exists('name', $params)) throw new Exception('Rform.php: Attribute "name" is required');
$tag = new HTMLtag('input',
array('id' => Rform::getID($params, $parser),
'type' => 'radio',
'class' => 'extrradio'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['radio'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->check();
return((string) $tag);
}
static function renderSubmit ($input, $params, $parser, $frame) {
$tag = new HTMLtag('input',
array('id' => Rform::getID($params, $parser),
'type' => 'submit',
'class' => 'extrsubmit'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['submit'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('data-name', 'submit');
if (array_key_exists('disabled', $params)) unset($params['disabled']);
if (array_key_exists('readonly', $params)) unset($params['readonly']);
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->check();
return((string) $tag);
}
static function renderReset ($input, $params, $parser, $frame) {
$tag = new HTMLtag('input',
array('id' => Rform::getID($params, $parser),
'type' => 'reset',
'class' => 'extrreset'));
$tag->copy(HTMLtag::$attributes['html5'], $params);
$tag->copy(HTMLtag::$attributes['reset'], $params);
if ($parser->extR['usejquery']) {
$tag->attr(Rform::addExtRAttributes($params, $parser));
$tag->attr('data-name', 'reset');
if (array_key_exists('disabled', $params)) unset($params['disabled']);
if (array_key_exists('readonly', $params)) unset($params['readonly']);
$tag->attr('class', Rform::addJqueryClasses($params));
}
$tag->check();
return((string) $tag);
}
public function copies ($keys, $arr, $data=false) {
foreach ($keys as $key => $value) {
if (is_numeric($key)) {
$this->copy ($value, $arr, $data);
} else {
$this->copy ($key, $arr, $value);
}
}
}
static function renderSlider ($input, $params, $parser, $frame) {
global $wgVersion;
// if (version_compare($wgVersion, '1.16')<0) throw new Exception ('Rform.php: type="slider" can only be used for MediaWiki 1.16 or later');
if(!$parser->extR['usejquery']) throw new Exception ('Rform.php: type="slider" can only be used if jQuery is available and the MediaWiki version is 1.22.x or higher');
if (!array_key_exists('name', $params)) throw new Exception ('Rform.php: Attribute "name" for slider is required');
if (array_key_exists('value', $params) && array_key_exists('values', $params)) throw new Exception ('Rform.php: Attributes "value" and "values" for a slider can not be used at the same time');
$nhandle = 1;
if (array_key_exists('range', $params) && (strcasecmp($params['range'], 'true')==0)) { // more than one handle
if (!array_key_exists('values', $params)) throw new Exception ('Rform.php: Attribute "values" for slider is required for several handles');
$nhandle = count(explode(' ', trim($params['values'])));
}
$div = new HTMLtag('div',
array('id' => Rform::getID($params, $parser),
'class' => 'extrslider'));
$div->copy(HTMLtag::$attributes['html5'], $params);
$div->copy(HTMLtag::$attributes['slider'], $params, true);
if ($parser->extR['usejquery']) {
$div->attr(Rform::addExtRAttributes($params, $parser));
$div->attr('class', Rform::addJqueryClasses($params));
}
$div->check();
$div->body = '';
for ($i=0; $i<$nhandle; $i++) {
$inp = new HTMLtag('input');
$inp->attr(array('type' => 'hidden',
'data-form' => $parser->extR['form'],
'data-name' => $params['name'],
'data-item' => (string) $i));;
if ($nhandle==1)
$inp->attr('name', $params['name']);
else
$inp->attr('name', $params['name'] . '_' . $i);
$inp->check();
$div->body .= (string) $inp;
}
return((string) $div);
}
static function renderRinput ($input, $params, $parser, $frame) {
global $extr;
try {
// type="text" size="Länge" maxlength="MaxLänge" name="Name" value="Wert"
if (!property_exists($parser, 'extR')) throw new Exception ('Rform.php: Rform is missing');
$type = array_key_exists('type', $params) ? $params['type'] : 'text';
switch ($type) {
case 'area':
return (Rform::renderArea($input, $params, $parser, $frame));
case 'text':
return (Rform::renderText($input, $params, $parser, $frame));
case 'checkbox':
return (Rform::renderCheckbox($input, $params, $parser, $frame));
case 'radio':
return (Rform::renderRadio($input, $params, $parser, $frame));
case 'submit':
return (Rform::renderSubmit($input, $params, $parser, $frame));
case 'reset':
return (Rform::renderReset($input, $params, $parser, $frame));
case 'slider':
return (Rform::renderSlider($input, $params, $parser, $frame));
}
throw new Exception ('Rform.php: \'Rinput\' invalid type attribute');
}
catch(Exception $e) {
$extr->exception_log($e);
return ('<pre style="color:red">' . htmlentities($e->getMessage()) . '</pre>in<pre>' . htmlentities(new HTMLtag('Rinput', $params)) . '</pre>');
}
}
// deprecated
static function renderRarea ($input, $params, $parser, $frame) {
global $extr;
try {
if (!property_exists($parser, 'extR')) throw new Exception ('Rform.php: Rform is missing');
return(renderArea ($input, $params, $parser, $frame));
}
catch (Exception $e) {
$extr->exception_log($e);
return ('<pre style="color:red">' . htmlentities($e->getMessage()) . '</pre>in<pre>' . htmlentities(new HTMLtag('Rarea', $params)) . '</pre>');
}
}
}
?>