[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/administrator/components/com_languages/actions/ -> save.action.php (source)

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