Item.php 11.6 KB
<?php

namespace common\modules\map\models;


use Yii;
use yii\behaviors\AttributeBehavior;
use yii\helpers\{
    ArrayHelper, Inflector
};
//
use thread\app\base\models\ActiveRecord;
use thread\app\base\module\abstracts\Module;
use thread\modules\user\models\User;
use thread\actions\fileapi\UploadBehavior;
//
use common\modules\map\Map as MainModule;
use common\modules\location\models\Region;
use common\components\GalleryManager;
use common\behaviors\GalleryBehavior;


/**
 * Class Item
 *
 * @property integer id
 * @property string alias
 * @property integer user_id
 * @property integer type_id
 * @property integer area_id
 * @property integer lat
 * @property integer lng
 * @property string type {'1', '2'}
 * @property string web_site
 * @property string youtube_link
 * @property string gallery_image
 * @property string gallery_file
 * @property integer investment
 * @property integer position
 * @property integer created_at
 * @property integer updated_at
 * @property boolean published
 * @property boolean deleted
 * @author Alla Kuzmenko
 * @package common\modules\map\models
 */
class Item extends ActiveRecord
{
    /**
     *
     */
    const moduleName = 'map';

    const default_name_archive = 'archive/map_item';

    /** @var null GalleryManager */
    private $_galleryManager = null;


    /**
     * @return array
     */
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => AttributeBehavior::className(),
                'attributes' => [
                    self::EVENT_BEFORE_INSERT => 'alias',
                    self::EVENT_BEFORE_UPDATE => 'alias',
                    self::EVENT_BEFORE_VALIDATE => 'alias',
                ],
                'value' => function ($event) {
                    return Inflector::slug($this->alias);
                },
            ],
            'uploadBehavior' => [
                'class' => UploadBehavior::class,
                'attributes' => [
                    /*'gallery_image' => [
                        'path' => self::getModule()->getItemUploadPath(),
                        'tempPath' => Yii::getAlias('@root') . '/temp',
                        'url' => self::getModule()->getItemUploadUrl()
                    ],*/
                    'gallery_file' => [
                        'path' => self::getModule()->getItemUploadPath(),
                        'tempPath' => Yii::getAlias('@root') . '/temp',
                        'url' => self::getModule()->getItemUploadUrl()
                    ]
                ]
            ],
            'GalleryBehavior' => [
                'class' => GalleryBehavior::class,
                'galleryRow' => 'gallery_image',
                'path' => Yii::$app->getModule('map')->getItemUploadPath()
            ]
        ]);
    }

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

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

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['alias', 'type', 'user_id', 'coordinates', 'type_id', 'area_id', 'region_id', 'investment', 'type_of_promoter', 'electric_power', 'heat_capacity', 'date_of_entry', 'current_stage_of_project_development'], 'required'],
            [['gallery_image', 'gallery_file'], 'string', 'max' => 1024],
            [['web_site', 'youtube_link', 'alias', 'web_site_investor', 'date_of_entry',], 'string', 'max' => 255],
            [['created_at', 'updated_at', 'position', 'investment', 'region_id'], 'integer'],
            [['type'], 'in', 'range' => array_keys(MainModule::typeRange())],
            [['alias'], 'unique'],
            [['published', 'deleted'], 'in', 'range' => array_keys(static::statusKeyRange())],
            [['position', 'investment', 'electric_power', 'share_of_project_equity_offered'], 'default', 'value' => 0],
            //
            [['electric_power', 'heat_capacity', 'share_of_project_equity_offered'], 'number'],
            [['type_of_promoter'], 'in', 'range' => array_keys(static::type_of_promoterKeyRange())],
            [['current_stage_of_project_development'], 'in', 'range' => array_keys(static::current_stage_of_project_developmentKeyRange())],
            [['date_of_entry'], 'default', 'value' => ''],
        ];
    }

    /**
     * @return array
     */
    public function scenarios()
    {
        return [
            'published' => ['published'],
            'deleted' => ['deleted'],
            'backend' => [
                'alias',
                'user_id',
                'type',
                'gallery_image',
                'web_site',
                'youtube_link',
                'position',
                'published',
                'deleted',
                'coordinates',
                'investment',
                'gallery_file',
                'type_id',
                'area_id',
                'electric_power',
                'region_id',
                'web_site_investor',
                'heat_capacity',
                'type_of_promoter',
                'date_of_entry',
                'current_stage_of_project_development',
                'share_of_project_equity_offered',
            ],
        ];
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'alias' => Yii::t('app', 'Alias'),
            'type_id' => Yii::t('map', 'type'),
            'area_id' => Yii::t('map', 'area'),
            'user_id' => Yii::t('map', 'User'),
            'gallery_image' => Yii::t('map', 'Gallery image'),
            'gallery_file' => Yii::t('map', 'Gallery file'),
            'youtube_link' => Yii::t('map', 'Video link'),
            'web_site' => Yii::t('map', 'web_site'),
            'type' => Yii::t('map', 'Type'),
            'coordinates' => Yii::t('map', 'Coordinates'),
            'investment' => Yii::t('map', 'Investment'),
            'electric_power' => Yii::t('map', 'Electric power'),
            'region_id' => Yii::t('map', 'Region'),
            'web_site_investor' => Yii::t('map', 'web site investor'),
            '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'),
            'heat_capacity' => Yii::t('map', 'heat_capacity'),
            'type_of_promoter' => Yii::t('map', 'type_of_promoter'),
            'date_of_entry' => Yii::t('map', 'date_of_entry'),
            'share_of_project_equity_offered' => Yii::t('map', 'Share of project equity offered'),
            'current_stage_of_project_development' => Yii::t('map', 'Current stage of project development'),
        ];
    }

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

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

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

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_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 array
     */
    public function getGalleryFileLink()
    {
        $data = [];
        $module = self::getModule();
        $path = $module->getItemUploadPath($this);
        $url = $module->getItemUploadUrl($this);
        if (!empty($this->gallery_file)) {
            $fileNames = explode(',', $this->gallery_file);
            foreach ($fileNames as $key => $val) {
                if (is_file($path . '/' . $val)) {
                    $data[$key] = $url . '/' . $val;
                }
            }
        }
        return $data;
    }


    /**
     * @return array
     */
    public static function type_of_promoterKeyRange()
    {
        return [
            'Municipal project' => Yii::t('map', 'Municipal project'),
            'Commercial project' => Yii::t('map', 'Commercial project')
        ];
    }

    /**
     * @return array
     */
    public static function current_stage_of_project_developmentKeyRange()
    {
        return [
//            'Idea' => Yii::t('map', 'Idea'),
            'Under development' => Yii::t('map', 'Under development'),
            'Ready to build' => Yii::t('map', 'Ready to build'),
//            'Turnkey' => Yii::t('map', 'Turnkey'),
            'Operating' => Yii::t('map', 'Operating'),
            'Expansion' => Yii::t('map', 'Expansion'),
        ];
    }

    /**
     * @return string
     */
    public function getArchiveName()
    {
        return self::default_name_archive . $this->id . ".zip";
    }

    /**
     *
     * @return string
     */
    public function getGalleryLink()
    {
        $module = self::getModule();
        $path = $module->getItemUploadPath($this);
        $url = $module->getItemUploadUrl($this);
        $images = $this->getGalleryManager()->getFileNames($this->gallery_image);
        $imagesSources = [];
        foreach ($images as $image) {
            if (file_exists($path . $image) && $image) {
                $imagesSources[] = $url . $image;
            }
        }
        return $imagesSources;
    }


    /**
     * @return mixed
     */
    public function getGalleryItems()
    {
        $galleryParams = [];
        $newsModule = self::getModule();
        $path = $newsModule->getItemUploadPath();
        $url = $newsModule->getItemUploadUrl();

        $gallery = json_decode($this->gallery_image, true);
        if ($gallery) {
            foreach ($gallery as $imageParams) {
                $imageParams[GalleryManager::NAME_IMAGE_PATH] = '';
                $image = $imageParams[GalleryManager::NAME_IMAGE] ? $imageParams[GalleryManager::NAME_IMAGE] : '';

                if (is_file($path . $image)) {
                    $imageParams[GalleryManager::NAME_IMAGE_PATH] = $url . $image;
                    $galleryParams[] = $imageParams;
                }
            }
        }


        return $galleryParams;
    }

    /**
     * @return mixed
     */
    public function getGalleryItemsPath()
    {
        $galleryParams = [];
        $newsModule = self::getModule();
        $path = $newsModule->getItemUploadPath();
        $url = $newsModule->getItemUploadUrl();

        $gallery = json_decode($this->gallery_image, true);
        if ($gallery) {
            foreach ($gallery as $imageParams) {
                $imageParams[GalleryManager::NAME_IMAGE_PATH] = '';
                $image = $imageParams[GalleryManager::NAME_IMAGE] ? $imageParams[GalleryManager::NAME_IMAGE] : '';

                if (is_file($path . $image)) {
                    $imageParams[GalleryManager::NAME_IMAGE_PATH] = $path . $image;
                    $galleryParams[] = $imageParams;
                }
            }
        }


        return $galleryParams;
    }


    public function getGalleryManager()
    {
        if ($this->_galleryManager === null) {
            $this->_galleryManager = new GalleryManager();

        }
        return $this->_galleryManager;
    }

}