modules/api/v1/controllers/ProductCategoryController.php
/**
     * Creates a new ProductCategory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new ProductCategory model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new ProductCategory model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new ProductCategory();
        $postData = Yii::$app->request->post();
        $productCategoryData['ProductCategory'] = $postData;
        $model->scenario = ProductCategory::SCENARIO_CREATE;
        $image = UploadedFile::getInstanceByName('image');
        $model->image = $image;
        if ($model->load($productCategoryData) && $model->validate()) {
            if (!empty($image)) {
                $uploadDirPath = Yii::getAlias('@productCategoryImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@productCategoryImageThumbRelativePath');
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                $ext = $image->extension;
                $fileName = pathinfo($image->name, PATHINFO_FILENAME);
                $fileName = $fileName . '_' . time() . '.' . $ext;
                $image->saveAs($uploadDirPath . '/' . $fileName);
                $actualImagePath = $uploadDirPath . '/' . $fileName;
                $thumbImagePath = $uploadThumbDirPath . '/' . $fileName;
                Image::thumbnail($actualImagePath, Yii::$app->params['profile_picture_thumb_width'], Yii::$app->params['profile_picture_thumb_height'])->save($thumbImagePath, ['quality' => Yii::$app->params['profile_picture_thumb_quality']]);
                $model->image = $fileName;
            }
            $model->save();
        }
        if (!empty($model->image) && file_exists(Yii::getAlias('@productCategoryImageThumbRelativePath') . '/' . $model->image)) {
            $model->image = Yii::$app->request->getHostInfo() . Yii::getAlias('@productCategoryImageThumbAbsolutePath') . '/' . $model->image;
        } else {
            $model->image = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new ProductCategory ( ) ; $ postData = Yii :: $ app -> request -> post ( ) ; $ productCategoryData [ 'ProductCategory' ] = $ postData ; $ model -> scenario = ProductCategory :: SCENARIO_CREATE ; $ image = UploadedFile :: getInstanceByName ( 'image' ) ; $ model -> image = $ image ; if ( $ model -> load ( $ productCategoryData ) && $ model -> validate ( ) ) { if ( ! empty ( $ image ) ) { $ uploadDirPath = Yii :: getAlias ( '@productCategoryImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@productCategoryImageThumbRelativePath' ) ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } $ ext = $ image -> extension ; $ fileName = pathinfo ( $ image -> name , PATHINFO_FILENAME ) ; $ fileName = $ fileName . '_' . time ( ) . '.' . $ ext ; $ image -> saveAs ( $ uploadDirPath . '/' . $ fileName ) ; $ actualImagePath = $ uploadDirPath . '/' . $ fileName ; $ thumbImagePath = $ uploadThumbDirPath . '/' . $ fileName ; Image :: thumbnail ( $ actualImagePath , Yii :: $ app -> params [ 'profile_picture_thumb_width' ] , Yii :: $ app -> params [ 'profile_picture_thumb_height' ] ) -> save ( $ thumbImagePath , [ 'quality' => Yii :: $ app -> params [ 'profile_picture_thumb_quality' ] ] ) ; $ model -> image = $ fileName ; } $ model -> save ( ) ; } if ( ! empty ( $ model -> image ) && file_exists ( Yii :: getAlias ( '@productCategoryImageThumbRelativePath' ) . '/' . $ model -> image ) ) { $ model -> image = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@productCategoryImageThumbAbsolutePath' ) . '/' . $ model -> image ; } else { $ model -> image = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; } return $ model ; }
Creates a new ProductCategory model.
Creates a new ProductCategory model .
// Create profile upload directory if not exist // Create profile thumb upload directory if not exist // Upload profile picture // Create thumb of profile picture // Insert profile picture name into database