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.

111 lines
2.5 KiB

6 years ago
<?php
namespace common\modules\banners\entities;
use common\modules\banners\entities\queries\BannerPlaceQuery;
3 years ago
use DomainException;
use yii\db\ActiveQuery;
6 years ago
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;
6 years ago
public static function create($title, $width, $height, $active): self
{
$place = new static();
$place->title = $title;
$place->width = $width;
$place->height = $height;
$place->active = $active;
6 years ago
return $place;
}
public function edit($title, $width, $height, $active): void
{
$this->title = $title;
$this->width = $width;
$this->height = $height;
$this->active = $active;
}
6 years ago
/**
* @inheritdoc
*/
3 years ago
public static function tableName(): string
6 years ago
{
return 'banners_places';
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('banners', 'ID'),
'title' => Yii::t('banners', 'Title'),
'width' => Yii::t('banners', 'Width'),
6 years ago
'height' => Yii::t('banners', 'Height'),
'active' => Yii::t('banners', 'Status'),
];
}
public function activate(): void
{
if ($this->isActive()) {
3 years ago
throw new DomainException('Place is already active.');
}
$this->active = self::STATUS_ACTIVE;
}
6 years ago
public function draft(): void
{
if ($this->isDraft()) {
3 years ago
throw new DomainException('Place is already draft.');
}
$this->active = self::STATUS_DRAFT;
}
6 years ago
public function isActive(): bool
{
return $this->active == self::STATUS_ACTIVE;
}
6 years ago
public function isDraft(): bool
{
return $this->active == self::STATUS_DRAFT;
}
6 years ago
3 years ago
public function getBanners(): ActiveQuery
{
return $this->hasMany(Banner::class, ['place_id' => 'id']);
}
6 years ago
3 years ago
public function getActiveBanners(): ActiveQuery
{
return $this->hasMany(Banner::class, ['place_id' => 'id'])->andWhere(['active' => Banner::STATUS_ACTIVE]);
}
6 years ago
public static function find(): BannerPlaceQuery
{
return new BannerPlaceQuery(static::class);
}
6 years ago
}