/** * Creates a new Photo model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Photo(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Photo model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Photo(); $request = Yii::$app->request; $page_id = $request->get('page_id'); $model->create_date = time(); $model->update_date = time(); if ($model->load(Yii::$app->request->post())) { $model->page_id = $page_id; $model->parent_id = 0; $model->create_date = strtotime($model->create_date); $model->update_date = strtotime($model->update_date); if ($model->save()) { return $this->redirect(['view', 'id' => $model->id, 'page_id' => $page_id]); } } $page = Page::findOne($page_id); return $this->render('create', ['model' => $model, 'page_id' => $page_id, 'page' => $page]); }
public function actionPhoto() { $sAlbum = $_GET['album']; if ($_GET['category']) { $sCategory = $_GET['category']; } else { $sCategory = 0; } $sSubCategory = $_GET['sub_category']; $this->redirectOnHomepageIfGuest($user, $isMyProfile); $widget_language = explode('-', Yii::$app->language)[0]; $model = new Photo(); $image = new UploadImage(); // Завантаження зображень $image->scenario = 'photo'; $all_categories = Category::getAllTranslatedCategory(); $all_subcategories = Category::getAllTranslatedSubCategory(); //перевірка/створення потрібних дерикторій для збереження картинок if (!file_exists("img/photos/" . date('Y'))) { mkdir("img/photos/" . date('Y')); } if (!file_exists("img/photos/" . date('Y') . "/" . date('m'))) { mkdir("img/photos/" . date('Y') . "/" . date('m')); } if (!file_exists("img/photos/" . date('Y') . "/" . date('m') . "/" . date('d'))) { mkdir("img/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()) { //збереження фотографії на сервері $user_id = Yii::$app->user->getId(); $image->file = UploadedFile::getInstance($image, 'file'); if ($image->file && $image->validate()) { // Зберігаємо зображення на сервер і в БД $model->src = time() . $user_id . '.' . $image->file->extension; $image->savePhoto($model->src); //////////////***************/////// } } if ($model->load($request->post()) && $model->validate()) { if (!Category::isSetCategory($model->id_level_1) || !SubCategoryController::isValidCategory($model->id_level_1, $model->id_level_2) && Category::hasSubCategory($model->id_level_1) || !User::checkAlbum($model->id_album)) { return $this->redirect("/"); } if ($request->post('mark')) { $marks_arr_new = $request->post('mark'); } else { $marks_arr_new = []; } if ($model->id_marks) { $marks_arr_new[$model->id_marks] = ''; } $marks_arr_new = \app\components\Marks::format($marks_arr_new); $marks_tmp = []; foreach ($marks_arr_new as $k => $v) { $marks_tmp[] = $k; } //список тих що в базі даних $marks_tmp = Marks::getMarksByNames($marks_tmp); //масив id тих що в бд $marks_tmp_id = []; foreach ($marks_tmp as $v) { $marks_tmp_id[] = $v['id']; } //обновилення coununter тих о в бд if ($marks_tmp_id) { Marks::updateCountMarks($marks_tmp_id); } //позначаємо ті що є в бд foreach ($marks_tmp as $k => $v) { $marks_arr_new[$k] = true; } //список тих що нема в бд $marks_tmp = []; foreach ($marks_arr_new as $k => $v) { if (!$v) { $marks_tmp[] = $k; } } if ($marks_tmp) { Marks::insertMarks($marks_tmp); } $marks_tmp = Marks::getMarksByNames($marks_tmp); foreach ($marks_tmp as $v) { $marks_tmp_id[] = $v['id']; } //зберігаємо данні в БД $model->date = date('Y-m-d H:i:s'); $model->id_author = Yii::$app->user->getId(); $model->views = 1; if (isset($_POST['private'])) { $model->erotic = 1; } else { $model->erotic = 0; } //рандом для того щоб рейтинг завжди був різний у всіх фото, це потрібно для пошуку $model->rating_all = 1 + mt_rand() % 100000000.0 / 10000000000000.0; if (Album::isPrivacyAlbum($model->id_album)) { $model->is_closed = 1; } if ($_POST['private'] == 1) { $model->erotic = $_POST['private']; } $model->save(); if ($marks_tmp_id) { Marks::addMarksToPhoto($marks_tmp_id, $model->id); } // Якщо це перше фото в альбомі, то встановлюємо його як обкладинку альбому if (Photo::thisFirstAlbumPhoto($model->id_album) == 1) { Photo::setPhotoToCover($model->id, $model->id_album); } //формуємо повідомлення про вдале додання нової фотографії \Yii::$app->getSession()->setFlash('notify', 'Фотографію успішно збережено!'); return $this->redirect("/photo" . $model->id); } $ssSub = (new Query())->select('COUNT(1)')->from('level_2')->where(['id_level_1' => $sCategory])->scalar(); $marks = Marks::find()->select(['name as label'])->asArray()->all(); return $this->render('photo', ['model' => $model, 'marks' => $marks, 'image' => $image, 'user' => $user, 'isMyProfile' => $isMyProfile, 'all_categories' => $all_categories, 'all_subcategories' => $all_subcategories, 'widget_language' => $widget_language, 'sAlbum' => $sAlbum, 'sCategory' => $sCategory, 'sSubCategory' => $sSubCategory, 'ssSub' => $ssSub]); }