modules/admin/controllers/ProductCategoryController.php
/**
     * Updates an existing ProductCategory model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
Updates an existing ProductCategory model.
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
Updates an existing ProductCategory model . If update is successful the browser will be redirected to the ' view ' page . @param integer $id @return mixed @throws NotFoundHttpException if the model cannot be found
public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $parent_category = ProductCategory::find()->where(['parent_category_id' => null])->all();
        $old_image = $model->image;
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return \yii\widgets\ActiveForm::validate($model);
        }
        $new_image = UploadedFile::getInstance($model, 'image');
        if ($model->load(Yii::$app->request->post())) {
            if (!empty($new_image)) {
                $uploadDirPath = Yii::getAlias('@productCategoryImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@productCategoryImageThumbRelativePath');
                $thumbImagePath = '';
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (file_exists($uploadDirPath . '/' . $old_image) && !empty($old_image)) {
                    unlink($uploadDirPath . '/' . $old_image);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                if (file_exists($uploadThumbDirPath . '/' . $old_image) && !empty($old_image)) {
                    unlink($uploadThumbDirPath . '/' . $old_image);
                }
                $ext = $new_image->extension;
                $fileName = time() . rand(99999, 88888) . '.' . $ext;
                $new_image->saveAs($uploadDirPath . '/' . $fileName);
                $actualImagePath = $uploadDirPath . '/' . $fileName;
                $thumbImagePath = $uploadThumbDirPath . '/' . $fileName;
                Image::getImagine()->open($actualImagePath)->thumbnail(new Box(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;
            } else {
                $model->image = $old_image;
            }
            if ($model->save()) {
                Yii::$app->session->setFlash(Growl::TYPE_SUCCESS, "Category/Subcategory updated successfully.");
            } else {
                Yii::$app->session->setFlash(Growl::TYPE_DANGER, "Error while updating Category/Subcategory.");
            }
            return $this->redirect(['index']);
        }
        return $this->render('update', [
            'model' => $model,
            'parent_category' => $parent_category,
        ]);
    }
public function actionUpdate ( $ id ) { $ model = $ this -> findModel ( $ id ) ; $ parent_category = ProductCategory :: find ( ) -> where ( [ 'parent_category_id' => null ] ) -> all ( ) ; $ old_image = $ model -> image ; if ( Yii :: $ app -> request -> isAjax && $ model -> load ( Yii :: $ app -> request -> post ( ) ) ) { \ Yii :: $ app -> response -> format = \ yii \ web \ Response :: FORMAT_JSON ; return \ yii \ widgets \ ActiveForm :: validate ( $ model ) ; } $ new_image = UploadedFile :: getInstance ( $ model , 'image' ) ; if ( $ model -> load ( Yii :: $ app -> request -> post ( ) ) ) { if ( ! empty ( $ new_image ) ) { $ uploadDirPath = Yii :: getAlias ( '@productCategoryImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@productCategoryImageThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( file_exists ( $ uploadDirPath . '/' . $ old_image ) && ! empty ( $ old_image ) ) { unlink ( $ uploadDirPath . '/' . $ old_image ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } if ( file_exists ( $ uploadThumbDirPath . '/' . $ old_image ) && ! empty ( $ old_image ) ) { unlink ( $ uploadThumbDirPath . '/' . $ old_image ) ; } $ ext = $ new_image -> extension ; $ fileName = time ( ) . rand ( 99999 , 88888 ) . '.' . $ ext ; $ new_image -> saveAs ( $ uploadDirPath . '/' . $ fileName ) ; $ actualImagePath = $ uploadDirPath . '/' . $ fileName ; $ thumbImagePath = $ uploadThumbDirPath . '/' . $ fileName ; Image :: getImagine ( ) -> open ( $ actualImagePath ) -> thumbnail ( new Box ( 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 ; } else { $ model -> image = $ old_image ; } if ( $ model -> save ( ) ) { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_SUCCESS , " Category/Subcategory updated successfully. " ) ; } else { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_DANGER , " Error while updating Category/Subcategory. " ) ; } return $ this -> redirect ( [ 'index' ] ) ; } return $ this -> render ( 'update' , [ 'model' => $ model , 'parent_category' => $ parent_category , ] ) ; }
Updates an existing ProductCategory model.
Updates an existing ProductCategory model .
// Create product image upload directory if not exist //unlink real image if update // Create product image thumb upload directory if not exist //unlink thumb image if update // Upload profile picture // Create thumb of profile picture // Insert profile picture name into database