PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/lang_data.php

http://prails.googlecode.com/
PHP | 261 lines | 204 code | 31 blank | 26 comment | 27 complexity | 1e276b8c59eab95029d1127f6a169674 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. PRails Web Framework
  4. Copyright (C) 2010 Robert Kunze
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. class LangData
  17. {
  18. var $obj_sql;
  19. var $language_id;
  20. var $arr_item_cache;
  21. function LangData($str_lang)
  22. {
  23. $this->obj_sql = new TblClass("tbl_prailsbase_");
  24. $this->setLanguage($str_lang);
  25. $this->arr_item_cache = Array();
  26. }
  27. function setLanguage($str_lang) {
  28. if (IS_SETUP) {
  29. $arr_result = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM ".tbl_prailsbase_language." WHERE ".(strlen($str_lang) > 0 ? "abbreviation='".$str_lang."'" : "isDefault=1")));
  30. if (strlen($str_lang) <= 0 && DEFAULT_LANGUAGE != $arr_result["language_id"]) {
  31. try {
  32. $conf = getConfiguration();
  33. $conf["DEFAULT_LANGUAGE"] = (int)$arr_result["language_id"];
  34. $toSave = Array();
  35. foreach ($conf as $name => $val) {
  36. $toSave[] = Array("name" => $name, "value" => $val);
  37. }
  38. updateConfiguration($toSave);
  39. } catch(Exception $ex) {};
  40. }
  41. $str_lang = $arr_result["abbreviation"];
  42. $_SESSION["LangData_LANGUAGE_SETTING"]["currentLanguage"] = $str_lang;
  43. $this->language_id = $arr_result["language_id"];
  44. $_SESSION["LangData_LANGUAGE_SETTING"]["currentLanguageId"] = $this->language_id;
  45. setcookie("defaultLang", $this->language_id);
  46. if (!$_SESSION["LangData_LANGUAGE_SETTING"][$str_lang]) $_SESSION["LangData_LANGUAGE_SETTING"][$str_lang] = Array();
  47. }
  48. }
  49. function getText($str_item)
  50. {
  51. if (!$this->arr_item_cache[$str_item] || !ENV_PRODUCTION)
  52. {
  53. $res = $this->obj_sql->SqlQuery(
  54. "SELECT " .
  55. " content " .
  56. "FROM ".tbl_prailsbase_texts." " .
  57. "WHERE " .
  58. " fk_language_id=".(int)$this->language_id." " .
  59. " AND " .
  60. " identifier='".$str_item."'"
  61. );
  62. if ($res != null) $arr_result = @array_pop($res);
  63. if (!$arr_result) $arr_result["content"] = "{".$str_item."}";
  64. $arr_result["content"] = stripslashes(preg_replace('/^(.*)(<br>|<br\/>)$/i', '$1', $arr_result["content"]));
  65. if (strlen($arr_result["content"]) < 1024) {
  66. $this->arr_item_cache[$str_item] = $arr_result["content"];
  67. }
  68. } else
  69. {
  70. $arr_result["content"] = $this->arr_item_cache[$str_item];
  71. }
  72. return $arr_result["content"];
  73. }
  74. function selectTextByIdentifier($str_item) {
  75. $arr_result = @array_pop($this->obj_sql->SqlQuery(
  76. "SELECT " .
  77. " * " .
  78. "FROM ".tbl_prailsbase_texts." " .
  79. "WHERE " .
  80. " fk_language_id=".(int)$this->language_id." " .
  81. " AND " .
  82. " identifier='".$str_item."'"
  83. ));
  84. if (!$arr_result) $arr_result["content"] = "{".$str_item."}";
  85. $arr_result["content"] = stripslashes(preg_replace('/^(.*)(<br>|<br\/>)$/i', '$1', $arr_result["content"]));
  86. if (strlen($arr_result["content"]) < 1024) {
  87. $this->arr_item_cache[$str_item] = $arr_result["content"];
  88. }
  89. $arr_result["custom"] = $this->_decodeCustom($arr_result["custom"]);
  90. return $arr_result;
  91. }
  92. function listLanguages() {
  93. return $this->obj_sql->SqlQuery("SELECT * FROM ".tbl_prailsbase_language." WHERE 1=1 ORDER BY name");
  94. }
  95. function listTexts() {
  96. $arr_result = $this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE fk_language_id > 0 GROUP BY identifier, texts_id, fk_language_id, content");
  97. $arr_return = Array();
  98. foreach ($arr_result as $arr_entry) {
  99. $parts = explode(".", $arr_entry["identifier"]);
  100. $curr = &$arr_return;
  101. foreach ($parts as $p) {
  102. if (!is_array($curr[$p])) {
  103. $curr[$p] = Array();
  104. }
  105. $curr = &$curr[$p];
  106. }
  107. $curr = $arr_entry["content"];
  108. }
  109. return $arr_return;
  110. }
  111. function listAllTextsFromRoot($rootNode) {
  112. $arr_result = $this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE fk_language_id > 0 AND identifier LIKE '".$rootNode.".%'");
  113. foreach($arr_result as &$item) {
  114. $item["custom"] = $this->_decodeCustom($item["custom"]);
  115. }
  116. return $arr_result;
  117. }
  118. function findTextByContent($word) {
  119. $arr_result = $this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE fk_language_id > 0 AND content LIKE '%".$word."%'");
  120. $arr_return = Array();
  121. foreach ($arr_result as $res) {
  122. array_push($arr_return, Array("id" => "text_.".$res['identifier'], "name" => $res["identifier"], "type" => "text", "custom" => $this->_decodeCustom($res["custom"])));
  123. }
  124. return $arr_return;
  125. }
  126. function getAllTextsByIdentifier($ident) {
  127. $texts = $this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_language AS b LEFT JOIN tbl_prailsbase_texts AS a ON identifier='".$ident."' AND b.language_id=a.fk_language_id WHERE 1=1");
  128. foreach ($texts as &$text) {
  129. $text["default"] = $text["isDefault"];
  130. $text["custom"] = $this->_decodeCustom($text["custom"]);
  131. }
  132. return $texts;
  133. }
  134. function getAllTextsById($id) {
  135. $arr_text = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE texts_id=".(int)$id));
  136. return $this->getAllTextsByIdentifier($arr_text["identifier"]);
  137. }
  138. function updateTexts($arr_data) {
  139. if (!is_array($arr_data["content"])) return false;
  140. foreach ($arr_data["content"] as $lang=>$text) {
  141. $entry["fk_language_id"] = $lang;
  142. $entry["title"] = $arr_data["title"];
  143. $entry["description"] = $arr_data["description"];
  144. $entry["identifier"] = $arr_data["identifier"];
  145. $entry["decorator"] = $arr_data["decorator"];
  146. $entry["type"] = $arr_data["type"];
  147. $entry["content"] = $text;
  148. $entry["custom"] = $this->_encodeCustom($arr_data["custom"]);
  149. if (strlen($arr_data["old_identifier"]) > 0) {
  150. $exists = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE fk_language_id=".(int)$lang." AND identifier='".$arr_data["old_identifier"]."'"));
  151. } else {
  152. $exists = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE fk_language_id=".(int)$lang." AND identifier='".$entry["identifier"]."'"));
  153. }
  154. if ($exists != null) {
  155. $this->obj_sql->UpdateQuery(tbl_prailsbase_texts, $entry, "texts_id=".(int)$exists["texts_id"]);
  156. } else {
  157. $this->obj_sql->InsertQuery(tbl_prailsbase_texts, $entry);
  158. }
  159. if (strlen($text) < 1024) {
  160. $this->arr_item_cache[$arr_data["identifier"]] = $text;
  161. }
  162. }
  163. return true;
  164. }
  165. function updateTextType($id, $type) {
  166. $exists = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE texts_id=".(int)$id));
  167. if ($exists != null) {
  168. $entry["type"] = $type;
  169. $this->obj_sql->UpdateQuery(tbl_prailsbase_texts, $entry, "identifier='".$exists["identifier"]."'");
  170. return true;
  171. }
  172. return false;
  173. }
  174. function insertText($arr_data) {
  175. $arr_data["custom"] = $this->_encodeCustom($arr_data["custom"]);
  176. $this->obj_sql->InsertQuery(tbl_prailsbase_texts, $arr_data);
  177. }
  178. function deleteTexts($id) {
  179. $arr_text = @array_pop($this->obj_sql->SqlQuery("SELECT * FROM tbl_prailsbase_texts WHERE texts_id=".(int)$id));
  180. $this->deleteTextByIdentifier($arr_text["identifier"]);
  181. }
  182. function deleteTextByIdentifier($ident) {
  183. $this->obj_sql->DeleteQuery(tbl_prailsbase_texts, "identifier='".$ident."' OR fk_language_id=0");
  184. }
  185. function deleteSection($section) {
  186. $this->obj_sql->DeleteQuery(tbl_prailsbase_texts, "identifier LIKE '".$section.".%' OR fk_language_id=0");
  187. }
  188. function updateLanguage($id, $arr_data) {
  189. $arr_data["isDefault"] = $arr_data["default"];
  190. // $arr_languages = $this->listLanguages();
  191. $this->obj_sql->UpdateQuery(tbl_prailsbase_language, Array("isDefault" => "0"), "1=1");
  192. /*
  193. foreach ($arr_languages as $lang) {
  194. if ($lang["isDefault"] == 0 && $id == $lang["language_id"]) {
  195. $arr_data["isDefault"] = 1;
  196. }
  197. }//*/
  198. $this->obj_sql->UpdateQuery(tbl_prailsbase_language, $arr_data, "language_id=".(int)$id."");
  199. }
  200. function insertLanguage($arr_data) {
  201. $arr_data["isDefault"] = $arr_data["default"];
  202. if (($count = count($this->listLanguages())) <= 0) {
  203. $arr_data["isDefault"] = 1;
  204. }
  205. if ($arr_data["isDefault"] == 1 && $count > 0) {
  206. // if the to-be-created language should be the new default, then
  207. // set all other default languages to non-default
  208. $this->obj_sql->UpdateQuery(tbl_prailsbase_language, Array("isDefault" => "0"), "1=1");
  209. }
  210. $id = $this->obj_sql->InsertQuery(tbl_prailsbase_language, $arr_data);
  211. return $id;
  212. }
  213. function deleteLanguage($id) {
  214. $this->obj_sql->DeleteQuery(tbl_prailsbase_texts, "fk_language_id=".(int)$id." OR fk_language_id=0");
  215. $this->obj_sql->DeleteQuery(tbl_prailsbase_language, "language_id=".(int)$id."");
  216. }
  217. function deleteLanguageOnly($id) {
  218. $this->obj_sql->DeleteQuery(tbl_prailsbase_language, "language_id=".(int)$id."");
  219. }
  220. function _encodeCustom($data) {
  221. return base64_encode(json_encode($data));
  222. }
  223. function _decodeCustom($field) {
  224. return json_decode(base64_decode($field), true);
  225. }
  226. }
  227. ?>