| 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 <algorithm> |
| 22 | #include <map> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "DataModel/Cluster.h" |
| 26 | #include "DataModel/ObjectIdentifier.h" |
| 27 | #include "DataModel/Signal.h" |
| 28 | #include "DataModel/Track.h" |
| 29 | #include "Server/Web/HtmlTag.h" |
| 30 | #include "Server/Web/HtmlTagButtonCancel.h" |
| 31 | #include "Server/Web/HtmlTagButtonOK.h" |
| 32 | #include "Server/Web/HtmlTagButtonPopupWide.h" |
| 33 | #include "Server/Web/HtmlTagInputHidden.h" |
| 34 | #include "Server/Web/HtmlTagInputTextWithLabel.h" |
| 35 | #include "Server/Web/HtmlTagSelect.h" |
| 36 | #include "Server/Web/WebClient.h" |
| 37 | #include "Server/Web/WebClientCluster.h" |
| 38 | #include "Server/Web/WebClientStatic.h" |
| 39 | |
| 40 | using DataModel::Cluster; |
| 41 | using DataModel::ObjectIdentifier; |
| 42 | using DataModel::Relation; |
| 43 | using DataModel::Signal; |
| 44 | using DataModel::Track; |
| 45 | using std::map; |
| 46 | using std::string; |
| 47 | using std::to_string; |
| 48 | using std::vector; |
| 49 | |
| 50 | namespace Server { namespace Web |
| 51 | { |
| 52 | void WebClientCluster::HandleClusterList() |
| 53 | { |
| 54 | HtmlTag content; |
| 55 | content.AddChildTag(HtmlTag("h1").AddContent(Languages::TextCluster)); |
| 56 | HtmlTag table("table"); |
| 57 | map<string,string> clusterArgument; |
| 58 | const map<string,Cluster*> clusterList = manager.ClusterListByName(); |
| 59 | for (auto& cluster : clusterList) |
| 60 | { |
| 61 | HtmlTag row("tr"); |
| 62 | row.AddChildTag(HtmlTag("td").AddContent(cluster.first)); |
| 63 | const string& clusterIdString = to_string(cluster.second->GetID()); |
| 64 | clusterArgument["cluster"] = clusterIdString; |
| 65 | row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextEdit, "clusteredit_list_" + clusterIdString, clusterArgument))); |
| 66 | row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextDelete, "clusteraskdelete_" + clusterIdString, clusterArgument))); |
| 67 | table.AddChildTag(row); |
| 68 | } |
| 69 | content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(table)); |
| 70 | content.AddChildTag(HtmlTagButtonCancel()); |
| 71 | content.AddChildTag(HtmlTagButtonPopupWide(Languages::TextNew, "clusteredit_0")); |
| 72 | client.ReplyHtmlWithHeader(content); |
| 73 | } |
| 74 | |
| 75 | HtmlTag WebClientCluster::HtmlTagSelectTrackEntry(const string& priority, |
| 76 | const TrackID trackID, |
| 77 | const map<string,ObjectID>& trackOptions, |
| 78 | const Relation::Data inverted, |
| 79 | const map<string,ObjectID>& invertedOptions) |
| 80 | { |
| 81 | HtmlTag content("div"); |
| 82 | content.AddId("track_priority_" + priority); |
| 83 | |
| 84 | HtmlTagButton deleteButton(Languages::TextDelete, "track_delete_" + priority); |
| 85 | deleteButton.AddAttribute("onclick", "deleteElement('track_priority_" + priority + "');return false;"); |
| 86 | deleteButton.AddClass("wide_button"); |
| 87 | content.AddChildTag(deleteButton); |
| 88 | |
| 89 | HtmlTag contentObject("div"); |
| 90 | contentObject.AddId("track_object_" + priority); |
| 91 | contentObject.AddClass("inline-block"); |
| 92 | |
| 93 | contentObject.AddChildTag(HtmlTagSelect("track_id_" + priority, trackOptions, trackID).AddClass("select_relation_id")); |
| 94 | contentObject.AddChildTag(HtmlTagSelect("track_inverted_" + priority, invertedOptions, inverted).AddClass("select_relation_data")); |
| 95 | content.AddChildTag(contentObject); |
| 96 | return content; |
| 97 | } |
| 98 | |
| 99 | HtmlTag WebClientCluster::HtmlTagSelectTrack(const vector<Relation*>& relations, |
| 100 | const ClusterID clusterID) const |
| 101 | { |
| 102 | HtmlTag content("div"); |
| 103 | content.AddId("tab_tracks"); |
| 104 | content.AddClass("tab_content"); |
| 105 | content.AddClass("hidden"); |
| 106 | |
| 107 | HtmlTag div("div"); |
| 108 | div.AddChildTag(HtmlTagInputHidden("trackcounter", to_string(relations.size()))); |
| 109 | div.AddId("tracks"); |
| 110 | |
| 111 | const map<string,ObjectID> trackOptions = GetTrackOptions(clusterID); |
| 112 | map<string,Relation::Data> invertedOptions; |
| 113 | invertedOptions[Languages::GetText(Languages::TextDefault)] = 0; |
| 114 | invertedOptions[Languages::GetText(Languages::TextInverted2)] = 1; |
| 115 | unsigned int counter = 1; |
| 116 | for (auto relation : relations) |
| 117 | { |
| 118 | const TrackID trackID = relation->ObjectID2(); |
| 119 | const Relation::Data inverted = relation->GetData(); |
| 120 | div.AddChildTag(HtmlTagSelectTrackEntry(to_string(counter), trackID, trackOptions, inverted, invertedOptions)); |
| 121 | ++counter; |
| 122 | } |
| 123 | div.AddChildTag(HtmlTag("div").AddId("track_new_" + to_string(counter))); |
| 124 | |
| 125 | content.AddChildTag(div); |
| 126 | HtmlTagButton newTrackButton(Languages::TextNew, "newtrack"); |
| 127 | newTrackButton.AddAttribute("onclick", "addSlave('track');return false;"); |
| 128 | newTrackButton.AddClass("wide_button"); |
| 129 | content.AddChildTag(newTrackButton); |
| 130 | |
| 131 | content.AddChildTag(HtmlTag("br")); |
| 132 | return content; |
| 133 | } |
| 134 | |
| 135 | void WebClientCluster::HandleClusterEdit(const map<string, string>& arguments) |
| 136 | { |
| 137 | HtmlTag content; |
| 138 | ClusterID clusterID = Utils::Utils::GetIntegerMapEntry(arguments, "cluster", ClusterNone); |
| 139 | string name = Utils::Utils::GetStringMapEntry(arguments, "name"); |
| 140 | vector<Relation*> tracks; |
| 141 | |
| 142 | if (clusterID != ClusterNone) |
| 143 | { |
| 144 | Cluster* cluster = manager.GetCluster(clusterID); |
| 145 | if (cluster) |
| 146 | { |
| 147 | name = cluster->GetName(); |
| 148 | tracks = cluster->GetTracks(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | content.AddChildTag(HtmlTag("h1").AddContent(name).AddId("popup_title")); |
| 153 | HtmlTag tabMenu("div"); |
| 154 | tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("basic", Languages::TextBasic, true)); |
| 155 | tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("tracks", Languages::TextTracks)); |
| 156 | content.AddChildTag(tabMenu); |
| 157 | |
| 158 | HtmlTag formContent("form"); |
| 159 | formContent.AddId("editform"); |
| 160 | formContent.AddChildTag(HtmlTagInputHidden("cmd", "clustersave")); |
| 161 | formContent.AddChildTag(HtmlTagInputHidden("cluster", to_string(clusterID))); |
| 162 | |
| 163 | HtmlTag basicContent("div"); |
| 164 | basicContent.AddId("tab_basic"); |
| 165 | basicContent.AddClass("tab_content"); |
| 166 | basicContent.AddChildTag(HtmlTagInputTextWithLabel("name", Languages::TextName, name).AddAttribute("onkeyup", "updateName();")); |
| 167 | formContent.AddChildTag(basicContent); |
| 168 | |
| 169 | formContent.AddChildTag(HtmlTagSelectTrack(tracks, clusterID)); |
| 170 | |
| 171 | content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(formContent)); |
| 172 | content.AddChildTag(HtmlTagButtonCancel()); |
| 173 | content.AddChildTag(HtmlTagButtonOK()); |
| 174 | client.ReplyHtmlWithHeader(content); |
| 175 | } |
| 176 | |
| 177 | void WebClientCluster::HandleClusterSave(const map<string, string>& arguments) |
| 178 | { |
| 179 | ClusterID clusterID = Utils::Utils::GetIntegerMapEntry(arguments, "cluster", ClusterNone); |
| 180 | string name = Utils::Utils::GetStringMapEntry(arguments, "name"); |
| 181 | const unsigned int count = Utils::Utils::GetIntegerMapEntry(arguments, "trackcounter", 0); |
| 182 | |
| 183 | vector<Relation*> tracks; |
| 184 | { |
| 185 | for (unsigned int index = 1; index <= count; ++index) |
| 186 | { |
| 187 | const string indexAsString = to_string(index); |
| 188 | const TrackID trackID = Utils::Utils::GetIntegerMapEntry(arguments, "track_id_" + indexAsString, TrackNone); |
| 189 | if (trackID == TrackNone) |
| 190 | { |
| 191 | continue; |
| 192 | } |
| 193 | const bool inverted = Utils::Utils::GetBoolMapEntry(arguments, "track_inverted_" + indexAsString, false); |
| 194 | |
| 195 | tracks.push_back(new Relation(&manager, |
| 196 | ObjectIdentifier(ObjectTypeCluster, clusterID), |
| 197 | ObjectIdentifier(ObjectTypeTrack, trackID), |
| 198 | Relation::RelationTypeClusterTrack, |
| 199 | 0, |
| 200 | static_cast<Relation::Data>(inverted))); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | string result; |
| 205 | if (!manager.ClusterSave(clusterID, name, tracks, result)) |
| 206 | { |
| 207 | client.ReplyResponse(WebClient::ResponseError, result); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | client.ReplyResponse(WebClient::ResponseInfo, Languages::TextClusterSaved, name); |
| 212 | } |
| 213 | |
| 214 | void WebClientCluster::HandleClusterAskDelete(const map<string, string>& arguments) |
| 215 | { |
| 216 | ClusterID clusterID = Utils::Utils::GetIntegerMapEntry(arguments, "cluster", ClusterNone); |
| 217 | |
| 218 | if (clusterID == ControlNone) |
| 219 | { |
| 220 | client.ReplyHtmlWithHeaderAndParagraph(Languages::TextClusterDoesNotExist); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | const Cluster* cluster = manager.GetCluster(clusterID); |
| 225 | if (!cluster) |
| 226 | { |
| 227 | client.ReplyHtmlWithHeaderAndParagraph(Languages::TextClusterDoesNotExist); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | HtmlTag content; |
| 232 | content.AddContent(HtmlTag("h1").AddContent(Languages::TextDeleteCluster)); |
| 233 | content.AddContent(HtmlTag("p").AddContent(Languages::TextAreYouSureToDelete, cluster->GetName())); |
| 234 | content.AddContent(HtmlTag("form").AddId("editform") |
| 235 | .AddContent(HtmlTagInputHidden("cmd", "clusterdelete")) |
| 236 | .AddContent(HtmlTagInputHidden("cluster", to_string(clusterID)) |
| 237 | )); |
| 238 | content.AddContent(HtmlTagButtonCancel()); |
| 239 | content.AddContent(HtmlTagButtonOK()); |
| 240 | client.ReplyHtmlWithHeader(content); |
| 241 | } |
| 242 | |
| 243 | void WebClientCluster::HandleClusterDelete(const map<string, string>& arguments) |
| 244 | { |
| 245 | ClusterID clusterID = Utils::Utils::GetIntegerMapEntry(arguments, "cluster", ClusterNone); |
| 246 | const Cluster* cluster = manager.GetCluster(clusterID); |
| 247 | if (!cluster) |
| 248 | { |
| 249 | client.ReplyResponse(WebClient::ResponseError, Languages::TextClusterDoesNotExist); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | string name = cluster->GetName(); |
| 254 | |
| 255 | if (!manager.ClusterDelete(clusterID)) |
| 256 | { |
| 257 | client.ReplyResponse(WebClient::ResponseError, Languages::TextClusterDoesNotExist); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | client.ReplyResponse(WebClient::ResponseInfo, Languages::TextClusterDeleted, name); |
| 262 | } |
| 263 | |
| 264 | map<string,ObjectID> WebClientCluster::GetTrackOptions(const ClusterID clusterId) const |
| 265 | { |
| 266 | map<string, ObjectID> trackOptions; |
| 267 | |
| 268 | map<string, Track*> tracks = manager.TrackListMasterByName(); |
| 269 | for (auto& track : tracks) |
| 270 | { |
| 271 | Cluster* clusterOfTrack = track.second->GetCluster(); |
| 272 | if (clusterOfTrack && clusterOfTrack->GetID() != clusterId) |
| 273 | { |
| 274 | continue; |
| 275 | } |
| 276 | trackOptions[track.first] = track.second->GetID(); |
| 277 | } |
| 278 | return trackOptions; |
| 279 | } |
| 280 | }} // namespace Server::Web |