Exemplo n.º 1
0
 public function actionUploadPhoto($id)
 {
     if (Club::isGuest($id, Yii::$app->user->getId())) {
         return $this->redirect("/club{$id}");
     }
     $model = new ClubPhoto();
     $image = new UploadImage();
     $image->scenario = 'photo';
     if (!file_exists("img/club/photos/" . date(Y))) {
         mkdir("img/club/photos/" . date(Y));
     }
     if (!file_exists("img/club/photos/" . date(Y) . "/" . date(m))) {
         mkdir("img/club/photos/" . date(Y) . "/" . date(m));
     }
     if (!file_exists("img/club/photos/" . date(Y) . "/" . date(m) . "/" . date(d))) {
         mkdir("img/club/photos/" . date(Y) . "/" . date(m) . "/" . date(d));
     }
     $request = Yii::$app->request;
     if ($request->isAjax && $model->load($request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
     if (Yii::$app->request->post()) {
         $img_src = '';
         $user_id = Yii::$app->user->getId();
         $image->file = UploadedFile::getInstance($image, 'file');
         if ($image->file && $image->validate()) {
             $model->club_id = $id;
             $model->src = time() . $user_id . '.' . $image->file->extension;
             $img_src = $model->src;
         }
     }
     if ($model->load($request->post()) && $model->validate()) {
         $image->savePhotoClub($img_src);
         $model->date = date('Y-m-d H:i:s');
         $model->id_author = Yii::$app->user->getId();
         $model->views = 0;
         $model->rating_all = 1;
         $model->save();
         Club::photoCounterPP($id);
         \Yii::$app->getSession()->setFlash('notify', 'Фотографія успішно додана!');
         return $this->redirect("/club" . $id);
     }
     return $this->render('upload_photo', ['model' => $model, 'image' => $image]);
 }