<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 18.09.17
     * Time: 12:20
     * @var $this \frontend\widgets\SeoBreadcrumbs
     */
    
    namespace frontend\widgets;
    use yii\base\InvalidConfigException;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Html;
    use yii\helpers\Url;
    use yii\widgets\Breadcrumbs;
    
    class SeoBreadcrumbs extends Breadcrumbs
    {
        public $tag = 'ul';
        public $options =  [
                                'class' => 'breadcrumb',
                                'itemscope' => true,
                                'itemtype' => " http://schema.org/BreadcrumbList"
                            ];
        public $encodeLabels = false;
        public $itemTemplate = "<li itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">{link}<meta itemprop=\"position\" content=\"{number}\" /></li>\n";
        public $homeLink = [
            'label' => '<span itemprop="name">Интернет-магазин светильников</span>',
            'url' => '/',
            'itemprop' => 'item',
        ];
        
        public $activeItemTemplate = '<li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">{link}<meta itemprop="position" content="{number}" /></span></li>';
    
        public function run()
        {
            if (empty($this->links)) {
                return;
            }
            $links = [];
            if ($this->homeLink === null) {
                $links[] = $this->renderItem_([
                                                 'label' => Yii::t('yii', 'Home'),
                                                 'url' => Yii::$app->homeUrl,
                                             ], $this->itemTemplate,1);
            } elseif ($this->homeLink !== false) {
                $links[] = $this->renderItem_($this->homeLink, $this->itemTemplate, 1, true);
            }
            foreach ($this->links as $key => $link) {
                if (!is_array($link)) {
                    $link = ['label' => $link];
                }
                $links[] = $this->renderItem_($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate, $key + 2);
            }
            echo Html::tag($this->tag, implode('', $links), $this->options);
        }
    
        protected function renderItem_($link, $template, $number, $home = false)
        {
            $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
            if (array_key_exists('label', $link)) {
                $label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
            } else {
                throw new InvalidConfigException('The "label" element is required for each link.');
            }
            if (isset($link['template'])) {
                $template = $link['template'];
            }
            if (isset($link['url'])) {
                $options = $link;
                unset($options['template'], $options['label'], $options['url']);
                if (!$home){
                    $label = "<link itemprop='item' href=" .
                        \Yii::$app->request->getUrl() .
                        "><span itemprop='name'>" .
                        $label . "</span>";
                }
                $link = Html::a($label, $link['url'], $options);
            } else {
                $link = "<link itemprop=\"item\" href=".\Yii::$app->request->getUrl().">$label";
            }
            return strtr($template, ['{link}' => $link, '{number}' => $number]);
        }
    }