<?php

namespace frontend\widgets;
use yii\base\Widget;
use Yii;
use yii\web\View;
use common\models\Banner;

class BannerWidget extends Widget
{
    /**
     * @var string SYSTEM NAME точки-размещения в шаблоне
     */
    public $title = '';

    public function init ()
    {
        parent::init ();
    }

    public function run ()
    {
        // слайдер
        $banner = Banner::find()->where([Banner::tableName().'.title'=>$this->title, Banner::tableName().'.status' => '1'] )->one();
        if (! empty ($banner))
        {
            // html
            return $this->htmlBanner($banner);
        }
    }


    // ==== DISPLAY ====

    public function htmlBanner ($banner)
    {
        ob_start();

        echo '<div class="banner-widget '.$banner->title.'">';

            if (Banner::getFileExtension ($banner->image) == 'swf')
            {
                echo '
                <object width="'.$banner->width.'" height="'.$banner->height.'">
                    <param name="movie" value="'.$banner->image.'">
                    <param name="quality" value="high" />
                    <param name="menu" value="false" />
                    <param name="wmode" value="" />
                    <embed src="'.$banner->image.'" width="'.$banner->location->width.'" height="'.$banner->location->height.'" type="application/x-shockwave-flash" wmode="" quality="high" menu="false"></embed>
                </object>';
            }
            else
            {
                if ($banner->image == '')
                {
                    $banner->image = 'notpic.gif';
                }

                if ($banner->url != '')
                {
                    echo '<a href="'.$banner->url.'">';
                }

                echo '<img src="'.$banner->image.'"  width="'.$banner->width.'" height="'.$banner->height.'" alt="'.$banner->alt.'">';

                if ($banner->url != '')
                {
                    echo '</a>';
                }
            }

        echo '</div>';

        return ob_get_clean();
    }
}