You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					142 lines
				
				4.8 KiB
			
		
		
			
		
	
	
					142 lines
				
				4.8 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\widgets;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | use Yii;
 | ||
|  | use yii\base\Widget;
 | ||
| 
											13 years ago
										 | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy.
 | ||
|  |  *
 | ||
|  |  * For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page
 | ||
|  |  * for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home"
 | ||
|  |  * to return to the homepage.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example,
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * ~~~
 | ||
| 
											13 years ago
										 |  * // $this is the view object currently being used
 | ||
| 
											13 years ago
										 |  * echo Breadcrumbs::widget(array(
 | ||
| 
											13 years ago
										 |  *     'links' => array(
 | ||
|  |  *         array('label' => 'Sample Post', 'url' => array('post/edit', 'id' => 1)),
 | ||
|  |  *         'Edit',
 | ||
|  |  *     ),
 | ||
|  |  * ));
 | ||
|  |  * ~~~
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view.
 | ||
|  |  * You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different
 | ||
| 
											13 years ago
										 |  * views. In the layout view, you assign this view parameter to the [[links]] property like the following:
 | ||
|  |  *
 | ||
|  |  * ~~~
 | ||
| 
											13 years ago
										 |  * // $this is the view object currently being used
 | ||
| 
											13 years ago
										 |  * echo Breadcrumbs::widget(array(
 | ||
| 
											13 years ago
										 |  *     'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : array(),
 | ||
|  |  * ));
 | ||
|  |  * ~~~
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class Breadcrumbs extends Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the name of the breadcrumb container tag.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $tag = 'ul';
 | ||
|  | 	/**
 | ||
|  | 	 * @var array the HTML attributes for the breadcrumb container tag.
 | ||
|  | 	 */
 | ||
|  | 	public $options = array('class' => 'breadcrumb');
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether to HTML-encode the link labels.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $encodeLabels = true;
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the first hyperlink in the breadcrumbs (called home link).
 | ||
| 
											13 years ago
										 | 	 * If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]]
 | ||
|  | 	 * with the label 'Home'. If this property is false, the home link will not be rendered.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $homeLink;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array list of links to appear in the breadcrumbs. If this property is empty,
 | ||
|  | 	 * the widget will not render anything. Each array element represents a single link in the breadcrumbs
 | ||
|  | 	 * with the following structure:
 | ||
| 
											13 years ago
										 | 	 *
 | ||
| 
											13 years ago
										 | 	 * ~~~
 | ||
| 
											13 years ago
										 | 	 * array(
 | ||
| 
											13 years ago
										 | 	 *     'label' => 'label of the link',  // required
 | ||
|  | 	 *     'url' => 'url of the link',      // optional, will be processed by Html::url()
 | ||
| 
											13 years ago
										 | 	 * )
 | ||
| 
											13 years ago
										 | 	 * ~~~
 | ||
|  | 	 *
 | ||
|  | 	 * If a link is active, you only need to specify its "label", and instead of writing `array('label' => $label)`,
 | ||
|  | 	 * you should simply use `$label`.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $links = array();
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the template used to render each inactive item in the breadcrumbs. The token `{link}`
 | ||
|  | 	 * will be replaced with the actual HTML link for each inactive item.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $itemTemplate = "<li>{link} <span class=\"divider\">/</span></li>\n";
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the template used to render each active item in the breadcrumbs. The token `{link}`
 | ||
|  | 	 * will be replaced with the actual HTML link for each active item.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $activeItemTemplate = "<li class=\"active\">{link}</li>\n";
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders the widget.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (empty($this->links)) {
 | ||
| 
											13 years ago
										 | 			return;
 | ||
|  | 		}
 | ||
|  | 		$links = array();
 | ||
|  | 		if ($this->homeLink === null) {
 | ||
| 
											13 years ago
										 | 			$links[] = $this->renderItem(array(
 | ||
| 
											13 years ago
										 | 				'label' => Yii::t('yii', 'Home'),
 | ||
| 
											13 years ago
										 | 				'url' => Yii::$app->homeUrl,
 | ||
|  | 			), $this->itemTemplate);
 | ||
| 
											13 years ago
										 | 		} elseif ($this->homeLink !== false) {
 | ||
| 
											13 years ago
										 | 			$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 		foreach ($this->links as $link) {
 | ||
| 
											13 years ago
										 | 			if (!is_array($link)) {
 | ||
|  | 				$link = array('label' => $link);
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 			$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
 | ||
|  | 		}
 | ||
|  | 		echo Html::tag($this->tag, implode('', $links), $this->options);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders a single breadcrumb item.
 | ||
|  | 	 * @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional.
 | ||
|  | 	 * @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link.
 | ||
|  | 	 * @return string the rendering result
 | ||
|  | 	 * @throws InvalidConfigException if `$link` does not have "label" element.
 | ||
|  | 	 */
 | ||
|  | 	protected function renderItem($link, $template)
 | ||
|  | 	{
 | ||
|  | 		if (isset($link['label'])) {
 | ||
|  | 			$label = $this->encodeLabels ? Html::encode($link['label']) : $link['label'];
 | ||
|  | 		} else {
 | ||
|  | 			throw new InvalidConfigException('The "label" element is required for each link.');
 | ||
|  | 		}
 | ||
|  | 		if (isset($link['url'])) {
 | ||
|  | 			return strtr($template, array('{link}' => Html::a($label, $link['url'])));
 | ||
|  | 		} else {
 | ||
|  | 			return strtr($template, array('{link}' => $label));
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |