CBreadcrumbs.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * CBreadcrumbs class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CBreadcrumbs displays a list of links indicating the position of the current page in the whole website.
  12. *
  13. * For example, breadcrumbs like "Home > Sample Post > Edit" means the user is viewing an edit page
  14. * for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home"
  15. * to return to the homepage.
  16. *
  17. * To use CBreadcrumbs, one usually needs to configure its {@link links} property, which specifies
  18. * the links to be displayed. For example,
  19. *
  20. * <pre>
  21. * $this->widget('zii.widgets.CBreadcrumbs', array(
  22. * 'links'=>array(
  23. * 'Sample post'=>array('post/view', 'id'=>12),
  24. * 'Edit',
  25. * ),
  26. * ));
  27. * </pre>
  28. *
  29. * Because breadcrumbs usually appears in nearly every page of a website, the widget is better to be placed
  30. * in a layout view. One can define a property "breadcrumbs" in the base controller class and assign it to the widget
  31. * in the layout, like the following:
  32. *
  33. * <pre>
  34. * $this->widget('zii.widgets.CBreadcrumbs', array(
  35. * 'links'=>$this->breadcrumbs,
  36. * ));
  37. * </pre>
  38. *
  39. * Then, in each view script, one only needs to assign the "breadcrumbs" property as needed.
  40. *
  41. * @author Qiang Xue <qiang.xue@gmail.com>
  42. * @package zii.widgets
  43. * @since 1.1
  44. */
  45. class CBreadcrumbs extends CWidget
  46. {
  47. /**
  48. * @var string the tag name for the breadcrumbs container tag. Defaults to 'div'.
  49. */
  50. public $tagName='div';
  51. /**
  52. * @var array the HTML attributes for the breadcrumbs container tag.
  53. */
  54. public $htmlOptions=array('class'=>'breadcrumbs');
  55. /**
  56. * @var boolean whether to HTML encode the link labels. Defaults to true.
  57. */
  58. public $encodeLabel=true;
  59. /**
  60. * @var string the first hyperlink in the breadcrumbs (called home link).
  61. * If this property is not set, it defaults to a link pointing to {@link CWebApplication::homeUrl} with label 'Home'.
  62. * If this property is false, the home link will not be rendered.
  63. */
  64. public $homeLink;
  65. /**
  66. * @var array list of hyperlinks to appear in the breadcrumbs. If this property is empty,
  67. * the widget will not render anything. Each key-value pair in the array
  68. * will be used to generate a hyperlink by calling CHtml::link(key, value). For this reason, the key
  69. * refers to the label of the link while the value can be a string or an array (used to
  70. * create a URL). For more details, please refer to {@link CHtml::link}.
  71. * If an element's key is an integer, it means the element will be rendered as a label only (meaning the current page).
  72. *
  73. * The following example will generate breadcrumbs as "Home > Sample post > Edit", where "Home" points to the homepage,
  74. * "Sample post" points to the "index.php?r=post/view&id=12" page, and "Edit" is a label. Note that the "Home" link
  75. * is specified via {@link homeLink} separately.
  76. *
  77. * <pre>
  78. * array(
  79. * 'Sample post'=>array('post/view', 'id'=>12),
  80. * 'Edit',
  81. * )
  82. * </pre>
  83. */
  84. public $links=array();
  85. /**
  86. * @var string String, specifies how each active item is rendered. Defaults to
  87. * "<a href="{url}">{label}</a>", where "{label}" will be replaced by the corresponding item
  88. * label while "{url}" will be replaced by the URL of the item.
  89. * @since 1.1.11
  90. */
  91. public $activeLinkTemplate='<a href="{url}">{label}</a>';
  92. /**
  93. * @var string String, specifies how each inactive item is rendered. Defaults to
  94. * "<span>{label}</span>", where "{label}" will be replaced by the corresponding item label.
  95. * Note that inactive template does not have "{url}" parameter.
  96. * @since 1.1.11
  97. */
  98. public $inactiveLinkTemplate='<span>{label}</span>';
  99. /**
  100. * @var string the separator between links in the breadcrumbs. Defaults to ' &raquo; '.
  101. */
  102. public $separator=' &raquo; ';
  103. /**
  104. * Renders the content of the portlet.
  105. */
  106. public function run()
  107. {
  108. if(empty($this->links))
  109. return;
  110. $definedLinks = $this->links;
  111. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  112. $links=array();
  113. if($this->homeLink===null)
  114. $definedLinks=array_merge(array(Yii::t('zii','Home') => Yii::app()->homeUrl),$definedLinks);
  115. elseif($this->homeLink!==false)
  116. $links[]=$this->homeLink;
  117. foreach($definedLinks as $label=>$url)
  118. {
  119. if(is_string($label) || is_array($url))
  120. $links[]=strtr($this->activeLinkTemplate,array(
  121. '{url}'=>CHtml::normalizeUrl($url),
  122. '{label}'=>$this->encodeLabel ? CHtml::encode($label) : $label,
  123. ));
  124. else
  125. $links[]=str_replace('{label}',$this->encodeLabel ? CHtml::encode($url) : $url,$this->inactiveLinkTemplate);
  126. }
  127. echo implode($this->separator,$links);
  128. echo CHtml::closeTag($this->tagName);
  129. }
  130. }