File: select.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 (319 lines) | stat: -rw-r--r-- 10,999 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 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/select.h>
#include <assets/view.h>
#include <assets/page.h>
#include <assets/header.h>
#include <filter/roles.h>
#include <filter/string.h>
#include <filter/passage.h>
#include <filter/google.h>
#include <filter/url.h>
#include <webserver/request.h>
#include <locale/translate.h>
#include <resource/logic.h>
#include <resource/external.h>
#include <resource/sword.h>
#include <sync/logic.h>
#include <access/bible.h>
#include <client/logic.h>
#include <dialog/select.h>
#include <database/usfmresources.h>
#include <database/config/general.h>
#include <lexicon/logic.h>
#include <sword/logic.h>
#include <access/logic.h>
#include <config/globals.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wsuggest-override"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#ifndef HAVE_PUGIXML
#include <pugixml/pugixml.hpp>
#endif
#ifdef HAVE_PUGIXML
#include <pugixml.hpp>
#endif
#pragma GCC diagnostic pop


std::string resource_select_url ()
{
  return "resource/select";
}


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


std::string resource_select (Webserver_Request& webserver_request)
{
  std::string page {};
  Assets_Header header = Assets_Header (translate("Resources"), webserver_request);
  page = header.run ();
  Assets_View view {};

  
  view.set_variable ("page", resource_logic_selector_page (webserver_request));
  bool is_def = false;
  if (webserver_request.query["type"] == "def") is_def = true;
  if (is_def) view.set_variable ("type", "def");
  std::string caller = resource_logic_selector_caller (webserver_request);
  view.set_variable ("caller", caller);
  

  std::string disconnected_info {};
#ifdef HAVE_CLIENT
  if (!client_logic_client_enabled ()) {
    disconnected_info = translate ("Connect to Bibledit Cloud to have access to the full range of available resources.");
  }
#endif

  
  // The layout of the page to select resources to add should not use <select> elements,
  // because if the list of resources is long,
  // the <select> makes it hard to look for a resources.
  // See issue https://github.com/bibledit/cloud/issues/1050 for more details.
  // Rather it now uses one web page listing all resources.
  // The page initially was built through Flate iterations.
  // But Flate had limits on the number of rendering steps.
  // As a result the first part of the page was rendered, and the latter part was not done well.
  // So now the page is rendered section by section, not through Flate, but in the XML code below.

  const auto href = [&caller] (const std::string name) {
    return caller + "?add=" + name;
  };
  
  {
    pugi::xml_document document {};
    for (const std::string& name : access_bible::bibles (webserver_request)) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("bibles", block.str());
  }

  {
    std::vector<std::string> usfm_resources;
#ifdef HAVE_CLIENT
    // Client takes resources available from the Cloud.
    usfm_resources = client_logic_usfm_resources_get ();
#else
    // Cloud takes its locally available USFM resources.
    Database_UsfmResources database_usfmresources;
    usfm_resources = database_usfmresources.getResources ();
#endif
    pugi::xml_document document {};
    for (const std::string& name : usfm_resources) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("usfm", block.str());
  }

  {
    pugi::xml_document document {};
    for (const std::string& name : resource_external_get_original_language_resources()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("web_orig", block.str());
  }

  {
    pugi::xml_document document {};
    for (const std::string& name : resource_external_get_bibles()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("web_bibles", block.str());
  }

  {
    pugi::xml_document document {};
    for (const std::string& name : lexicon_logic_resource_names()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("lexicon", block.str());
  }

  {
    pugi::xml_document document {};
    for (const std::string& name : sword_logic_get_available()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("sword", block.str());
  }

  {
    const std::vector <std::string> resources = {
      resource_logic_yellow_divider (),
      resource_logic_green_divider (),
      resource_logic_blue_divider (),
      resource_logic_violet_divider (),
      resource_logic_red_divider (),
      resource_logic_orange_divider (),
      resource_logic_rich_divider(),
    };
    pugi::xml_document document {};
    for (const std::string& name : resources) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("divider", block.str());
  }

  {
    pugi::xml_document document {};
    for (const std::string& name : resource_logic_bible_gateway_module_list_get()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("biblegateway", block.str());
  }
  
  {
    pugi::xml_document document {};
    for (const std::string& name : resource_logic_study_light_module_list_get()) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("studylight", block.str());
  }
  
  // The comparative resources are stored as one resource per line.
  // The line contains multiple properties of the resource.
  // One of those properties is the title.
  // This selection mechanism here shows that title only.
  {
    std::vector <std::string> resources;
    std::vector<std::string> raw_resources =
#ifdef HAVE_CLOUD
    database::config::general::get_comparative_resources ();
#else
    resource_logic_comparative_resources_get_list_on_client ();
#endif
    for (const auto & raw_resource : raw_resources) {
      std::string title {};
      if (resource_logic_parse_comparative_resource (raw_resource, &title)) {
        resources.push_back(title);
      }
    }
    pugi::xml_document document {};
    for (const std::string& name : resources) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("comparative", block.str());
  }
 
  // The translated resources are stored as one resource per line.
  // The line contains multiple properties of the resource.
  // One of those properties is the title.
  // This selection mechanism here shows that title only.
  {
    std::vector <std::string> resources;
    std::vector<std::string> raw_resources =
#ifdef HAVE_CLOUD
    database::config::general::get_translated_resources ();
#else
    resource_logic_translated_resources_get_list_on_client ();
#endif
    for (const auto & raw_resource : raw_resources) {
      std::string title {};
      if (resource_logic_parse_translated_resource (raw_resource, &title)) {
        resources.push_back(title);
      }
    }
    pugi::xml_document document {};
    for (const std::string& name : resources) {
      pugi::xml_node p = document.append_child("p");
      pugi::xml_node a = p.append_child("a");
      a.append_attribute("href") = href(name).c_str();
      a.text().set(name.c_str());
    }
    std::stringstream block {};
    document.print (block, "", pugi::format_raw);
    view.set_variable ("translated", block.str());
  }

  // Whether to show or to hide sensitive Bible resources.
  // This is to accomodate the followers of Jesus in countries where they could be in danger.
  if (!config_globals_hide_bible_resources) {
    view.enable_zone ("sensitive_bible_resource");
  }

  // If Google Translate has not yet been set up, then enable a bit of information about that.
  // Do this only in the Cloud.
#ifdef HAVE_CLOUD
  auto [ json_key, json_error ] = filter::google::get_json_key_value_error ();
  if (json_key.empty()) view.enable_zone("setup_translated");
#endif

  
  page += view.render ("resource", "select");
  page += assets_page::footer ();
  return page;
}