PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/4.6/administrator/components/com_languages/actions/save.action.php

http://miacms.googlecode.com/
PHP | 200 lines | 157 code | 21 blank | 22 comment | 23 complexity | ee846f247063d5639be73c52620d8c05 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * @package MiaCMS
  4. * @subpackage Languages
  5. * @author MiaCMS see README.php
  6. * @copyright see README.php
  7. * See COPYRIGHT.php for copyright notices and details.
  8. * @license GNU/GPL Version 2, see LICENSE.php
  9. * MiaCMS is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the License.
  12. */
  13. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  14. class saveAction extends Action
  15. {
  16. function execute(&$controller, &$request)
  17. {
  18. $iso639 = strtolower(mosGetParam($_POST, 'iso639'));
  19. $iso3166 = mosGetParam($_POST, 'iso3166_2');
  20. $iso3166_3 = mosGetParam($_POST, 'iso3166_3');
  21. $lang = $iso639;
  22. $lang .= strlen($iso3166) == 2 ? '_'.$iso3166 : '';
  23. $root = mamboCore::get('rootPath');
  24. $langfile = $root.'/language/'.$lang.'.xml';
  25. $act = mosGetParam($_POST,'act');
  26. switch ($act)
  27. {
  28. case 'language':
  29. if (file_exists($langfile)) {
  30. $this->updatelanguage($lang);
  31. }
  32. else {
  33. $this->createlanguage($iso639, $iso3166, $iso3166_3);
  34. }
  35. return $controller->redirect('index', 'language');
  36. break;
  37. case 'catalogs':
  38. default:
  39. $this->updatecatalog(false);
  40. return $controller->redirect('index', 'catalogs');
  41. break;
  42. }
  43. }
  44. function createLanguage($iso639, $iso3166, $iso3166_3) {
  45. $locales = mamboLanguage::getLocales();
  46. $default = $locales['locales'][$iso639];
  47. $lang = $iso639;
  48. $lang .= strlen($iso3166) == 2 ? '_'.$iso3166 : '';
  49. $pluralform = mosGetParam($_POST,'plural_form');
  50. $language =& new mamboLanguage($lang);
  51. foreach ($default as $k => $v) {
  52. if (in_array($k, array_keys(get_class_vars(get_class($language))))) {
  53. $language->$k = $v;
  54. }
  55. }
  56. foreach ($_POST as $k => $v) {
  57. if (in_array($k, array_keys(get_class_vars(get_class($language))))) {
  58. $language->$k = $v;
  59. }
  60. }
  61. $language->name = $lang;
  62. $language->description = $language->title.' Locale';
  63. if (!empty($language->territory)) $language->description .= ' For '.$language->territory;
  64. $language->locale = $lang.'.'.$language->charset.','.$lang.','.$iso639.','.strtolower($language->title);
  65. $language->iso3166_3 = $iso3166_3;
  66. $language->creationdate = date('d-m-Y');
  67. $language->author = 'Mambo Foundation Inc.';
  68. $language->authorurl = 'http://www.mambo-foundation.org';
  69. $language->authoremail = 'translation@miacms.org';
  70. $language->copyright = 'Refer to copyright.php';
  71. $language->license = 'http://www.gnu.org/copyleft/gpl.html GNU/GPL';
  72. $language->setPlurals($pluralform );
  73. $textdomain = rtrim($language->path, '\/');
  74. $dir = $textdomain.'/'.$language->name;
  75. $untranslated = $textdomain.'/untranslated';
  76. $charset = $language->charset;
  77. $langfiles = mosReadDirectory($untranslated,'.pot$');
  78. @mkdir($dir);
  79. @mkdir($dir.'/LC_MESSAGES');
  80. //$gettext_admin = new PHPGettextAdmin();
  81. foreach ($langfiles as $domain) {
  82. $domain = substr($domain,0,-4);
  83. /*if (file_exists("$textdomain/glossary/$lang.$charset.po")) {
  84. copy("$textdomain/glossary/$lang.$charset.po", "$dir/$lang.po");
  85. $gettext_admin->initialize_translation($domain, $textdomain, $lang, $charset);
  86. $gettext_admin->compile($lang, $textdomain, $charset);
  87. } else {*/
  88. copy("$untranslated/$domain.pot", "$dir/$domain.po");
  89. //}
  90. }
  91. //if (!file_exists("$textdomain/$lang/$lang.po")) {
  92. // @copy("$textdomain/glossary/untranslated.pot", "$textdomain/$lang/$lang.po");
  93. //}
  94. $language->save();
  95. }
  96. function updatelanguage($lang)
  97. {
  98. $pluralform = mosGetParam($_POST,'plural_form');
  99. $language =& new mamboLanguage($lang);
  100. $language->load();
  101. foreach ($_POST as $k => $v) {
  102. if (in_array($k, array_keys(get_class_vars(get_class($language))))) {
  103. $language->$k = $v;
  104. }
  105. }
  106. $language->setPlurals($pluralform);
  107. $language->save();
  108. }
  109. function updatecatalog($compile = true, $add_to_dict = true)
  110. {
  111. $domain = mosGetParam($_POST,'domain');
  112. $textdomain = mosGetParam($_POST,'textdomain');
  113. $lang = mosGetParam($_POST,'lang');
  114. $comments = mosGetParam($_POST,'comments');
  115. $headers = mosGetParam($_POST,'headers');
  116. $catalog = new PHPGettext_catalog($domain, $textdomain);
  117. $catalog->setproperty('mode', 'po');
  118. $catalog->setproperty('lang', $lang);
  119. $catalog->load();
  120. $catalog->setComments($comments);
  121. $catalog->setHeaders($headers);
  122. $plural_forms = $catalog->headers['Plural-Forms'];
  123. preg_match('/nplurals[\s]*[=]{1}[\s]*([\d]+);[\s]*plural[\s]*[=]{1}[\s]*(.*);/', $plural_forms, $matches);
  124. $is_plural = $matches[1] > 1;
  125. foreach ($_POST as $key => $value) {
  126. if (preg_match('/^([a-z]+[_]?[a-z]+?)[_]?([0-9]+)?_([0-9]+)$/', $key, $matches)) {
  127. switch ($matches[1])
  128. {
  129. case 'msgid':
  130. if (get_magic_quotes_gpc() == 1){
  131. $value = stripslashes($value);
  132. //$value = htmlentities($value);
  133. }
  134. $messages[$matches[3]]['msgid'] = $value;
  135. break;
  136. case 'msgid_plural':
  137. if ($is_plural){
  138. $messages[$matches[3]]['msgid_plural'] = $value;
  139. }
  140. break;
  141. case 'msgstr':
  142. if (!empty($messages[$matches[3]]['msgid_plural'])) {
  143. if ($matches[2] != '') {
  144. $messages[$matches[3]]['msgstr'][$matches[2]] = stripslashes($value);
  145. } else {
  146. $messages[$matches[3]]['msgstr'][0] = stripslashes($value);
  147. $messages[$matches[3]]['msgstr'][1] = '';
  148. }
  149. } else {
  150. $messages[$matches[3]]['msgstr'] = stripslashes($value);
  151. }
  152. break;
  153. case 'fuzzy':
  154. $messages[$matches[3]]['fuzzy'] = $value == 'true' ? true : false;
  155. break;
  156. }
  157. }
  158. }
  159. foreach ($messages as $index => $arr) {
  160. if (strcmp($catalog->strings[$index]->msgid, $arr['msgid']) == 0) {
  161. $catalog->strings[$index]->setmsgstr($arr['msgstr']);
  162. $catalog->strings[$index]->msgid_plural = isset($arr['msgid_plural'])?$arr['msgid_plural']:null;;
  163. $catalog->strings[$index]->setfuzzy($arr['fuzzy']);
  164. }
  165. }
  166. $catalog->save();
  167. $language = new mamboLanguage($lang);
  168. $language->save();
  169. $configuration =& mamboCore::getMamboCore();
  170. $gettext_admin = new PHPGettextAdmin($configuration->get('mosConfig_locale_use_gettext'));
  171. $gettext_admin->add_to_dict($domain, $textdomain, $lang, $language->charset, $language->plural_form['expression']);
  172. $catalog->load();
  173. if ($compile) {
  174. $catalog->setproperty('mode', 'mo');
  175. $catalog->save();
  176. }
  177. }
  178. }
  179. ?>