File: /home/safarimaris/home/safarimaris/common/models/Country.php
<?php
namespace common\models;
use Yii;
use yii\db\Query;
use yii\caching\TagDependency;
use yii\helpers\Url;
/**
* This is the model class for table "country".
*
* @property integer $id
* @property integer $ordinal
* @property string $name
* @property string $name2
* @property string $url
* @property string $logo
* @property string $reason1
* @property string $reason2
* @property string $reason3
* @property string $reason4
* @property string $reason5
* @property string $seasonText
* @property string $seasonTextRight
* @property string $seo
* @property string $regionId
* @property string $descr
* @property string $descrBoat
* @property string $descrResort
* @property string $descrSite
* @property string $descrInterest
* @property integer $m1
* @property integer $m2
* @property integer $m3
* @property integer $m4
* @property integer $m5
* @property integer $m6
* @property integer $m7
* @property integer $m8
* @property integer $m9
* @property integer $m10
* @property integer $m11
* @property integer $m12
*
* @property Region $region
* @property Entity[] $entities
* @property Site[] $sites
*/
class Country extends \yii\db\ActiveRecord
{
private $_url;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'country';
}
public function behaviors()
{
return [
[
'class' => \mongosoft\file\UploadImageBehavior::className(),
'attribute' => 'logo',
'scenarios' => ['update', 'create'],
//'placeholder' => '@app/modules/user/assets/images/userpic.jpg',
'path' => '@imgPath/country',
'url' => '@imgUrl/country',
'thumbs' => Yii::$app->params['thumbs'],
],
'galleryBehavior' => [
'class' => \zxbodya\yii2\galleryManager\GalleryBehavior::className(),
'type' => 'country',
'extension' => 'jpg',
'directory' => Yii::getAlias('@imgPath') . '/country/gallery',
'url' => Yii::getAlias('@imgUrl') . '/country/gallery',
'versions' => Yii::$app->params['gallery'],
'hasName' => true,
'hasDescription' => false,
]
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['ordinal', 'name', 'url'], 'required', 'on'=>array('create', 'update')],
[['logo'], 'required', 'on'=>array('create')],
[
'url',
'match', 'not' => true, 'pattern' => '/[^a-zA-Z0-9_-]/',
'message' => Yii::t('app', 'Not valid url'),
],
[['url'],'unique'],
[['ordinal', 'regionId', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12'], 'integer'],
[['seasonTextRight', 'reason1', 'reason2', 'reason3', 'reason4', 'reason5', 'seasonText', 'seo', 'descr', 'descrBoat', 'descrResort', 'descrSite', 'descrInterest'], 'string'],
[['name', 'name2', 'url'], 'string', 'max' => 255],
[['logo'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('model', 'ID'),
'ordinal' => Yii::t('model', 'Ordinal number'),
'name' => Yii::t('model', 'Country'),
'name2' => Yii::t('model', 'Name 2'),
'url' => Yii::t('model', 'Url'),
'sketch' => Yii::t('model', 'Sketch'),
'logo' => Yii::t('model', 'Logo'),
'reason1' => Yii::t('model', 'Reason 1'),
'reason2' => Yii::t('model', 'Reason 2'),
'reason3' => Yii::t('model', 'Reason 3'),
'reason4' => Yii::t('model', 'Reason 4'),
'reason5' => Yii::t('model', 'Reason 5'),
'seasonText' => Yii::t('model', 'Season Text'),
'seasonTextRight' => Yii::t('model', 'Season Text Right'),
'seo' => Yii::t('model', 'Seo'),
'regionId' => Yii::t('model', 'Region'),
'descr' => Yii::t('model', 'Description'),
'descrBoat' => Yii::t('model', 'Safari Description'),
'descrResort' => Yii::t('model', 'Resort Description'),
'descrSite' => Yii::t('model', 'Site Description'),
'descrInterest' => Yii::t('model', 'Interest Description'),
'm1' => Yii::t('model', 'Jan'),
'm2' => Yii::t('model', 'Feb'),
'm3' => Yii::t('model', 'Mar'),
'm4' => Yii::t('model', 'Apr'),
'm5' => Yii::t('model', 'May'),
'm6' => Yii::t('model', 'Jun'),
'm7' => Yii::t('model', 'Jul'),
'm8' => Yii::t('model', 'Aug'),
'm9' => Yii::t('model', 'Sep'),
'm10' => Yii::t('model', 'Okt'),
'm11' => Yii::t('model', 'Nov'),
'm12' => Yii::t('model', 'Dec'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEntities()
{
return $this->hasMany(Entity::className(), ['countryId' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getSites()
{
return $this->hasMany(Site::className(), ['countryId' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRegion()
{
return $this->hasOne(Region::className(), ['id' => 'regionId']);
}
public function getBoats()
{
return (new Query())
->select([
'c.url',
'c.name as countryName',
'e.seoUrl',
'e.name',
'e.price',
'e.ratingValue',
'e.reviewCount',
'CONCAT("/images/entity/h300-", e.logo) as logo',
'df.name as fleet'
])
->from('{{%entity}} e')
->innerJoin('{{%entity_boat}} eb', 'eb.entityId=e.id')
->innerJoin('{{%country}} c', 'c.id=e.countryId')
->leftJoin('{{%dic_fleet}} df', 'df.id=eb.fleetId')
->where('c.id = '.$this->id)
->orderBy('e.ordinal, e.name')
->groupBy('e.id')
->limit(6)
->all();
}
public function getResorts()
{
return (new Query())
->select([
'c.url',
'c.name as countryName',
'e.seoUrl',
'e.name',
'e.price',
'e.ratingValue',
'e.reviewCount',
'CONCAT("/images/entity/h300-", e.logo) as logo'
])
->from('{{%entity}} e')
->innerJoin('{{%entity_resort}} er', 'er.entityId=e.id')
->innerJoin('{{%country}} c', 'c.id=e.countryId')
->where('c.id = '.$this->id)
->orderBy('e.ordinal, e.name')
->groupBy('e.id')
->limit(6)
->all();
}
public function getSites6()
{
return (new Query())
->select([
'c.url as countryUrl',
'c.name as countryName',
's.url',
's.name',
'CONCAT("/images/site/h300-", s.logo) as logo'
])
->from('{{%site}} s')
->innerJoin('{{%country}} c', 'c.id=s.countryId')
->where('c.id = '.$this->id)
->orderBy('s.name')
->groupBy('s.id')
->limit(6)
->all();
}
public function getFeatures()
{
return Yii::$app->db->cache(function(){
return (new Query())
->select(['f.id', 'f.name'])
->from('{{%feature}} f')
->innerJoin('{{%product_tag_link}} ptl', 'ptl.tagId = f.id')
->innerJoin('{{%product}} p', 'p.id = ptl.productId')
->innerJoin('{{%entity}} e', 'e.id = p.entityId')
->where('e.countryId = :countryId', [':countryId' => $this->id])
->orderBy('f.ordinal, f.name')
->groupBy('f.id')
->all();
}, Yii::$app->params['cacheExpire'], new TagDependency(['tags' => 'index_features']));
}
public function getProducts()
{
return (new Query())
->select([
'p.seoUrl',
'p.name',
'e.price',
'CONCAT("/images/product/h300-", p.logo) as logo'
])
->from('{{%product}} p')
->innerJoin('{{%entity}} e', 'e.id=p.entityId')
->where('p.main = 1 AND e.countryId = :countryId', [':countryId' => $this->id])
->orderBy('p.name')
->groupBy('p.id')
->limit(6)
->all();
}
public function isReason()
{
$isReason = false;
for ($i = 1; $i<=5; $i++) {
$field = 'reason'.$i;
if ($this->$field) {
$isReason = true;
break;
}
}
return $isReason;
}
public function isSeason()
{
$isSeason = false;
for ($i = 1; $i<=12; $i++) {
$field = 'm'.$i;
if ($this->$field) {
$isSeason = true;
break;
}
}
return $isSeason;
}
public function isWhen()
{
return $this->isSeason() || $this->seasonText || $this->seasonTextRight;
}
public function getUrl()
{
if ($this->_url === null)
$this->_url = Url::to(['country/show', 'url' => $this->url], true);
return $this->_url;
}
public function getOgImage()
{
return substr(Url::home(true), 0, -1).$this->getThumbUploadUrl('logo', 'w850');
}
}