| •1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include "DataModel/Counter.h" |
| 24 |
#include "Utils/Utils.h" |
| 25 |
#include "Server/Web/HtmlTag.h" |
| 26 |
#include "Server/Web/HtmlTagButtonCancel.h" |
| 27 |
#include "Server/Web/HtmlTagButtonCommandWide.h" |
| 28 |
#include "Server/Web/HtmlTagButtonOK.h" |
| 29 |
#include "Server/Web/HtmlTagButtonPopupWide.h" |
| 30 |
#include "Server/Web/HtmlTagInputHidden.h" |
| 31 |
#include "Server/Web/HtmlTagInputIntegerWithLabel.h" |
| 32 |
#include "Server/Web/HtmlTagInputTextWithLabel.h" |
| 33 |
#include "Server/Web/HtmlTagCounter.h" |
| 34 |
#include "Server/Web/WebClient.h" |
| 35 |
#include "Server/Web/WebClientCounter.h" |
| 36 |
|
| 37 |
using namespace DataModel; |
| 38 |
using LayoutPosition = DataModel::LayoutItem::LayoutPosition; |
| 39 |
using LayoutItemSize = DataModel::LayoutItem::LayoutItemSize; |
| 40 |
using LayoutRotation = DataModel::LayoutItem::LayoutRotation; |
| 41 |
using std::map; |
| 42 |
using std::string; |
| 43 |
using std::to_string; |
| 44 |
using std::vector; |
| 45 |
|
| 46 |
namespace Server { namespace Web |
| 47 |
{ |
| 48 |
void WebClientCounter::HandleCounterEdit(const map<string, string>& arguments) |
| 49 |
{ |
| 50 |
HtmlTag content; |
| 51 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone); |
| 52 |
string name = Languages::GetText(Languages::TextNew); |
| 53 |
int max = Utils::Utils::GetIntegerMapEntry(arguments, "max", 1); |
| 54 |
int min = Utils::Utils::GetIntegerMapEntry(arguments, "min", 0); |
| 55 |
LayoutPosition posx = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0); |
| 56 |
LayoutPosition posy = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0); |
| 57 |
LayoutPosition posz = Utils::Utils::GetIntegerMapEntry(arguments, "posz", LayerUndeletable); |
| 58 |
LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0); |
| 59 |
|
| 60 |
if (counterID > CounterNone) |
| 61 |
{ |
| 62 |
const DataModel::Counter* counter = manager.GetCounter(counterID); |
| 63 |
if (counter != nullptr) |
| 64 |
{ |
| 65 |
name = counter->GetName(); |
| 66 |
max = counter->GetMax(); |
| 67 |
min = counter->GetMin(); |
| 68 |
posx = counter->GetPosX(); |
| 69 |
posy = counter->GetPosY(); |
| 70 |
posz = counter->GetPosZ(); |
| 71 |
rotation = counter->GetRotation(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
content.AddChildTag(HtmlTag("h1").AddContent(name).AddId("popup_title")); |
| 76 |
HtmlTag tabMenu("div"); |
| 77 |
tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("main", Languages::TextBasic, true)); |
| 78 |
tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("position", Languages::TextPosition)); |
| 79 |
|
| 80 |
content.AddChildTag(tabMenu); |
| 81 |
|
| 82 |
HtmlTag formContent; |
| 83 |
formContent.AddChildTag(HtmlTagInputHidden("cmd", "countersave")); |
| 84 |
formContent.AddChildTag(HtmlTagInputHidden("counter", to_string(counterID))); |
| 85 |
|
| 86 |
HtmlTag mainContent("div"); |
| 87 |
mainContent.AddId("tab_main"); |
| 88 |
mainContent.AddClass("tab_content"); |
| 89 |
mainContent.AddChildTag(HtmlTagInputTextWithLabel("name", Languages::TextName, name).AddAttribute("onkeyup", "updateName();")); |
| 90 |
mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("max", Languages::TextMax, max, 0, 255)); |
| 91 |
mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("min", Languages::TextMin, min, -255, 0)); |
| 92 |
formContent.AddChildTag(mainContent); |
| 93 |
|
| 94 |
formContent.AddChildTag(client.HtmlTagTabPosition(posx, posy, posz, rotation)); |
| 95 |
|
| 96 |
content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(HtmlTag("form").AddId("editform").AddChildTag(formContent))); |
| 97 |
content.AddChildTag(HtmlTagButtonCancel()); |
| 98 |
content.AddChildTag(HtmlTagButtonOK()); |
| 99 |
client.ReplyHtmlWithHeader(content); |
| 100 |
} |
| 101 |
|
| 102 |
void WebClientCounter::HandleCounterSave(const map<string, string>& arguments) |
| 103 |
{ |
| 104 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone); |
| 105 |
const string name = Utils::Utils::GetStringMapEntry(arguments, "name"); |
| 106 |
const int max = Utils::Utils::GetIntegerMapEntry(arguments, "max", 1); |
| 107 |
const int min = Utils::Utils::GetIntegerMapEntry(arguments, "min", 0); |
| 108 |
const LayoutPosition posX = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0); |
| 109 |
const LayoutPosition posY = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0); |
| 110 |
const LayoutPosition posZ = Utils::Utils::GetIntegerMapEntry(arguments, "posz", 0); |
| 111 |
const LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0); |
| 112 |
string result; |
| 113 |
if (!manager.CounterSave(counterID, |
| 114 |
name, |
| 115 |
max, |
| 116 |
min, |
| 117 |
posX, |
| 118 |
posY, |
| 119 |
posZ, |
| 120 |
rotation, |
| 121 |
result)) |
| 122 |
{ |
| 123 |
client.ReplyResponse(WebClient::ResponseError, result); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterSaved, name); |
| 128 |
} |
| 129 |
|
| 130 |
void WebClientCounter::HandleCounterList() |
| 131 |
{ |
| 132 |
HtmlTag content; |
| 133 |
content.AddChildTag(HtmlTag("h1").AddContent(Languages::TextCounters)); |
| 134 |
HtmlTag table("table"); |
| 135 |
const map<string,DataModel::Counter*> counterList = manager.CounterListByName(); |
| 136 |
map<string,string> counterArgument; |
| 137 |
for (auto& counter : counterList) |
| 138 |
{ |
| 139 |
HtmlTag row("tr"); |
| 140 |
row.AddChildTag(HtmlTag("td").AddContent(counter.first)); |
| 141 |
const string& counterIdString = to_string(counter.second->GetID()); |
| 142 |
counterArgument["counter"] = counterIdString; |
| 143 |
row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextEdit, "counteredit_list_" + counterIdString, counterArgument))); |
| 144 |
row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextDelete, "counteraskdelete_" + counterIdString, counterArgument))); |
| 145 |
table.AddChildTag(row); |
| 146 |
} |
| 147 |
content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(table)); |
| 148 |
content.AddChildTag(HtmlTagButtonCancel()); |
| 149 |
content.AddChildTag(HtmlTagButtonPopupWide(Languages::TextNew, "counteredit_0")); |
| 150 |
client.ReplyHtmlWithHeader(content); |
| 151 |
} |
| 152 |
|
| 153 |
void WebClientCounter::HandleCounterAskDelete(const map<string, string>& arguments) |
| 154 |
{ |
| 155 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone); |
| 156 |
if (counterID == CounterNone) |
| 157 |
{ |
| 158 |
client.ReplyHtmlWithHeaderAndParagraph(Languages::TextCounterDoesNotExist); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
const DataModel::Counter* counter = manager.GetCounter(counterID); |
| 163 |
if (!counter) |
| 164 |
{ |
| 165 |
client.ReplyHtmlWithHeaderAndParagraph(Languages::TextCounterDoesNotExist); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
HtmlTag content; |
| 170 |
const string& counterName = counter->GetName(); |
| 171 |
content.AddContent(HtmlTag("h1").AddContent(Languages::TextDeleteCounter)); |
| 172 |
content.AddContent(HtmlTag("p").AddContent(Languages::TextAreYouSureToDelete, counterName)); |
| 173 |
content.AddContent(HtmlTag("form").AddId("editform") |
| 174 |
.AddContent(HtmlTagInputHidden("cmd", "counterdelete")) |
| 175 |
.AddContent(HtmlTagInputHidden("counter", to_string(counterID)) |
| 176 |
)); |
| 177 |
content.AddContent(HtmlTagButtonCancel()); |
| 178 |
content.AddContent(HtmlTagButtonOK()); |
| 179 |
client.ReplyHtmlWithHeader(content); |
| 180 |
} |
| 181 |
|
| 182 |
void WebClientCounter::HandleCounterDelete(const map<string, string>& arguments) |
| 183 |
{ |
| 184 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter", CounterNone); |
| 185 |
const DataModel::Counter* counter = manager.GetCounter(counterID); |
| 186 |
if (!counter) |
| 187 |
{ |
| 188 |
client.ReplyResponse(WebClient::ResponseError, Languages::TextCounterDoesNotExist); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
string name = counter->GetName(); |
| 193 |
string result; |
| 194 |
if (!manager.CounterDelete(counterID, result)) |
| 195 |
{ |
| 196 |
client.ReplyResponse(WebClient::ResponseError, result); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterDeleted, name); |
| 201 |
} |
| 202 |
|
| 203 |
void WebClientCounter::HandleCounterGet(const map<string, string>& arguments) |
| 204 |
{ |
| 205 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter"); |
| 206 |
const DataModel::Counter* counter = manager.GetCounter(counterID); |
| 207 |
if (!counter) |
| 208 |
{ |
| 209 |
client.ReplyHtmlWithHeader(HtmlTag()); |
| 210 |
return; |
| 211 |
} |
| 212 |
client.ReplyHtmlWithHeader(HtmlTagCounter(counter)); |
| 213 |
} |
| 214 |
|
| 215 |
void WebClientCounter::HandleCounterIncrement(const map<string, string>& arguments) |
| 216 |
{ |
| 217 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter"); |
| 218 |
const bool result = manager.Count(counterID, CounterTypeIncrement); |
| 219 |
if (result) |
| 220 |
{ |
| 221 |
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterIncremented); |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
client.ReplyResponse(WebClient::ResponseWarning, Languages::TextCounterReachedLimit); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void WebClientCounter::HandleCounterDecrement(const map<string, string>& arguments) |
| 230 |
{ |
| 231 |
const CounterID counterID = Utils::Utils::GetIntegerMapEntry(arguments, "counter"); |
| 232 |
const bool result = manager.Count(counterID, CounterTypeDecrement); |
| 233 |
if (result) |
| 234 |
{ |
| 235 |
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextCounterDecremented); |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
client.ReplyResponse(WebClient::ResponseWarning, Languages::TextCounterReachedLimit); |
| 240 |
} |
| 241 |
} |
| 242 |
}} // namespace Server::Web |