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