Item.php 6.53 KB
<?php

namespace frontend\modules\map\models;

use Yii;
use yii\helpers\Inflector;
use yii\helpers\Url;
use yii\db\{
    ActiveRecord, ActiveQuery
};
use yii\helpers\ArrayHelper;
//
use thread\app\model\interfaces\BaseFrontModel;
//
use common\modules\map\models\ItemLang;
//
use frontend\modules\location\models\Region;

/**
 * Class Item
 *
 * @package frontend\modules\map\models
 * @author Alla Kuzmenko
 * @copyright (c) 2017
 *
 *
 * @property \yii\db\ActiveQuery $lang
 */
class Item extends \common\modules\map\models\Item implements BaseFrontModel
{

    /**
     * @return array
     */
    public function scenarios()
    {
        return [
            'backend' => [
                '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',
            ],
        ];
    }

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            [['type_of_promoter', 'electric_power', 'heat_capacity', 'date_of_entry', 'current_stage_of_project_development'], 'required'],
        ]);
    }

    /**
     * @return bool
     */
    public function beforeValidate()
    {
        if ($this->isNewRecord) {
            $this->type = 'project';
            $this->user_id = Yii::$app->getUser()->getId();
        }
        $this->alias = Inflector::slug($_POST['ItemLang']['title'] ?? '');

        return parent::beforeValidate();
    }

    /**
     * @return bool
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->type = 'project';
            $this->user_id = Yii::$app->getUser()->getId();
            $this->alias = Inflector::slug($_POST['ItemLang']['title'] ?? '');
        }

        return parent::beforeSave($insert);
    }

    /**
     * @return mixed
     */
    public static function find()
    {
        return parent::find()->innerJoinWith(["lang"])->enabled();
    }

    /**
     * @return mixed
     */
    public static function findBase()
    {
        return self::find()->orderBy(['position' => SORT_ASC]);
    }

    /**
     * @return mixed
     */
    public static function findByParams()
    {
        $params = \Yii::$app->request->get();
        $q = self::findBase()->innerJoinWith(['typeItem'])->innerJoinWith(['area'])->orderBy(['position' => SORT_ASC]);
        //area
        if (isset($params['type'])) {
            $q->andWhere([
                'type' => $params['type'],
            ]);
        }
        if (isset($params['region']) && $params['region'] != 'all') {
            $q->andWhere([
                'region_id' => $params['region']
            ]);
        }
        if (isset($params['power']) && isset($params['power']['min']) && isset($params['power']['max'])) {
            $q->andWhere(['BETWEEN', 'electric_power', (int)$params['power']['min'], (int)$params['power']['max']]);
        }
        if (isset($params['inv']) && isset($params['inv']['min']) && isset($params['inv']['max'])) {
            $q->andWhere(['BETWEEN', 'investment', (int)$params['inv']['min'], (int)$params['inv']['max']]);
        }
        if (isset($params['cap']) && isset($params['cap']['min']) && isset($params['cap']['max'])) {
            $q->andWhere(['BETWEEN', 'heat_capacity', (int)$params['cap']['min'], (int)$params['cap']['max']]);
        }
        if (isset($params['area'])) {
            $types = array_keys($params['area']);
            if (!empty($types)) {
                $q->andWhere([
                    'IN', 'type_id', $types
                ]);
            }
            $area = [];
            foreach ($params['area'] as $type) {
                $area = ArrayHelper::merge($type, $area);
            }
            if (!empty($area)) {
                $q->andWhere([
                    'IN', 'area_id', $area
                ]);
            }
        } elseif (count($params) > 1) {
            $p = $params;
            unset($p['page']);
            if (count($p) > 1) {
                $q->andWhere([
                    'IN', 'type_id', 0
                ]);
            }
        }

        return $q;
    }

    /**
     *
     * @param integer $id
     * @return ActiveRecord|null
     */
    public static function findById($id)
    {
        return self::findBase()->byID($id)->one();
    }

    /**
     *
     * @param string $alias
     * @return ActiveRecord|null
     */
    public static function findByAlias($alias)
    {
        return self::findBase()->byAlias($alias)->one();
    }


    /**
     *
     * @return string
     */
    public function getUrl($scheme = false)
    {
        return Url::toRoute(['/map/item/view', 'alias' => $this->alias], $scheme);
    }

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

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

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

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

    /**
     * @param $type
     * @return ActiveQuery
     */
    public static function findByType(string $type)
    {
        return self::findBase()->andWhere(['type' => $type])->asArray()->all();
    }

    /**
     * @param $alias
     * @return mixed
     */
    public static function getByAlias($alias)
    {
        return self::findBase()->innerJoinWith(['typeItem'])->innerJoinWith(['area'])->byAlias($alias)->one();
    }

    /**
     * @return string
     */
    public function getArchiveLink()
    {
        if (is_file($this->module->getItemUploadPath() . $this->getArchiveName())) {
            return $this->module->getItemUploadUrl() . $this->getArchiveName();
        } else {
            return false;
        }
    }
}