/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $currentUser = \Yii::$app->user->identity; if (!$currentUser->isAuthor()) { //不是 作者 无权操作 return $this->goHome(); } if ($model->load(Yii::$app->request->post())) { if (isset($model->user_id) || $currentUser->id != $model->user_id && !$currentUser->isAdmin()) { $model->user_id = $currentUser->id; } $model->user_id = $currentUser->id; //处理slug $title = mb_strlen($model->title, 'utf8') > 6 ? mb_substr($model->title, 0, 5) : $model->title; $model->slug = \app\helpers\Pinyin::encode($title, 'all'); while (Post::getPostBySlug($model->slug)) { $model->slug .= '-1'; } if ($model->save()) { $this->flash('发布成功!', 'info'); return $this->redirect(['update', 'id' => $model->id]); } } $model->comment_status = Post::COMMENT_STATUS_OK; $model->user_id = $currentUser->id; return $this->render('create', ['model' => $model, 'isAdmin' => $currentUser->isAdmin()]); }
/** * create a post * * @return null|string|\yii\web\Response * @throws Exception * @throws \Exception */ public function actionCreate() { try { Yii::trace('Trace :' . __METHOD__, __METHOD__); $response = null; $model = new Post(['scenario' => 'create']); $query = (new Query())->select('name, id')->from('categories')->all(); $categories = ArrayHelper::map($query, 'id', 'name'); if ($model->load($_POST) === true && $model->validate() === true) { $model->category_id = (int) $model->category_name; $model->created = Yii::$app->formatter->asDateTime('now', 'php:Y-m-d H:i:s'); $model->user_id = Yii::$app->user->id; $status = $model->save(); if ($status === true) { $sidebarCacheName = isset(Yii::$app->params['cache']['sidebar']) ? Yii::$app->params['cache']['sidebar'] : null; if ($sidebarCacheName !== null) { Yii::$app->cache->delete($sidebarCacheName); } $response = $this->redirect(['/post/view', 'slug' => $model->slug]); } } if ($response === null) { $response = $this->render('create', ['model' => $model, 'categories' => $categories]); } return $response; } catch (Exception $e) { Yii::error($e->getMessage(), __METHOD__); throw $e; } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); 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 Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'category' => Category::find()->all(), 'authors' => User::find()->all()]); } }
/** * Update the specified resource in storage. * * @param PostFormRequest $request * @param Post $post * @return Response */ public function update(PostFormRequest $request, $post) { $post->fill($request->all()); $post->save(); if ($tags = $request->input('tags')) { $post->retag($request->input('tags')); } return $post->load('tagged'); }
public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['list']); } else { $model->status = POST::STATUS_ENABLED; return $this->render('create', ['model' => $model]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $model->author_name = Yii::$app->user->identity->nickname; if ($model->load(Yii::$app->request->post())) { $model->author_id = Yii::$app->user->id; if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', ['model' => $model]); }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $createdAt = time(); $model->created_at = $createdAt; $model->updated_at = $createdAt; if ($model->load(Yii::$app->request->post()) && $model->save()) { $this->saveCategoriesList($model); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'categoriesList' => $this->getCategoriesList()]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post())) { $model->datetime = date("Y-m-d H:i:s"); $model->Participant_id = Yii::$app->user->id; if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } //die(var_dump($model->getErrors())); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { /** @var Category[] $categories */ $categories = Category::find()->where(['id' => $model->categories_id])->all(); foreach ($categories as $category) { $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'category_names' => ArrayHelper::map(Category::find()->all(), 'id', 'name')]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $post = Yii::$app->request->post(); if ($model->load($post)) { $model->user_id = $userId = \Yii::$app->user->identity->id; if ($model->save()) { return $this->redirect(Yii::$app->request->referrer); } } elseif (Yii::$app->request->isAjax) { return $this->renderAjax('_form', ['model' => $model]); } else { return $this->render('_form', ['model' => $model]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $model->create_at = $model->update_at = $model->reply_at = time(); $model->user_id = User::getCurrentId(); if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->point_id > 0) { $point = Point::findOne($model->point_id); $point->post_num += 1; $point->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'point' => Point::findOne(isset($_GET['point']) ? $_GET['point'] : '')]); } }
public function actionNew() { $postModel = new Post(); $category = new Category(); $categoryList = ArrayHelper::map($category->find()->asArray()->all(), 'id', 'title'); $postList = $postModel->find()->asArray()->all(); $message = ''; if ($postModel->load(Yii::$app->request->post()) && $postModel->save()) { $selectedCategories = Category::find()->where(['id' => $postModel->categoryIds])->all(); foreach ($selectedCategories as $category) { $postModel->link('categories', $category); } $message = 'Category added success'; } return $this->render('new', ['model' => $postModel, 'message' => $message, 'categoryList' => $categoryList, 'postList' => $postList]); }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $pages = Page::find()->all(); $request = Yii::$app->request; $model->page_id = $request->get('page_id'); $model->create_date = mktime(); $model->update_date = mktime(); if ($model->load(Yii::$app->request->post())) { $model->create_date = strtotime($model->create_date); $model->update_date = strtotime($model->update_date); if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model, 'pages' => $pages]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); /*if ($model->load(Yii::$app->request->post())) { $image = UploadedFile::getInstance($model, 'foto_konten'); if ($model->save() && $model->upload()) { return $this->redirect(['site/index']); } } else { return $this->render('create', [ 'model' => $model, ]); }*/ if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['site/index']); } else { return $this->render('create', ['model' => $model]); } }
public function actionIndex() { if (Yii::$app->user->isGuest) { $userBibi = UserBibi::getAllDisplayUser(); return $this->render('index', ['userBibi' => $userBibi]); } else { $mySchedule = Activity::getJoinActivity(); $newPost = new Post(); $sentPost = false; if ($newPost->load(Yii::$app->request->post())) { $newPost->datetime = date("Y-m-d H:i:s"); $newPost->Participant_id = Yii::$app->user->id; if ($newPost->save()) { $sentPost = true; } } return $this->render('myindex', ['schedule' => $mySchedule, 'newPost' => $newPost, 'sentPost' => $sentPost]); } }
public function actionCreate() { $id = Yii::$app->user->identity->getId(); $get_user = \dektrium\user\models\User::getUser($id); $model = new Post(); $tag = new Tag(); $model->date = date("Y-m-d h:i:s"); $model->author = $get_user->getModels()[0]['username']; $model->user_id = $id; $model->rating_minus = 0; $model->rating_plus = 0; $tags = array(); $tmp = array(); $upload = new UploadForm(); if (Yii::$app->request->isPost) { } if (isset($_POST['Upload'])) { print_r($_POST['Post']); } // $model->image_url="s"; if (isset($_POST['Tag'])) { $pieces = explode(",", Yii::$app->request->post()['Tag']['name']); $dataProvider = Tag::getAllTags(); // //print_r(Yii::$app->request->post() ); $count = $dataProvider->getModels(); for ($i = 0; $i < count($count); $i++) { for ($j = 0; $j < count($pieces); $j++) { if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) { array_push($tmp, $pieces[$j]); // //print_r($tmp); $pieces[$j] = ""; // echo $pieces[$j]; } } } $ids = "'" . implode("','", $tmp) . "'"; // //print_r($tmp); // //print_r($ids); $dataProvider2 = Tag::getTagsIn($ids); //print_r($dataProvider2->getModels()); for ($i = 0; $i < count($pieces); $i++) { $tag = new Tag(); $tag->name = $pieces[$i]; $tag->save(); array_push($tags, $tag->tag_id); } for ($i = 0; $i < count($dataProvider2->getModels()); $i++) { array_push($tags, $dataProvider2->getModels()[$i]['tag_id']); } } if ($model->load(Yii::$app->request->post())) { // print_r(); if ($_POST['UploadForm']) { // $random = ''; // for ($i = 0; $i < 30; $i++) { // $random .= chr(mt_rand(33, 126)); // } $upload->imageFile = UploadedFile::getInstance($upload, 'imageFile'); if ($upload->upload()) { $model->image_url = 'uploads/' . $upload->imageFile->name; $model->save(); // return; } } $model->save(); for ($i = 0; $i < count($tags); $i++) { $postTags = new Post_tags(); $postTags->tag_id = $tags[$i]; $postTags->post_id = $model->post_id; $postTags->save(); } // $upload->file = UploadedFile::getInstance($model ,'image_url'); // //// echo $upload->file=$model->formName(); // $upload->file->saveAs(); // if ($model->validate()) { // $upload->file->saveAs('uploads' ); // } // $file->saveAs('@app/uploads'); return $this->redirect(['/post/view', 'id' => $model->post_id]); } else { return $this->render('create', ['model' => $model, 'tag' => $tag, 'upload' => $upload]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreatepost() { if (!\Yii::$app->user->isGuest) { $modelpost = new Post(); $arrayrouter = array(); if (Yii::$app->user->identity->role == 10) { $modelsrouter = Router::find()->select(['id', 'title'])->all(); } if (Yii::$app->user->identity->role == 5) { $modelsuser = Users::find()->where(['role' => 1, 'id_manager' => Yii::$app->user->identity->id])->select(['id'])->all(); if ($modelsuser != NULL) { $n_user = 0; $arrayuser = array(); foreach ($modelsuser as $modeluser) { $arrayuser[$n_user] = $modeluser->id; $n_user++; } } $modelsobject = Object::find()->where(['id_user' => $arrayuser])->select(['id', 'title'])->all(); $n_object = 0; $arrayobject = array(); if ($modelsobject != NULL) { foreach ($modelsobject as $modelobject) { $arrayobject[$n_object] = $modelobject->id; $n_object++; } } $modelsrouter = Router::find()->where(['id_object' => $arrayobject])->select(['id', 'title'])->all(); } if (Yii::$app->user->identity->role == 1) { $modelsobject = Object::find()->where(['id_user' => Yii::$app->user->identity->id])->select(['id', 'title'])->all(); $n_object = 0; $arrayobject = array(); if ($modelsobject != NULL) { foreach ($modelsobject as $modelobject) { $arrayobject[$n_object] = $modelobject->id; $n_object++; } } $modelsrouter = Router::find()->where(['id_object' => $arrayobject])->select(['id', 'title'])->all(); } if ($modelsrouter != NULL) { foreach ($modelsrouter as $modelrouter) { $arrayrouter[$modelrouter->id] = $modelrouter->title; } } if ($modelpost->load(Yii::$app->request->post())) { if ($modelpost->save()) { Yii::$app->session->setFlash('info', 'Пост добавлен.'); return $this->redirect(['post', 'id' => $modelpost->id]); } else { Yii::$app->session->setFlash('error', 'Пост не создан.'); return $this->refresh(); } } else { return $this->render('createpost', ['model' => $modelpost, 'arrayrouter' => $arrayrouter]); } } else { return $this->redirect(['login']); } }