Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Printer / ConfigForm.php
blob21b8314bc5200040b4f6cba0e7e3e9a7dc4269e1
1 <?php
3 require_once 'HTMLPurifier/Printer.php';
5 class HTMLPurifier_Printer_ConfigForm extends HTMLPurifier_Printer
8 /**
9 * Printers for specific fields
10 * @protected
12 var $fields = array();
14 /**
15 * Documentation URL, can have fragment tagged on end
16 * @protected
18 var $docURL;
20 /**
21 * Name of form element to stuff config in
22 * @protected
24 var $name;
26 /**
27 * Whether or not to compress directive names, clipping them off
28 * after a certain amount of letters. False to disable or integer letters
29 * before clipping.
30 * @protected
32 var $compress = false;
34 /**
35 * @param $name Form element name for directives to be stuffed into
36 * @param $doc_url String documentation URL, will have fragment tagged on
37 * @param $compress Integer max length before compressing a directive name, set to false to turn off
39 function HTMLPurifier_Printer_ConfigForm(
40 $name, $doc_url = null, $compress = false
41 ) {
42 parent::HTMLPurifier_Printer();
43 $this->docURL = $doc_url;
44 $this->name = $name;
45 $this->compress = $compress;
46 // initialize sub-printers
47 $this->fields['default'] = new HTMLPurifier_Printer_ConfigForm_default();
48 $this->fields['bool'] = new HTMLPurifier_Printer_ConfigForm_bool();
51 /**
52 * Sets default column and row size for textareas in sub-printers
53 * @param $cols Integer columns of textarea, null to use default
54 * @param $rows Integer rows of textarea, null to use default
56 function setTextareaDimensions($cols = null, $rows = null) {
57 if ($cols) $this->fields['default']->cols = $cols;
58 if ($rows) $this->fields['default']->rows = $rows;
61 /**
62 * Retrieves styling, in case it is not accessible by webserver
64 function getCSS() {
65 return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
68 /**
69 * Retrieves JavaScript, in case it is not accessible by webserver
71 function getJavaScript() {
72 return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
75 /**
76 * Returns HTML output for a configuration form
77 * @param $config Configuration object of current form state
78 * @param $allowed Optional namespace(s) and directives to restrict form to.
80 function render($config, $allowed = true, $render_controls = true) {
81 $this->config = $config;
82 $this->prepareGenerator($config);
84 $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed);
85 $all = array();
86 foreach ($allowed as $key) {
87 list($ns, $directive) = $key;
88 $all[$ns][$directive] = $config->get($ns, $directive);
91 $ret = '';
92 $ret .= $this->start('table', array('class' => 'hp-config'));
93 $ret .= $this->start('thead');
94 $ret .= $this->start('tr');
95 $ret .= $this->element('th', 'Directive');
96 $ret .= $this->element('th', 'Value');
97 $ret .= $this->end('tr');
98 $ret .= $this->end('thead');
99 foreach ($all as $ns => $directives) {
100 $ret .= $this->renderNamespace($ns, $directives);
102 if ($render_controls) {
103 $ret .= $this->start('tbody');
104 $ret .= $this->start('tr');
105 $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
106 $ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
107 $ret .= '[<a href="?">Reset</a>]';
108 $ret .= $this->end('td');
109 $ret .= $this->end('tr');
110 $ret .= $this->end('tbody');
112 $ret .= $this->end('table');
113 return $ret;
117 * Renders a single namespace
118 * @param $ns String namespace name
119 * @param $directive Associative array of directives to values
120 * @protected
122 function renderNamespace($ns, $directives) {
123 $ret = '';
124 $ret .= $this->start('tbody', array('class' => 'namespace'));
125 $ret .= $this->start('tr');
126 $ret .= $this->element('th', $ns, array('colspan' => 2));
127 $ret .= $this->end('tr');
128 $ret .= $this->end('tbody');
129 $ret .= $this->start('tbody');
130 foreach ($directives as $directive => $value) {
131 $ret .= $this->start('tr');
132 $ret .= $this->start('th');
133 if ($this->docURL) {
134 $url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
135 $ret .= $this->start('a', array('href' => $url));
137 $attr = array('for' => "{$this->name}:$ns.$directive");
139 // crop directive name if it's too long
140 if (!$this->compress || (strlen($directive) < $this->compress)) {
141 $directive_disp = $directive;
142 } else {
143 $directive_disp = substr($directive, 0, $this->compress - 2) . '...';
144 $attr['title'] = $directive;
147 $ret .= $this->element(
148 'label',
149 $directive_disp,
150 // component printers must create an element with this id
151 $attr
153 if ($this->docURL) $ret .= $this->end('a');
154 $ret .= $this->end('th');
156 $ret .= $this->start('td');
157 $def = $this->config->def->info[$ns][$directive];
158 $type = $def->type;
159 if (!isset($this->fields[$type])) $type = 'default';
160 $type_obj = $this->fields[$type];
161 if ($def->allow_null) {
162 $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
164 $ret .= $type_obj->render($ns, $directive, $value, $this->name, $this->config);
165 $ret .= $this->end('td');
166 $ret .= $this->end('tr');
168 $ret .= $this->end('tbody');
169 return $ret;
175 * Printer decorator for directives that accept null
177 class HTMLPurifier_Printer_ConfigForm_NullDecorator extends HTMLPurifier_Printer {
179 * Printer being decorated
181 var $obj;
183 * @param $obj Printer to decorate
185 function HTMLPurifier_Printer_ConfigForm_NullDecorator($obj) {
186 parent::HTMLPurifier_Printer();
187 $this->obj = $obj;
189 function render($ns, $directive, $value, $name, $config) {
190 $this->prepareGenerator($config);
191 $ret = '';
192 $ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
193 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
194 $ret .= $this->text(' Null/Disabled');
195 $ret .= $this->end('label');
196 $attr = array(
197 'type' => 'checkbox',
198 'value' => '1',
199 'class' => 'null-toggle',
200 'name' => "$name"."[Null_$ns.$directive]",
201 'id' => "$name:Null_$ns.$directive",
202 'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
204 if ($value === null) $attr['checked'] = 'checked';
205 $ret .= $this->elementEmpty('input', $attr);
206 $ret .= $this->text(' or ');
207 $ret .= $this->elementEmpty('br');
208 $ret .= $this->obj->render($ns, $directive, $value, $name, $config);
209 return $ret;
214 * Swiss-army knife configuration form field printer
216 class HTMLPurifier_Printer_ConfigForm_default extends HTMLPurifier_Printer {
217 var $cols = 18;
218 var $rows = 5;
219 function render($ns, $directive, $value, $name, $config) {
220 $this->prepareGenerator($config);
221 // this should probably be split up a little
222 $ret = '';
223 $def = $config->def->info[$ns][$directive];
224 if (is_array($value)) {
225 switch ($def->type) {
226 case 'lookup':
227 $array = $value;
228 $value = array();
229 foreach ($array as $val => $b) {
230 $value[] = $val;
232 case 'list':
233 $value = implode(PHP_EOL, $value);
234 break;
235 case 'hash':
236 $nvalue = '';
237 foreach ($value as $i => $v) {
238 $nvalue .= "$i:$v" . PHP_EOL;
240 $value = $nvalue;
241 break;
242 default:
243 $value = '';
246 if ($def->type === 'mixed') {
247 return 'Not supported';
248 $value = serialize($value);
250 $attr = array(
251 'name' => "$name"."[$ns.$directive]",
252 'id' => "$name:$ns.$directive"
254 if ($value === null) $attr['disabled'] = 'disabled';
255 if (is_array($def->allowed)) {
256 $ret .= $this->start('select', $attr);
257 foreach ($def->allowed as $val => $b) {
258 $attr = array();
259 if ($value == $val) $attr['selected'] = 'selected';
260 $ret .= $this->element('option', $val, $attr);
262 $ret .= $this->end('select');
263 } elseif (
264 $def->type == 'text' || $def->type == 'itext' ||
265 $def->type == 'list' || $def->type == 'hash' || $def->type == 'lookup'
267 $attr['cols'] = $this->cols;
268 $attr['rows'] = $this->rows;
269 $ret .= $this->start('textarea', $attr);
270 $ret .= $this->text($value);
271 $ret .= $this->end('textarea');
272 } else {
273 $attr['value'] = $value;
274 $attr['type'] = 'text';
275 $ret .= $this->elementEmpty('input', $attr);
277 return $ret;
282 * Bool form field printer
284 class HTMLPurifier_Printer_ConfigForm_bool extends HTMLPurifier_Printer {
285 function render($ns, $directive, $value, $name, $config) {
286 $this->prepareGenerator($config);
287 $ret = '';
288 $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
290 $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
291 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
292 $ret .= $this->text(' Yes');
293 $ret .= $this->end('label');
295 $attr = array(
296 'type' => 'radio',
297 'name' => "$name"."[$ns.$directive]",
298 'id' => "$name:Yes_$ns.$directive",
299 'value' => '1'
301 if ($value) $attr['checked'] = 'checked';
302 $ret .= $this->elementEmpty('input', $attr);
304 $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
305 $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
306 $ret .= $this->text(' No');
307 $ret .= $this->end('label');
309 $attr = array(
310 'type' => 'radio',
311 'name' => "$name"."[$ns.$directive]",
312 'id' => "$name:No_$ns.$directive",
313 'value' => '0'
315 if (!$value) $attr['checked'] = 'checked';
316 $ret .= $this->elementEmpty('input', $attr);
318 $ret .= $this->end('div');
320 return $ret;