modules/admin/controllers/TailorController.php
/**
     * Updates an existing Tailor 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 Tailor 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 Tailor 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);
        $old_image = $model->shop_image;
        $new_image = UploadedFile::getInstance($model, 'shop_image');
        $old_image_voucher = $model->voucher;
        $new_image_voucher = UploadedFile::getInstance($model, 'voucher');
        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);
        }
        if ($model->load(Yii::$app->request->post())) {
            $postData = Yii::$app->request->post('Tailor');
            if (!empty($new_image)) {
                $uploadDirPath = Yii::getAlias('@tailorShopImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@tailorShopImageThumbRelativePath');
                $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->shop_image = $fileName;
            } else {
                $model->shop_image = $old_image;
            }
            if (!empty($new_image_voucher)) {
                $uploadDirPath = Yii::getAlias('@tailorVoucherImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@tailorVoucherImageThumbRelativePath');
                $thumbImagePath = '';
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (file_exists($uploadDirPath . '/' . $old_image_voucher) && !empty($old_image_voucher)) {
                    unlink($uploadDirPath . '/' . $old_image_voucher);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                if (file_exists($uploadThumbDirPath . '/' . $old_image_voucher) && !empty($old_image_voucher)) {
                    unlink($uploadThumbDirPath . '/' . $old_image_voucher);
                }
                $ext = $new_image_voucher->extension;
                $fileName = time() . rand(99999, 88888) . '.' . $ext;
                $new_image_voucher->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->voucher = $fileName;
            } else {
                $model->voucher = $old_image_voucher;
            }
            if (!empty($postData['is_voucher_image_empty']) && $postData['is_voucher_image_empty'] == 1) {
                $uploadDirPath = Yii::getAlias('@tailorVoucherImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@tailorVoucherImageThumbRelativePath');
                if (file_exists($uploadDirPath . '/' . $model->voucher) && !empty($model->voucher)) {
                    unlink($uploadDirPath . '/' . $model->voucher);
                }
                if (file_exists($uploadThumbDirPath . '/' . $model->voucher) && !empty($model->voucher)) {
                    unlink($uploadThumbDirPath . '/' . $model->voucher);
                }
                $model->voucher = null;
            }
            if ($model->save()) {
                Yii::$app->session->setFlash(Growl::TYPE_SUCCESS, "Tailor updated successfully.");
            } else {
                Yii::$app->session->setFlash(Growl::TYPE_DANGER, "Error while updating Tailor.");
            }
            return $this->redirect(['index']);
        }
        return $this->render('update', [
            'model' => $model,
        ]);
    }
public function actionUpdate ( $ id ) { $ model = $ this -> findModel ( $ id ) ; $ old_image = $ model -> shop_image ; $ new_image = UploadedFile :: getInstance ( $ model , 'shop_image' ) ; $ old_image_voucher = $ model -> voucher ; $ new_image_voucher = UploadedFile :: getInstance ( $ model , 'voucher' ) ; 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 ) ; } if ( $ model -> load ( Yii :: $ app -> request -> post ( ) ) ) { $ postData = Yii :: $ app -> request -> post ( 'Tailor' ) ; if ( ! empty ( $ new_image ) ) { $ uploadDirPath = Yii :: getAlias ( '@tailorShopImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@tailorShopImageThumbRelativePath' ) ; $ 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 -> shop_image = $ fileName ; } else { $ model -> shop_image = $ old_image ; } if ( ! empty ( $ new_image_voucher ) ) { $ uploadDirPath = Yii :: getAlias ( '@tailorVoucherImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@tailorVoucherImageThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( file_exists ( $ uploadDirPath . '/' . $ old_image_voucher ) && ! empty ( $ old_image_voucher ) ) { unlink ( $ uploadDirPath . '/' . $ old_image_voucher ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } if ( file_exists ( $ uploadThumbDirPath . '/' . $ old_image_voucher ) && ! empty ( $ old_image_voucher ) ) { unlink ( $ uploadThumbDirPath . '/' . $ old_image_voucher ) ; } $ ext = $ new_image_voucher -> extension ; $ fileName = time ( ) . rand ( 99999 , 88888 ) . '.' . $ ext ; $ new_image_voucher -> 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 -> voucher = $ fileName ; } else { $ model -> voucher = $ old_image_voucher ; } if ( ! empty ( $ postData [ 'is_voucher_image_empty' ] ) && $ postData [ 'is_voucher_image_empty' ] == 1 ) { $ uploadDirPath = Yii :: getAlias ( '@tailorVoucherImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@tailorVoucherImageThumbRelativePath' ) ; if ( file_exists ( $ uploadDirPath . '/' . $ model -> voucher ) && ! empty ( $ model -> voucher ) ) { unlink ( $ uploadDirPath . '/' . $ model -> voucher ) ; } if ( file_exists ( $ uploadThumbDirPath . '/' . $ model -> voucher ) && ! empty ( $ model -> voucher ) ) { unlink ( $ uploadThumbDirPath . '/' . $ model -> voucher ) ; } $ model -> voucher = null ; } if ( $ model -> save ( ) ) { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_SUCCESS , " Tailor updated successfully. " ) ; } else { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_DANGER , " Error while updating Tailor. " ) ; } return $ this -> redirect ( [ 'index' ] ) ; } return $ this -> render ( 'update' , [ 'model' => $ model , ] ) ; }
Updates an existing Tailor model.
Updates an existing Tailor 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 tailor shop image // Create thumb of tailor shop image // Insert tailor shop image name into database // 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 tailor shop image // Create thumb of tailor shop image // Insert tailor shop image name into database // unlink images with thumb