00001 <?php
00002
00003
00004
00005 define ('HTML_ATTR_EMPTY', '[[[[[empty]]]]');
00006
00007
00008
00009
00010
00011
00012
00013 class html
00014 {
00015
00016
00017
00018
00019 const EMPTY_ATTRIBUTE = '[[[[[empty]]]]';
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 public static function a($text, $href, $descr, $attrs = array()) {
00033
00034
00035 $url = str_replace(Config::get_url(Config::URL_SERVER), "", $href);
00036 $url = str_replace(Config::get_url(Config::URL_SERVER_SAFE), "", $href);
00037
00038 if (Url::current()->is_ancestor_of($url)) {
00039 html::_appendClass($attrs, 'active');
00040 }
00041 if (Url::current()->is_same_as($url)) {
00042 html::_appendClass($attrs, 'self');
00043 }
00044
00045 $attrs['href'] = $href;
00046 if ($descr) {
00047 $attrs['title'] = $descr;
00048 }
00049
00050 return html::tag('a', $text, $attrs);
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 public static function title($text, $descr = "") {
00062 $arrFilter = array("-", ",", ".", "(", ")", ":", "'", '"', "&");
00063 $keywords = str_replace($arrFilter, " ", $descr);
00064 $keywords = str_replace(" ", " ", $keywords);
00065 $keywords = str_replace(" ", ",", $keywords);
00066
00067 $ret = self::tag('title', String::escape($text)) . "\n";
00068 $ret .= self::meta('title', $text) . "\n";
00069 $ret .= self::meta('description', $descr) . "\n";
00070 $ret .= self::meta('keywords', $keywords) . "\n";
00071 return $ret;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 public static function img($path, $alt, $attrs = array()) {
00084 if ($alt === '') {
00085 $alt = self::EMPTY_ATTRIBUTE;
00086 }
00087 $attrs['src'] = $path;
00088 $attrs['alt'] = $alt;
00089 return self::tag_selfclosing('img', $attrs);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 public static function error($text) {
00099 return html::p($text, "error");
00100 }
00101
00102 public static function success($text) {
00103 return html::p($text, "success");
00104 }
00105
00106 public static function note($text) {
00107 return html::p($text, "note");
00108 }
00109
00110 public static function warning($text) {
00111 return html::p($text, "warning");
00112 }
00113
00114 public static function info($text) {
00115 return html::p($text, "info");
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 public static function div($text, $cls = '') {
00127 return html::tag('div', $text, array('class' => $cls));
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 public static function p($text, $cls = '') {
00139 return html::tag('p', $text, array('class' => $cls));
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 public static function span($text, $cls = '') {
00151 return html::tag('span', $text, array('class' => $cls));
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 public static function b($text, $cls = "") {
00164 return html::tag('strong', $text, array('class' => $cls));
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 public static function em($text, $cls = "") {
00176 return html::tag('em', $text, array('class' => $cls));
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 public static function h($text, $level, $cls="") {
00189 return html::tag('h' . $level, $text, array('class' => $cls));
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 public static function tag($tag, $text, $attrs = array()) {
00202 return '<' . $tag . html::attrs($attrs) . ">" . $text . "</" . $tag . ">";
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 public static function tag_selfclosing($tag, $attrs = array()) {
00214 return '<' . $tag . html::attrs($attrs) . " />";
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 public static function li($items, $cls = '', $useOrdered = false) {
00232 $c = count($items);
00233 if ($c == 0) {
00234 return '';
00235 }
00236
00237 $li = '';
00238 $i = 0;
00239 foreach($items as $item) {
00240 $arr_cls = array($cls);
00241 $arr_cls[] = (++$i % 2) ? 'uneven' : 'even';
00242 if ($i === 1) {
00243 $arr_cls[] = 'first';
00244 }
00245 if ($i === $c) {
00246 $arr_cls[] = 'last';
00247 }
00248 $li .= html::tag('li', $item, array('class' => $arr_cls));
00249 }
00250
00251 $tag = ($useOrdered) ? 'ol' : 'ul';
00252 return html::tag($tag, $li, array('class' => $cls));
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 public static function dl($items, $cls = '') {
00268 $c = count($items);
00269 if ($c == 0) {
00270 return '';
00271 }
00272
00273 $list = "\n";
00274 $i = 0;
00275 foreach($items as $dt => $dd) {
00276 $arr_cls = array($cls);
00277 $arr_cls[] = (++$i % 2) ? 'uneven' : 'even';
00278 if ($i === 1) {
00279 $arr_cls[] = 'first';
00280 }
00281 if ($i === $c) {
00282 $arr_cls[] = 'last';
00283 }
00284 $list .= html::tag('dt', $dt, array('class' => $arr_cls));
00285 $list .= "\n";
00286 $list .= html::tag('dd', $dd, array('class' => $arr_cls));
00287 $list .= "\n";
00288 }
00289
00290 return html::tag('dl', $list, array('class' => $cls));
00291 }
00292
00293
00294
00295
00296 public static function submit($text, $name, $descr, $attrs = array()) {
00297 $attrs['alt'] = $descr;
00298 $attrs['title'] = $descr;
00299 $attrs['value'] = $text;
00300 return self::input('submit', $name, $attrs);
00301 }
00302
00303 public static function label($title, $for, $cls = '') {
00304 $attrs = array(
00305 'for' => $for,
00306 'class' => $cls
00307 );
00308 return self::tag('label', self::span($title), $attrs);
00309 }
00310
00311
00312
00313
00314 public static function input($type, $name, $attrs) {
00315 $attrs['name'] = $name;
00316 $attrs['type'] = $type;
00317 self::_appendClass($attrs, $type);
00318 return self::tag_selfclosing('input', $attrs);
00319 }
00320
00321
00322
00323
00324 public static function form($name, $action, $content, $method = 'post', $attrs = array()) {
00325 $attrs['name'] = $name;
00326 $attrs['id'] = Arr::get_item($attrs, 'id', $name);
00327 $attrs['action'] = $action;
00328 $attrs['method'] = $method;
00329 return self::tag('form', $content, $attrs);
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 public static function select($name, $options, $value, $attrs = array()) {
00342 $opts = self::options(Arr::force($options), Arr::force($value, false));
00343 $attrs['name'] = $name;
00344 return html::tag('select', $opts, $attrs);
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354 private static function options($options, $selected_values) {
00355 $opts = '';
00356 foreach($options as $option => $display) {
00357 $opts .= "\n" . self::option($option, $display, $selected_values);
00358 }
00359 return $opts;
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 private static function option($key, $display, $selected_values) {
00371 $ret = '';
00372 if (is_array($display)) {
00373 $opts = self::options($display, $selected_values);
00374 $ret = html::tag('optgroup', $opts, array('label' => $key));
00375 }
00376 else {
00377 $opt_attrs = array();
00378 if (empty($key)) {
00379 $key = self::EMPTY_ATTRIBUTE;
00380 }
00381 elseif (in_array($key, $selected_values)) {
00382 $opt_attrs['selected'] = 'selected';
00383 }
00384 $opt_attrs['value'] = $key;
00385 $ret = html::tag('option', String::escape($display), $opt_attrs);
00386 }
00387 return $ret;
00388 }
00389
00390
00391
00392
00393
00394
00395 public static function attr($name, $value) {
00396 $value = str_replace("\n", ' ', $value);
00397 if ($value === self::EMPTY_ATTRIBUTE) {
00398 $value = '';
00399 } else if (empty($value) && strval($value) !== '0') {
00400 return '';
00401 }
00402
00403
00404 $clean_name = preg_replace('|[^\w:_-]|', '', $name);
00405 if ($clean_name === '') {
00406 return '';
00407 }
00408
00409 return ' ' . $clean_name . '="' . String::escape($value) . '"';
00410 }
00411
00412 public static function attrs($arr) {
00413 $ret = '';
00414 foreach($arr as $key => $value) {
00415 if ($key === 'class' && is_array($value)) {
00416 $value = implode(' ', $value);
00417 }
00418 $ret .= html::attr($key, $value);
00419 }
00420 return $ret;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429 public static function include_js($path) {
00430 return '<script type="text/javascript" src="' . String::clear_html($path) . '"></script>';
00431 }
00432
00433
00434
00435
00436
00437
00438
00439 public static function script_js($content) {
00440 $content = "<!--// <![CDATA[\n" . $content . "\n// ]]> -->";
00441 $attrs = array(
00442 'type' => 'text/javascript'
00443 );
00444 return html::tag('script', $content, $attrs);
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454 public static function include_css($path, $media = 'screen') {
00455 return '<style type="text/css" media="' . String::clear_html($media) . '">@import url(' . String::clear_html($path) . ');</style>';
00456 }
00457
00458
00459
00460
00461
00462
00463
00464 public static function style($content) {
00465 $attrs = array(
00466 'type' => 'text/css'
00467 );
00468 return html::tag('style', $content, $attrs);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478 public static function meta($name, $content) {
00479 $attrs = array(
00480 'name' => $name,
00481 'content' => $content
00482 );
00483 return self::tag_selfclosing('meta', $attrs);
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493 public static function meta_http_equiv($equiv, $content) {
00494 $attrs = array(
00495 'http-equiv' => $equiv,
00496 'content' => $content
00497 );
00498 return self::tag_selfclosing('meta', $attrs);
00499 }
00500
00501
00502
00503
00504 public static function br($cls = ''){
00505 return '<br' . html::attr('class', $cls) . ' />';
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 public static function flash($file, $attrs = array(), $params = array()) {
00518
00519 $alternative = tr('You need flash to be installed, to view this content', 'core');
00520 if (!isset($params['wmode'])) {
00521 $params['wmode'] = 'transparent';
00522 }
00523 $param_tags = '';
00524 foreach($params as $key => $value) {
00525 $param_tags .= self::tag_selfclosing('param', array('name' => $key, 'value' => $value)) . "\n";
00526 }
00527
00528 self::_appendClass($attrs, 'player');
00529 $ie_attrs = array_merge($attrs, array(
00530 'classid' => 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000',
00531 'codebase' => 'http://get.adobe.com/shockwave/'
00532 ));
00533 $other_attrs = array_merge($attrs, array(
00534 'type' => 'application/x-shockwave-flash',
00535 'data' => $file,
00536 ));
00537
00538 $ret = '';
00539
00540 $ret .= "<!--[if !IE]> -->\n" . html::tag('object', $param_tags, $other_attrs) . "\n<!-- <![endif]-->\n";
00541
00542 $param_tags .= self::tag_selfclosing('param', array('name' => 'movie', 'value' => $file)) . "\n";
00543 $ret .= "<!--[if IE]> \n" . html::tag('object', $param_tags, $ie_attrs) . "\n<![endif]-->\n";
00544
00545 return $ret;
00546 }
00547
00548
00549
00550
00551 public static function td($text, $attr = array(), $is_head = false) {
00552 if ($is_head) {
00553 return self::tag('th', $text, $attr);
00554 }
00555 return self::tag('td', $text, $attr);
00556 }
00557
00558
00559
00560
00561
00562
00563 public static function tr($cells, $attr = array()) {
00564 $text = implode("\n", Arr::force($cells));
00565 return self::tag('tr', $text, $attr);
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 public static function table($rows, $head, $summary, $attr = array()) {
00586 $ret = '';
00587
00588 $body = self::table_build_rows($rows);
00589 $head = self::table_build_rows($head);
00590
00591 $content = '';
00592 $content .= ($head) ? html::tag('thead', $head) . "\n" : '';
00593 $content .= ($body) ? html::tag('tbody', $body) . "\n" : '';
00594
00595 $attr['summary'] = $summary;
00596 $ret .= html::tag(
00597 'table',
00598 $content,
00599 $attr
00600 );
00601
00602 return $ret;
00603 }
00604
00605
00606
00607
00608
00609
00610 private function table_build_rows($rows) {
00611 $ret = '';
00612 $i = 0;
00613 $c = count($rows);
00614
00615 if ($c && !is_array($rows[0])) {
00616 $rows = array($rows);
00617 }
00618
00619 foreach($rows as $cells) {
00620 $arr_cls = array();
00621 $arr_cls[] = (++$i % 2) ? 'uneven' : 'even';
00622 if ($i === 1) {
00623 $arr_cls[] = 'first';
00624 }
00625 if ($i === $c) {
00626 $arr_cls[] = 'last';
00627 }
00628 $ret .= html::tr($cells, array('class' => $arr_cls));
00629 }
00630 return $ret;
00631 }
00632
00633 private static function _appendClass(&$attrs, $cls) {
00634 $oldcls = Arr::get_item($attrs, 'class', '');
00635 if (!empty($oldcls)) {
00636 $oldcls .= ' ';
00637 }
00638 $oldcls .= $cls;
00639 $attrs['class'] = $oldcls;
00640 }
00641
00642 }