ferencd@0: #include "category_sender.h" ferencd@0: #include "templater.h" ferencd@0: #include "url_breaker.h" ferencd@0: #include "logger.h" ferencd@0: #include "json.h" ferencd@0: #include ferencd@0: ferencd@0: #include ferencd@0: #include ferencd@0: ferencd@0: category_sender::category_sender(tnt::HttpRequest &request, tnt::HttpReply &reply, const std::string &what) : web_component(request, reply, "") ferencd@0: { ferencd@0: ferencd@0: // identify the category ferencd@0: static std::map name_to_dbtype = { {"soups", "0"}, {"sides", "2"}, {"mains", "3"}, {"sweets", "4"}, {"starters", "1"} }; ferencd@0: ferencd@0: url_breaker r("l/category", boost::to_lower_copy(what)); ferencd@0: std::string food_type = r["category"]; ferencd@0: // identify the language ferencd@0: auto pars = request.getQueryParams(); ferencd@0: m_lang = pars.arg("l"); ferencd@0: ferencd@0: if(food_type == "about") ferencd@0: { ferencd@0: char cCurrentPath[FILENAME_MAX] = {0}; ferencd@0: ferencd@0: if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) ferencd@0: { ferencd@0: log_err() << "Cannot get working path"; ferencd@0: m_translated = "Internal server error"; ferencd@0: return; ferencd@0: } ferencd@0: ferencd@0: std::string filename(cCurrentPath); ferencd@0: ferencd@0: std::ifstream fin(filename + "/theme/current/About.html", std::ios::in | std::ios::binary); ferencd@0: if(fin) ferencd@0: { ferencd@0: std::ostringstream oss; ferencd@0: oss << fin.rdbuf(); ferencd@0: m_translated = oss.str(); ferencd@0: } ferencd@0: ferencd@0: } ferencd@0: ferencd@0: if(name_to_dbtype.find(food_type) == name_to_dbtype.end()) ferencd@0: { ferencd@0: log_critical() << "Not found:" << what; ferencd@0: m_status = HTTP_NOT_FOUND; ferencd@0: return; ferencd@0: } ferencd@0: m_category = food_type; m_category[0] = std::toupper(m_category[0]); ferencd@0: food_type = name_to_dbtype[food_type]; ferencd@0: ferencd@0: ferencd@0: ferencd@0: // generate the page elements as translateable ferencd@0: try { ferencd@0: tntdb::Connection conn = tntdb::connect("sqlite:lang.db"); ferencd@0: ferencd@0: std::string v = std::string("select * from food where type='") + food_type + "'"; ferencd@0: tntdb::Result result = conn.select(v); ferencd@0: std::vector keys; ferencd@0: for (tntdb::Result::const_iterator it = result.begin(); it != result.end(); ++it) ferencd@0: { ferencd@0: std::string name_src = ""; ferencd@0: std::string img = ""; ferencd@0: int food_idx = -1; ferencd@0: std::string type; ferencd@0: std::string key; ferencd@0: ferencd@0: tntdb::Row row = *it; ferencd@0: ferencd@0: row[0].get(food_idx); ferencd@0: row[1].get(name_src); ferencd@0: row[2].get(type); ferencd@0: row[3].get(img); ferencd@0: row[4].get(key); ferencd@0: ferencd@0: template_struct food("foods", "fooditem"); ferencd@0: ferencd@0: food["name"] = ""; ferencd@0: ferencd@0: // fix the image, if it contains {#rroot} ferencd@0: stringholder sh(img); ferencd@0: sh.replace_all("{#rroot}", RECIPES_ROOT + key + SLASH); ferencd@0: sh.replace_all("//", "/"); ferencd@0: ferencd@0: food["img"] = sh.get(); ferencd@0: food["desc"] = ""; ferencd@0: food["key"] = key; ferencd@0: ferencd@0: keys.push_back(key); ferencd@0: ferencd@0: populate_short_description(key); ferencd@0: ferencd@0: m_foodStructs.push_back(food); ferencd@0: } ferencd@0: ferencd@0: prepareLanguages(); ferencd@0: ferencd@0: template_vector_par tvp_foods("foods", m_foodStructs); ferencd@0: template_vector_par tvp_languages("languages", m_languageStructs); ferencd@0: ferencd@0: std::string page = templater().templatize("category" ctg_to_name[food_type], "lang" m_lang).templatize(tvp_languages).templatize(tvp_foods).get(); ferencd@0: ferencd@0: std::map > translations; ferencd@0: m_translated = translator::translate(page, m_lang, translations); ferencd@0: ferencd@0: stringholder sh(m_translated); ferencd@0: sh.replace_all("{#jsfun}", prepareLangJs(translations)); ferencd@0: sh.replace_all("{#trjsfun}", prepareLangTrs(keys)); ferencd@0: sh.replace_all("{#divChanger}", "changeDivs('" + m_lang + "');"); ferencd@0: m_translated = sh.get(); ferencd@0: ferencd@0: ferencd@0: } ferencd@0: catch (std::exception& ex) ferencd@0: { ferencd@0: log_critical() << ex.what(); ferencd@0: m_status = HTTP_NOT_FOUND; ferencd@0: } ferencd@0: } ferencd@0: ferencd@0: unsigned category_sender::send() ferencd@0: { ferencd@0: mreply.out() << m_translated; ferencd@0: return HTTP_OK; ferencd@0: } ferencd@0: ferencd@0: bool category_sender::populate_short_description(const std::string &key) const ferencd@0: { ferencd@0: char cCurrentPath[FILENAME_MAX] = {0}; ferencd@0: ferencd@0: if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) ferencd@0: { ferencd@0: log_err() << "Cannot get current path"; ferencd@0: return false; ferencd@0: } ferencd@0: ferencd@0: std::string filename(cCurrentPath); ferencd@0: filename += RECIPES_ROOT + "/" + key + "/descriptor.json"; ferencd@0: std::ifstream fin(filename.c_str(), std::ios::in | std::ios::binary); ferencd@0: ferencd@0: if(fin) ferencd@0: { ferencd@0: std::ostringstream oss; ferencd@0: oss << fin.rdbuf(); ferencd@0: std::string fileData(oss.str()); ferencd@0: auto j = nlohmann::json::parse(fileData); ferencd@0: auto langs = j["lang"]; ferencd@0: for(const auto& l : langs) ferencd@0: { ferencd@0: std::string filenameDesc(cCurrentPath); ferencd@0: std::string cpp_string; ferencd@0: l.get_to(cpp_string); ferencd@0: ferencd@0: filenameDesc += RECIPES_ROOT + key + "/" + cpp_string+ "/descr.md"; ferencd@0: std::ifstream finDesc(filenameDesc.c_str(), std::ios::in | std::ios::binary); ferencd@0: if(finDesc) ferencd@0: { ferencd@0: std::ostringstream ossDesc; ferencd@0: ossDesc << finDesc.rdbuf(); ferencd@0: std::string fileDataDesc(ossDesc.str()); ferencd@0: ferencd@0: dictionary::add_translation(key + "_desc", cpp_string, fileDataDesc); ferencd@0: } ferencd@0: } ferencd@0: } ferencd@0: else ferencd@0: { ferencd@0: log_err() << "Cannot get descriptor file:" << filename; ferencd@0: return false; ferencd@0: } ferencd@0: ferencd@0: return true; ferencd@0: } ferencd@0: ferencd@0: std::string category_sender::prepareLangTrs(const std::vector& keys) ferencd@0: { ferencd@0: std::string javascript = "function changeTrs(l) {"; ferencd@0: for(const auto &k : keys) ferencd@0: { ferencd@0: javascript += "$('#tr_" + k + "').data('href', '/r/" + k + "?l=' + l);\n"; ferencd@0: } ferencd@0: ferencd@0: javascript += "}\n"; ferencd@0: return javascript; ferencd@0: }