File: translated1edit.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (196 lines) | stat: -rw-r--r-- 6,488 bytes parent folder | download | duplicates (2)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 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 <resource/translated1edit.h>
#include <assets/view.h>
#include <assets/page.h>
#include <assets/header.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <filter/url.h>
#include <filter/archive.h>
#include <filter/google.h>
#include <webserver/request.h>
#include <locale/translate.h>
#include <resource/logic.h>
#include <database/config/general.h>
#include <database/books.h>
#include <journal/index.h>
#include <dialog/yes.h>
#include <dialog/entry.h>
#include <dialog/select.h>
#include <tasks/logic.h>
#include <menu/logic.h>
#include <access/logic.h>
#include <client/logic.h>


std::string resource_translated1edit_url ()
{
  return "resource/translated1edit";
}


bool resource_translated1edit_acl (Webserver_Request& webserver_request)
{
  return access_logic::privilege_view_resources (webserver_request);
}


std::string resource_translated1edit (Webserver_Request& webserver_request)
{
  std::string page {};
  Assets_Header header = Assets_Header (translate("Translated resource"), webserver_request);
  header.add_bread_crumb (menu_logic_settings_menu (), menu_logic_settings_text ());
  page = header.run ();
  Assets_View view {};
  std::string error {};
  std::string success {};
  
  
  std::string name = webserver_request.query ["name"];
  if (name.empty()) name = webserver_request.post_get("val1");
  view.set_variable ("name", name);

  
  std::string checkbox = webserver_request.post_get("checkbox");
  bool checked = filter::strings::convert_to_bool (webserver_request.post_get("checked"));

  
  bool resource_edited {false};


  std::string title {};
  std::string original_resource {};
  std::string source_language {};
  std::string target_language {};
  bool cache {false};
  {
    std::vector <std::string> resources = database::config::general::get_translated_resources ();
    for (const auto & resource : resources) {
      resource_logic_parse_translated_resource (resource, &title, &original_resource, &source_language, &target_language, &cache);
      if (title == name) break;
    }
  }

  
  // The translated resource's original resource.
  {
    constexpr const char* identification {"original"};
    if (webserver_request.post_count(identification)) {
      original_resource = webserver_request.post_get(identification);
      resource_edited = true;
    }
    dialog::select::Settings settings {
      .identification = identification,
      .values = resource_logic_get_names (webserver_request, true),
      .selected = original_resource,
      .parameters = { {"name", name} },
    };
    view.set_variable(identification, dialog::select::ajax(settings));
  }

  
  // The supported languages for translation.
  const std::vector <std::pair <std::string, std::string>> languages {filter::google::get_languages ("en")};
  std::vector<std::string> values, displayed;
  for (const auto & language : languages) {
    values.push_back(language.first);
    displayed.push_back(language.second);
  }

  
  // The language of the original resource.
  {
    constexpr const char* identification {"source"};
    if (webserver_request.post_count(identification)) {
      source_language = webserver_request.post_get(identification);
      resource_edited = true;
    }
    dialog::select::Settings settings {
      .identification = identification,
      .values = values,
      .displayed = displayed,
      .selected = source_language,
      .parameters = { {"name", name} },
    };
    view.set_variable(identification, dialog::select::ajax(settings));
  }
  
  
  // The language to translate the resource into.
  {
    constexpr const char* identification {"target"};
    if (webserver_request.post_count(identification)) {
      target_language = webserver_request.post_get(identification);
      resource_edited = true;
    }
    dialog::select::Settings settings {
      .identification = identification,
      .values = values,
      .displayed = displayed,
      .selected = target_language,
      .parameters = { {"name", name} },
    };
    view.set_variable(identification, dialog::select::ajax(settings));
  }
  
  
  // Whether to cache the resource on client devices.
  if (checkbox == "cache") {
    cache = checked;
    resource_edited = true;
  }


  // If the resource was edited, then take some steps.
  if (resource_edited) {
    // Save the translated resource.
    std::vector <std::string> resources = database::config::general::get_translated_resources ();
    error = translate ("Could not save");
    for (size_t i = 0; i < resources.size(); i++) {
      std::string title2 {};
      resource_logic_parse_translated_resource (resources[i], &title2);
      if (title2 == title) {
        std::string resource = resource_logic_assemble_translated_resource (title, original_resource, source_language, target_language, cache);
        resources[i] = resource;
        success = translate ("Saved");
        error.clear();
      }
    }
    database::config::general::set_translated_resources (resources);
    // Update the list of resources not to be cached on the client devices.
    if (cache) client_logic_no_cache_resource_remove(title);
    else client_logic_no_cache_resource_add(title);
    // Store the list of translated resources for download by the client devices.
    {
      std::string path = resource_logic_translated_resources_list_path ();
      filter_url_file_put_contents (path, filter::strings::implode (resources, "\n"));
    }
  }
  

  view.set_variable ("success", success);
  view.set_variable ("error", error);
  view.set_variable ("title", title);
  view.set_variable ("cache", filter::strings::get_checkbox_status (cache));
  page += view.render ("resource", "translated1edit");
  page += assets_page::footer ();
  return page;
}