backend/controllers/ModuleController.php
/**
     * Creates a new Module model.
     * Module zip uploaded to temporary directory and extracted there.
     * Check the module directory and move the first directory of the extracted module.
     * If the module configuration is valid, save the module, if not remove the module.
     * If creation is successful, the browser will be redirected to the 'index' page.
     *
     * @return mixed
     */
Creates a new Module model.
Module zip uploaded to temporary directory and extracted there.
Check the module directory and move the first directory of the extracted module.
If the module configuration is valid, save the module, if not remove the module.
If creation is successful, the browser will be redirected to the 'index' page.

@return mixed
Creates a new Module model . Module zip uploaded to temporary directory and extracted there . Check the module directory and move the first directory of the extracted module . If the module configuration is valid save the module if not remove the module . If creation is successful the browser will be redirected to the ' index ' page . @return mixed
public function actionCreate()
    {
        $errors = [];
        $model = new Module(['scenario' => 'create']);
        if (!is_dir($this->_dir)) {
            FileHelper::createDirectory($this->_dir, 0755);
        }
        if (!is_dir($this->_tmp)) {
            FileHelper::createDirectory($this->_tmp, 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,
                    'error' => [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,
                    'error' => [Yii::t('writesdown', 'Failed to extract file.')],
                ]);
            }
            $baseDir = substr($zipArchive->getNameIndex(0), 0, strpos($zipArchive->getNameIndex(0), '/'));
            $zipArchive->close();
            unlink($tmpPath);
            $configPath = $this->_tmp . $baseDir . '/config/main.php';
            if (!is_file($configPath)) {
                FileHelper::removeDirectory($this->_tmp);
                return $this->render('create', [
                    'model' => $model,
                    'error' => [Yii::t('writesdown', 'File configuration does not exist.')],
                ]);
            }
            $config = require($configPath);
            $model->setAttributes($config);
            $model->setAttributes(['directory' => $baseDir, 'status' => Module::STATUS_NOT_ACTIVE]);
            if ($model->validate(['directory'])) {
                rename($this->_tmp . $baseDir, $this->_dir . $baseDir);
            }
            FileHelper::removeDirectory($this->_tmp);
            $frontendClass = ArrayHelper::getValue($model->config, 'frontend.class', false);
            $backendClass = ArrayHelper::getValue($model->config, 'backend.class', false);
            if (!($frontendClass || $backendClass)) {
                $errors[] = Yii::t('writesdown', 'Invalid config.');
            }
            if ($backendClass && !class_exists($backendClass)) {
                $errors[] = Yii::t('writesdown', 'Invalid backend config.');
            }
            if ($frontendClass && !class_exists($frontendClass)) {
                $errors[] = Yii::t('writesdown', 'Invalid frontend config.');
            }
            $model->config = Json::encode($model->config);
            if (!$errors && $model->validate(['name', 'title', 'config', 'directory']) && $model->save(false)) {
                Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Module successfully installed'));
                return $this->redirect(['index']);
            } else {
                if (!$model->hasErrors('directory')) {
                    FileHelper::removeDirectory($this->_dir . $baseDir);
                }
                $errors = ArrayHelper::merge($errors, $model->getFirstErrors());
            }
        }
        return $this->render('create', [
            'model' => $model,
            'error' => $errors,
        ]);
    }
public function actionCreate ( ) { $ errors = [ ] ; $ model = new Module ( [ 'scenario' => 'create' ] ) ; if ( ! is_dir ( $ this -> _dir ) ) { FileHelper :: createDirectory ( $ this -> _dir , 0755 ) ; } if ( ! is_dir ( $ this -> _tmp ) ) { FileHelper :: createDirectory ( $ this -> _tmp , 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 , 'error' => [ 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 , 'error' => [ Yii :: t ( 'writesdown' , 'Failed to extract file.' ) ] , ] ) ; } $ baseDir = substr ( $ zipArchive -> getNameIndex ( 0 ) , 0 , strpos ( $ zipArchive -> getNameIndex ( 0 ) , '/' ) ) ; $ zipArchive -> close ( ) ; unlink ( $ tmpPath ) ; $ configPath = $ this -> _tmp . $ baseDir . '/config/main.php' ; if ( ! is_file ( $ configPath ) ) { FileHelper :: removeDirectory ( $ this -> _tmp ) ; return $ this -> render ( 'create' , [ 'model' => $ model , 'error' => [ Yii :: t ( 'writesdown' , 'File configuration does not exist.' ) ] , ] ) ; } $ config = require ( $ configPath ) ; $ model -> setAttributes ( $ config ) ; $ model -> setAttributes ( [ 'directory' => $ baseDir , 'status' => Module :: STATUS_NOT_ACTIVE ] ) ; if ( $ model -> validate ( [ 'directory' ] ) ) { rename ( $ this -> _tmp . $ baseDir , $ this -> _dir . $ baseDir ) ; } FileHelper :: removeDirectory ( $ this -> _tmp ) ; $ frontendClass = ArrayHelper :: getValue ( $ model -> config , 'frontend.class' , false ) ; $ backendClass = ArrayHelper :: getValue ( $ model -> config , 'backend.class' , false ) ; if ( ! ( $ frontendClass || $ backendClass ) ) { $ errors [ ] = Yii :: t ( 'writesdown' , 'Invalid config.' ) ; } if ( $ backendClass && ! class_exists ( $ backendClass ) ) { $ errors [ ] = Yii :: t ( 'writesdown' , 'Invalid backend config.' ) ; } if ( $ frontendClass && ! class_exists ( $ frontendClass ) ) { $ errors [ ] = Yii :: t ( 'writesdown' , 'Invalid frontend config.' ) ; } $ model -> config = Json :: encode ( $ model -> config ) ; if ( ! $ errors && $ model -> validate ( [ 'name' , 'title' , 'config' , 'directory' ] ) && $ model -> save ( false ) ) { Yii :: $ app -> getSession ( ) -> setFlash ( 'success' , Yii :: t ( 'writesdown' , 'Module successfully installed' ) ) ; return $ this -> redirect ( [ 'index' ] ) ; } else { if ( ! $ model -> hasErrors ( 'directory' ) ) { FileHelper :: removeDirectory ( $ this -> _dir . $ baseDir ) ; } $ errors = ArrayHelper :: merge ( $ errors , $ model -> getFirstErrors ( ) ) ; } } return $ this -> render ( 'create' , [ 'model' => $ model , 'error' => $ errors , ] ) ; }
Creates a new Module model.
Creates a new Module model .