#include "CommentView.h" #include "model/BlogSession.h" #include "model/Comment.h" #include "model/User.h" #include #include CommentView::CommentView(BlogSession &session, long long parentId) : session_(session) { Wt::Dbo::ptr parent = session_.load(parentId); comment_ = std::make_unique(); comment_.modify()->parent = parent; comment_.modify()->post = parent->post; edit(); } CommentView::CommentView(BlogSession &session, Wt::Dbo::ptr comment) : session_(session), comment_(comment) { comment_ = comment; renderView(); } void CommentView::cancel() { if (isNew()) removeFromParent(); else { Wt::Dbo::Transaction t(session_); renderView(); t.commit(); } } void CommentView::renderView() { clear(); bool isRootComment = !comment_->parent; setTemplateText(isRootComment ? tr("blog-root-comment") : tr("blog-comment")); bindString("collapse-expand", Wt::WString::Empty); // NYI auto replyText = std::make_unique(isRootComment ? tr("comment-add") : tr("comment-reply")); replyText->setStyleClass("link"); replyText->clicked().connect(this, &CommentView::reply); bindWidget("reply", std::move(replyText)); bool mayEdit = session_.user() && (comment_->author == session_.user() || session_.user()->role == User::Admin); if (mayEdit) { auto editText = std::make_unique(tr("comment-edit")); editText->setStyleClass("link"); editText->clicked().connect(this, &CommentView::edit); bindWidget("edit", std::move(editText)); } else bindString("edit", Wt::WString::Empty); bool mayDelete = (session_.user() && session_.user() == comment_->author) || session_.user() == comment_->post->author; if (mayDelete) { auto deleteText = std::make_unique(tr("comment-delete")); deleteText->setStyleClass("link"); deleteText->clicked().connect(this, &CommentView::rm); bindWidget("delete", std::move(deleteText)); } else bindString("delete", Wt::WString::Empty); typedef std::vector> CommentVector; CommentVector comments; { Wt::Dbo::collection> cmts = comment_->children.find().orderBy("date"); comments.insert(comments.end(), cmts.begin(), cmts.end()); } auto children = std::make_unique(); for (int i = (int)comments.size() - 1; i >= 0; --i) children->addWidget(std::make_unique(session_, comments[i])); bindWidget("children", std::move(children)); } bool CommentView::isNew() const { return comment_.id() == -1; } void CommentView::rm() { Wt::Dbo::Transaction t(session_); comment_.modify()->setDeleted(); renderView(); t.commit(); } void CommentView::reply() { Wt::Dbo::Transaction t(session_); Wt::WContainerWidget *c = resolve("children"); c->insertWidget(0, std::make_unique(session_, comment_.id())); t.commit(); } void CommentView::save() { Wt::Dbo::Transaction t(session_); bool isNew = comment_.id() == -1; Comment *comment = comment_.modify(); comment->setText(editArea_->text()); if (isNew) { session_.add(comment_); comment->date = Wt::WDateTime::currentDateTime(); comment->author = session_.user(); session_.commentsChanged().emit(comment_); } renderView(); t.commit(); } void CommentView::edit() { clear(); Wt::Dbo::Transaction t(session_); setTemplateText(tr("blog-edit-comment")); auto editArea = std::make_unique(); editArea_ = editArea.get(); editArea_->setText(comment_->textSrc()); editArea_->setFocus(); auto save = std::make_unique(tr("save")); save->clicked().connect(this, &CommentView::save); auto cancel = std::make_unique(tr("cancel")); cancel->clicked().connect(this, &CommentView::cancel); bindWidget("area", std::move(editArea)); bindWidget("save", std::move(save)); bindWidget("cancel", std::move(cancel)); t.commit(); }