backend/controllers/WidgetController.php
/**
     * Register new widget.
     * Widget zip uploaded to temporary directory and extracted there.
     * Check the widget directory and move the first directory of the extracted widget.
     * If the widget configuration is valid, save the widget, if not remove the widget.
     * If registration is successful, the browser will be redirected to the 'index' page.
     *
     * @return mixed
     */
Register new widget.
Widget zip uploaded to temporary directory and extracted there.
Check the widget directory and move the first directory of the extracted widget.
If the widget configuration is valid, save the widget, if not remove the widget.
If registration is successful, the browser will be redirected to the 'index' page.

@return mixed
Register new widget . Widget zip uploaded to temporary directory and extracted there . Check the widget directory and move the first directory of the extracted widget . If the widget configuration is valid save the widget if not remove the widget . If registration is successful the browser will be redirected to the ' index ' page . @return mixed
public function actionCreate()
    {
        $errors = [];
        $model = new Widget(['scenario' => 'upload']);
        if (!is_dir($this->_tmp)) {
            FileHelper::createDirectory($this->_tmp, 0755);
        }
        if (!is_dir($this->_dir)) {
            FileHelper::createDirectory($this->_dir, 0755);
        }
        if (($model->file = UploadedFile::getInstance($model, 'file')) && $model->validate(['file'])) {
            $tmpPath = $this->_tmp . $model->file->name;
            if (!$model->file->saveAs($tmpPath)) {
                return $this->render('create', [
                    'model' => $model,
                    'errors' => [Yii::t('writesdown', 'Failed to move uploaded file')],
                ]);
            }
            $zipArchive = new \ZipArchive();
            $zipArchive->open($tmpPath);
            if (!$zipArchive->extractTo($this->_tmp)) {
                $zipArchive->close();
                FileHelper::removeDirectory($this->_tmp);
                return $this->render('create', [
                    'model' => $model,
                    'errors' => [Yii::t('writesdown', 'Failed to extract file.')],
                ]);
            }
            $baseDir = substr($zipArchive->getNameIndex(0), 0, strpos($zipArchive->getNameIndex(0), '/'));
            $zipArchive->close();
            $configPath = $this->_tmp . $baseDir . '/config/main.php';
            if (!is_file($configPath)) {
                FileHelper::removeDirectory($this->_tmp);
                return $this->render('create', [
                    'model' => $model,
                    'errors' => [Yii::t('writesdown', 'File configuration does not exist.')],
                ]);
            }
            $config = require($configPath);
            if (is_dir($this->_dir . $baseDir)) {
                $errors['dirExist'] = Yii::t('writesdown', 'Widget with the same directory already exist.');
            } else {
                rename($this->_tmp . $baseDir, $this->_dir . $baseDir);
            }
            FileHelper::removeDirectory($this->_tmp);
            if (!isset($config['title'])
                || !(isset($config['config']['class']) && class_exists($config['config']['class']))
            ) {
                $errors[] = Yii::t('writesdown', 'Invalid configuration.');
            }
            if (!$errors) {
                Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Widget successfully installed.'));
                return $this->redirect(['index']);
            } else {
                if (!ArrayHelper::getValue($errors, 'dirExist')) {
                    FileHelper::removeDirectory($this->_dir . $baseDir);
                }
                $errors = ArrayHelper::merge($errors, $model->getFirstErrors());
            }
        }
        return $this->render('create', [
            'model' => $model,
            'errors' => $errors,
        ]);
    }
public function actionCreate ( ) { $ errors = [ ] ; $ model = new Widget ( [ 'scenario' => 'upload' ] ) ; if ( ! is_dir ( $ this -> _tmp ) ) { FileHelper :: createDirectory ( $ this -> _tmp , 0755 ) ; } if ( ! is_dir ( $ this -> _dir ) ) { FileHelper :: createDirectory ( $ this -> _dir , 0755 ) ; } if ( ( $ model -> file = UploadedFile :: getInstance ( $ model , 'file' ) ) && $ model -> validate ( [ 'file' ] ) ) { $ tmpPath = $ this -> _tmp . $ model -> file -> name ; if ( ! $ model -> file -> saveAs ( $ tmpPath ) ) { return $ this -> render ( 'create' , [ 'model' => $ model , 'errors' => [ Yii :: t ( 'writesdown' , 'Failed to move uploaded file' ) ] , ] ) ; } $ zipArchive = new \ ZipArchive ( ) ; $ zipArchive -> open ( $ tmpPath ) ; if ( ! $ zipArchive -> extractTo ( $ this -> _tmp ) ) { $ zipArchive -> close ( ) ; FileHelper :: removeDirectory ( $ this -> _tmp ) ; return $ this -> render ( 'create' , [ 'model' => $ model , 'errors' => [ Yii :: t ( 'writesdown' , 'Failed to extract file.' ) ] , ] ) ; } $ baseDir = substr ( $ zipArchive -> getNameIndex ( 0 ) , 0 , strpos ( $ zipArchive -> getNameIndex ( 0 ) , '/' ) ) ; $ zipArchive -> close ( ) ; $ configPath = $ this -> _tmp . $ baseDir . '/config/main.php' ; if ( ! is_file ( $ configPath ) ) { FileHelper :: removeDirectory ( $ this -> _tmp ) ; return $ this -> render ( 'create' , [ 'model' => $ model , 'errors' => [ Yii :: t ( 'writesdown' , 'File configuration does not exist.' ) ] , ] ) ; } $ config = require ( $ configPath ) ; if ( is_dir ( $ this -> _dir . $ baseDir ) ) { $ errors [ 'dirExist' ] = Yii :: t ( 'writesdown' , 'Widget with the same directory already exist.' ) ; } else { rename ( $ this -> _tmp . $ baseDir , $ this -> _dir . $ baseDir ) ; } FileHelper :: removeDirectory ( $ this -> _tmp ) ; if ( ! isset ( $ config [ 'title' ] ) || ! ( isset ( $ config [ 'config' ] [ 'class' ] ) && class_exists ( $ config [ 'config' ] [ 'class' ] ) ) ) { $ errors [ ] = Yii :: t ( 'writesdown' , 'Invalid configuration.' ) ; } if ( ! $ errors ) { Yii :: $ app -> getSession ( ) -> setFlash ( 'success' , Yii :: t ( 'writesdown' , 'Widget successfully installed.' ) ) ; return $ this -> redirect ( [ 'index' ] ) ; } else { if ( ! ArrayHelper :: getValue ( $ errors , 'dirExist' ) ) { FileHelper :: removeDirectory ( $ this -> _dir . $ baseDir ) ; } $ errors = ArrayHelper :: merge ( $ errors , $ model -> getFirstErrors ( ) ) ; } } return $ this -> render ( 'create' , [ 'model' => $ model , 'errors' => $ errors , ] ) ; }
Register new widget.
Register new widget .