Source for file main.php

Documentation is available at main.php

  1. <?php
  2.  
  3. class midcom_admin_controls_l10n_main {
  4.  
  5. var $_debug_prefix;
  6.  
  7. var $_config;
  8. var $_topic;
  9. var $_snippetdir;
  10. /** which language is edited */
  11.  
  12. var $_lang;
  13.  
  14. /** path of the component */
  15.  
  16. var $_component;
  17.  
  18. /** path of the l10n library */
  19.  
  20. var $_library;
  21. /** data to be saved */
  22.  
  23. var $_save_new;
  24. var $_save_update;
  25.  
  26. /** midcom int'l service */
  27.  
  28. var $_i18n;
  29. /** midcom_l10n instance with current $_library */
  30.  
  31. var $_l10n;
  32.  
  33.  
  34. function midcom_admin_controls_l10n_main($topic, $config) {
  35. $this->_debug_prefix = "midcom_admin_controls_l10n::";
  36.  
  37. $this->_config = $config;
  38. $this->_topic = $topic;
  39.  
  40. $this->_lang = false;
  41. $this->_component = false;
  42. $this->_library = false;
  43. $this->_save_new = false;
  44. $this->_save_update = false;
  45. $this->_i18n =& $GLOBALS["midcom"]->get_service("i18n");
  46. $this->_i18n->set_charset("UTF-8");
  47. $this->_l10n = null;
  48. }
  49.  
  50.  
  51. function can_handle($argc, $argv) {
  52. debug_push($this->_debug_prefix . "can_handle");
  53.  
  54. // welcome screen
  55. if ($argc == 0) {
  56. debug_add("showing language and component selection screen", MIDCOM_LOG_DEBUG);
  57. debug_pop();
  58. return true;
  59. }
  60. // "edit", f_lang, f_component set
  61. if (($argc == 1) && ($argv[0] == "edit"))
  62. {
  63. $this->_component = $_REQUEST["f_component"];
  64. $this->_lang = $_REQUEST["f_lang"];
  65. debug_add("editing component '".$this->_component."', language '".$this->_lang."'", MIDCOM_LOG_DEBUG);
  66. return true;
  67. }
  68. if (($argc == 1) && ($argv[0] == "save"))
  69. {
  70. $this->_component = $_REQUEST["f_component"];
  71. $this->_lang = $_REQUEST["f_lang"];
  72. debug_add("saving data for component '".$this->_component."', language '".$this->_lang."'", MIDCOM_LOG_DEBUG);
  73. if (array_key_exists("string_id", $_REQUEST))
  74. {
  75. $this->_save_update = Array (
  76. "id" => $_REQUEST["string_id"],
  77. "value" => $_REQUEST["string_value"]
  78. );
  79. }
  80.  
  81. if ((array_key_exists("new_stringid", $_REQUEST)) &&
  82. ($_REQUEST["new_stringid"] != "") &&
  83. (array_key_exists("new_en", $_REQUEST)) &&
  84. ($_REQUEST["new_en"] != ""))
  85. {
  86. $this->_save_new = Array (
  87. "stringid" => $_REQUEST["new_stringid"],
  88. "en" => $_REQUEST["new_en"]
  89. );
  90. if ((array_key_exists("new_loc", $_REQUEST)) &&
  91. ($_REQUEST["new_loc"] != ""))
  92. {
  93. $this->_save_new["loc"] = $_REQUEST["new_loc"];
  94. }
  95. }
  96.  
  97. return true;
  98. }
  99.  
  100. debug_pop();
  101. return true;
  102. }
  103.  
  104.  
  105. function handle() {
  106. debug_push($this->_debug_prefix . "handle");
  107.  
  108. // make sure text is displayed as utf-8
  109. header("Content-type: text/html; charset=UTF-8");
  110. if ($this->_component)
  111. {
  112. $loader =& $GLOBALS["midcom"]->get_component_loader();
  113. //$lib = $loader->path_to_snippetpath($this->_component) . "/_l10n";
  114. debug_add("Loading i10n class for ".$this->_component, MIDCOM_LOG_DEBUG);
  115. $this->_l10n = $this->_i18n->get_l10n($this->_component);
  116. $changes = false;
  117. // update data
  118. if ($this->_save_update)
  119. {
  120. debug_add("Updating strings", MIDCOM_LOG_DEBUG);
  121. foreach ($this->_save_update["id"] as $k => $v)
  122. {
  123. $id = $this->_save_update["id"][$k];
  124. $loc = $this->_save_update["value"][$k];
  125. $origloc = $this->_l10n->get($id, $this->_lang);
  126. if ($this->_l10n->string_exists($id, $this->_lang))
  127. {
  128. if ($loc == $origloc)
  129. {
  130. debug_add(" '$id' is unchanged, skipping it.");
  131. continue;
  132. }
  133. if ($loc == "")
  134. {
  135. debug_add(" Resetting '$id'", MIDCOM_LOG_DEBUG);
  136. $this->_l10n->delete($id, $this->_lang);
  137. $changes = true;
  138. }
  139. else
  140. {
  141. debug_add(" Updating '$id' -> '$loc'", MIDCOM_LOG_DEBUG);
  142. $this->_l10n->update($id, $this->_lang, $loc);
  143. $changes = true;
  144. }
  145. }
  146. else if ($loc != "")
  147. {
  148. debug_add(" Creating '$id' -> '$loc'", MIDCOM_LOG_DEBUG);
  149. $this->_l10n->update($id, $this->_lang, $loc);
  150. $changes = true;
  151. }
  152. else
  153. {
  154. debug_add(" Ignoring '$id' -> '$loc'", MIDCOM_LOG_DEBUG);
  155. }
  156. }
  157. }
  158. // create new strings
  159. if ($this->_save_new)
  160. {
  161. debug_add("Creating new string", MIDCOM_LOG_DEBUG);
  162. // create english string
  163. $this->_l10n->update($this->_save_new["stringid"], "en", $this->_save_new["en"]);
  164.  
  165. // create loc'd string
  166. if (array_key_exists("loc", $this->_save_new))
  167. $this->_l10n->update($this->_save_new["stringid"], $this->_lang, $this->_save_new["loc"]);
  168. $changes = true;
  169. }
  170. if ($changes)
  171. {
  172. debug_add("Changes have been made, Flushing to disk now.");
  173. $this->_l10n->flush();
  174. }
  175. }
  176.  
  177. debug_pop();
  178. return true;
  179. }
  180.  
  181. function show() {
  182. debug_push($this->_debug_prefix . "show");
  183. if ($this->_lang && $this->_component)
  184. {
  185. $this->_show_edit();
  186. }
  187. else
  188. {
  189. $this->_show_select_lang();
  190. }
  191.  
  192. debug_pop();
  193. return true;
  194. }
  195.  
  196.  
  197. function _show_select_lang() {
  198. global $view_language_db;
  199. $view_language_db = $this->_i18n->get_language_db();
  200. midcom_show_style("select_lang");
  201. }
  202.  
  203.  
  204. function _show_edit() {
  205. global $midcom;
  206. global $view_component;
  207. global $view_strings;
  208. global $view_lang;
  209. global $view_language_db;
  210.  
  211. $this->_show_permission_check();
  212. // _lang and _component have to be set!
  213.  
  214. $view_component = $this->_component;
  215. $view_lang = $this->_lang;
  216. $view_language_db = $this->_i18n->get_language_db();
  217. $view_strings = Array();
  218. $ids = $this->_l10n->get_all_string_ids();
  219.  
  220. if (is_array($ids) && (count($ids) > 0))
  221. {
  222. foreach ($ids as $id)
  223. {
  224. if ($this->_l10n->string_exists($id, $this->_lang))
  225. {
  226. $loc = $this->_l10n->get($id, $this->_lang);
  227. }
  228. else
  229. {
  230. $loc = "";
  231. }
  232. $view_strings[$id] = array(
  233. "en" => $this->_l10n->get($id, "en"),
  234. $this->_lang => $loc
  235. );
  236. }
  237. }
  238.  
  239. midcom_show_style("edit");
  240. }
  241.  
  242. function _show_permission_check() {
  243. if ($this->_component == "midcom")
  244. {
  245. $path = MIDCOM_ROOT . '/midcom/locale';
  246. }
  247. else
  248. {
  249. $path = MIDCOM_ROOT . '/' . str_replace(".", "/", $this->_component) . '/locale';
  250. }
  251. $en = "{$path}/default.en.txt";
  252. $main = "{$path}/default.{$this->_lang}.txt";
  253. if ( ! is_writable($path)
  254. || (file_exists($en) && ! is_writable($en))
  255. || (file_exists($main) && ! is_writable($main)))
  256. {
  257. midcom_show_style('permission_denied');
  258. }
  259. }
  260.  
  261. function get_metadata() {
  262. // metadata for the current element
  263. return array (
  264. MIDCOM_META_CREATOR => 0,
  265. MIDCOM_META_EDITOR => 0,
  266. MIDCOM_META_CREATED => 0,
  267. MIDCOM_META_EDITED => 0
  268. );
  269. }
  270.  
  271. }
  272.  

Documentation generated on Mon, 21 Nov 2005 18:17:59 +0100 by phpDocumentor 1.3.0RC3