WebView(pModel model, Wt::WContainerWidget* parent=nullptr) : Wt::WTemplate(parent) { setTemplateText(tr("page-view")); if (model) { bindString("title", model->title); bindString("body", model->body); } }
void CommentsWidget::show_form_() { if (fApp->is_banned()) { bindString("add", tr("facts.common.BannedIp")); } else { bindWidget("add", new CommentAddForm(this)); } }
TopicTemplate::TopicTemplate(const char *trKey) : Wt::WTemplate(tr(trKey)) { setInternalPathEncoding(true); #ifndef WT_TARGET_JAVA setCondition("if:cpp", true); setCondition("if:java", false); bindString("doc-url", "http://www.webtoolkit.eu/wt/doc/reference/html/"); #else setCondition("if:cpp", false); setCondition("if:java", true); bindString("doc-url", "http://www.webtoolkit.eu/" "jwt/latest/doc/javadoc/eu/webtoolkit/jwt/"); #endif }
int TranslationUpdateQuery::bind() { int err = bindPrimaryKey(); if ( !err ) err = bindInt64( ":sid", m_record.sid ); if ( !err ) err = bindString( ":text", m_record.text ); if ( !err ) err = bindInt( ":fmark", m_record.fmark ); if ( !err ) err = bindInt( ":rmark", m_record.rmark ); return err; }
int WordsByPatternQuery::bind() { quint64 min = 0, max = 0; WordKey::getPatternKeyLimits( m_pattern, min, max ); bindUint64( ":min", min ); bindUint64( ":max", max ); bindString( ":pattern", m_pattern + "%" ); return 0; }
void Sqlite3Database::updateNote(const Note ¬e) { auto date_str = pt::to_iso_string(note.reminder()); clearStatement(); stmt_cache_ << "UPDATE notes SET title=?1,content=?2," << "notebook=?3,last_change=datetime('now','localtime')," << "reminder=?4 where (id=?5)"; auto result = prepareStatement(stmt_cache_.str()); bindString(result, 1, note.title()); bindString(result, 2, note.content()); bindInt(result, 3, note.notebook()); bindString(result, 4, date_str); bindInt(result, 5, note.id()); if (isError(executeStep(result))) throw DatabaseException("updating note " + note.title() + " failed"); }
void AuthWidget::createLoggedInView() { setTemplateText(tr("Wt.Auth.template.logged-in")); bindString("user-name", login_.user().identity(Identity::LoginName)); WPushButton *logout = new WPushButton(tr("Wt.Auth.logout")); logout->clicked().connect(this, &AuthWidget::logout); bindWidget("logout", logout); }
// DEMO : sqlite3 example with prepared statement parameters void Sqlite3Database::newNote(Note ¬e) { auto date_str = pt::to_iso_string(note.reminder()); clearStatement(); stmt_cache_ << "INSERT INTO notes(title,content,notebook,reminder) VALUES(" << "?1, ?2, ?3, ?4" << ")"; auto result = prepareStatement(stmt_cache_.str()); bindString(result, 1, note.title()); bindString(result, 2, note.content()); bindInt(result, 3, note.notebook()); bindString(result, 4, date_str); if (isError(executeStep(result))) throw DatabaseException("inserting note " + note.title() + " failed"); // get the generated ID note.id(getLastInsertId()); }
/* **************************************************************************** * Bind Template */ void EditUser::BindTemplate() { bindString("username", target_->name); if (target_->role == User::Admin) { roleButton_->setText(tr("demote-admin")); } else { roleButton_->setText(tr("promote-user")); } } // end void EditUser::BindTemplate
void AuthWidget::updatePasswordLoginView() { if (model_->passwordAuth()) { setCondition("if:passwords", true); updateView(model_); WInteractWidget *login = resolve<WInteractWidget *>("login"); if (!login) { login = new WPushButton(tr("Wt.Auth.login")); login->clicked().connect(this, &AuthWidget::attemptPasswordLogin); bindWidget("login", login); model_->configureThrottling(login); if (model_->baseAuth()->emailVerificationEnabled()) { WText *text = new WText(tr("Wt.Auth.lost-password")); text->clicked().connect(this, &AuthWidget::handleLostPassword); bindWidget("lost-password", text); } else bindEmpty("lost-password"); if (registrationEnabled_) { WInteractWidget *w; if (!basePath_.empty()) { w = new WAnchor (WLink(WLink::InternalPath, basePath_ + "register"), tr("Wt.Auth.register")); } else { w = new WText(tr("Wt.Auth.register")); w->clicked().connect(this, &AuthWidget::registerNewUser); } bindWidget("register", w); } else bindEmpty("register"); if (model_->baseAuth()->emailVerificationEnabled() && registrationEnabled_) bindString("sep", " | "); else bindEmpty("sep"); } model_->updateThrottling(login); } else { bindEmpty("lost-password"); bindEmpty("sep"); bindEmpty("register"); bindEmpty("login"); } }
void WTemplateFormView::updateViewField(WFormModel *model, WFormModel::Field field) { const std::string var = field; if (model->isVisible(field)) { setCondition("if:" + var, true); WWidget *edit = resolveWidget(var); if (!edit) { edit = createFormWidget(field); if (!edit) { LOG_ERROR("updateViewField: createFormWidget('" << field << "') returned 0"); return; } bindWidget(var, edit); } WFormWidget *fedit = dynamic_cast<WFormWidget *>(edit); if (fedit) { if (fedit->validator() != model->validator(field) && model->validator(field)) fedit->setValidator(model->validator(field)); updateViewValue(model, field, fedit); } else updateViewValue(model, field, edit); WText *info = resolve<WText *>(var + "-info"); if (!info) { info = new WText(); bindWidget(var + "-info", info); } bindString(var + "-label", model->label(field)); const WValidator::Result& v = model->validation(field); info->setText(v.message()); indicateValidation(field, model->isValidated(field), info, edit, v); edit->setDisabled(model->isReadOnly(field)); } else { setCondition("if:" + var, false); bindEmpty(var); bindEmpty(var + "-info"); } }
void process() { updateModel(model_); if (model_->validate()) { // Do something with the data in the model: show it. bindString("submit-info", Wt::WString::fromUTF8("Saved user data for ") + model_->userData(), Wt::PlainText); // Udate the view: Delete any validation message in the view, etc. updateView(model_); // Set the focus on the first field in the form. Wt::WLineEdit *viewField = resolve<Wt::WLineEdit*>(UserFormModel::FirstNameField); viewField->setFocus(); } else { bindEmpty("submit-info"); // Delete the previous user data. updateView(model_); } }
CommentsWidget::CommentsWidget(const FactPtr& fact, Wt::WContainerWidget* p): Wt::WTemplate(tr("facts.comment.comments_template"), p), fact_(fact) { dbo::Transaction t(fApp->session()); Q query = fact->comments().find(); query.orderBy("comment_index desc"); CommentsModel* model = new CommentsModel(query, this); view_ = new CommentsView(model); show_button_(); bindWidget("comments", view_); if (fApp->admin() && !fApp->environment().ajax()) { bindWidget("save", new Wt::WPushButton(tr("facts.admin.Save"))); } else { bindString("save", ""); } int index = fApp->comment_index(fact); if (index != -1) { view_->goto_index(index); } t.commit(); }
unique_ptr<SqlStatement> CommonClause::createStatementAndBindValuesToPlaceholders(SqliteDatabase* db, const string& sql) const { auto stmt = db->prepStatement(sql); if (stmt == nullptr) return nullptr; // bind the actual column values to the placeholders int curPlaceholderIdx = 1; // leftmost placeholder is at position 1 for (int i=0; i < colVals.size(); ++i) { const ColValInfo& curCol = colVals[i]; // NULL or NOT NULL has to handled directly as literal value when // creating the sql statement's text if ((curCol.type == ColValType::Null) || (curCol.type == ColValType::NotNull)) continue; switch (curCol.type) { case ColValType::Int: stmt->bindInt(curPlaceholderIdx, intVals[curCol.indexInList]); break; case ColValType::Double: stmt->bindDouble(curPlaceholderIdx, doubleVals[curCol.indexInList]); break; case ColValType::String: stmt->bindString(curPlaceholderIdx, stringVals[curCol.indexInList]); break; default: throw std::runtime_error("unhandled argument type when binding argument values to statement"); } ++curPlaceholderIdx; } return stmt; }
void RegistrationWidget::update() { if (model_->passwordAuth()) bindString("password-description", tr("Wt.Auth.password-registration")); else bindEmpty("password-description"); updateView(model_); if (!created_) { WLineEdit *password = resolve<WLineEdit *> (RegistrationModel::ChoosePasswordField); WLineEdit *password2 = resolve<WLineEdit *> (RegistrationModel::RepeatPasswordField); WText *password2Info = resolve<WText *> (RegistrationModel::RepeatPasswordField + std::string("-info")); if (password && password2 && password2Info) model_->validatePasswordsMatchJS(password, password2, password2Info); } WAnchor *isYou = resolve<WAnchor *>("confirm-is-you"); if (!isYou) { isYou = new WAnchor(std::string("#"), tr("Wt.Auth.confirm-is-you")); isYou->hide(); bindWidget("confirm-is-you", isYou); } if (model_->isConfirmUserButtonVisible()) { if (!isYou->clicked().isConnected()) isYou->clicked().connect(this, &RegistrationWidget::confirmIsYou); isYou->show(); } else isYou->hide(); if (model_->isFederatedLoginVisible()) { if (!conditionValue("if:oauth")) { setCondition("if:oauth", true); if (model_->passwordAuth()) bindString("oauth-description", tr("Wt.Auth.or-oauth-registration")); else bindString("oauth-description", tr("Wt.Auth.oauth-registration")); WContainerWidget *icons = new WContainerWidget(); icons->addStyleClass("Wt-field"); for (unsigned i = 0; i < model_->oAuth().size(); ++i) { const OAuthService *service = model_->oAuth()[i]; WImage *w = new WImage("css/oauth-" + service->name() + ".png", icons); w->setToolTip(service->description()); w->setStyleClass("Wt-auth-icon"); w->setVerticalAlignment(AlignMiddle); OAuthProcess *const process = service->createProcess(service->authenticationScope()); w->clicked().connect(process, &OAuthProcess::startAuthenticate); process->authenticated().connect (boost::bind(&RegistrationWidget::oAuthDone, this, process, _1)); WObject::addChild(process); } bindWidget("icons", icons); } } else { setCondition("if:oauth", false); bindEmpty("icons"); } if (!created_) { WPushButton *okButton = new WPushButton(tr("Wt.Auth.register")); WPushButton *cancelButton = new WPushButton(tr("Wt.WMessageBox.Cancel")); bindWidget("ok-button", okButton); bindWidget("cancel-button", cancelButton); okButton->clicked().connect(this, &RegistrationWidget::doRegister); cancelButton->clicked().connect(this, &RegistrationWidget::close); created_ = true; } }
void WTemplate::bindInt(const std::string& varName, int value) { bindString(varName, boost::lexical_cast<std::string>(value), XHTMLUnsafeText); }
void setStatusText(const WString& newMessage) { bindString("status-text", newMessage); }
int WordUpdateQuery::bind() { int err = bindPrimaryKey(); if ( !err ) err = bindString( ":text", m_record.text ); return err; }
// inline constructor UserFormView() { model_ = new UserFormModel(this); setTemplateText(tr("userForm-template")); addFunction("id", &WTemplate::Functions::id); /* * First Name */ setFormWidget(UserFormModel::FirstNameField, new Wt::WLineEdit()); /* * Last Name */ setFormWidget(UserFormModel::LastNameField, new Wt::WLineEdit()); /* * Country */ Wt::WComboBox *countryCB = new Wt::WComboBox(); countryCB->setModel(model_->countryModel()); countryCB->activated().connect(std::bind([=] () { std::string code = model_->countryCode(countryCB->currentIndex()); model_->updateCityModel(code); })); setFormWidget(UserFormModel::CountryField, countryCB, [=] () { // updateViewValue() std::string code = boost::any_cast<std::string> (model_->value(UserFormModel::CountryField)); int row = model_->countryModelRow(code); countryCB->setCurrentIndex(row); }, [=] () { // updateModelValue() std::string code = model_->countryCode(countryCB->currentIndex()); model_->setValue(UserFormModel::CountryField, code); }); /* * City */ Wt::WComboBox *cityCB = new Wt::WComboBox(); cityCB->setModel(model_->cityModel()); setFormWidget(UserFormModel::CityField, cityCB); /* * Birth Date */ Wt::WLineEdit *dateEdit = new Wt::WLineEdit(); Wt::WDatePicker *birthDP = new Wt::WDatePicker(dateEdit); bindWidget("birth-dp", birthDP); setFormWidget(UserFormModel::BirthField, dateEdit, [=] () { // updateViewValue() Wt::WDate date = boost::any_cast<Wt::WDate> (model_->value(UserFormModel::BirthField)); birthDP->setDate(date); }, [=] () { // updateModelValue() Wt::WDate date = birthDP->date(); model_->setValue(UserFormModel::BirthField, date); }); /* * Children */ setFormWidget(UserFormModel::ChildrenField, new Wt::WSpinBox()); /* * Remarks */ Wt::WTextArea *remarksTA = new Wt::WTextArea(); remarksTA->setColumns(40); remarksTA->setRows(5); setFormWidget(UserFormModel::RemarksField, remarksTA); /* * Title & Buttons */ Wt::WString title = Wt::WString("Create new user"); bindString("title", title); Wt::WPushButton *button = new Wt::WPushButton("Save"); bindWidget("submit-button", button); bindString("submit-info", Wt::WString()); button->clicked().connect(this, &UserFormView::process); updateView(model_); }
// inline constructor UserFormView() { model = std::make_shared<UserFormModel>(); setTemplateText(tr("userForm-template")); addFunction("id", &WTemplate::Functions::id); addFunction("block", &WTemplate::Functions::id); /* * First Name */ setFormWidget(UserFormModel::FirstNameField, Wt::cpp14::make_unique<Wt::WLineEdit>()); /* * Last Name */ setFormWidget(UserFormModel::LastNameField, Wt::cpp14::make_unique<Wt::WLineEdit>()); /* * Country */ auto countryCB = Wt::cpp14::make_unique<Wt::WComboBox>(); auto countryCB_ = countryCB.get(); countryCB->setModel(model->countryModel()); countryCB_->activated().connect([=] { std::string code = model->countryCode(countryCB_->currentIndex()); model->updateCityModel(code); }); setFormWidget(UserFormModel::CountryField, std::move(countryCB), [=] { // updateViewValue() std::string code = Wt::asString(model->value(UserFormModel::CountryField)).toUTF8(); int row = model->countryModelRow(code); countryCB_->setCurrentIndex(row); }, [=] { // updateModelValue() std::string code = model->countryCode(countryCB_->currentIndex()); model->setValue(UserFormModel::CountryField, code); }); /* * City */ auto cityCB = Wt::cpp14::make_unique<Wt::WComboBox>(); cityCB->setModel(model->cityModel()); setFormWidget(UserFormModel::CityField, std::move(cityCB)); /* * Birth Date */ auto dateEdit = Wt::cpp14::make_unique<Wt::WDateEdit>(); auto dateEdit_ = dateEdit.get(); setFormWidget(UserFormModel::BirthField, std::move(dateEdit), [=] { // updateViewValue() Wt::WDate date = Wt::cpp17::any_cast<Wt::WDate> (model->value(UserFormModel::BirthField)); dateEdit_->setDate(date); }, [=] { // updateModelValue() Wt::WDate date = dateEdit_->date(); model->setValue(UserFormModel::BirthField, date); }); /* * Children */ setFormWidget(UserFormModel::ChildrenField, Wt::cpp14::make_unique<Wt::WSpinBox>()); /* * Remarks */ auto remarksTA = Wt::cpp14::make_unique<Wt::WTextArea>(); remarksTA->setColumns(40); remarksTA->setRows(5); setFormWidget(UserFormModel::RemarksField, std::move(remarksTA)); /* * Title & Buttons */ Wt::WString title = Wt::WString("Create new user"); bindString("title", title); auto button = Wt::cpp14::make_unique<Wt::WPushButton>("Save"); auto button_ = bindWidget("submit-button", std::move(button)); bindString("submit-info", Wt::WString()); button_->clicked().connect(this, &UserFormView::process); updateView(model.get()); }
int TranslationsByPatternQuery::bind() { QString pattern = SqlGenerator::createPattern( m_pattern, m_match ); return bindString( ":pattern", pattern ); }
bool SqliteDatabase::copyTable(const string& srcTabName, const string& dstTabName, int* errCodeOut, bool copyStructureOnly) { // ensure validity of parameters if (srcTabName.empty()) return false; if (dstTabName.empty()) return false; if (!(hasTable(srcTabName))) return false; if (hasTable(dstTabName)) return false; // retrieve the CREATE TABLE statement that describes the source table's structure int err; auto stmt = prepStatement("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", &err); if (errCodeOut != nullptr) *errCodeOut = err; if (err != SQLITE_OK) return false; stmt->bindString(1, srcTabName); auto _sqlCreate = execScalarQueryString(stmt, &err); if (errCodeOut != nullptr) *errCodeOut = err; if (_sqlCreate == nullptr) return false; if (err != SQLITE_DONE) return false; if (_sqlCreate->isNull()) return false; string sqlCreate = _sqlCreate->get(); // Modify the string to be applicable to the new database name // // The string starts with "CREATE TABLE srcName (id ...". We search // for the opening parenthesis and replace everthing before that // character with "CREATE TABLE dstName ". auto posParenthesis = sqlCreate.find_first_of("("); if (posParenthesis == string::npos) return false; sqlCreate.replace(0, posParenthesis-1, "CREATE TABLE " + dstTabName); // before we create the new table and copy the contents, we // explicitly start a transaction to be able to restore the // original database state in case of errors auto tr = startTransaction(TRANSACTION_TYPE::IMMEDIATE, TRANSACTION_DESTRUCTOR_ACTION::ROLLBACK, &err); if (tr == nullptr) return false; if (err != SQLITE_DONE) return false; // actually create the new table bool isSuccess = execNonQuery(sqlCreate, &err); if (errCodeOut != nullptr) *errCodeOut = err; if (!isSuccess) { tr->rollback(); return false; } // if we're only requested to copy the schema, skip // the conent copying if (!copyStructureOnly) { string sqlCopy = "INSERT INTO " + dstTabName + " SELECT * FROM " + srcTabName; stmt = prepStatement(sqlCopy, &err); if (errCodeOut != nullptr) *errCodeOut = err; if (err != SQLITE_OK) return false; isSuccess = execNonQuery(stmt, &err); if (errCodeOut != nullptr) *errCodeOut = err; if (!isSuccess) { tr->rollback(); return false; } } // we're done. Commit all changes at once tr->commit(&err); if (errCodeOut != nullptr) *errCodeOut = err; return (err == SQLITE_DONE); }