Item.php 2.78 KB
<?php

namespace backend\modules\map\models;

use Yii;
use ZipArchive;

/**
 * Class Item
 * @package backend\modules\map\models
 * @author Alla Kuzmenko
 * @copyright (c), Thread
 */
class Item extends \common\modules\map\models\Item
{

    /**
     * @param $params
     * @return \yii\data\ActiveDataProvider
     */
    public function search($params)
    {
        return (new search\Item())->search($params);
    }

    /**
     * @param $params
     * @return \yii\data\ActiveDataProvider
     */
    public function trash($params)
    {
        return (new search\Item())->trash($params);
    }

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

    /**
     * @return array
     */
    public static function type_of_promoterKeyRange()
    {
        $l = Yii::$app->params['themes']['language'];
        Yii::$app->params['themes']['language'] = Yii::$app->language;
        $r = [
            'Municipal project' => Yii::t('map', 'Municipal project'),
            'Commercial project' => Yii::t('map', 'Commercial project'),
        ];
        Yii::$app->params['themes']['language'] = $l;
        return $r;
    }

    /**
     * @return array
     */
    public static function current_stage_of_project_developmentKeyRange()
    {
        $l = Yii::$app->params['themes']['language'];
        Yii::$app->params['themes']['language'] = Yii::$app->language;
        $r = [
//            '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'),
        ];
        Yii::$app->params['themes']['language'] = $l;
        return $r;
    }

    public function afterSave($insert, $changedAttributes)
    {
        if (isset($changedAttributes['gallery_file'])) {
            if (is_file($this->module->getItemUploadPath() . $this->getArchiveName())) {
                unlink($this->module->getItemUploadPath() . $this->getArchiveName());
            }
            $files = $this->getGalleryFileLink();
            if (!empty($files)) {
                //собираем архив
                $zip = new ZipArchive();
                if ($zip->open('..' . $this->module->getItemUploadUrl() . $this->getArchiveName(), ZIPARCHIVE::CREATE)) {
                    foreach ($files as $file) {
                        $zip->addFile('..' . $file);
                    }
                }
                $zip->close(); //Завершаем работу с архивом
            }
        }
        parent::afterSave($insert, $changedAttributes);
    }
}