PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbBreadcrumbs.php

https://github.com/max-rautkin/yii-admincrud
PHP | 102 lines | 54 code | 10 blank | 38 comment | 10 complexity | abc4daa5b4b77cebb535801cac69a9c7 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*## TbCrumb class file.
  3. *
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  7. * @package bootstrap.widgets
  8. */
  9. Yii::import('zii.widgets.CBreadcrumbs');
  10. /**
  11. * Bootstrap breadcrumb widget.
  12. * @see <http://twitter.github.com/bootstrap/components.html#breadcrumbs>
  13. */
  14. class TbBreadcrumbs extends CBreadcrumbs
  15. {
  16. /**
  17. * @var string the separator between links in the breadcrumbs. Defaults to '/'.
  18. */
  19. public $separator = '/';
  20. /**
  21. *### .init()
  22. *
  23. * Initializes the widget.
  24. */
  25. public function init()
  26. {
  27. if (isset($this->htmlOptions['class'])) {
  28. $this->htmlOptions['class'] .= ' breadcrumb';
  29. } else {
  30. $this->htmlOptions['class'] = 'breadcrumb';
  31. }
  32. // apply bootstrap style
  33. $this->separator = '<span class="divider">' . $this->separator . '</span>';
  34. }
  35. /**
  36. *### .run()
  37. *
  38. * Renders the content of the widget.
  39. *
  40. * @throws CException
  41. */
  42. public function run()
  43. {
  44. // Hide empty breadcrumbs.
  45. if (empty($this->links)) {
  46. return;
  47. }
  48. $links = '';
  49. if (!isset($this->homeLink)) {
  50. $content = CHtml::link(Yii::t('zii', 'Home'), Yii::app()->homeUrl);
  51. $links .= $this->renderItem($content);
  52. } else if ($this->homeLink !== false) {
  53. $links .= $this->renderItem($this->homeLink);
  54. }
  55. $count = count($this->links);
  56. $counter = 0;
  57. foreach ($this->links as $label => $url) {
  58. ++$counter; // latest is the active one
  59. if (is_string($label) || is_array($url)) {
  60. $content = CHtml::link($this->encodeLabel ? CHtml::encode($label) : $label, $url);
  61. $links .= $this->renderItem($content);
  62. } else {
  63. $links .= $this->renderItem(
  64. $this->encodeLabel ? CHtml::encode($url) : $url,
  65. ($counter === $count)
  66. );
  67. }
  68. }
  69. echo CHtml::tag('ul', $this->htmlOptions, $links);
  70. }
  71. /**
  72. *### .renderItem()
  73. *
  74. * Renders a single breadcrumb item.
  75. *
  76. * @param string $content the content.
  77. * @param boolean $active whether the item is active.
  78. *
  79. * @return string the markup.
  80. */
  81. protected function renderItem($content, $active = false)
  82. {
  83. ob_start();
  84. echo CHtml::openTag('li', $active ? array('class' => 'active') : array());
  85. echo $content;
  86. if (!$active) {
  87. echo $this->separator;
  88. }
  89. echo '</li>';
  90. return ob_get_clean();
  91. }
  92. }