Item.php 2.87 KB
<?php

namespace common\modules\partners\models;

use Yii;
use thread\app\base\models\ActiveRecord;
use thread\app\base\module\abstracts\Module;
use common\modules\partners\Partners as MainModule;


/**
 * Class Item
 *
 * @property integer id
 * @property string image_link
 * @property string site_link
 * @property integer position
 * @property integer created_at
 * @property integer updated_at
 * @property boolean published
 * @property boolean deleted
 * @property \yii\db\ActiveQuery $lang
 * @property null|string $imageLink
 *
 * @author Alla Kuzmenko
 * @package common\modules\partners\models
 */
class Item extends ActiveRecord
{

    const moduleName = 'partners';

    /**
     * @return null|object|string
     */
    public static function getDb()
    {
        return MainModule::getDb();
    }

    /**
     * @return string
     */
    public static function tableName()
    {
        return '{{%partners_item}}';
    }

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['created_at', 'updated_at', 'position'], 'integer'],
            [['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
            [['image_link', 'site_link'], 'string', 'max' => 255],
        ];
    }

    /**
     * @return array
     */
    public function scenarios()
    {
        return [
            'published' => ['published'],
            'deleted' => ['deleted'],
            'backend' => [
                'image_link',
                'site_link',
                'position',
                'published',
                'deleted'
            ]
        ];
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'image_link' => Yii::t('app', 'Image'),
            'site_link' => Yii::t(self::moduleName, 'site link'),
            'position' => Yii::t('app', 'Position'),
            'created_at' => Yii::t('app', 'Create time'),
            'updated_at' => Yii::t('app', 'Update time'),
            'published' => Yii::t('app', 'Published'),
            'deleted' => Yii::t('app', 'Deleted'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLang()
    {
        return $this->hasOne(ItemLang::class, ['rid' => 'id']);
    }

    /**
     * @return null|string
     */
    public function getImageLink()
    {
        $module = $this->getModule();
        $path = $module->getItemUploadPath();
        $url = $module->getItemUploadUrl();

        return (!empty($this->image_link) && is_file($path . '/' . $this->image_link))
            ? $url . '/' . $this->image_link
            : null;
    }

    /**
     * @return Module|null the module instance, `null` if the module does not exist.
     */
    public static function getModule()
    {
        return Yii::$app->getModule(self::moduleName);
    }
}