root/afridex/plugins/Flutter/purifier_lib/HTMLPurifier/Printer/ConfigForm.php @ 22

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