Area.php 3.9 KB
<?php

namespace common\modules\map\models;

use Yii;

use thread\app\base\models\ActiveRecord;
use thread\app\base\module\abstracts\Module;
use common\modules\map\Map as MainModule;

/**
 * Class Area
 *
 * @property integer id
 * @property string type_map
 * @property string image_link
 * @property integer position
 * @property integer created_at
 * @property integer updated_at
 * @property boolean published
 * @property \yii\db\ActiveQuery $lang
 * @property null|string $imageLink
 * @property boolean deleted
 * @author Alla Kuzmenko
 * @package common\modules\map\models
 */
class Area extends ActiveRecord
{
    /**
     *
     */
    const moduleName = 'map';


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

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

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['type_map'], 'required'],
            [['image_link', 'image_link2'], 'string', 'max' => 1024],
            [['type_map'], 'in', 'range' => array_keys(MainModule::typeRange())],
            [['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
            [['position'], 'default', 'value' => 0],
            [['published'], 'default', 'value' => self::STATUS_KEY_ON]

        ];
    }

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

            ],
        ];
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'image_link' => Yii::t('app', 'Image link'),
            'image_link2' => Yii::t('app', 'Image link'),
            'type_map' => Yii::t('map', 'Type'),
            '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(AreaLang::class, ['rid' => 'id']);
    }

    /**
     * @return Module|null the module instance, `null` if the module does not exist.
     */

    /**
     * @return null|\yii\base\Module
     */
    public static function getModule()
    {
        return Yii::$app->getModule(self::moduleName);
    }

    /**
     * @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 null|string
     */
    public function getImageLink2()
    {
        $module = $this->getModule();
        $path = $module->getItemUploadPath();
        $url = $module->getItemUploadUrl();

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

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

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

}