gradido/src/cpsp/adminTopic.cpsp

228 lines
9.2 KiB
Plaintext

<%@ page class="AdminTopicPage" %>
<%@ page form="true" %>
<%@ page compressed="true" %>
<%@ page baseClass="SessionHTTPRequestHandler" %>
<%@ page ctorArg="Session*" %>
<%@ header include="SessionHTTPRequestHandler.h" %>
<%!
#include "../controller/HederaAccount.h"
#include "../controller/HederaTopic.h"
#include "../controller/Group.h"
#include "../SingletonManager/SessionManager.h"
#include "../ServerConfig.h"
#include "../lib/DataTypeConverter.h"
#include "../lib/Profiler.h"
#include "../lib/Success.h"
#include "Poco/Timespan.h"
#include "Poco/URI.h"
%>
<%%
const char* pageName = "Topic";
auto user = mSession->getNewUser();
auto sm = SessionManager::getInstance();
Profiler hedera_time;
std::string name = "";
int auto_renew_account = 0;
int auto_renew_period = 604800; // 7 Tage
int group_id = 0;
Poco::URI uri(request.getURI());
auto uri_query = uri.getQueryParameters();
std::string action = "";
Poco::AutoPtr<controller::HederaTopic> query_hedera_topic;
// parsing get query params
if(uri_query.size() >= 2) {
if(uri_query[0].first == "action") {
action = uri_query[0].second;
}
if(uri_query[1].first == "topic_id") {
std::string topic_id_from_query;
int topic_id = 0;
topic_id_from_query = uri_query[1].second;
if(DataTypeConverter::strToInt(topic_id_from_query, topic_id) != DataTypeConverter::NUMBER_PARSE_OKAY) {
addError(new Error("Int Convert Error", "Error converting topic_id_from_query to int"));
} else {
auto hedera_topic = controller::HederaTopic::load(topic_id);
if(hedera_topic.isNull()) {
addError(new Error("Action", "hedera topic not found"));
} else {
query_hedera_topic = hedera_topic;
}
}
}
}
// actions
if(!query_hedera_topic.isNull())
{
if(action == "getTopicInfos")
{
hedera_time.reset();
if(query_hedera_topic->updateWithGetTopicInfos(user)) {
addNotification(new ParamSuccess("Hedera", "hedera get topic infos success in ", hedera_time.string()));
} else {
addError(new ParamError("Hedera", "hedera get topic infos failed in ", hedera_time.string()));
}
getErrors(query_hedera_topic);
}
}
else if(!form.empty())
{
name = form.get("topic-name", "");
auto auto_renew_account_string = form.get("topic-auto-renew-account", "0");
auto auto_renew_period_string = form.get("topic-auto-renew-period", "604800");
auto group_id_string = form.get("topic-group", "-1");
if(name != "" && !sm->isValid(name, VALIDATE_NAME)) {
addError(new Error("Topic", "Name not valid, at least 3 Character"));
}
if(!sm->isValid(auto_renew_account_string, VALIDATE_ONLY_INTEGER)) {
addError(new Error("Topic", "auto renew account id not an integer"));
} else {
if(DataTypeConverter::strToInt(auto_renew_account_string, auto_renew_account) != DataTypeConverter::NUMBER_PARSE_OKAY) {
addError(new Error("Int convert error", "Error converting auto renew account id to int"));
}
}
if(!sm->isValid(auto_renew_period_string, VALIDATE_ONLY_INTEGER)) {
addError(new Error("Topic", "auto renew period not an integer"));
} else {
if(DataTypeConverter::strToInt(auto_renew_period_string, auto_renew_period) != DataTypeConverter::NUMBER_PARSE_OKAY) {
addError(new Error("Int convert error", "Error converting auto renew period to int"));
}
}
if(!sm->isValid(group_id_string, VALIDATE_ONLY_INTEGER)) {
addError(new Error("Topic", "group_id not an integer"));
} else {
if(DataTypeConverter::strToInt(group_id_string, group_id) != DataTypeConverter::NUMBER_PARSE_OKAY) {
addError(new Error("Int convert error", "Error converting group_id to int"));
}
}
}
auto hedera_accounts = controller::HederaAccount::load("user_id", user->getModel()->getID());
//std::vector<Poco::AutoPtr<controller::HederaAccount>> hedera_accounts;
auto groups = controller::Group::listAll();
std::map<int, int> group_indices;
int count = 0;
for(auto it = groups.begin(); it != groups.end(); it++) {
group_indices.insert(std::pair<int, int>((*it)->getModel()->getID(), count));
count++;
}
auto hedera_topics = controller::HederaTopic::listAll();
//std::vector<Poco::AutoPtr<controller::HederaTopic>> hedera_topics;
%><%@ include file="header_large.cpsp" %>
<style type="text/css">
.center-form-form .input-40 {
margin-left:0;
width:40%;
display:inline-block;
}
</style>
<%= getErrorsHtml() %>
<div class="content-container info-container">
<h1>Topic Admin Page</h1>
</div>
<div class="center-form-container">
<div class="content-list">
<div class="content-list-title">
<h2>Hedera Topics</h2>
</div>
<div class="content-list-table">
<div class="row">
<div class="cell header-cell c2">Topic ID</div>
<div class="cell header-cell c3">Name</div>
<div class="cell header-cell c3">Network Type</div>
<div class="cell header-cell c4">Auto Renew Account Balance</div>
<div class="cell header-cell c4">Auto Renew Period</div>
<div class="cell header-cell c3">Group ID</div>
<div class="cell header-cell c3">Current Timeout</div>
<div class="cell header-cell c2">Sequence Number</div>
<div class="cell header-cell c3" title="Last Time Get update from Hedera">Last Updated</div>
<div class="cell header-cell c5">Aktionen</div>
</div>
<% for(auto it = hedera_topics.begin(); it != hedera_topics.end(); it++) {
auto hedera_topic_model = (*it)->getModel();
auto updateUrl = ServerConfig::g_serverPath + "/topic?action=getTopicInfos&topic_id=" + std::to_string(hedera_topic_model->getID());
auto auto_renew_account = (*it)->getAutoRenewAccount();
auto renew_account_model = auto_renew_account->getModel();
std::string timeout_color = "success-color";
if(hedera_topic_model->getCurrentTimeout() < Poco::DateTime()) {
timeout_color = "alert-color";
} else if((hedera_topic_model->getCurrentTimeout() - Poco::DateTime()) < Poco::Timespan(2,0,0,0,0)) {
timeout_color = "orange-color";
}
%>
<div class="row">
<div class="cell c2"><%= (*it)->getTopicHederaId()->getModel()->toString() %></div>
<div class="cell c3"><%= hedera_topic_model->getName() %></div>
<div class="cell c3"><%= model::table::HederaAccount::hederaNetworkTypeToString(renew_account_model->getNetworkType()) %></div>
<div class="cell c4"><%= renew_account_model->getBalanceString() %></div>
<div class="cell c4"><%= hedera_topic_model->getAutoRenewPeriodString() %></div>
<div class="cell c3"><%= hedera_topic_model->getGroupId() %></div>
<div class="cell c3 <%= timeout_color %>"><%= hedera_topic_model->getCurrentTimeoutString() %></div>
<div class="cell c2"><%= hedera_topic_model->getSequenceNumber() %></div>
<div class="cell c3"><%= hedera_topic_model->getUpdatedString() %></div>
<div class="cell c5">
<button class="form-button" title="Query on Hedera, cost some fees" onclick="window.location.href='<%= updateUrl %>'" >
get topic infos
</button>
</div>
</div>
<% } %>
</div>
</div>
<div class="center-form-title">
<h3>Ein neues Topic anlegen</h3>
</div>
<div class="center-form-form">
<form method="POST" action="<%= ServerConfig::g_serverPath %>/topic">
<label class="form-label" for="topic-name">Name</label>
<input type="text" class="form-control" id="topic-name" name="topic-name" value="<%= name %>">
<label class="form-label" for="topic-auto-renew-account">Auto Renew Hedera Account</label>
<select name="topic-auto-renew-account" id="topic-auto-renew-account">
<% for(auto it = hedera_accounts.begin(); it != hedera_accounts.end(); it++) {
auto model = (*it)->getModel();
%>
<option title="<%= model->toString() %>" value="<%= model->getID() %>" <% if(auto_renew_account == model->getID()) {%>selected="selected"<% } %>><%= (*it)->toShortSelectOptionName() %></option>
<% } %>
</select>
<label class="form-label" for="topic-auto-renew-period">Auto Renew Period in seconds</label>
<div><input class="form-control input-40" id="topic-auto-renew-period" value="<%= auto_renew_period %>" type="number" name="topic-auto-renew-period"/><span style="margin-left:8px" id="readable-auto-renew-period"></span><div>
<label class="form-label" for="topic-group">Group</label>
<select class="form-control" name="topic-group" id="topic-group">
<option value="-1">Keine Gruppe</option>
<% for(auto it = groups.begin(); it != groups.end(); it++) {
auto group_model = (*it)->getModel(); %>
<option title="<%= group_model->getDescription() %>" value="<%= group_model->getID() %>" <% if(group_id == group_model->getID()) {%>selected="selected"<% } %>><%= group_model->getName() %></option>
<% } %>
</select>
<input class="center-form-submit form-button" type="submit" name="submit" value="<%= gettext("Add Topic") %>">
</form>
</div>
</div>
<%@ include file="footer.cpsp" %>
<script type="text/javascript" src="<%= ServerConfig::g_php_serverPath %>/js/time_calculations.js"></script>
<script type="text/javascript">
var input = document.getElementById("topic-auto-renew-period");
var span = document.getElementById("readable-auto-renew-period");
span.innerHTML = '~ ' + getExactTimeDuration(input.value);
input.addEventListener('keyup', function(e) {
span.innerHTML = '~ ' + getExactTimeDuration(input.value);
});
</script>