ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ConfigForm.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
12  protected $fields = array();
13 
17  protected $docURL;
18 
22  protected $name;
23 
29  protected $compress = false;
30 
36  public function __construct(
37  $name, $doc_url = null, $compress = false
38  ) {
40  $this->docURL = $doc_url;
41  $this->name = $name;
42  $this->compress = $compress;
43  // initialize sub-printers
46  }
47 
53  public function setTextareaDimensions($cols = null, $rows = null) {
54  if ($cols) $this->fields['default']->cols = $cols;
55  if ($rows) $this->fields['default']->rows = $rows;
56  }
57 
61  public static function getCSS() {
62  return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.css');
63  }
64 
68  public static function getJavaScript() {
69  return file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/Printer/ConfigForm.js');
70  }
71 
78  public function render($config, $allowed = true, $render_controls = true) {
79  if (is_array($config) && isset($config[0])) {
80  $gen_config = $config[0];
81  $config = $config[1];
82  } else {
83  $gen_config = $config;
84  }
85 
86  $this->config = $config;
87  $this->genConfig = $gen_config;
88  $this->prepareGenerator($gen_config);
89 
91  $all = array();
92  foreach ($allowed as $key) {
93  list($ns, $directive) = $key;
94  $all[$ns][$directive] = $config->get($ns .'.'. $directive);
95  }
96 
97  $ret = '';
98  $ret .= $this->start('table', array('class' => 'hp-config'));
99  $ret .= $this->start('thead');
100  $ret .= $this->start('tr');
101  $ret .= $this->element('th', 'Directive', array('class' => 'hp-directive'));
102  $ret .= $this->element('th', 'Value', array('class' => 'hp-value'));
103  $ret .= $this->end('tr');
104  $ret .= $this->end('thead');
105  foreach ($all as $ns => $directives) {
106  $ret .= $this->renderNamespace($ns, $directives);
107  }
108  if ($render_controls) {
109  $ret .= $this->start('tbody');
110  $ret .= $this->start('tr');
111  $ret .= $this->start('td', array('colspan' => 2, 'class' => 'controls'));
112  $ret .= $this->elementEmpty('input', array('type' => 'submit', 'value' => 'Submit'));
113  $ret .= '[<a href="?">Reset</a>]';
114  $ret .= $this->end('td');
115  $ret .= $this->end('tr');
116  $ret .= $this->end('tbody');
117  }
118  $ret .= $this->end('table');
119  return $ret;
120  }
121 
127  protected function renderNamespace($ns, $directives) {
128  $ret = '';
129  $ret .= $this->start('tbody', array('class' => 'namespace'));
130  $ret .= $this->start('tr');
131  $ret .= $this->element('th', $ns, array('colspan' => 2));
132  $ret .= $this->end('tr');
133  $ret .= $this->end('tbody');
134  $ret .= $this->start('tbody');
135  foreach ($directives as $directive => $value) {
136  $ret .= $this->start('tr');
137  $ret .= $this->start('th');
138  if ($this->docURL) {
139  $url = str_replace('%s', urlencode("$ns.$directive"), $this->docURL);
140  $ret .= $this->start('a', array('href' => $url));
141  }
142  $attr = array('for' => "{$this->name}:$ns.$directive");
143 
144  // crop directive name if it's too long
145  if (!$this->compress || (strlen($directive) < $this->compress)) {
146  $directive_disp = $directive;
147  } else {
148  $directive_disp = substr($directive, 0, $this->compress - 2) . '...';
149  $attr['title'] = $directive;
150  }
151 
152  $ret .= $this->element(
153  'label',
154  $directive_disp,
155  // component printers must create an element with this id
156  $attr
157  );
158  if ($this->docURL) $ret .= $this->end('a');
159  $ret .= $this->end('th');
160 
161  $ret .= $this->start('td');
162  $def = $this->config->def->info["$ns.$directive"];
163  if (is_int($def)) {
164  $allow_null = $def < 0;
165  $type = abs($def);
166  } else {
167  $type = $def->type;
168  $allow_null = isset($def->allow_null);
169  }
170  if (!isset($this->fields[$type])) $type = 0; // default
171  $type_obj = $this->fields[$type];
172  if ($allow_null) {
173  $type_obj = new HTMLPurifier_Printer_ConfigForm_NullDecorator($type_obj);
174  }
175  $ret .= $type_obj->render($ns, $directive, $value, $this->name, array($this->genConfig, $this->config));
176  $ret .= $this->end('td');
177  $ret .= $this->end('tr');
178  }
179  $ret .= $this->end('tbody');
180  return $ret;
181  }
182 
183 }
184 
192  protected $obj;
196  public function __construct($obj) {
198  $this->obj = $obj;
199  }
200  public function render($ns, $directive, $value, $name, $config) {
201  if (is_array($config) && isset($config[0])) {
202  $gen_config = $config[0];
203  $config = $config[1];
204  } else {
205  $gen_config = $config;
206  }
207  $this->prepareGenerator($gen_config);
208 
209  $ret = '';
210  $ret .= $this->start('label', array('for' => "$name:Null_$ns.$directive"));
211  $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
212  $ret .= $this->text(' Null/Disabled');
213  $ret .= $this->end('label');
214  $attr = array(
215  'type' => 'checkbox',
216  'value' => '1',
217  'class' => 'null-toggle',
218  'name' => "$name"."[Null_$ns.$directive]",
219  'id' => "$name:Null_$ns.$directive",
220  'onclick' => "toggleWriteability('$name:$ns.$directive',checked)" // INLINE JAVASCRIPT!!!!
221  );
222  if ($this->obj instanceof HTMLPurifier_Printer_ConfigForm_bool) {
223  // modify inline javascript slightly
224  $attr['onclick'] = "toggleWriteability('$name:Yes_$ns.$directive',checked);toggleWriteability('$name:No_$ns.$directive',checked)";
225  }
226  if ($value === null) $attr['checked'] = 'checked';
227  $ret .= $this->elementEmpty('input', $attr);
228  $ret .= $this->text(' or ');
229  $ret .= $this->elementEmpty('br');
230  $ret .= $this->obj->render($ns, $directive, $value, $name, array($gen_config, $config));
231  return $ret;
232  }
233 }
234 
239  public $cols = 18;
240  public $rows = 5;
241  public function render($ns, $directive, $value, $name, $config) {
242  if (is_array($config) && isset($config[0])) {
243  $gen_config = $config[0];
244  $config = $config[1];
245  } else {
246  $gen_config = $config;
247  }
248  $this->prepareGenerator($gen_config);
249  // this should probably be split up a little
250  $ret = '';
251  $def = $config->def->info["$ns.$directive"];
252  if (is_int($def)) {
253  $type = abs($def);
254  } else {
255  $type = $def->type;
256  }
257  if (is_array($value)) {
258  switch ($type) {
260  $array = $value;
261  $value = array();
262  foreach ($array as $val => $b) {
263  $value[] = $val;
264  }
266  $value = implode(PHP_EOL, $value);
267  break;
269  $nvalue = '';
270  foreach ($value as $i => $v) {
271  $nvalue .= "$i:$v" . PHP_EOL;
272  }
273  $value = $nvalue;
274  break;
275  default:
276  $value = '';
277  }
278  }
279  if ($type === HTMLPurifier_VarParser::MIXED) {
280  return 'Not supported';
281  $value = serialize($value);
282  }
283  $attr = array(
284  'name' => "$name"."[$ns.$directive]",
285  'id' => "$name:$ns.$directive"
286  );
287  if ($value === null) $attr['disabled'] = 'disabled';
288  if (isset($def->allowed)) {
289  $ret .= $this->start('select', $attr);
290  foreach ($def->allowed as $val => $b) {
291  $attr = array();
292  if ($value == $val) $attr['selected'] = 'selected';
293  $ret .= $this->element('option', $val, $attr);
294  }
295  $ret .= $this->end('select');
296  } elseif (
297  $type === HTMLPurifier_VarParser::TEXT ||
298  $type === HTMLPurifier_VarParser::ITEXT ||
299  $type === HTMLPurifier_VarParser::ALIST ||
300  $type === HTMLPurifier_VarParser::HASH ||
302  ) {
303  $attr['cols'] = $this->cols;
304  $attr['rows'] = $this->rows;
305  $ret .= $this->start('textarea', $attr);
306  $ret .= $this->text($value);
307  $ret .= $this->end('textarea');
308  } else {
309  $attr['value'] = $value;
310  $attr['type'] = 'text';
311  $ret .= $this->elementEmpty('input', $attr);
312  }
313  return $ret;
314  }
315 }
316 
321  public function render($ns, $directive, $value, $name, $config) {
322  if (is_array($config) && isset($config[0])) {
323  $gen_config = $config[0];
324  $config = $config[1];
325  } else {
326  $gen_config = $config;
327  }
328  $this->prepareGenerator($gen_config);
329  $ret = '';
330  $ret .= $this->start('div', array('id' => "$name:$ns.$directive"));
331 
332  $ret .= $this->start('label', array('for' => "$name:Yes_$ns.$directive"));
333  $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
334  $ret .= $this->text(' Yes');
335  $ret .= $this->end('label');
336 
337  $attr = array(
338  'type' => 'radio',
339  'name' => "$name"."[$ns.$directive]",
340  'id' => "$name:Yes_$ns.$directive",
341  'value' => '1'
342  );
343  if ($value === true) $attr['checked'] = 'checked';
344  if ($value === null) $attr['disabled'] = 'disabled';
345  $ret .= $this->elementEmpty('input', $attr);
346 
347  $ret .= $this->start('label', array('for' => "$name:No_$ns.$directive"));
348  $ret .= $this->element('span', "$ns.$directive:", array('class' => 'verbose'));
349  $ret .= $this->text(' No');
350  $ret .= $this->end('label');
351 
352  $attr = array(
353  'type' => 'radio',
354  'name' => "$name"."[$ns.$directive]",
355  'id' => "$name:No_$ns.$directive",
356  'value' => '0'
357  );
358  if ($value === false) $attr['checked'] = 'checked';
359  if ($value === null) $attr['disabled'] = 'disabled';
360  $ret .= $this->elementEmpty('input', $attr);
361 
362  $ret .= $this->end('div');
363 
364  return $ret;
365  }
366 }
367 
368 // vim: et sw=4 sts=4