1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\XoopsObject;
14: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: include_once \XoopsBaseConfig::get('root-path') . '/modules/xlanguage/include/vars.php';
28:
29: 30: 31:
32: class XlanguageLanguage extends XoopsObject
33: {
34: 35: 36:
37: public function __construct()
38: {
39: $this->initVar('xlanguage_id', XOBJ_DTYPE_INT, 0, false, 10);
40: $this->initVar('xlanguage_name', XOBJ_DTYPE_TXTBOX, '', false);
41: $this->initVar('xlanguage_description', XOBJ_DTYPE_TXTBOX, '', false);
42: $this->initVar('xlanguage_code', XOBJ_DTYPE_TXTBOX, '', false);
43: $this->initVar('xlanguage_charset', XOBJ_DTYPE_TXTBOX, 'utf-8', false);
44: $this->initVar('xlanguage_image', XOBJ_DTYPE_TXTBOX, '_unknown.png', false);
45: $this->initVar('xlanguage_weight', XOBJ_DTYPE_INT, 1, false, 10);
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: public function getValues($keys = null, $format = 's', $maxDepth = 1)
56: {
57: $ret = parent::getValues();
58: $ret['xlanguage_image'] = \XoopsBaseConfig::get('url') . '/media/xoops/images/flags/' . \Xoops\Module\Helper::getHelper('xlanguage')->getConfig('theme') . '/' . $this->getVar('xlanguage_image');
59: return $ret;
60: }
61:
62: public function cleanVarsForDB()
63: {
64: $system = System::getInstance();
65: foreach (parent::getValues() as $k => $v) {
66: if ($k !== 'dohtml') {
67: if ($this->vars[$k]['data_type'] == XOBJ_DTYPE_STIME || $this->vars[$k]['data_type'] == XOBJ_DTYPE_MTIME || $this->vars[$k]['data_type'] == XOBJ_DTYPE_LTIME) {
68: $value = $system->cleanVars($_POST[$k], 'date', date('Y-m-d'), 'date') + $system->cleanVars($_POST[$k], 'time', date('u'), 'int');
69: $this->setVar($k, isset($_POST[$k]) ? $value : $v);
70: } elseif ($this->vars[$k]['data_type'] == XOBJ_DTYPE_INT) {
71: $value = $system->cleanVars($_POST, $k, $v, 'int');
72: $this->setVar($k, $value);
73: } elseif ($this->vars[$k]['data_type'] == XOBJ_DTYPE_ARRAY) {
74: $value = $system->cleanVars($_POST, $k, $v, 'array');
75: $this->setVar($k, $value);
76: } else {
77: $value = $system->cleanVars($_POST, $k, $v, 'string');
78: $this->setVar($k, $value);
79: }
80: }
81: }
82: }
83: }
84:
85: 86: 87:
88: class XlanguageXlanguageHandler extends XoopsPersistableObjectHandler
89: {
90: 91: 92:
93: public function __construct(Connection $db = null)
94: {
95: parent::__construct($db, 'xlanguage', 'XlanguageLanguage', 'xlanguage_id', 'xlanguage_name');
96: $this->loadConfig();
97: }
98:
99: 100: 101:
102: public function loadConfig()
103: {
104: $xoops = Xoops::getInstance();
105: $this->configPath = \XoopsBaseConfig::get('var-path') . '/configs/';
106: $this->configFile = $xoops->registry()->get('XLANGUAGE_CONFIG_FILE');
107: $this->configFileExt = '.php';
108: return $this->cached_config = $this->loadFileConfig();
109: }
110:
111: 112: 113:
114: public function loadFileConfig()
115: {
116: $cached_config = $this->readConfig();
117: if (empty($cached_config)) {
118: $cached_config = $this->createConfig();
119: }
120: return $cached_config;
121: }
122:
123: 124: 125:
126: public function readConfig()
127: {
128: $path_file = $this->configPath . $this->configFile . $this->configFileExt;
129: XoopsLoad::load('XoopsFile');
130: $file = XoopsFile::getHandler('file', $path_file);
131: return eval(@$file->read());
132: }
133:
134: 135: 136:
137: public function createConfig()
138: {
139: $cached_config = array();
140: foreach ($this->getAllLanguage(false) as $key => $language) {
141: $cached_config[$language['xlanguage_name']] = $language;
142: }
143: $this->writeConfig($cached_config);
144: return $cached_config;
145: }
146:
147: 148: 149: 150: 151:
152: public function writeConfig($data)
153: {
154: if ($this->createPath($this->configPath)) {
155: $path_file = $this->configPath . $this->configFile . $this->configFileExt;
156: XoopsLoad::load('XoopsFile');
157: $file = XoopsFile::getHandler('file', $path_file);
158: return $file->write('return ' . var_export($data, true) . ';');
159: }
160: }
161:
162: 163: 164: 165: 166: 167:
168: private function createPath($pathname, $pathout = null)
169: {
170: $xoops = Xoops::getInstance();
171: $pathname = substr($pathname, strlen(\XoopsBaseConfig::get('root-path')));
172: $pathname = str_replace(DIRECTORY_SEPARATOR, '/', $pathname);
173:
174: $dest = ($pathout === null) ? \XoopsBaseConfig::get('root-path') : $pathout;
175: $paths = explode('/', $pathname);
176:
177: foreach ($paths as $path) {
178: if (!empty($path)) {
179: $dest = $dest . '/' . $path;
180: if (!is_dir($dest)) {
181: if (!mkdir($dest, 0755)) {
182: return false;
183: } else {
184: $this->writeIndex($xoops->path('uploads'), 'index.html', $dest);
185: }
186: }
187: }
188: }
189: return true;
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: private function writeIndex($folder_in, $source_file, $folder_out)
200: {
201: if (!is_dir($folder_out)) {
202: if (!$this->createPath($folder_out)) {
203: return false;
204: }
205: }
206:
207:
208: if (is_file($folder_in . '/' . $source_file)) {
209: return copy($folder_in . '/' . $source_file, $folder_out . '/' . basename($source_file));
210: }
211: return false;
212: }
213:
214: 215: 216: 217: 218:
219: public function getByName($name = null)
220: {
221: $xoops = Xoops::getInstance();
222: $name = empty($name) ? $xoops->getConfig('locale') : strtolower($name);
223:
224: $file_config = $xoops->registry()->get('XLANGUAGE_CONFIG_FILE');
225: if (!XoopsLoad::fileExists($file_config) || !isset($this->cached_config)) {
226: $this->loadConfig();
227: }
228:
229: if (isset($this->cached_config[$name])) {
230: return $this->cached_config[$name];
231: }
232: return null;
233: }
234:
235: 236: 237: 238: 239:
240: public function getAllLanguage($asobject = true)
241: {
242: $criteria = new CriteriaCompo();
243: $criteria->setSort('xlanguage_weight');
244: $criteria->setOrder('asc');
245:
246: return parent::getAll($criteria, null, $asobject, true);
247: }
248:
249: 250: 251:
252: public function renderlist()
253: {
254: $xoops = Xoops::getInstance();
255: $xoops->tpl()->assign('theme', \Xoops\Module\Helper::getHelper('xlanguage')->getConfig('theme'));
256: $xoops->tpl()->assign('languages', $this->getAllLanguage(false));
257: return $xoops->tpl()->fetch('admin:xlanguage/xlanguage_admin_list.tpl');
258: }
259: }
260: