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