2021-10-31 23:11:48 +01:00
|
|
|
#include "admin_panel.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"
|
|
|
|
|
2022-01-07 16:36:39 +01:00
|
|
|
#include "admin_node.h"
|
2021-11-01 00:39:39 +01:00
|
|
|
|
2021-10-31 23:11:48 +01:00
|
|
|
void AdminPanel::handle_request_main(Request *request) {
|
2021-11-01 00:39:39 +01:00
|
|
|
//todo check permissions
|
|
|
|
|
|
|
|
String seg = request->get_current_path_segment();
|
|
|
|
|
|
|
|
if (seg == "") {
|
|
|
|
render_admin_panel_list(request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < _controllers.size(); ++i) {
|
|
|
|
AdminPanelSection &s = _controllers[i];
|
|
|
|
|
|
|
|
if (s.section_url == seg) {
|
2022-01-07 16:36:39 +01:00
|
|
|
AdminNode *c = s.controller;
|
2021-11-01 00:39:39 +01:00
|
|
|
|
|
|
|
request->push_path();
|
|
|
|
|
|
|
|
if (c->admin_full_render()) {
|
|
|
|
c->admin_handle_request_main(request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-14 22:11:35 +01:00
|
|
|
render_headers(request);
|
2021-11-14 22:25:19 +01:00
|
|
|
render_segment_body_top(request);
|
2021-11-01 00:39:39 +01:00
|
|
|
render_controller_panel(request, c);
|
2021-11-14 22:11:35 +01:00
|
|
|
render_footer(request);
|
2021-11-01 00:39:39 +01:00
|
|
|
|
|
|
|
//request->pop_path();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request->send_error(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdminPanel::render_admin_panel_list(Request *request) {
|
2021-11-14 22:11:35 +01:00
|
|
|
render_headers(request);
|
2021-11-14 22:25:19 +01:00
|
|
|
render_main_body_top(request);
|
2021-11-01 00:39:39 +01:00
|
|
|
|
|
|
|
String rootlink = request->get_url_root();
|
|
|
|
|
|
|
|
HTMLBuilder b;
|
|
|
|
|
|
|
|
Vector<AdminSectionLinkInfo> links;
|
|
|
|
|
|
|
|
b.div()->cls("content");
|
|
|
|
|
|
|
|
for (int i = 0; i < _controllers.size(); ++i) {
|
|
|
|
b.div()->cls("section");
|
|
|
|
|
|
|
|
AdminPanelSection &s = _controllers[i];
|
|
|
|
|
2021-11-01 21:21:03 +01:00
|
|
|
b.div()->cls("section_header");
|
|
|
|
b.w(s.controller->admin_get_section_name());
|
|
|
|
b.cdiv();
|
|
|
|
|
2021-11-01 00:39:39 +01:00
|
|
|
s.controller->admin_add_section_links(&links);
|
|
|
|
|
|
|
|
for (int j = 0; j < links.size(); ++j) {
|
|
|
|
AdminSectionLinkInfo &li = links[j];
|
|
|
|
|
2021-11-14 22:33:26 +01:00
|
|
|
b.div()->cls("section_entry");
|
2021-11-01 01:47:43 +01:00
|
|
|
b.a()->href(rootlink + s.section_url + "/" + li.link);
|
2021-11-01 00:39:39 +01:00
|
|
|
b.w(li.name);
|
|
|
|
b.ca();
|
2021-11-14 22:33:26 +01:00
|
|
|
b.cdiv();
|
2021-11-01 00:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
links.clear();
|
|
|
|
|
|
|
|
b.cdiv();
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cdiv();
|
|
|
|
|
2021-11-14 22:11:35 +01:00
|
|
|
render_footer(request);
|
2021-11-01 00:39:39 +01:00
|
|
|
|
|
|
|
request->body += b.result;
|
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:36:39 +01:00
|
|
|
void AdminPanel::render_controller_panel(Request *request, AdminNode *controller) {
|
2021-11-01 00:39:39 +01:00
|
|
|
//set up headers
|
|
|
|
controller->admin_handle_request_main(request);
|
|
|
|
//set up footers
|
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:36:39 +01:00
|
|
|
void AdminPanel::register_admin_controller(const String §ion, AdminNode *controller) {
|
2021-11-01 00:39:39 +01:00
|
|
|
AdminPanelSection sec;
|
|
|
|
|
|
|
|
sec.section_url = section;
|
|
|
|
sec.controller = controller;
|
|
|
|
|
|
|
|
_controllers.push_back(sec);
|
2021-10-31 23:11:48 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 00:39:39 +01:00
|
|
|
void AdminPanel::clear() {
|
|
|
|
_controllers.clear();
|
2021-10-31 23:11:48 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 22:11:35 +01:00
|
|
|
void AdminPanel::render_headers(Request *request) {
|
|
|
|
request->head += _default_headers;
|
2021-11-14 22:25:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminPanel::render_main_body_top(Request *request) {
|
|
|
|
request->body += _default_main_body_top;
|
|
|
|
}
|
2021-11-14 22:17:21 +01:00
|
|
|
|
2021-11-14 22:25:19 +01:00
|
|
|
void AdminPanel::render_segment_body_top(Request *request) {
|
|
|
|
request->body += _default_segment_body_top;
|
2021-11-14 22:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdminPanel::render_footer(Request *request) {
|
|
|
|
request->body += _default_footer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdminPanel::set_default_header(const String &val) {
|
|
|
|
_default_headers = val;
|
|
|
|
}
|
2021-11-14 22:25:19 +01:00
|
|
|
void AdminPanel::set_default_main_body_top(const String &val) {
|
|
|
|
_default_main_body_top = val;
|
2021-11-14 22:17:21 +01:00
|
|
|
}
|
2021-11-14 22:25:19 +01:00
|
|
|
void AdminPanel::set_default_segment_body_top(const String &val) {
|
|
|
|
_default_segment_body_top = val;
|
|
|
|
}
|
|
|
|
|
2021-11-14 22:11:35 +01:00
|
|
|
void AdminPanel::set_default_footer(const String &val) {
|
|
|
|
_default_footer = val;
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:11:48 +01:00
|
|
|
AdminPanel *AdminPanel::get_singleton() {
|
|
|
|
return _self;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdminPanel::AdminPanel() :
|
2022-01-07 16:36:39 +01:00
|
|
|
WebNode() {
|
2021-10-31 23:11:48 +01:00
|
|
|
|
|
|
|
if (_self) {
|
|
|
|
printf("AdminPanel::AdminPanel(): Error! self is not null!/n");
|
|
|
|
}
|
|
|
|
|
|
|
|
_self = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdminPanel::~AdminPanel() {
|
|
|
|
if (_self == this) {
|
|
|
|
_self = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AdminPanel *AdminPanel::_self = nullptr;
|