Bom galeria fiz este Widget para exibir os banners,
só que tem posição que gostaria de exibir 4 imagens sem se repetir
segue o widget:
<?php
namespace common\components;
use yii\base\Widget;
use yii\helpers\Html;
use common\models\Banner;
use common\models\Posicao;
use common\models\Status;
use yii\db\Expression;
use yii;
class BannerWidget extends Widget {
public $posicao;
public $banner;
public $qtd;
public function init(){
parent::init();
if($this->posicao === null) {
$this->banner = Html::img('@web/imagens/banner/padrao.png', ['alt'=>Yii::$app->name, 'class' => 'img-responsive']);
} else {
$posicao = Posicao::findOne(['ds_titulo'=>$this->posicao]);
$status = Status::find()->where(['ds_titulo' => 'Ativo'])->one();
$banners = Banner::find()->where('id_posicao = :posicao AND ds_img != "" AND dt_final >= DATE(NOW()) AND id_status = :status', ['posicao'=>$posicao->id_posicao, 'status' => $status->id_status])->distinct()->orderBy(new Expression('rand()'))->all();
$count = Banner::find()->where('id_posicao = :posicao AND ds_img != "" AND dt_final >= DATE(NOW()) AND id_status = :status', ['posicao'=>$posicao->id_posicao, 'status' => $status->id_status])->count();
if($banners) {
//var_dump($banners); exit;
foreach($banners as $banner) {
$i = 12/$this->qtd;
//var_dump($count); exit;
$this->banner = '<div class="col-sm-'.$i.'">';
$this->banner .= Html::img('@web/imagens/banner/' . $banner->ds_img, ['alt'=>$banner->ds_titulo, 'class' => 'img-responsive']);
$this->banner .= '</div>';
}
} else {
$this->banner = Html::img('@web/imagens/banner/padrao.png', ['alt'=>Yii::$app->name, 'class' => 'img-responsive']);
}
}
}
public function run(){
return $this->banner;
}
}
serve tbm pra qm está precisando, só falta ajustar para exibir mais de uma imagem
na view fica assim:
<?= BannerWidget::widget(['posicao' => 'Topo 700x90', 'qtd' => 1]) ?>