1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
19:
20: 21: 22: 23: 24: 25: 26:
27: class cModuleFileTranslation extends cModuleHandler {
28:
29: 30: 31: 32: 33:
34: private $_modulePath;
35:
36: 37: 38: 39: 40:
41: static $fileName = '';
42:
43: 44: 45: 46: 47:
48: static $langArray = array();
49:
50: 51: 52: 53: 54:
55: static $savedIdMod = NULL;
56: static $originalTranslationDivider = '=';
57:
58: 59: 60: 61: 62: 63: 64: 65:
66: public function __construct($idmodul = NULL, $static = false, $overrideIdlang = NULL) {
67: parent::__construct($idmodul);
68:
69:
70:
71: if ($idmodul != NULL) {
72: $this->_modulePath = $this->getModulePath();
73: }
74:
75:
76: if ($overrideIdlang != NULL) {
77: $this->_idlang = $overrideIdlang;
78: }
79:
80: $this->_encoding = self::getEncoding($this->_idlang);
81:
82:
83: if ($static == true) {
84: if (self::$savedIdMod != $idmodul) {
85:
86: $language = $this->_getValueFromProperties('language', 'code');
87: $country = $this->_getValueFromProperties('country', 'code');
88: self::$fileName = 'lang_' . $language . '_' . strtoupper($country) . '.txt';
89:
90: self::$langArray = $this->getTranslationArray();
91: self::$savedIdMod = $idmodul;
92: }
93: } else {
94: self::$savedIdMod = -1;
95:
96:
97: $language = $this->_getValueFromProperties('language', 'code');
98: $country = $this->_getValueFromProperties('country', 'code');
99: self::$fileName = 'lang_' . $language . '_' . strtoupper($country) . '.txt';
100: }
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110:
111: private function _getValueFromProperties($type, $name) {
112: cApiPropertyCollection::reset();
113: $propColl = new cApiPropertyCollection();
114: $propColl->changeClient($this->_client);
115: return $propColl->getValue('idlang', $this->_idlang, $type, $name, '');
116: }
117:
118: 119: 120: 121: 122:
123: public function getLangArray() {
124: return self::$langArray;
125: }
126:
127: 128: 129: 130:
131: public function saveTranslations() {
132: $db = cRegistry::getDb();
133:
134: $oLangColl = new cApiLanguageCollection();
135: $ids = $oLangColl->getAllIds();
136: foreach ($ids as $idlang) {
137: $sql = 'SELECT * FROM `%s` WHERE idlang = %d AND idmod = %d';
138: $sql = $db->prepare($sql, $this->_cfg['tab']['mod_translations'], $idlang, $this->_idmod);
139: $db->query($sql);
140:
141: $this->_idlang = $idlang;
142:
143: $language = $this->_getValueFromProperties('language', 'code');
144: $country = $this->_getValueFromProperties('country', 'code');
145: self::$fileName = 'lang_' . $language . '_' . strtoupper($country) . '.txt';
146:
147: $translations = array();
148: while ($db->nextRecord()) {
149: $original = mb_convert_encoding(urldecode(cSecurity::unfilter($db->f('original'))), "UTF-8");
150: $translation = mb_convert_encoding(urldecode(cSecurity::unfilter($db->f('translation'))), "UTF-8");
151: $translations[$original] = $translation;
152: }
153:
154: $text = $this->readInput();
155: if (!$text) {
156: $text = "";
157: }
158: $text .= $this->readOutput();
159:
160: mb_ereg_search_init($text, 'mi18n\(["|\'](.*?)["|\']\)');
161: while(mb_ereg_search()) {
162: $translation = mb_ereg_search_getregs();
163: if(!isset($translations[$translation[1]])) {
164: $translations[$translation[1]] = $translation[1];
165: }
166: }
167:
168: if (count($translations) != 0) {
169: if ($this->saveTranslationArray($translations) == false) {
170: cWarning(__FILE__, __LINE__, 'Could not save translate idmod=' . $this->_idmod . ' !');
171: }
172: }
173: }
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: private function _serializeArray($wordListArray) {
184: $retString = '';
185: foreach ($wordListArray as $key => $value) {
186:
187: $retString .= $key . self::$originalTranslationDivider . $value . "\r\n";
188: }
189:
190: return $retString;
191: }
192:
193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
204: private function _unserializeArray($string) {
205: $retArray = array();
206:
207: $words = preg_split('((\r\n)|(\r)|(\n))', substr($string, 0, strlen($string) - strlen(PHP_EOL)));
208:
209: foreach ($words as $key => $value) {
210: $oriTrans = preg_split('/(?<!\\\\)' . self::$originalTranslationDivider . '/', $value);
211:
212: if (isset($oriTrans[1])) {
213: $retArray[cString::recodeString($oriTrans[0], $this->_fileEncoding, $this->_encoding)] = cString::recodeString(str_replace("\=", "=", $oriTrans[1]), $this->_fileEncoding, $this->_encoding);
214: } else {
215:
216: $keys = array_keys($retArray);
217: $lastKey = end($keys);
218: $newValue = PHP_EOL . cString::recodeString(str_replace("\=", "=", $oriTrans[0]), $this->_fileEncoding, $this->_encoding);
219: $retArray[$lastKey] .= $newValue;
220: }
221: }
222:
223: return $retArray;
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: public function saveTranslationArray($wordListArray) {
234: $fileName = $this->_modulePath . $this->_directories['lang'] . self::$fileName;
235:
236: if (!$this->createModuleDirectory('lang') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['lang'])) {
237: return false;
238: }
239:
240: $escapedArray = array();
241: foreach ($wordListArray as $key => $value) {
242: $newKey = mb_ereg_replace("=", "\\=", $key);
243: $newValue = mb_ereg_replace("=", "\\=", $value);
244: $escapedArray[$newKey] = $newValue;
245: }
246:
247: if (cFileHandler::write($fileName, $this->_serializeArray($escapedArray)) === false) {
248: return false;
249: } else {
250: return true;
251: }
252: }
253:
254: 255: 256: 257: 258:
259: public function getTranslationArray() {
260: if (cFileHandler::exists($this->_modulePath . $this->_directories['lang'] . self::$fileName)) {
261: $array = $this->_unserializeArray(cFileHandler::read($this->_modulePath . $this->_directories['lang'] . self::$fileName));
262: return $array;
263: } else {
264: return array();
265: }
266: }
267:
268: }
269: