1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <workspace/settings.h>
#include <assets/view.h>
#include <assets/page.h>
#include <assets/header.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <webserver/request.h>
#include <locale/translate.h>
#include <database/config/general.h>
#include <workspace/logic.h>
#include <workspace/index.h>
#include <dialog/yes.h>
#include <filter/url.h>
#include <menu/logic.h>
#include <workspace/organize.h>
std::string workspace_settings_url ()
{
return "workspace/settings";
}
bool workspace_settings_acl (Webserver_Request& webserver_request)
{
return roles::access_control (webserver_request, roles::consultant);
}
std::string workspace_settings (Webserver_Request& webserver_request)
{
std::string name = webserver_request.query ["name"];
webserver_request.database_config_user()->set_active_workspace (name);
if (webserver_request.query.count ("preset")) {
int preset = filter::strings::convert_to_int (webserver_request.query ["preset"]);
workspace_set_urls (webserver_request, workspace_get_default_urls (preset));
workspace_set_widths (webserver_request, workspace_get_default_widths (preset));
workspace_set_heights (webserver_request, workspace_get_default_heights (preset));
}
if (webserver_request.post_count("save")) {
std::map <int, std::string> urls;
std::map <int, std::string> widths;
std::map <int, std::string> row_heights;
int to14 = 0;
int to2 = 0;
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 5; column++) {
std::string key = std::to_string (row) + std::to_string (column);
urls [to14] = webserver_request.post_get("url" + key);
widths [to14] = webserver_request.post_get("width" + key);
to14++;
}
std::string key = std::to_string (row);
row_heights [to2] = webserver_request.post_get("height" + key);
to2++;
}
workspace_set_urls (webserver_request, urls);
workspace_set_widths (webserver_request, widths);
workspace_set_heights (webserver_request, row_heights);
// If no "px" or "%" is given, then default to "%".
// https://github.com/bibledit/cloud/issues/643
std::string workspacewidth = filter::strings::trim(webserver_request.post_get("workspacewidth"));
if (!workspacewidth.empty()) {
size_t pos_px = workspacewidth.find ("px");
size_t pos_pct = workspacewidth.find ("%");
if (pos_px == std::string::npos) {
if (pos_pct == std::string::npos) {
workspacewidth.append("%");
}
}
}
workspace_set_entire_width (webserver_request, workspacewidth);
redirect_browser (webserver_request, workspace_index_url ());
return std::string();
}
std::string page;
Assets_Header header = Assets_Header (translate("Edit workspace"), webserver_request);
header.add_bread_crumb (menu_logic_settings_menu (), menu_logic_settings_text ());
header.add_bread_crumb (workspace_organize_url (), menu_logic_workspace_organize_text ());
page = header.run ();
Assets_View view;
std::map <int, std::string> urls = workspace_get_urls (webserver_request, false);
std::map <int, std::string> widths = workspace_get_widths (webserver_request);
for (const auto & element : urls) {
int key = element.first;
int row = static_cast<int>(round (key / 5)) + 1;
int column = key % 5 + 1;
std::string variable = "url" + std::to_string (row) + std::to_string (column);
view.set_variable (variable, urls[key]);
variable = "width" + std::to_string (row) + std::to_string (column);
view.set_variable (variable, widths[key]);
}
std::map <int, std::string> row_heights = workspace_get_heights (webserver_request);
for (auto & element : row_heights) {
int key = element.first;
int row = key + 1;
std::string variable = "height" + std::to_string (row);
view.set_variable (variable, row_heights [key]);
}
std::string workspacewidth = workspace_get_entire_width (webserver_request);
view.set_variable ("workspacewidth", workspacewidth);
view.set_variable ("name", name);
std::vector <std::string> samples = workspace_get_default_names ();
for (size_t i = 0; i < samples.size (); i++) {
std::string sample = "<a href=\"settings?name=##name##&preset=" + std::to_string (i + 1) + "\">" + samples[i] + "</a>";
samples [i] = sample;
}
view.set_variable ("samples", filter::strings::implode (samples, "\n|\n"));
view.set_variable ("help", translate ("See the help below for information about what to enter"));
page += view.render ("workspace", "settings");
page += assets_page::footer ();
return page;
}
|