mourne_rcpp_fw/app/buildings/building_controller.cpp

120 lines
3.1 KiB
C++

#include "building_controller.h"
#include "core/html/form_validator.h"
#include "core/html/html_builder.h"
#include "core/http/cookie.h"
#include "core/http/http_session.h"
#include "core/http/request.h"
#include "core/http/session_manager.h"
#include "building_model.h"
void BuildingController::handle_request_default(Request *request) {
}
void BuildingController::admin_handle_request_main(Request *request) {
String seg = request->get_current_path_segment();
if (seg == "") {
admin_render_building_list(request);
return;
} else if (seg == "new") {
admin_render_building(request, Ref<Building>());
return;
}
request->send_error(404);
}
String BuildingController::admin_get_section_name() {
return "Buildings";
}
void BuildingController::admin_add_section_links(Vector<AdminSectionLinkInfo> *links) {
links->push_back(AdminSectionLinkInfo("- Building Editor", ""));
}
bool BuildingController::admin_full_render() {
return false;
}
void BuildingController::admin_render_building_list(Request *request) {
Vector<Ref<Building> > buildings = BuildingModel::get_singleton()->get_all();
HTMLBuilder b;
b.div()->cls("back")->f()->a()->href(request->get_url_root_parent())->f()->w("<--- Back")->ca()->cdiv();
b.br();
b.div()->cls("top_menu")->f()->w("Building Editor")->cdiv();
b.br();
b.div()->cls("top_menu")->f()->a()->href(request->get_url_root("new"))->f()->w("Create New")->ca()->cdiv();
b.br();
b.div()->cls("list_container");
for (int i = 0; i < buildings.size(); ++i) {
Ref<Building> building = buildings[i];
if (!building.is_valid()) {
continue;
}
if (i % 2 == 0) {
b.div()->cls("row");
} else {
b.div()->cls("row second");
}
{
b.div()->cls("attr_box")->f()->w(String::num(building->id))->cdiv();
b.div()->cls("attr_box")->f()->w(String::num(building->rank))->cdiv();
b.div()->cls("attr_box")->f()->w(String::num(building->next_rank))->cdiv();
b.div()->cls("name")->f()->w(building->name)->cdiv();
b.div()->cls("actionbox")->f()->a()->href(request->get_url_root("edit/" + String::num(building->id)))->f()->w("Edit")->ca()->cdiv();
}
b.cdiv();
}
b.cdiv();
request->body += b.result;
}
void BuildingController::admin_render_building(Request *request, Ref<Building> building) {
Vector<Ref<Building> > buildings = BuildingModel::get_singleton()->get_all();
HTMLBuilder b;
b.div("back")->f()->fa(request->get_url_root_parent(), "<--- Back")->cdiv();
b.br();
b.fdiv("top_menu", "Building Editor");
b.br();
b.div("top_menu")->f()->fa(request->get_url_root("new"), "Create New")->cdiv();
b.br();
}
void BuildingController::migrate() {
BuildingModel::get_singleton()->migrate();
}
void BuildingController::add_default_data() {
BuildingModel::get_singleton()->add_default_data();
}
BuildingController *BuildingController::get_singleton() {
return _self;
}
BuildingController::BuildingController() :
AdminController() {
if (_self) {
printf("BuildingController::BuildingController(): Error! self is not null!/n");
}
_self = this;
}
BuildingController::~BuildingController() {
if (_self == this) {
_self = nullptr;
}
}
BuildingController *BuildingController::_self = nullptr;