Browse Source

Banners module

master
Egorka 6 years ago
parent
commit
5b4f2e0b2f
  1. 2
      backend/config/main.php
  2. 2
      backend/controllers/SliderController.php
  3. 4
      common/bootstrap/SetUp.php
  4. 23
      common/modules/banners/BannersModule.php
  5. 156
      common/modules/banners/controllers/manage/BannerController.php
  6. 154
      common/modules/banners/controllers/manage/PlaceController.php
  7. 156
      common/modules/banners/entities/Banner.php
  8. 108
      common/modules/banners/entities/BannerPlace.php
  9. 20
      common/modules/banners/entities/queries/BannerPlaceQuery.php
  10. 30
      common/modules/banners/entities/queries/BannerQuery.php
  11. 100
      common/modules/banners/forms/BannerForm.php
  12. 47
      common/modules/banners/forms/BannerPlaceForm.php
  13. 58
      common/modules/banners/forms/search/BannerSearch.php
  14. 58
      common/modules/banners/forms/search/PlaceSearch.php
  15. 61
      common/modules/banners/helpers/BannerHelper.php
  16. 2
      common/modules/banners/manifest.php
  17. 36
      common/modules/banners/messages/ru/banners.php
  18. 42
      common/modules/banners/migrations/m180821_084231_create_banners_table.php
  19. 33
      common/modules/banners/migrations/m180821_100724_create_banners_places_table.php
  20. 31
      common/modules/banners/migrations/m180821_100959_add_banners_place_id_field.php
  21. 31
      common/modules/banners/repositories/BannerPlaceRepository.php
  22. 31
      common/modules/banners/repositories/BannerRepository.php
  23. 82
      common/modules/banners/services/BannerManageService.php
  24. 47
      common/modules/banners/services/BannerPlaceManageService.php
  25. 131
      common/modules/banners/views/manage/banner/_form.php
  26. 16
      common/modules/banners/views/manage/banner/create.php
  27. 86
      common/modules/banners/views/manage/banner/index.php
  28. 18
      common/modules/banners/views/manage/banner/update.php
  29. 95
      common/modules/banners/views/manage/banner/view.php
  30. 74
      common/modules/banners/views/manage/place/_form.php
  31. 16
      common/modules/banners/views/manage/place/create.php
  32. 56
      common/modules/banners/views/manage/place/index.php
  33. 18
      common/modules/banners/views/manage/place/update.php
  34. 76
      common/modules/banners/views/manage/place/view.php
  35. 42
      common/modules/banners/widgets/BannerWidget.php
  36. 18
      common/modules/banners/widgets/views/banner.php
  37. 4
      core/entities/post/Post.php
  38. 2
      frontend/web/themes/start/layouts/home.php

2
backend/config/main.php

@ -8,7 +8,7 @@ $params = array_merge(
return [
'id' => 'app-backend',
'language' => 'ru_RU',
'language' => 'ru',
'basePath' => dirname(__DIR__),
'aliases' => [
'@staticRoot' => $params['staticPath'],

2
backend/controllers/SliderController.php

@ -30,7 +30,7 @@ class SliderController extends Controller
{
return [
'access' => [
'class' => AccessControl::className(),
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['create', 'view', 'index', 'update', 'delete'],

4
common/bootstrap/SetUp.php

@ -147,9 +147,5 @@ class SetUp implements BootstrapInterface
}
}
// todo move that to pages module
// pages search
$app->params['search_rules'][] = "SELECT title, content, CONCAT('/page/', id) AS url FROM {{pages}}";
}
}

23
common/modules/banners/BannersModule.php

@ -32,7 +32,7 @@ class BannersModule extends \yii\base\Module implements ModuleInterface
$app->controllerMap['migrate']['migrationPath'][] = '@common/modules/banners/migrations';
// add search rules
$app->params['search_rules'][] = "SELECT title, CONCAT('/banners/manage/banner/view/', id) AS url FROM {{banners}}";
$app->params['search_rules'][] = "SELECT title, title as content, CONCAT('/banners/manage/banner/view/', id) AS url FROM {{banners}}";
$app->getUrlManager()->addRules([
'banners/manage/banner/view/<id:\d+>' => 'banners/manage/banner/view',
@ -44,19 +44,28 @@ class BannersModule extends \yii\base\Module implements ModuleInterface
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/modules/banners/messages',
],
'banners_public' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/modules/banners/messages',
],
]);
// add menu items
if (basename($app->getBasePath()) === 'backend') {
if (basename(\Yii::$app->getBasePath()) === 'backend') {
$app->params['adminMenu'][] = [
'label' => \Yii::t( 'banners', 'Banners' ),
'icon' => 'flag',
'items' => [
[
'label' => \Yii::t( 'banners', 'Banners' ),
'icon' => 'caret-right',
'url' => [ '/banners/manage/banner/index' ],
'visible' => \Yii::$app->user->can( 'admin' ) || \Yii::$app->user->can( 'BannersManagement' )
//'active' => \Yii::$app->controller->getUniqueId() == 'banners/manage/banner'
],
[
'label' => \Yii::t( 'banners', 'Places' ),
'icon' => 'caret-right',
'url' => [ '/banners/manage/place/index' ],
//'active' => \Yii::$app->controller->getUniqueId() == 'banners/manage/place'
],
],
'visible' => $app->user->can( 'admin' ) || \Yii::$app->user->can( 'BannersManagement' ),
];
}
}

156
common/modules/banners/controllers/manage/BannerController.php

@ -0,0 +1,156 @@
<?php
namespace common\modules\banners\controllers\manage;
use common\modules\banners\entities\Banner;
use common\modules\banners\forms\BannerForm;
use common\modules\banners\forms\search\BannerSearch;
use common\modules\banners\services\BannerManageService;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
class BannerController extends Controller
{
private $service;
public function __construct($id, $module, BannerManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['BannersManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BannerSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
public function actionView($id)
{
return $this->render('view', [
'banner' => $this->findModel($id),
]);
}
/**
* @return mixed
*/
public function actionCreate()
{
$form = new BannerForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$form = $this->service->create($form);
return $this->redirect(['view', 'id' => $form->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
else {
$form->active = $form->active ?: Banner::STATUS_ACTIVE;
}
return $this->render('create', [
'model' => $form,
]);
}
/**
* @param $id
*
* @return string|\yii\web\Response
* @throws NotFoundHttpException
*/
public function actionUpdate($id)
{
$banner = $this->findModel($id);
$form = new BannerForm($banner);
$form->start_at = date('d.m.Y H:i:s', $form->start_at);
$form->end_at = date('d.m.Y H:i:s', $form->end_at);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($banner->id, $form);
return $this->redirect(['view', 'id' => $banner->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $form,
'banner' => $banner,
]);
}
/**
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
try {
$this->service->remove($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
/**
* @param integer $id
* @return Banner the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id): Banner
{
if (($model = Banner::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested banner does not exist.');
}
}

154
common/modules/banners/controllers/manage/PlaceController.php

@ -0,0 +1,154 @@
<?php
namespace common\modules\banners\controllers\manage;
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\forms\BannerPlaceForm;
use common\modules\banners\forms\search\PlaceSearch;
use common\modules\banners\services\BannerPlaceManageService;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
class PlaceController extends Controller
{
private $service;
public function __construct($id, $module, BannerPlaceManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['BannersManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PlaceSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
public function actionView($id)
{
return $this->render('view', [
'place' => $this->findModel($id),
]);
}
/**
* @return mixed
*/
public function actionCreate()
{
$form = new BannerPlaceForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$form = $this->service->create($form);
return $this->redirect(['view', 'id' => $form->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
else {
$form->active = $form->active ?: BannerPlace::STATUS_ACTIVE;
}
return $this->render('create', [
'model' => $form,
]);
}
/**
* @param $id
*
* @return string|\yii\web\Response
* @throws NotFoundHttpException
*/
public function actionUpdate($id)
{
$place = $this->findModel($id);
$form = new BannerPlaceForm($place);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($place->id, $form);
return $this->redirect(['view', 'id' => $place->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $form,
'place' => $place,
]);
}
/**
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
try {
$this->service->remove($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
/**
* @param integer $id
* @return BannerPlace the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id): BannerPlace
{
if (($model = BannerPlace::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested place does not exist.');
}
}

156
common/modules/banners/entities/Banner.php

@ -0,0 +1,156 @@
<?php
namespace common\modules\banners\entities;
use common\modules\banners\entities\queries\BannerQuery;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
use Yii;
/**
* This is the model class for table "banners".
*
* @property int $id
* @property string $title
* @property string $image
* @property string $url
* @property string $target
* @property int $active
* @property int $start_at
* @property int $end_at
* @property int $created_at
* @property int $updated_at
* @property string $include_urls
* @property string $exclude_urls
* @property int $views
* @property int $clicks
* @property int $place_id
*
* @property BannerPlace $place
*/
class Banner extends ActiveRecord
{
const STATUS_DRAFT = 0;
const STATUS_ACTIVE = 1;
const TARGET_BLANK = '_blank';
const TARGET_SELF = '_self';
const FILE_ORIGINAL_PATH = '@staticRoot/origin/banners';
public static function create($title, $image, $url, $target, $start_at, $end_at, $include_urls, $exclude_urls, $active, $place_id): self
{
$banner = new static();
$banner->title = $title;
$banner->image = $image;
$banner->url = $url;
$banner->target = $target;
$banner->start_at = $start_at;
$banner->end_at = $end_at;
$banner->include_urls = $include_urls;
$banner->exclude_urls = $exclude_urls;
$banner->active = $active;
$banner->place_id = $place_id;
return $banner;
}
public function setImage(UploadedFile $image): void
{
$this->image = $image;
}
public function edit($title, $image, $url, $target, $start_at, $end_at, $include_urls, $exclude_urls, $active, $place_id): void
{
$this->title = $title;
$this->image = $image;
$this->url = $url;
$this->target = $target;
$this->start_at = $start_at;
$this->end_at = $end_at;
$this->include_urls = $include_urls;
$this->exclude_urls = $exclude_urls;
$this->active = $active;
$this->place_id = $place_id;
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'banners';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('banners', 'ID'),
'title' => Yii::t('banners', 'Title'),
'url' => Yii::t('banners', 'URL'),
'image' => Yii::t('banners', 'Image'),
'target' => Yii::t('banners', 'Target'),
'active' => Yii::t('banners', 'Status'),
'start_at' => Yii::t('banners', 'Start At'),
'end_at' => Yii::t('banners', 'End At'),
'created_at' => Yii::t('banners', 'Created At'),
'updated_at' => Yii::t('banners', 'Updated At'),
'include_urls' => Yii::t('banners', 'Show only on URLs'),
'exclude_urls' => Yii::t('banners', 'Not show on URLs'),
'views' => Yii::t('banners', 'Views'),
'clicks' => Yii::t('banners', 'Visits'),
'place_id' => Yii::t('banners', 'Place'),
];
}
public function activate(): void
{
if ($this->isActive()) {
throw new \DomainException('Banner is already active.');
}
$this->active = self::STATUS_ACTIVE;
}
public function draft(): void
{
if ($this->isDraft()) {
throw new \DomainException('Banner is already draft.');
}
$this->active = self::STATUS_DRAFT;
}
public function isActive(): bool
{
return $this->active == self::STATUS_ACTIVE;
}
public function isDraft(): bool
{
return $this->active == self::STATUS_DRAFT;
}
public function getPlace()
{
return $this->hasOne(BannerPlace::class, ['id' => 'place_id']);
}
######################################
public function behaviors(): array
{
return [
TimestampBehavior::class,
];
}
public static function find(): BannerQuery
{
return new BannerQuery(static::class);
}
}

108
common/modules/banners/entities/BannerPlace.php

@ -0,0 +1,108 @@
<?php
namespace common\modules\banners\entities;
use common\modules\banners\entities\queries\BannerPlaceQuery;
use yii\db\ActiveRecord;
use Yii;
/**
* This is the model class for table "banners".
*
* @property int $id
* @property string $title
* @property int $width
* @property int $height
* @property int $active
*
*
* @property Banner[] $banners
* @property Banner[] $activeBanners
*/
class BannerPlace extends ActiveRecord
{
const STATUS_DRAFT = 0;
const STATUS_ACTIVE = 1;
public static function create($title, $width, $height, $active): self
{
$place = new static();
$place->title = $title;
$place->width = $width;
$place->height = $height;
$place->active = $active;
return $place;
}
public function edit($title, $width, $height, $active): void
{
$this->title = $title;
$this->width = $width;
$this->height = $height;
$this->active = $active;
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'banners_places';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('banners', 'ID'),
'title' => Yii::t('banners', 'Title'),
'width' => Yii::t('banners', 'Width'),
'height' => Yii::t('banners', 'Height'),
'active' => Yii::t('banners', 'Status'),
];
}
public function activate(): void
{
if ($this->isActive()) {
throw new \DomainException('Place is already active.');
}
$this->active = self::STATUS_ACTIVE;
}
public function draft(): void
{
if ($this->isDraft()) {
throw new \DomainException('Place is already draft.');
}
$this->active = self::STATUS_DRAFT;
}
public function isActive(): bool
{
return $this->active == self::STATUS_ACTIVE;
}
public function isDraft(): bool
{
return $this->active == self::STATUS_DRAFT;
}
public function getBanners()
{
return $this->hasMany(Banner::class, ['place_id' => 'id']);
}
public function getActiveBanners()
{
return $this->hasMany(Banner::class, ['place_id' => 'id'])->andWhere(['active' => Banner::STATUS_ACTIVE]);
}
public static function find(): BannerPlaceQuery
{
return new BannerPlaceQuery(static::class);
}
}

20
common/modules/banners/entities/queries/BannerPlaceQuery.php

@ -0,0 +1,20 @@
<?php
namespace common\modules\banners\entities\queries;
use common\modules\banners\entities\BannerPlace;
use yii\db\ActiveQuery;
class BannerPlaceQuery extends ActiveQuery
{
/**
* @param null $alias
* @return $this
*/
public function active($alias = null)
{
return $this->andWhere([
($alias ? $alias . '.' : '') . 'active' => BannerPlace::STATUS_ACTIVE,
]);
}
}

30
common/modules/banners/entities/queries/BannerQuery.php

@ -0,0 +1,30 @@
<?php
namespace common\modules\banners\entities\queries;
use common\modules\banners\entities\Banner;
use yii\helpers\Url;
use yii\db\ActiveQuery;
class BannerQuery extends ActiveQuery
{
/**
* @param null $alias
* @return $this
*/
public function active()
{
return $this->andWhere(['active' => Banner::STATUS_ACTIVE]);
}
public function showTime()
{
return $this->andWhere(['<', 'start_at', time()])->andWhere(['>', 'end_at', time()]);
}
/*public function excludeFree()
{
$current = Url::current([]);
return $this->andWhere(['not rlike', 'exclude_urls', '^'.$current.'$']);
}*/
}

100
common/modules/banners/forms/BannerForm.php

@ -0,0 +1,100 @@
<?php
namespace common\modules\banners\forms;
use common\modules\banners\entities\Banner;
use yii\base\Model;
use Yii;
use yii\web\UploadedFile;
class BannerForm extends Model
{
public $title;
/**
* @var UploadedFile
*/
public $image;
public $url;
public $target;
public $active;
public $start_at;
public $end_at;
public $include_urls;
public $exclude_urls;
public $place_id;
public $_banner;
public function __construct(Banner $banner = null, $config = [])
{
if ($banner) {
$this->title = $banner->title;
$this->image = $banner->image;
$this->url = $banner->url;
$this->target = $banner->target;
$this->active = $banner->active;
$this->start_at = $banner->start_at;
$this->end_at = $banner->end_at;
$this->include_urls = $banner->include_urls;
$this->exclude_urls = $banner->exclude_urls;
$this->place_id = $banner->place_id;
$this->_banner = $banner;
}
parent::__construct($config);
}
public function rules(): array
{
return [
[['title', 'place_id', 'start_at', 'end_at'], 'required'],
[['active', 'place_id', 'active'], 'integer'],
[['title', 'target', 'url'], 'string', 'max' => 255],
[['include_urls', 'exclude_urls'], 'string'],
[['image'], 'file', 'extensions' => 'png, jpg, gif'],
[['start_at','end_at'],'safe'],
/*[['start_at', 'end_at'], 'date',
'format' => 'php:d.m.Y H:i'
],*/
];
}
public function attributeLabels() {
return [
'id' => Yii::t('banners', 'ID'),
'title' => Yii::t('banners', 'Title'),
'url' => Yii::t('banners', 'URL'),
'image' => Yii::t('banners', 'Image'),
'target' => Yii::t('banners', 'Target'),
'active' => Yii::t('banners', 'Status'),
'start_at' => Yii::t('banners', 'Start At'),
'end_at' => Yii::t('banners', 'End At'),
'created_at' => Yii::t('banners', 'Created At'),
'updated_at' => Yii::t('banners', 'Updated At'),
'include_urls' => Yii::t('banners', 'Show only on URLs'),
'exclude_urls' => Yii::t('banners', 'Not show on URLs'),
'views' => Yii::t('banners', 'Views'),
'clicks' => Yii::t('banners', 'Visits'),
'place_id' => Yii::t('banners', 'Place'),
];
}
public function beforeValidate() {
if (parent::beforeValidate()) {
$this->image = UploadedFile::getInstance($this, 'image');
$this->start_at = strtotime($this->start_at);
$this->end_at = strtotime($this->end_at);
return true;
}
return false;
}
/*public function afterValidate() {
if (parent::afterValidate()) {
$this->start_at = strtotime($this->start_at);
$this->end_at = strtotime($this->end_at);
return true;
}
return false;
}*/
}

47
common/modules/banners/forms/BannerPlaceForm.php

@ -0,0 +1,47 @@
<?php
namespace common\modules\banners\forms;
use common\modules\banners\entities\BannerPlace;
use yii\base\Model;
use Yii;
class BannerPlaceForm extends Model
{
public $title;
public $width;
public $height;
public $active;
public $_place;
public function __construct(BannerPlace $place = null, $config = [])
{
if ($place) {
$this->title = $place->title;
$this->width = $place->width;
$this->height = $place->height;
$this->active = $place->active;
$this->_place = $place;
}
parent::__construct($config);
}
public function rules(): array
{
return [
[['title'], 'required'],
[['width', 'height', 'active'], 'integer'],
];
}
public function attributeLabels() {
return [
'id' => Yii::t('banners', 'ID'),
'title' => Yii::t('banners', 'Title'),
'width' => Yii::t('banners', 'Width'),
'height' => Yii::t('banners', 'Height'),
'active' => Yii::t('banners', 'Status'),
];
}
}

58
common/modules/banners/forms/search/BannerSearch.php

@ -0,0 +1,58 @@
<?php
namespace common\modules\banners\forms\search;
use common\modules\banners\entities\Banner;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class BannerSearch extends Model
{
public $id;
public $title;
public $active;
public $place_id;
public $target;
public function rules(): array
{
return [
[['id', 'active', 'place_id'], 'integer'],
[['title', 'target'], 'safe'],
];
}
/**
* @param array $params
* @return ActiveDataProvider
*/
public function search(array $params): ActiveDataProvider
{
$query = Banner::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => ['id' => SORT_DESC]
]
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'active' => $this->active,
'place_id' => $this->place_id,
'target' => $this->target
]);
$query
->andFilterWhere(['like', 'title', $this->title]);
return $dataProvider;
}
}

58
common/modules/banners/forms/search/PlaceSearch.php

@ -0,0 +1,58 @@
<?php
namespace common\modules\banners\forms\search;
use common\modules\banners\entities\BannerPlace;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class PlaceSearch extends Model
{
public $id;
public $title;
public $width;
public $height;
public $active;
public function rules(): array
{
return [
[['id', 'active', 'width', 'height'], 'integer'],
[['title'], 'safe'],
];
}
/**
* @param array $params
* @return ActiveDataProvider
*/
public function search(array $params): ActiveDataProvider
{
$query = BannerPlace::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => ['id' => SORT_DESC]
]
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'active' => $this->active,
'width' => $this->width,
'height' => $this->height
]);
$query
->andFilterWhere(['like', 'title', $this->title]);
return $dataProvider;
}
}

61
common/modules/banners/helpers/BannerHelper.php

@ -0,0 +1,61 @@
<?php
/**
* Created by Error202
* Date: 21.08.2018
*/
namespace common\modules\banners\helpers;
use common\modules\banners\entities\Banner;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use Yii;
class BannerHelper
{
public static function targetList(): array
{
return [
Banner::TARGET_SELF => Yii::t('banners', 'Self window'),
Banner::TARGET_BLANK => Yii::t('banners', 'Blank window'),
];
}
public static function targetName($target): string
{
return ArrayHelper::getValue(self::targetList(), $target);
}
public static function statusList(): array
{
return [
Banner::STATUS_DRAFT => Yii::t('banners', 'Draft'),
Banner::STATUS_ACTIVE => Yii::t('banners', 'Active'),
];
}
public static function statusName($status): string
{
return ArrayHelper::getValue(self::statusList(), $status);
}
public static function statusLabel($status): string
{
switch ($status) {
case Banner::STATUS_DRAFT:
$class = 'label label-default';
break;
case Banner::STATUS_ACTIVE:
$class = 'label label-success';
break;
default:
$class = 'label label-default';
}
return Html::tag('span', ArrayHelper::getValue(self::statusList(), $status), [
'class' => $class,
]);
}
}

2
common/modules/banners/manifest.php

@ -3,6 +3,6 @@
return [
'version' => '1.0.1',
'name' => 'banners',
'description' => 'Banners widget for site',
'description' => 'Banners management and rotation system for site',
'module' => 'BannersModule',
];

36
common/modules/banners/messages/ru/banners.php

@ -0,0 +1,36 @@
<?php
return [
'banners' => 'Баннеры',
'Banners management and rotation system for site' => 'Система управления ротацией баннеров',
'Banners' => 'Баннеры',
'Places' => 'Расположения',
'Title' => 'Название',
'Width' => 'Ширина',
'Height' => 'Высота',
'Create Place' => 'Новое расположение',
'Create Banner' => 'Новый баннер',
'Status' => 'Статус',
'Active' => 'Опубликовано',
'Draft' => 'Черновик',
'Publish' => 'Публикация',
'Update Place: {name}' => 'Редактирование расположения: {name}',
'Update Banner: {name}' => 'Редактирование баннера: {name}',
'Place' => 'Расположение',
'URL' => 'Ссылка',
'Target' => 'Цель',
'Self window' => 'Текущее окно',
'Blank window' => 'Новое окно',
'Image' => 'Изображение',
'Start At' => 'Начало показов',
'End At' => 'Окончание показов',
'Show only on URLs' => 'Показывать по ссылкам',
'Not show on URLs' => 'Не показывать по ссылкам',
'Views' => 'Просмотры',
'Visits' => 'Переходы',
'Created At' => 'Создан',
'Updated At' => 'Обновлен',
'Insert Code' => 'Код вставки',
'For template' => 'Для шаблона',
'For editor' => 'Для редактора',
];

42
common/modules/banners/migrations/m180821_084231_create_banners_table.php

@ -0,0 +1,42 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `banners`.
*/
class m180821_084231_create_banners_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->createTable('{{%banners}}', [
'id' => $this->primaryKey(),
'title' => $this->string(255)->notNull(),
'image' => $this->string(255)->notNull(),
'url' => $this->string(255),
'target' => $this->string(15)->defaultValue('_blank'),
'active' => $this->integer(1)->defaultValue(1),
'start_at' => $this->integer()->unsigned(),
'end_at' => $this->integer()->unsigned(),
'created_at' => $this->integer()->unsigned(),
'updated_at' => $this->integer()->unsigned(),
'include_urls' => 'LONGTEXT',
'exclude_urls' => 'LONGTEXT',
'views' => $this->integer()->unsigned()->defaultValue(0),
'clicks' => $this->integer()->unsigned()->defaultValue(0),
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%banners}}');
}
}

33
common/modules/banners/migrations/m180821_100724_create_banners_places_table.php

@ -0,0 +1,33 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `banners_places`.
*/
class m180821_100724_create_banners_places_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
$this->createTable('{{%banners_places}}', [
'id' => $this->primaryKey(),
'title' => $this->string(255)->notNull(),
'width' => $this->integer(),
'height' => $this->integer(),
'active' => $this->integer(1)->defaultValue(1)
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%banners_places}}');
}
}

31
common/modules/banners/migrations/m180821_100959_add_banners_place_id_field.php

@ -0,0 +1,31 @@
<?php
use yii\db\Migration;
/**
* Class m180821_100959_add_banners_place_id_field
*/
class m180821_100959_add_banners_place_id_field extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%banners}}', 'place_id', $this->integer());
$this->createIndex('idx_banners_place_id', '{{%banners}}', 'place_id');
$this->addForeignKey('frg_banners_place_id_banners_places_id', '{{%banners}}', 'place_id', 'banners_places', 'id', 'CASCADE');
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropForeignKey('frg_banners_place_id_banners_places_id', '{{%banners}}');
$this->dropIndex('idx_banners_place_id', '{{%banners}}');
$this->dropColumn('{{%banners}}', 'place_id');
}
}

31
common/modules/banners/repositories/BannerPlaceRepository.php

@ -0,0 +1,31 @@
<?php
namespace common\modules\banners\repositories;
use common\modules\banners\entities\BannerPlace;
use core\repositories\NotFoundException;
class BannerPlaceRepository
{
public function get($id): BannerPlace
{
if (!$place = BannerPlace::findOne($id)) {
throw new NotFoundException('Banner place is not found.');
}
return $place;
}
public function save(BannerPlace $place): void
{
if (!$place->save()) {
throw new \RuntimeException('Saving error.');
}
}
public function remove(BannerPlace $place): void
{
if (!$place->delete()) {
throw new \RuntimeException('Removing error.');
}
}
}

31
common/modules/banners/repositories/BannerRepository.php

@ -0,0 +1,31 @@
<?php
namespace common\modules\banners\repositories;
use common\modules\banners\entities\Banner;
use core\repositories\NotFoundException;
class BannerRepository
{
public function get($id): Banner
{
if (!$banner = Banner::findOne($id)) {
throw new NotFoundException('Banner is not found.');
}
return $banner;
}
public function save(Banner $banner): void
{
if (!$banner->save()) {
throw new \RuntimeException('Saving error.');
}
}
public function remove(Banner $banner): void
{
if (!$banner->delete()) {
throw new \RuntimeException('Removing error.');
}
}
}

82
common/modules/banners/services/BannerManageService.php

@ -0,0 +1,82 @@
<?php
namespace common\modules\banners\services;
use common\modules\banners\entities\Banner;
use common\modules\banners\forms\BannerForm;
use common\modules\banners\repositories\BannerRepository;
use yii\base\Security;
class BannerManageService
{
private $repository;
public function __construct(BannerRepository $repository)
{
$this->repository = $repository;
}
public function create(BannerForm $form): Banner
{
if ($form->image) {
$filename = $form->image->baseName . '_' . (new Security())->generateRandomString(5) . '.' . $form->image->extension;
$path = \Yii::getAlias(Banner::FILE_ORIGINAL_PATH);
if (!file_exists($path))
{
mkdir($path, 0777, true);
}
$form->image->saveAs($path . '/' . $filename);
$form->image = $filename;
}
$banner = Banner::create(
$form->title,
$form->image,
$form->url,
$form->target,
$form->start_at,
$form->end_at,
$form->include_urls,
$form->exclude_urls,
$form->active,
$form->place_id
);
$this->repository->save($banner);
return $banner;
}
public function edit($id, BannerForm $form): void
{
$banner = $this->repository->get($id);
if ($form->image) {
$filename = $form->image->baseName . '_' . (new Security())->generateRandomString(5) . '.' . $form->image->extension;
$path = \Yii::getAlias(Banner::FILE_ORIGINAL_PATH);
$form->image->saveAs($path . '/' . $filename);
$form->image = $filename;
}
else {
$form->image = $banner->image;
}
$banner->edit(
$form->title,
$form->image,
$form->url,
$form->target,
$form->start_at,
$form->end_at,
$form->include_urls,
$form->exclude_urls,
$form->active,
$form->place_id
);
$this->repository->save($banner);
}
public function remove($id): void
{
$banner = $this->repository->get($id);
$this->repository->remove($banner);
}
}

47
common/modules/banners/services/BannerPlaceManageService.php

@ -0,0 +1,47 @@
<?php
namespace common\modules\banners\services;
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\forms\BannerPlaceForm;
use common\modules\banners\repositories\BannerPlaceRepository;
class BannerPlaceManageService
{
private $repository;
public function __construct(BannerPlaceRepository $repository)
{
$this->repository = $repository;
}
public function create(BannerPlaceForm $form): BannerPlace
{
$place = BannerPlace::create(
$form->title,
$form->width,
$form->height,
$form->active
);
$this->repository->save($place);
return $place;
}
public function edit($id, BannerPlaceForm $form): void
{
$place = $this->repository->get($id);
$place->edit(
$form->title,
$form->width,
$form->height,
$form->active
);
$this->repository->save($place);
}
public function remove($id): void
{
$place = $this->repository->get($id);
$this->repository->remove($place);
}
}

131
common/modules/banners/views/manage/banner/_form.php

@ -0,0 +1,131 @@
<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
use common\modules\banners\helpers\BannerHelper;
use common\modules\banners\entities\BannerPlace;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use kartik\widgets\DateTimePicker;
/* @var $this yii\web\View */
/* @var $model \common\modules\banners\forms\BannerForm */
/* @var $form yii\widgets\ActiveForm */
$js2 = '
$(".hint-block").each(function () {
var $hint = $(this);
var label = $hint.parent().find("label");
label.html(label.html() + \' <i style="color:#3c8dbc" class="fa fa-question-circle" aria-hidden="true"></i>\');
label.addClass("help").popover({
html: true,
trigger: "hover",
placement: "bottom",
content: $hint.html()
});
$(this).hide();
});
';
$this->registerJs($js2);
?>
<div class="page-form">
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-md-10">
<div class="box box-default">
<div class="box-body">
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'place_id')->dropDownList(ArrayHelper::map(BannerPlace::find()->all(), 'id', 'title')) ?>
</div>
<div class="col-md-8">
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="row">
<div class="col-md-8">
<?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-4">
<?= $form->field($model, 'target')->dropDownList(BannerHelper::targetList()) ?>
</div>
</div>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'image')->widget(FileInput::class, [
'options' => [
'accept' => 'image/*',
],
'pluginOptions' => [
'showUpload' => false,
'showPreview' => false,
],
]) ?>
</div>
</div>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'start_at')->widget(DateTimePicker::class, [
'options' => [],
'removeButton' => false,
'pluginOptions' => [
'autoclose' => true,
'format' => 'dd.mm.yyyy hh:ii:ss',
]
]); ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'end_at')->widget(DateTimePicker::class, [
'options' => [],
'removeButton' => false,
'pluginOptions' => [
'autoclose' => true,
//'format' => 'dd.MM.yyyy hh:i',
'format' => 'dd.mm.yyyy hh:ii:ss',
]
]); ?>
</div>
</div>
<!--<div class="row">
<div class="col-md-6">
< ?= $form->field($model, 'include_urls')->textarea(['rows' => 4]) ?>
</div>
<div class="col-md-6">
< ?= $form->field($model, 'exclude_urls')->textarea(['rows' => 4]) ?>
</div>
</div>-->
</div>
</div>
<div class="form-group">
<?= Html::submitButton(Yii::t('buttons', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
</div>
<div class="col-md-2">
<div class="box box-default">
<div class="box-header with-border"><?= Yii::t('banners', 'Publish') ?></div>
<div class="box-body">
<?= $form->field($model, 'active')->radioList(BannerHelper::statusList()) ?>
</div>
</div>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>

16
common/modules/banners/views/manage/banner/create.php

@ -0,0 +1,16 @@
<?php
/* @var $this yii\web\View */
/* @var $model \common\modules\banners\forms\BannerForm */
$this->title = Yii::t('banners', 'Create Banner');
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Banners'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="banner-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

86
common/modules/banners/views/manage/banner/index.php

@ -0,0 +1,86 @@
<?php
use common\modules\banners\entities\Banner;
use yii\grid\ActionColumn;
use yii\helpers\Html;
use yii\grid\GridView;
use common\modules\banners\helpers\BannerHelper;
use yii\helpers\ArrayHelper;
use common\modules\banners\entities\BannerPlace;
/* @var $this yii\web\View */
/* @var $searchModel \common\modules\banners\forms\search\BannerSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('banners', 'Banners');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">
<p>
<?= Html::a(Yii::t('banners','Create Banner'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<div class="box">
<div class="box-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'title',
'value' => function (Banner $model) {
return Html::a(Html::encode($model->title), ['view', 'id' => $model->id]);
},
'format' => 'raw',
],
'url:url',
[
'attribute' => 'start_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->start_at);
},
'options' => ['style' => 'width: 180px;'],
],
[
'attribute' => 'end_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->end_at);
},
'options' => ['style' => 'width: 180px;'],
],
[
'attribute' => 'place_id',
'filter' => ArrayHelper::map(BannerPlace::find()->all(), 'id', 'title'),
'value' => 'place.title',
'label' => Yii::t('banners', 'Place'),
],
[
'attribute' => 'target',
'filter' => BannerHelper::targetList(),
'value' => function(Banner $banner) {
return BannerHelper::targetName($banner->target);
},
'options' => ['style' => 'width: 150px;'],
'contentOptions' => ['class' => 'text-center'],
],
[
'attribute' => 'active',
'filter' => BannerHelper::statusList(),
'format' => 'raw',
'value' => function(Banner $banner) {
return BannerHelper::statusLabel($banner->active);
},
'options' => ['style' => 'width: 150px;'],
'contentOptions' => ['class' => 'text-center'],
],
[
'class' => ActionColumn::class,
'options' => ['style' => 'width: 100px;'],
'contentOptions' => ['class' => 'text-center'],
],
],
]); ?>
</div>
</div>
</div>

18
common/modules/banners/views/manage/banner/update.php

@ -0,0 +1,18 @@
<?php
/* @var $this yii\web\View */
/* @var $banner \common\modules\banners\entities\Banner */
/* @var $model \common\modules\banners\forms\BannerForm */
$this->title = Yii::t('banners', 'Update Banner: {name}', ['name' => $banner->title]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Banners'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $banner->title, 'url' => ['view', 'id' => $banner->id]];
$this->params['breadcrumbs'][] = Yii::t('buttons', 'Editing');
?>
<div class="banner-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

95
common/modules/banners/views/manage/banner/view.php

@ -0,0 +1,95 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use common\modules\banners\entities\Banner;
use common\modules\banners\helpers\BannerHelper;
/* @var $this yii\web\View */
/* @var $banner Banner */
$this->title = $banner->title;
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Banners'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="banner-view">
<p>
<?= Html::a(Yii::t('banners','Banners'), ['index'], ['class' => 'btn btn-default']) ?>
<?= Html::a(Yii::t('buttons', 'Edit'), ['update', 'id' => $banner->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('buttons', 'Delete'), ['delete', 'id' => $banner->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('buttons', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<div class="row">
<div class="col-md-12">
<?php if ($banner->image): ?>
<div class="box">
<div class="box-body">
<?= Html::img('@static/origin/banners/' . $banner->image, [
'style' => 'margin: 20px'
]) ?>
</div>
</div>
<?php endif; ?>
<div class="box">
<div class="box-body">
<?= DetailView::widget([
'model' => $banner,
'attributes' => [
'id',
'title',
[
'attribute' => 'place.title',
'label' => Yii::t('banners', 'Place'),
],
'url:url',
[
'attribute' => 'target',
'value' => function(Banner $banner) {
return BannerHelper::targetName($banner->target);
}
],
[
'attribute' => 'start_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->start_at);
},
],
[
'attribute' => 'end_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->end_at);
},
],
[
'attribute' => 'created_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->created_at);
},
],
[
'attribute' => 'updated_at_at',
'value' => function(Banner $banner) {
return date('d.m.Y H:i', $banner->updated_at);
},
],
'views',
'clicks'
],
]) ?>
</div>
</div>
</div>
</div>
</div>

74
common/modules/banners/views/manage/place/_form.php

@ -0,0 +1,74 @@
<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
use common\modules\banners\helpers\BannerHelper;
/* @var $this yii\web\View */
/* @var $model \common\modules\banners\forms\BannerPlaceForm */
/* @var $form yii\widgets\ActiveForm */
$js2 = '
$(".hint-block").each(function () {
var $hint = $(this);
var label = $hint.parent().find("label");
label.html(label.html() + \' <i style="color:#3c8dbc" class="fa fa-question-circle" aria-hidden="true"></i>\');
label.addClass("help").popover({
html: true,
trigger: "hover",
placement: "bottom",
content: $hint.html()
});
$(this).hide();
});
';
$this->registerJs($js2);
?>
<div class="place-form">
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-md-10">
<div class="box box-default">
<div class="box-body">
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'width')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'height')->textInput(['maxlength' => true]) ?>
</div>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton(Yii::t('buttons', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
</div>
<div class="col-md-2">
<div class="box box-default">
<div class="box-header with-border"><?= Yii::t('banners', 'Publish') ?></div>
<div class="box-body">
<?= $form->field($model, 'active')->radioList(BannerHelper::statusList()) ?>
</div>
</div>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>

16
common/modules/banners/views/manage/place/create.php

@ -0,0 +1,16 @@
<?php
/* @var $this yii\web\View */
/* @var $model \common\modules\banners\forms\BannerPlaceForm */
$this->title = Yii::t('banners', 'Create Place');
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Places'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="place-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

56
common/modules/banners/views/manage/place/index.php

@ -0,0 +1,56 @@
<?php
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\helpers\BannerHelper;
use yii\grid\ActionColumn;
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel \common\modules\banners\forms\search\BannerSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('banners', 'Places');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="place-index">
<p>
<?= Html::a(Yii::t('banners','Create Place'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<div class="box">
<div class="box-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'title',
'value' => function (BannerPlace $model) {
return Html::a(Html::encode($model->title), ['view', 'id' => $model->id]);
},
'format' => 'raw',
],
'width',
'height',
[
'attribute' => 'active',
'filter' => BannerHelper::statusList(),
'format' => 'raw',
'value' => function(BannerPlace $place) {
return BannerHelper::statusLabel($place->active);
},
'options' => ['style' => 'width: 150px;'],
'contentOptions' => ['class' => 'text-center'],
],
[
'class' => ActionColumn::class,
'options' => ['style' => 'width: 100px;'],
'contentOptions' => ['class' => 'text-center'],
],
],
]); ?>
</div>
</div>
</div>

18
common/modules/banners/views/manage/place/update.php

@ -0,0 +1,18 @@
<?php
/* @var $this yii\web\View */
/* @var $place \common\modules\banners\entities\BannerPlace */
/* @var $model \common\modules\banners\forms\BannerPlaceForm */
$this->title = Yii::t('banners', 'Update Place: {name}', ['name' => $place->title]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Places'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $place->title, 'url' => ['view', 'id' => $place->id]];
$this->params['breadcrumbs'][] = Yii::t('buttons', 'Editing');
?>
<div class="place-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

76
common/modules/banners/views/manage/place/view.php

@ -0,0 +1,76 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\helpers\BannerHelper;
/* @var $this yii\web\View */
/* @var $place \common\modules\banners\entities\BannerPlace */
$this->title = $place->title;
$this->params['breadcrumbs'][] = ['label' => Yii::t('banners', 'Places'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="place-view">
<p>
<?= Html::a(Yii::t('banners','Places'), ['index'], ['class' => 'btn btn-default']) ?>
<?= Html::a(Yii::t('buttons', 'Edit'), ['update', 'id' => $place->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('buttons', 'Delete'), ['delete', 'id' => $place->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('buttons', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<div class="row">
<div class="col-md-9">
<div class="box">
<div class="box-body">
<?= DetailView::widget([
'model' => $place,
'attributes' => [
'id',
'title',
'width',
'height',
[
'attribute' => 'active',
'format' => 'raw',
'value' => function(BannerPlace $place) {
return BannerHelper::statusLabel($place->active);
}
],
],
]) ?>
</div>
</div>
</div>
<div class="col-md-3">
<div class="box box-default">
<div class="box-header with-border"><?= Yii::t('banners', 'Insert Code') ?></div>
<div class="box-body">
<?php
$code = Html::encode(
"<?= \common\modules\\banners\widgets\BannerWidget::widget(['id' => ".$place->id."]) ?>");
?>
<p><?= Yii::t('banners', 'For template') ?></p>
<pre><?= $code ?></pre>
<?php
$code = Html::encode(
"[?= \common\modules\\banners\widgets\BannerWidget::widget(['id' => ".$place->id."]) ?]");
?>
<p><?= Yii::t('banners', 'For editor') ?></p>
<pre><?= $code ?></pre>
</div>
</div>
</div>
</div>
</div>

42
common/modules/banners/widgets/BannerWidget.php

@ -0,0 +1,42 @@
<?php
/**
* Created by Error202
* Date: 31.07.2018
*/
namespace common\modules\banners\widgets;
use common\modules\banners\entities\Banner;
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\helpers\BannerHelper;
use yii\base\Widget;
class BannerWidget extends Widget
{
public $id;
public function run() {
$place = BannerPlace::findOne($this->id);
if (!$place) {
return 'Place is not found';
}
$banners = Banner::find()
->active()
->showTime()
//->excludeFree()
->andWhere(['place_id' => $place->id])
->all();
if (!$banners || $place->isDraft()) {
return '';
}
/* @var $banner Banner */
$banner = $banners[array_rand($banners)];
return $this->render('banner', [
'banner' => $banner,
]);
}
}

18
common/modules/banners/widgets/views/banner.php

@ -0,0 +1,18 @@
<?php
/**
* Created by Error202
* Date: 22.08.2018
*/
use yii\helpers\Html;
/**
* @var $this \yii\web\View
* @var $banner \common\modules\banners\entities\Banner;
*/
?>
<?= Html::a(Html::img('@static/origin/banners/' . $banner->image), $banner->url, [
'target' => $banner->target
]) ?>

4
core/entities/post/Post.php

@ -279,7 +279,7 @@ class Post extends ActiveRecord
*/
public function getPostComments()
{
return $this->hasMany(PostComment::className(), ['post_id' => 'id']);
return $this->hasMany(PostComment::class, ['post_id' => 'id']);
}
/**
@ -287,7 +287,7 @@ class Post extends ActiveRecord
*/
public function getPostTagAssignments()
{
return $this->hasMany(PostTagAssignment::className(), ['post_id' => 'id']);
return $this->hasMany(PostTagAssignment::class, ['post_id' => 'id']);
}
/**

2
frontend/web/themes/start/layouts/home.php

@ -43,7 +43,7 @@
<div class="container">
<?= \common\modules\forms\widgets\FormWidget::widget(['id' => 1]) ?>
<?= \common\modules\banners\widgets\BannerWidget::widget(['id' => 1]) ?>
<h1 class="my-4">Welcome to Modern Business</h1>

Loading…
Cancel
Save