Older/WebApplication/view/BlogView.cpp

116 lines
4.5 KiB
C++
Raw Normal View History

2024-11-01 19:05:20 +08:00
#include "BlogView.h"
#include "EditUsers.h"
#include "model/BlogSession.h"
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WText.h>
class BlogImpl : public Wt::WContainerWidget {
public:
BlogImpl(const std::string &basePath, Wt::Dbo::SqlConnectionPool &connectionPool, const std::string &rssFeedUrl,
BlogView *blogView)
: m_basePath(basePath), m_rssFeedUrl(rssFeedUrl), m_session(connectionPool) {
Wt::WApplication *app = Wt::WApplication::instance();
app->messageResourceBundle().use(Wt::WApplication::appRoot() + "blog");
app->useStyleSheet("/css/blog.css");
app->useStyleSheet("/css/asciidoc.css");
app->internalPathChanged().connect(this, &BlogImpl::handlePathChange);
m_loginStatus = this->addWidget(std::make_unique<Wt::WTemplate>(tr("blog-login-status")));
m_panel = this->addWidget(std::make_unique<Wt::WStackedWidget>());
m_items = this->addWidget(std::make_unique<Wt::WContainerWidget>());
m_session.login().changed().connect(this, &BlogImpl::onUserChanged);
auto loginWidget = std::make_unique<BlogLoginWidget>(m_session, basePath);
m_loginWidget = loginWidget.get();
m_loginWidget->hide();
auto loginLink = std::make_unique<Wt::WText>(tr("login"));
auto lPtr = loginLink.get();
loginLink->setStyleClass("link");
loginLink->clicked().connect(loginWidget_, &WWidget::show);
loginLink->clicked().connect(lPtr, &WWidget::hide);
auto registerLink = std::make_unique<Wt::WText>(tr("Wt.Auth.register"));
registerLink->setStyleClass("link");
registerLink->clicked().connect(loginWidget_, &BlogLoginWidget::registerNewUser);
auto archiveLink =
std::make_unique<Wt::WAnchor>(Wt::WLink(Wt::LinkType::InternalPath, basePath_ + "all"), tr("archive"));
loginStatus_->bindWidget("login", std::move(loginWidget));
loginStatus_->bindWidget("login-link", std::move(loginLink));
loginStatus_->bindWidget("register-link", std::move(registerLink));
loginStatus_->bindString("feed-url", rssFeedUrl_);
loginStatus_->bindWidget("archive-link", std::move(archiveLink));
onUserChanged();
loginWidget_->processEnvironment();
}
protected:
void handlePathChange(const std::string &) {
Wt::WApplication *app = Wt::WApplication::instance();
if (app->internalPathMatches(basePath_)) {
dbo::Transaction t(session_);
std::string path = app->internalPathNextPart(basePath_);
items_->clear();
if (users_) {
users_ = 0;
}
if (path.empty())
showPosts(session_
.find<Post>("where state = ? "
"order by date desc "
"limit 10")
.bind(Post::Published),
items_);
else if (path == "author") {
std::string author = app->internalPathNextPart(basePath_ + path + '/');
dbo::ptr<User> user = findUser(author);
if (user)
showPosts(user);
else
showError(tr("blog-no-author").arg(author));
} else if (path == "edituser") {
editUser(app->internalPathNextPart(basePath_ + path + '/'));
} else if (path == "all") {
showArchive(items_);
} else {
std::string remainder = app->internalPath().substr(basePath_.length());
showPostsByDateTopic(remainder, items_);
}
t.commit();
}
}
private:
std::string m_basePath, m_rssFeedUrl;
BlogSession m_session; BlogLoginWidget *m_loginWidget=nullptr;
Wt::WStackedWidget *m_panel = nullptr;
Wt::WTemplate *m_authorPanel = nullptr;
EditUsers *m_users = nullptr;
EditUser *m_userEditor = nullptr;
Wt::WTemplate *m_mustLoginWarning = nullptr;
Wt::WTemplate *m_mustBeAdministratorWarning = nullptr;
Wt::WTemplate *m_invalidUser = nullptr;
Wt::WTemplate *m_loginStatus = nullptr;
WContainerWidget *m_items = nullptr;
};
BlogView::BlogView(const std::string &basePath, Wt::Dbo::SqlConnectionPool &db, const std::string &rssFeedUrl)
: WCompositeWidget(), m_userChanged() {
m_impl = setImplementation(std::make_unique<BlogImpl>(basePath, db, rssFeedUrl, this));
}