public function testSetTags() { $sourceTags = [1, 3]; $post = $this->postModel->findOne(2); $post->setTags($sourceTags); $this->assertInstanceOf('common\\models\\Post', $post); $this->assertTrue($post->save(false)); $this->assertEquals($sourceTags, $post->getTags()); }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('您所请求的页面不存在'); } }
protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionDeletePost($id) { $post = Post::findOne(['id' => $id, 'user_id' => Yii::$app->user->id]); if (!empty($post->id)) { return $post->delete(); } return 'Not found post'; }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $slug * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function getModel($slug) { if (($model = Post::findOne(['slug' => $slug])) !== null) { return $model; } else { throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); } }
public function getPost($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Стать не найдена.'); } }
public function actionView($id) { $post = Post::findOne($id); if (!$post) { throw new \yii\web\HttpException(400, 'Wrong post id', 405); } return $this->render('view', ['post' => $post]); }
public function getCategory() { $post = Post::findOne($this->id); foreach (explode(',', $post->category_id) as $value) { $category = Category::findOne($value); $data[] = ['id' => $category->id, 'parent_id' => $category->parent_id, 'title' => $category->title, 'indent' => $this->getIndent($category->indent)]; } return $data; }
/** * Creates a new Comment model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed * @throws NotFoundHttpException */ public function actionCreate() { $model = new Comment(); if (Yii::$app->request->isGet) { throw new NotFoundHttpException('The requested page does not exist.'); } $model->author_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['../post/view', 'id' => $model->post_id]); } else { return $this->render('../post/view', ['model' => Post::findOne($model->post_id)]); } }
public function actionView($slug) { $post = Post::findOne(['slug' => $slug]); $comment = new Comment(); $comment->name = ''; $comment->email = ''; $comment->body = ''; if ($comment->load(Yii::$app->request->post())) { $comment->status = 0; if ($comment->save()) { Yii::$app->session->setFlash('success', ['type' => 'success', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'You\'re comment successfully stored and will shown after approval.', 'title' => 'Saving Comment']); } else { Yii::$app->session->setFlash('error', ['type' => 'danger', 'duration' => 12000, 'icon' => 'fa fa-chat', 'message' => 'Sorry but we couldn\'t store your comment.', 'title' => 'Saving Comment']); } } return $this->render('view', ['post' => $post, 'comment' => $comment]); }
public function actionCreate($id = null) { if ($id === null) { $post = new Post(); } else { if (($post = Post::findOne(['id' => $id, 'author_id' => Yii::$app->getUser()->getId()])) === null) { throw new HttpException(404, 'Cannot find the requested post.'); } } if (Yii::$app->getRequest()->getIsAjax() && $post->load(Yii::$app->getRequest()->post())) { Yii::$app->getResponse()->format = Response::FORMAT_JSON; return ActiveForm::validate($post); } if ($post->load(Yii::$app->getRequest()->post()) && $post->save()) { return $this->redirect(Post::findOne($id)->getUrl()); } $rootSections = Section::find()->roots()->orderBy('title')->all(); $mainSections = $post->rootSectionId === null ? [] : Section::find()->where(['section_id' => $post->rootSectionId])->orderBy('title')->all(); return $this->render('create', ['post' => $post, 'rootSections' => $rootSections, 'mainSections' => $mainSections]); }
/** * @param AcceptanceTester $I */ public function testComment(AcceptanceTester $I) { $I->wantTo('ensure that post comment works'); $postView = PostViewPage::openBy($I); // $I->see('Sample Post', 'h1'); $I->see('Sample Post'); $I->amGoingTo('submit post comment form with no data'); $postView->submitComment([]); $I->expectTo('see validations error'); $I->see('Name cannot be blank.', '.help-block'); $I->see('Email cannot be blank.', '.help-block'); $I->see('Content cannot be blank.', '.help-block'); $I->amGoingTo('submit post comment form with no correct email'); $postView->submitComment(['comment_author' => 'tester', 'comment_author_email' => 'tester.email', 'comment_content' => 'New comment']); $I->expectTo('see that email is not correct'); $I->see('Email is not a valid email address.'); $I->dontSee('Name cannot be blank.', '.help-block'); $I->dontSee('Content cannot be blank.', '.help-block'); $I->amGoingTo('submit post comment form with correct data'); $postView->submitComment(['comment_author' => 'tester', 'comment_author_email' => '*****@*****.**', 'comment_content' => 'New comment']); $I->expect('new comment saved'); $I->dontSee('Name cannot be blank.', '.help-block'); $I->dontSee('Email cannot be blank.', '.help-block'); $I->dontSee('Content cannot be blank.', '.help-block'); PostComment::deleteAll(['comment_author' => 'tester']); Post::findOne(1)->updateAttributes(['post_comment_count' => '1']); }
/** * Output tree of comments * @param array $comments Array of Comment * @param int $parent_id * @param array $options */ public static function outCommentsTree($comments, $parent_id, $options) { if (!isset($options) || !is_object($options)) { $showReplies = isset($options['showReplies']) ? $options['showReplies'] : true; $showReplyButton = isset($options['showReplyButton']) ? $options['showReplyButton'] : true; $postID = isset($options['postID']) ? $options['postID'] : false; $options = (object) compact('showReplies', 'showReplyButton', 'postID'); } if (isset($comments[$parent_id])) { foreach ($comments[$parent_id] as $comment) { if (is_null($comment->user)) { $username = '******'; $avatar = new Asset(); $avatar->assetable_type = Asset::ASSETABLE_USER; $imageUrl = $avatar->getDefaultFileUrl(); $userUrl = false; } else { $username = $comment->user->getDisplayName(); $avatar = $comment->user->getAsset(); $imageUrl = $avatar->getFileUrl(); $userUrl = $comment->user->getUrl(); } $commentDate = Yii::$app->formatter->asDate(strtotime($comment->created_at), 'd MMMM Y HH:mm'); $repliesCommentsCount = isset($comments[$comment->id]) ? count($comments[$comment->id]) : 0; $classRepliesCount = $repliesCommentsCount == 0 ? 'no-replies' : ''; $textRepliesCount = $repliesCommentsCount == 0 ? '' : $repliesCommentsCount; $isReply = $parent_id == 0 ? false : true; $own = isset(Yii::$app->user->id) && Yii::$app->user->id == $comment->user_id ? 'yes' : 'no'; $rating = $comment->getRating(); $ratingUpClass = ''; $ratingDownClass = ''; if (!Yii::$app->user->isGuest && Yii::$app->user->id != $comment->user_id) { $userRating = $comment->getUserVote(); if ($userRating == 1) { $ratingUpClass = 'voted'; } elseif ($userRating == -1) { $ratingDownClass = 'voted'; } } else { $ratingUpClass = 'disable'; $ratingDownClass = 'disable'; } $commentLevelClass = $parent_id == 0 ? 'lvl-one' : ''; if ($parent_id == 0 && $options->postID && $comment->getCommentableType() == Comment::COMMENTABLE_POST) { $post = Post::findOne($comment->commentable_id); if (isset($post->id) && $post->id !== $options->postID) { $options->postID = $post->id; ?> <div class="comment-theme"> <div class="theme-label">Комментарии по теме:</div> <div class="theme-link"> <a href="<?php echo $post->getUrl(); ?> " data-pjax="0"><?php echo $post->title; ?> </a> </div> </div> <?php } } $adminLink = ''; if (Yii::$app->user->can('admin')) { $adminLink = '<a class="admin-view-link" target="_blank" href="/admin/comment/' . $comment->id . '" data-pjax="0"></a>'; } ?> <div id="comment-<?php echo $comment->id; ?> " class="comment <?php echo $commentLevelClass; ?> " data-own="<?php echo $own; ?> " data-comment-id="<?php echo $comment->id; ?> " data-commentable-type="<?php echo $comment->getCommentableType(); ?> " data-commentable-id="<?php echo $comment->commentable_id; ?> " > <div class="comment-user"> <div class="user-photo"> <?php if ($userUrl) { ?> <a href="<?php echo $userUrl; ?> " data-pjax="0"> <?php } ?> <img src="<?php echo $imageUrl; ?> "> <?php if ($userUrl) { ?> </a> <?php } ?> </div> <div class="user-info"> <div class="user-name"> <?php if ($userUrl) { ?> <a href="<?php echo $userUrl; ?> " data-pjax="0"> <?php } ?> <?php echo $username; ?> <?php if ($userUrl) { ?> </a> <?php } ?> </div> <div class="post-time"><?php echo $commentDate; ?> </div> </div> </div> <div class="comment-links"> <div class="rating-counter"> <a href="javascript:void(0)" class="rating-up <?php echo $ratingUpClass; ?> " data-id="<?php echo $comment->id; ?> " data-type="comment"></a> <div class="rating-count <?php echo $rating >= 0 ? 'blue' : 'red'; ?> "><?php echo $rating; ?> </div> <a href="javascript:void(0)" class="rating-down <?php echo $ratingDownClass; ?> " data-id="<?php echo $comment->id; ?> " data-type="comment"></a> </div> <?php if (!Yii::$app->user->isGuest) { ?> <a href="<?php echo Url::to('/complain/' . $comment->id); ?> " class="button-complain" title="Пожаловаться" data-pjax="0"></a> <?php } ?> <?php if (!Yii::$app->user->isGuest && $options->showReplyButton) { ?> <a href="javascript:void(0)" class="button-reply" title="Ответить"></a> <?php } ?> <?php echo $adminLink; ?> </div> <div class="comment-body"> <?php echo $comment->getContent(); ?> </div> <?php if ($repliesCommentsCount > 0) { ?> <div class="comment-replies"> <a class="replies-toggle-btn toggle-button toggle-<?php echo $options->showReplies ? 'hide' : 'show'; ?> " data-target="comment-replies-content-<?php echo $comment->id; ?> " href="javascript:void(0)"> <div class="toggle-text"> <span><?php echo $options->showReplies ? 'Скрыть' : 'Показать'; ?> </span> ответы </div> <div class="toggle-icon"></div> </a> <div id="comment-replies-content-<?php echo $comment->id; ?> " class="toggle-content <?php echo $options->showReplies ? 'visible' : ''; ?> "> <?php self::outCommentsTree($comments, $comment->id, $options); ?> </div> </div> <?php } ?> </div> <?php } } }
public function actionEditPost($id) { if (!YiiForum::checkAuth('post_edit')) { return $this->noPermission(); } YiiForum::checkIsGuest(); $boardId = YiiForum::getGetValue('boardid'); $model = Post::findOne(['id' => $id]); $data = YiiForum::getPostValue('Post'); if ($data == null) { $thread = Thread::findOne(['id' => $model['thread_id']]); $locals = []; $locals['thread'] = $thread; $locals['currentBoard'] = $this->getBoard($boardId); $locals['model'] = $model; return $this->render('edit-post', $locals); } else { $model->load(Yii::$app->request->post()); $model->modify_time = TTimeHelper::getCurrentTime(); $model->save(); return $this->redirect(['view', 'id' => $model->thread_id]); } }
/** * Render file browser for editor and file input * * @param int|null $post_id * @param bool $editor * * @throws \yii\web\NotFoundHttpException * @return string */ public function actionPopup($post_id = null, $editor = false) { $this->layout = "blank"; $model = new Media(['scenario' => 'upload']); if ($post_id) { if ($post = Post::findOne($post_id)) { return $this->render('popup', ['post' => $post, 'model' => $model, 'editor' => $editor]); } else { throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); } } return $this->render('popup', ['model' => $model, 'editor' => $editor]); }
?> <?php echo Html::label(Yii::t('writesdown', 'Front page: '), 'option-front_page'); ?> <?php echo Html::dropDownList('Option[front_page][value]', $model->front_page->value, Post::findOne($model->front_page->value) ? ArrayHelper::map(Post::find()->select(['id', 'title'])->where(['id' => $model->front_page->value])->all(), 'id', 'title') : [], ['class' => 'search-post', 'disabled']); ?> <?php echo Html::label(Yii::t('writesdown', 'Posts page: '), 'option-posts_page'); ?> <?php echo Html::dropDownList('Option[posts_page][value]', $model->posts_page->value, Post::findOne($model->posts_page->value) ? ArrayHelper::map(Post::find()->select(['id', 'title'])->where(['id' => $model->posts_page->value])->all(), 'id', 'title') : [], ['class' => 'search-post', 'disabled']); ?> </div> </div> <div class="form-group"> <?php echo Html::label(Yii::t('writesdown', 'Posts per page'), 'option-post_per_page', ['class' => 'col-sm-2 control-label']); ?> <div class="col-sm-7"> <?php echo Html::input('number', 'Option[posts_per_page][value]', $model->posts_per_page->value, ['id' => 'option-post_per_page', 'min' => 1, 'step' => 1]); ?> </div>
public function getCategories(&$data = [], $parent = NULL) { $post = Post::findOne($this->id); $category = Category::find()->where(['parent_id' => $parent])->all(); foreach ($category as $key => $value) { $data[] = ['id' => $value->id, 'title' => $value->title, 'indent' => $value->indent]; unset($category[$key]); $this->getCategories($data, $value->id); } return $data; }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findPost($id) { if (($model = Post::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); }
/** * Deletes an existing Tag model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $tag = $this->findModel($id); $taggings = Tagging::find()->where(['taggable_type' => Tagging::TAGGABLE_POST, 'tag_id' => $id])->all(); foreach ($taggings as $tagging) { $post = Post::findOne($tagging->taggable_id); if ($post) { $cached_tag_list = []; $newTags = $post->getTags(); foreach ($newTags as $newTag) { if (strcmp($tag->name, $newTag->name) !== 0) { $cached_tag_list[] = $newTag->name; } } $post->cached_tag_list = implode(', ', $cached_tag_list); $post->save(true, ['cached_tag_list']); } } Tagging::deleteAll(['tag_id' => $tag->id]); $tag->delete(); return $this->redirect(['index']); }
/** * Возвращает модель поста. * @param int $id * @throws NotFoundHttpException в случае, когда пост не найден или не опубликован * @return Post */ public function getPost($id) { if (($model = Post::findOne($id)) !== null && $model->isPublished()) { return $model; } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
/** * @param FunctionalTester $I */ public function testProtected(FunctionalTester $I) { Post::findOne(1)->updateAttributes(['password' => 'postpassword']); $I->wantTo('ensure that protected post works'); $postView = PostViewPage::openBy($I); $I->see('Sample Post', 'h1'); $I->amGoingTo('submit password form with incorrect password'); $postView->submitPassword('wrong_password'); $I->expectTo('not see the post'); $I->dontSeeElement('.entry-meta'); $I->amGoingTo('submit password form with correct password'); $postView->submitPassword('postpassword'); $I->expectTo('see the post'); $I->seeElement('.entry-meta'); Post::findOne(1)->updateAttributes(['password' => '']); }
public function actionComment() { if (isset($_POST['user_id']) && isset($_POST['post_id']) && isset($_POST['content'])) { $comment = new Comment(); $comment['user_id'] = $_POST['user_id']; $comment['post_id'] = $_POST['post_id']; $comment['content'] = $_POST['content']; $comment['create_at'] = $_POST['create_at']; //$comment['create_at'] = Yii::$app->formatter->asDatetime("Y-m-d"); $comment->save(); $user = User::findOne(['id' => $_POST['user_id']]); if ($user['image'] != "") { $image = Yii::$app->request->baseUrl . "/images/" . $user['image']; } else { $image = Yii::$app->request->baseUrl . "/images/avatar-default.jpg"; } $id = $_POST['post_id']; $receiver = Post::findOne(['id' => $id])['user_id']; if ($receiver != Yii::$app->user->getId()) { $newNotify = new PostNotification(); $newNotify['post_id'] = $id; $newNotify['type'] = 1; $newNotify['status'] = 0; $newNotify['action_id'] = Yii::$app->user->getId(); $newNotify['receiver_id'] = $receiver; $newNotify['create_at'] = date("Y/m/d H:i"); $newNotify->save(); } $listComment = Comment::find()->where(['post_id' => $id])->asArray()->all(); foreach ($listComment as $oneComment) { $isAdded[$oneComment['user_id']] = 0; } $isAdded[$receiver] = 1; foreach ($listComment as $oneComment) { $receiver = $oneComment['user_id']; if ($receiver != Yii::$app->user->getId() && $isAdded[$receiver] != 1) { $newNotify = new PostNotification(); $newNotify['post_id'] = $id; $newNotify['type'] = 1; $newNotify['status'] = 0; $newNotify['action_id'] = Yii::$app->user->getId(); $newNotify['receiver_id'] = $receiver; $newNotify['create_at'] = date("Y/m/d H:i"); $newNotify->save(); } $isAdded[$receiver] = 1; } echo '<div class="box-comment">' . '<img class="img-circle img-sm" src="' . $image . '" alt="user image">' . '<div class="comment-text">' . '<span class="username">' . $user['username'] . '<span class="text-muted pull-right">' . $comment['create_at'] . '</span>' . '</span>' . $comment['content'] . '</div>' . '</div>'; } else { echo 'NO'; } }
/** * Finds the Post model based on its primary key value. * If the model is not found it will return null. * * @param integer $id * * @return Post|null */ protected function findPost($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { return null; } }
public function actionView($slug) { $postModel = new Post(); $postData = $postModel->findOne(['slug' => $slug]); return $this->render('view', ['node' => $postData]); }
public function testUpdate(AcceptanceTester $I) { $I->wantTo('ensure that update post works'); $updatePage = UpdatePage::openBy($I); $I->see('Update Post', 'h1'); $I->see('Categories'); $I->see('Tags'); $I->amGoingTo('submit post post title same post title'); $updatePage->submit(['post_title' => 'Sample Page']); $I->expectTo('see that post title already taken'); $I->see('Title "Sample Page" has already been taken.', '.help-block'); $I->amGoingTo('submit post form with correct data'); $updatePage->submit(['post_title' => 'Sample Post Update']); $I->expect('post updated'); $I->see('Post successfully saved.', '.alert'); Post::findOne(1)->updateAttributes(['post_title' => 'Sample Post']); }
public static function getPostById($id) { return Post::findOne(['id' => $id]); }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param string $postSlug * * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModelBySlug($postSlug) { $model = Post::findOne(['post_slug' => $postSlug, 'post_status' => 'publish']); if ($model) { return $model; } throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); }
/** * Url: /post/edit/{$id} * @param $id int Post id * @return mixed * @throws ForbiddenHttpException * @throws NotFoundHttpException */ public function actionPostEdit($id) { $model = Post::findOne($id); if (!isset($model)) { throw new NotFoundHttpException('Страница не найдена.'); } if ($model->content_category_id != Post::CATEGORY_BLOG || empty(Yii::$app->user) || $model->user_id != Yii::$app->user->id) { throw new ForbiddenHttpException("Вы не можете выполнить это действие."); } $image = $model->getAsset(); $tags = $model->getTags(); $assets = $model->getAssets(); $model->tags = []; foreach ($tags as $tag) { $model->tags[] = $tag->id; } $model->title = html_entity_decode($model->title); $model->content = html_entity_decode($model->content); if ($model->load(Yii::$app->request->post()) && $model->validate()) { // Set slug $model->slug = $model->genSlug($model->title); // Set image $uploadedFile = UploadedFile::getInstance($model, 'image'); if ($uploadedFile) { // Remove old assets foreach ($assets as $asset) { $asset->delete(); } // Save origionals $asset = new Asset(); $asset->assetable_type = Asset::ASSETABLE_POST; $asset->assetable_id = $model->id; $asset->uploadedFile = $uploadedFile; $asset->saveAsset(); // Save thumbnails $imageID = $asset->id; $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST); foreach ($thumbnails as $thumbnail) { $asset = new Asset(); $asset->parent_id = $imageID; $asset->thumbnail = $thumbnail; $asset->assetable_type = Asset::ASSETABLE_POST; $asset->assetable_id = $model->id; $asset->uploadedFile = $uploadedFile; $asset->saveAsset(); } } $existingTags = []; // Remove tags foreach ($tags as $tag) { if (!is_array($model->tags) || !in_array($tag->id, $model->tags)) { $model->removeTag($tag->id); } else { $existingTags[] = $tag->id; } } // Adding new tags if (is_array($model->tags)) { foreach ($model->tags as $id) { if (!in_array($id, $existingTags)) { $model->addTag($id); } } } $cached_tag_list = []; $newTags = $model->getTags(); foreach ($newTags as $newTag) { $cached_tag_list[] = $newTag->name; } $model->cached_tag_list = implode(', ', $cached_tag_list); $model->save(); return $this->redirect($model->getUrl()); } $title = 'Изменить запись в блоге'; return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | ' . $title, 'columnFirst' => ['blog_form' => ['view' => '@frontend/views/forms/blog_form', 'data' => compact('model', 'tags', 'image', 'title')]], 'columnSecond' => ['blog' => SiteBlock::getBlogPosts(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]); }