00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class HtmlHelper extends AppHelper {
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 var $tags = array(
00046 'meta' => '<meta%s/>',
00047 'metalink' => '<link href="%s"%s/>',
00048 'link' => '<a href="%s"%s>%s</a>',
00049 'mailto' => '<a href="mailto:%s" %s>%s</a>',
00050 'form' => '<form %s>',
00051 'formend' => '</form>',
00052 'input' => '<input name="%s" %s/>',
00053 'textarea' => '<textarea name="%s" %s>%s</textarea>',
00054 'hidden' => '<input type="hidden" name="%s" %s/>',
00055 'checkbox' => '<input type="checkbox" name="%s" %s/>',
00056 'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
00057 'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
00058 'selectstart' => '<select name="%s"%s>',
00059 'selectmultiplestart' => '<select name="%s[]"%s>',
00060 'selectempty' => '<option value=""%s> </option>',
00061 'selectoption' => '<option value="%s"%s>%s</option>',
00062 'selectend' => '</select>',
00063 'optiongroup' => '<optgroup label="%s"%s>',
00064 'optiongroupend' => '</optgroup>',
00065 'checkboxmultiplestart' => '',
00066 'checkboxmultipleend' => '',
00067 'password' => '<input type="password" name="%s" %s/>',
00068 'file' => '<input type="file" name="%s" %s/>',
00069 'file_no_model' => '<input type="file" name="%s" %s/>',
00070 'submit' => '<input type="submit" %s/>',
00071 'submitimage' => '<input type="image" src="%s" %s/>',
00072 'button' => '<input type="%s" %s/>',
00073 'image' => '<img src="%s" %s/>',
00074 'tableheader' => '<th%s>%s</th>',
00075 'tableheaderrow' => '<tr%s>%s</tr>',
00076 'tablecell' => '<td%s>%s</td>',
00077 'tablerow' => '<tr%s>%s</tr>',
00078 'block' => '<div%s>%s</div>',
00079 'blockstart' => '<div%s>',
00080 'blockend' => '</div>',
00081 'tag' => '<%s%s>%s</%s>',
00082 'tagstart' => '<%s%s>',
00083 'tagend' => '</%s>',
00084 'para' => '<p%s>%s</p>',
00085 'parastart' => '<p%s>',
00086 'label' => '<label for="%s"%s>%s</label>',
00087 'fieldset' => '<fieldset%s>%s</fieldset>',
00088 'fieldsetstart' => '<fieldset><legend>%s</legend>',
00089 'fieldsetend' => '</fieldset>',
00090 'legend' => '<legend>%s</legend>',
00091 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
00092 'style' => '<style type="text/css"%s>%s</style>',
00093 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
00094 'ul' => '<ul%s>%s</ul>',
00095 'ol' => '<ol%s>%s</ol>',
00096 'li' => '<li%s>%s</li>',
00097 'error' => '<div%s>%s</div>'
00098 );
00099
00100
00101
00102
00103
00104 var $base = null;
00105
00106
00107
00108
00109
00110 var $here = null;
00111
00112
00113
00114
00115
00116 var $params = array();
00117
00118
00119
00120
00121
00122 var $action = null;
00123
00124
00125
00126
00127
00128 var $data = null;
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 var $_crumbs = array();
00143
00144
00145
00146
00147
00148
00149 var $__docTypes = array(
00150 'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
00151 'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
00152 'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
00153 'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
00154 'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
00155 'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
00156 'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
00157 );
00158
00159
00160
00161
00162
00163
00164
00165 function addCrumb($name, $link = null, $options = null) {
00166 $this->_crumbs[] = array($name, $link, $options);
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 function docType($type = 'xhtml-strict') {
00184 if (isset($this->__docTypes[$type])) {
00185 return $this->output($this->__docTypes[$type]);
00186 }
00187 return null;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 function meta($type, $url = null, $attributes = array(), $inline = true) {
00199 if (!is_array($type)) {
00200 $types = array(
00201 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
00202 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
00203 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
00204 'keywords' => array('name' => 'keywords', 'content' => $url),
00205 'description' => array('name' => 'description', 'content' => $url),
00206 );
00207
00208 if ($type === 'icon' && $url === null) {
00209 $types['icon']['link'] = $this->webroot('favicon.ico');
00210 }
00211
00212 if (isset($types[$type])) {
00213 $type = $types[$type];
00214 } elseif (!isset($attributes['type']) && $url !== null) {
00215 if (is_array($url) && isset($url['ext'])) {
00216 $type = $types[$url['ext']];
00217 } else {
00218 $type = $types['rss'];
00219 }
00220 } elseif (isset($attributes['type']) && isset($types[$attributes['type']])) {
00221 $type = $types[$attributes['type']];
00222 unset($attributes['type']);
00223 } else {
00224 $type = array();
00225 }
00226 } elseif ($url !== null) {
00227 $inline = $url;
00228 }
00229 $attributes = array_merge($type, $attributes);
00230 $out = null;
00231
00232 if (isset($attributes['link'])) {
00233 if (isset($attributes['rel']) && $attributes['rel'] === 'icon') {
00234 $out = sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'), ' ', ' '));
00235 $attributes['rel'] = 'shortcut icon';
00236 } else {
00237 $attributes['link'] = $this->url($attributes['link'], true);
00238 }
00239 $out .= sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'), ' ', ' '));
00240 } else {
00241 $out = sprintf($this->tags['meta'], $this->_parseAttributes($attributes, array('type')));
00242 }
00243
00244 if ($inline) {
00245 return $this->output($out);
00246 } else {
00247 $view =& ClassRegistry::getObject('view');
00248 $view->addScript($out);
00249 }
00250 }
00251
00252
00253
00254
00255
00256
00257 function charset($charset = null) {
00258 if (empty($charset)) {
00259 $charset = strtolower(Configure::read('App.encoding'));
00260 }
00261 return $this->output(sprintf($this->tags['charset'], (!empty($charset) ? $charset : 'utf-8')));
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true) {
00280 if ($url !== null) {
00281 $url = $this->url($url);
00282 } else {
00283 $url = $this->url($title);
00284 $title = $url;
00285 $escapeTitle = false;
00286 }
00287
00288 if (isset($htmlAttributes['escape']) && $escapeTitle == true) {
00289 $escapeTitle = $htmlAttributes['escape'];
00290 }
00291
00292 if ($escapeTitle === true) {
00293 $title = h($title);
00294 } elseif (is_string($escapeTitle)) {
00295 $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
00296 }
00297
00298 if (!empty($htmlAttributes['confirm'])) {
00299 $confirmMessage = $htmlAttributes['confirm'];
00300 unset($htmlAttributes['confirm']);
00301 }
00302 if ($confirmMessage) {
00303 $confirmMessage = str_replace("'", "\'", $confirmMessage);
00304 $confirmMessage = str_replace('"', '\"', $confirmMessage);
00305 $htmlAttributes['onclick'] = "return confirm('{$confirmMessage}');";
00306 } elseif (isset($htmlAttributes['default']) && $htmlAttributes['default'] == false) {
00307 if (isset($htmlAttributes['onclick'])) {
00308 $htmlAttributes['onclick'] .= ' event.returnValue = false; return false;';
00309 } else {
00310 $htmlAttributes['onclick'] = 'event.returnValue = false; return false;';
00311 }
00312 unset($htmlAttributes['default']);
00313 }
00314 return $this->output(sprintf($this->tags['link'], $url, $this->_parseAttributes($htmlAttributes), $title));
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 function css($path, $rel = null, $htmlAttributes = array(), $inline = true) {
00328 if (is_array($path)) {
00329 $out = '';
00330 foreach ($path as $i) {
00331 $out .= "\n\t" . $this->css($i, $rel, $htmlAttributes, $inline);
00332 }
00333 if ($inline) {
00334 return $out . "\n";
00335 }
00336 return;
00337 }
00338
00339 if (strpos($path, ':
00340 $url = $path;
00341 } else {
00342 if ($path[0] !== '/') {
00343 $path = CSS_URL . $path;
00344 }
00345
00346 if (strpos($path, '?') === false) {
00347 if (strpos($path, '.css') === false) {
00348 $path .= '.css';
00349 }
00350 }
00351
00352 $path = $this->webroot($path);
00353
00354 $url = $path;
00355 if (strpos($path, '?') === false && ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force')) {
00356 $url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
00357 }
00358
00359 if (Configure::read('Asset.filter.css')) {
00360 $url = str_replace(CSS_URL, 'ccss/', $url);
00361 }
00362 }
00363
00364 if ($rel == 'import') {
00365 $out = sprintf($this->tags['style'], $this->_parseAttributes($htmlAttributes, null, '', ' '), '@import url(' . $url . ');');
00366 } else {
00367 if ($rel == null) {
00368 $rel = 'stylesheet';
00369 }
00370 $out = sprintf($this->tags['css'], $rel, $url, $this->_parseAttributes($htmlAttributes, null, '', ' '));
00371 }
00372 $out = $this->output($out);
00373
00374 if ($inline) {
00375 return $out;
00376 } else {
00377 $view =& ClassRegistry::getObject('view');
00378 $view->addScript($out);
00379 }
00380 }
00381
00382
00383
00384
00385
00386
00387
00388 function style($data, $inline = true) {
00389 if (!is_array($data)) {
00390 return $data;
00391 }
00392 $out = array();
00393 foreach ($data as $key=> $value) {
00394 $out[] = $key.':'.$value.';';
00395 }
00396 if ($inline) {
00397 return join(' ', $out);
00398 }
00399 return join("\n", $out);
00400 }
00401
00402
00403
00404
00405
00406
00407
00408 function getCrumbs($separator = '»', $startText = false) {
00409 if (count($this->_crumbs)) {
00410 $out = array();
00411 if ($startText) {
00412 $out[] = $this->link($startText, '/');
00413 }
00414
00415 foreach ($this->_crumbs as $crumb) {
00416 if (!empty($crumb[1])) {
00417 $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
00418 } else {
00419 $out[] = $crumb[0];
00420 }
00421 }
00422 return $this->output(join($separator, $out));
00423 } else {
00424 return null;
00425 }
00426 }
00427
00428
00429
00430
00431
00432
00433
00434 function image($path, $options = array()) {
00435 if (is_array($path)) {
00436 $path = $this->url($path);
00437 } elseif ($path[0] === '/') {
00438 $path = $this->webroot($path);
00439 } elseif (strpos($path, ':
00440 $path = $this->webroot(IMAGES_URL . $path);
00441
00442 if ((Configure::read('Asset.timestamp') == true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force') {
00443 $path .= '?' . @filemtime(str_replace('/', DS, WWW_ROOT . $path));
00444 }
00445 }
00446
00447 if (!isset($options['alt'])) {
00448 $options['alt'] = '';
00449 }
00450
00451 $url = false;
00452 if (!empty($options['url'])) {
00453 $url = $options['url'];
00454 unset($options['url']);
00455 }
00456
00457 $image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
00458
00459 if ($url) {
00460 return $this->output(sprintf($this->tags['link'], $this->url($url), null, $image));
00461 }
00462
00463 return $this->output($image);
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473 function tableHeaders($names, $trOptions = null, $thOptions = null) {
00474 $out = array();
00475 foreach ($names as $arg) {
00476 $out[] = sprintf($this->tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
00477 }
00478 $data = sprintf($this->tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
00479 return $this->output($data);
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
00492 if (empty($data[0]) || !is_array($data[0])) {
00493 $data = array($data);
00494 }
00495
00496 if ($oddTrOptions === true) {
00497 $useCount = true;
00498 $oddTrOptions = null;
00499 }
00500
00501 if ($evenTrOptions === false) {
00502 $continueOddEven = false;
00503 $evenTrOptions = null;
00504 }
00505
00506 if ($continueOddEven) {
00507 static $count = 0;
00508 } else {
00509 $count = 0;
00510 }
00511
00512 foreach ($data as $line) {
00513 $count++;
00514 $cellsOut = array();
00515 $i = 0;
00516 foreach ($line as $cell) {
00517 $cellOptions = array();
00518
00519 if (is_array($cell)) {
00520 $cellOptions = $cell[1];
00521 $cell = $cell[0];
00522 } elseif ($useCount) {
00523 $cellOptions['class'] = 'column-' . ++$i;
00524 }
00525 $cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
00526 }
00527 $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
00528 $out[] = sprintf($this->tags['tablerow'], $options, join(' ', $cellsOut));
00529 }
00530 return $this->output(join("\n", $out));
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 function tag($name, $text = null, $attributes = array(), $escape = false) {
00543 if ($escape) {
00544 $text = h($text);
00545 }
00546 if (!is_array($attributes)) {
00547 $attributes = array('class' => $attributes);
00548 }
00549 if ($text === null) {
00550 $tag = 'tagstart';
00551 } else {
00552 $tag = 'tag';
00553 }
00554 return $this->output(sprintf($this->tags[$tag], $name, $this->_parseAttributes($attributes, null, ' ', ''), $text, $name));
00555 }
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 function div($class = null, $text = null, $attributes = array(), $escape = false) {
00567 if ($class != null && !empty($class)) {
00568 $attributes['class'] = $class;
00569 }
00570 return $this->tag('div', $text, $attributes, $escape);
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 function para($class, $text, $attributes = array(), $escape = false) {
00582 if ($escape) {
00583 $text = h($text);
00584 }
00585 if ($class != null && !empty($class)) {
00586 $attributes['class'] = $class;
00587 }
00588 if ($text === null) {
00589 $tag = 'parastart';
00590 } else {
00591 $tag = 'para';
00592 }
00593 return $this->output(sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $text));
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605 function nestedList($list, $attributes = array(), $itemAttributes = array(), $tag = 'ul') {
00606 if (is_string($attributes)) {
00607 $tag = $attributes;
00608 $attributes = array();
00609 }
00610 $items = $this->__nestedListItem($list, $attributes, $itemAttributes, $tag);
00611 return sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $items);
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 function __nestedListItem($items, $attributes, $itemAttributes, $tag) {
00625 $out = '';
00626
00627 $index = 1;
00628 foreach ($items as $key => $item) {
00629 if (is_array($item)) {
00630 $item = $key . $this->nestedList($item, $attributes, $itemAttributes, $tag);
00631 }
00632 if (isset($itemAttributes['even']) && $index % 2 == 0) {
00633 $itemAttributes['class'] = $itemAttributes['even'];
00634 } else if (isset($itemAttributes['odd']) && $index % 2 != 0) {
00635 $itemAttributes['class'] = $itemAttributes['odd'];
00636 }
00637 $out .= sprintf($this->tags['li'], $this->_parseAttributes(array_diff_key($itemAttributes, array_flip(array('even', 'odd'))), null, ' ', ''), $item);
00638 $index++;
00639 }
00640 return $out;
00641 }
00642 }
00643 ?>