modules/admin/controllers/AdsController.php
/**
     * Creates a new Ads model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new Ads model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new Ads model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new Ads();
        $model->scenario = Ads::SCENARIO_CREATE;
        $ad_image = UploadedFile::getInstance($model, 'image');
        $category = ArrayHelper::map(ProductCategory::find()->where(['parent_category_id' => null])->andWhere(['IN', 'status', [ProductCategory::STATUS_APPROVE]])->all(), 'id', 'name');
        $subCategory = ArrayHelper::map(ProductCategory::find()->where(['>', 'parent_category_id', 0])->andWhere(['IN', 'status', [ProductCategory::STATUS_APPROVE]])->all(), 'id', 'name');
        $product = ArrayHelper::map(Product::find()->where(['IN', 'status_id', [ProductStatus::STATUS_APPROVED, ProductStatus::STATUS_IN_STOCK]])->all(), 'id', 'name');
        $brand = ArrayHelper::map(Brand::find()->where(['IN', 'status', [Brand::STATUS_APPROVE]])->all(), 'id', 'name');
        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())) {
            if (!empty($ad_image)) {
                $uploadDirPath = Yii::getAlias('@adsImageRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@adsImageThumbRelativePath');
                $thumbImagePath = '';
                if (!is_dir($uploadDirPath)) {
                    mkdir($uploadDirPath, 0777);
                }
                if (!is_dir($uploadThumbDirPath)) {
                    mkdir($uploadThumbDirPath, 0777);
                }
                $ext = $ad_image->extension;
                $fileName = time() . rand(99999, 88888) . '.' . $ext;
                $ad_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;
            }
            if ($model->save()) {
                Yii::$app->session->setFlash(Growl::TYPE_SUCCESS, "Ads created successfully.");
            } else {
                Yii::$app->session->setFlash(Growl::TYPE_DANGER, "Error while creating Ads.");
            }
            return $this->redirect(['index']);
        }
        return $this->render('create', [
            'model' => $model,
            'category' => $category,
            'subCategory' => $subCategory,
            'product' => $product,
            'brand' => $brand,
        ]);
    }
public function actionCreate ( ) { $ model = new Ads ( ) ; $ model -> scenario = Ads :: SCENARIO_CREATE ; $ ad_image = UploadedFile :: getInstance ( $ model , 'image' ) ; $ category = ArrayHelper :: map ( ProductCategory :: find ( ) -> where ( [ 'parent_category_id' => null ] ) -> andWhere ( [ 'IN' , 'status' , [ ProductCategory :: STATUS_APPROVE ] ] ) -> all ( ) , 'id' , 'name' ) ; $ subCategory = ArrayHelper :: map ( ProductCategory :: find ( ) -> where ( [ '>' , 'parent_category_id' , 0 ] ) -> andWhere ( [ 'IN' , 'status' , [ ProductCategory :: STATUS_APPROVE ] ] ) -> all ( ) , 'id' , 'name' ) ; $ product = ArrayHelper :: map ( Product :: find ( ) -> where ( [ 'IN' , 'status_id' , [ ProductStatus :: STATUS_APPROVED , ProductStatus :: STATUS_IN_STOCK ] ] ) -> all ( ) , 'id' , 'name' ) ; $ brand = ArrayHelper :: map ( Brand :: find ( ) -> where ( [ 'IN' , 'status' , [ Brand :: STATUS_APPROVE ] ] ) -> all ( ) , 'id' , 'name' ) ; 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 ( ) ) ) { if ( ! empty ( $ ad_image ) ) { $ uploadDirPath = Yii :: getAlias ( '@adsImageRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@adsImageThumbRelativePath' ) ; $ thumbImagePath = '' ; if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } $ ext = $ ad_image -> extension ; $ fileName = time ( ) . rand ( 99999 , 88888 ) . '.' . $ ext ; $ ad_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 ; } if ( $ model -> save ( ) ) { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_SUCCESS , " Ads created successfully. " ) ; } else { Yii :: $ app -> session -> setFlash ( Growl :: TYPE_DANGER , " Error while creating Ads. " ) ; } return $ this -> redirect ( [ 'index' ] ) ; } return $ this -> render ( 'create' , [ 'model' => $ model , 'category' => $ category , 'subCategory' => $ subCategory , 'product' => $ product , 'brand' => $ brand , ] ) ; }
Creates a new Ads model.
Creates a new Ads 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