OptionManipulator.php 5.97 KB
<?php
    
    namespace artbox\catalog\filter\models;
    
    use Imagine\Gd\Imagine;
    use Imagine\Image\Box;
    use Imagine\Image\ImageInterface;
    use yii\base\Model;
    use yii\web\UploadedFile;
    
    class OptionManipulator extends Model
    {
        public $optionId;
        
        public $sort;
        
        public $extra;
        
        public $groupId;
        /**
         * Fromat:
         * [
         *  'language_id' => 1,
         *  'alias' => 'option1',
         *  'value' => 'value1',
         * ],
         * [
         *  ...
         * ]
         *
         * @var array
         */
        public $data;
        
        protected $originPrefix;
        
        protected $thumbPrefix;
        
        public function init()
        {
            parent::init();
            
            if (!is_dir(\Yii::getAlias('@storage/options'))) {
                mkdir(\Yii::getAlias('@storage/options'));
            }
            
            if (!is_dir(\Yii::getAlias('@storage/options/thumb'))) {
                mkdir(\Yii::getAlias('@storage/options/thumb'));
            }
            
            $this->originPrefix = \Yii::getAlias('@storage/options/');
            $this->thumbPrefix = \Yii::getAlias('@storage/options/thumb/');
        }
        
        public function rules()
        {
            return [
                [
                    [
                        'sort',
                        'groupId',
                        'optionId',
                    ],
                    'integer',
                ],
                [
                    [ 'extra' ],
                    'string',
                ],
                [
                    [ 'data' ],
                    'safe',
                ],
            ];
        }
        
        public function create()
        {
            if ($this->extra === 'save_file') {
                $this->handleFileUpload();
            }
            
            $option = new Option(
                [
                    'group_id' => $this->groupId,
                    'sort'     => $this->sort,
                    'extra'    => $this->extra,
                ]
            );
            
            foreach ($this->data as $key => $row) {
                $model = $option->getVariationModel($row[ 'language_id' ]);
                
                $model->title = $row[ 'value' ];
                $model->slug = $row[ 'alias' ];
            }
            
            $result = $option->save();
            
            return $result;
        }
        
        public function update()
        {
            if ($this->extra === 'save_file') {
                $this->handleFileUpload();
            }
            
            $option = Option::findOne($this->optionId);
            
            foreach ($this->data as $key => $row) {
                $model = $option->getVariationModel($row[ 'language_id' ]);
                $model->title = $row[ 'value' ];
                $model->slug = $row[ 'alias' ];
            }
            
            $option->sort = $this->sort;
            
            $option->extra = $this->extra;
            
            return $option->save();
        }
        
        public function findOne($id)
        {
            /**
             * @var \artbox\catalog\filter\models\Option $option
             */
            $option = Option::find()
                            ->where(
                                [
                                    'id' => $id,
                                ]
                            )
                            ->with('languages')
                            ->one();
            
            $langs = [];
            foreach ($option->languages as $lang) {
                $langs[ $lang->language_id ] = [
                    'language_id' => $lang->language_id,
                    'value'       => $lang->title,
                    'alias'       => $lang->slug,
                ];
            }
            
            return [
                'optionId' => $id,
                'sort'     => $option->sort,
                'extra'    => $option->extra,
                'data'     => $langs,
            ];
        }
        
        public function delete($id)
        {
            $option = Option::findOne($id);
            
            return $option->delete();
        }
        
        public function findAll($id)
        {
            /**
             * @var \artbox\catalog\filter\models\Option[] $options
             */
            $options = Option::find()
                             ->where(
                                 [
                                     'group_id' => $id,
                                 ]
                             )
                             ->with('language')
                             ->orderBy('sort')
                             ->all();
            
            $data = [];
            
            foreach ($options as $option) {
                $data[] = [
                    'id'    => $option->id,
                    'extra' => $option->extra,
                    'value' => $option->language->title,
                    'alias' => $option->language->slug,
                    'sort'  => $option->sort,
                ];
            }
            
            return $data;
        }
        
        protected function handleFileUpload()
        {
            $file = UploadedFile::getInstanceByName('file');
            
            $file->saveAs(\Yii::getAlias($this->originPrefix . $file->name));
            
            $this->makeThumbnail($file->name);
            
            $this->extra = $file->name;
        }
        
        protected function makeThumbnail($name)
        {
            $imagine = new Imagine();
            
            $size = new Box(45, 45);
            
            $mode = ImageInterface::THUMBNAIL_INSET;
            
            $imagine->open($this->originPrefix . $name)
                    ->thumbnail($size, $mode)
                    ->save($this->thumbPrefix . $name);
        }
    }