<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class ModLang extends Module {
    
	protected $_filters = array(
        'check_admin' => ''
    );
	
    public function admin_list() {
        $this->_layout = 'content';
        
        $o_lang = new Language();
        $langs =& $o_lang->findAll();
        
        $this->assign('langs', $langs);
    }
    
    public function admin_add() {
        $this->_layout = 'content';
    }
    
    public function admin_create() {
        
        $locale_info =& ParamHolder::get('lang', array());
        if (sizeof($locale_info) != 2) {
            Notice::set('mod_lang/msg', __('Invalid language data!'));
            Content::redirect(Html::uriquery('mod_lang', 'admin_add'));
        }
        $file_info =& ParamHolder::get('lang_file', array(), PS_FILES);
        if (empty($file_info)) {
            Notice::set('mod_lang/msg', __('Invalid post file data!'));
            Content::redirect(Html::uriquery('mod_lang', 'admin_add'));
        }
        if (!$this->_saveLangFile($locale_info['locale'], $file_info)) {
            Notice::set('mod_lang/msg', __('Uploading language file failed!'));
            Content::redirect(Html::uriquery('mod_lang', 'admin_add'));
        }
        try {
            $o_lang = new Language();
            $o_lang->set($locale_info);
            $o_lang->save();
        } catch (Exception $ex) {
            Notice::set('mod_lang/msg', $ex->getMessage());
            Content::redirect(Html::uriquery('mod_lang', 'admin_add'));
        }
        
        Notice::set('mod_lang/msg', __('Uploading language file succeeded!'));
        Content::redirect(Html::uriquery('mod_lang', 'admin_list'));
    }
    
    public function admin_delete() {
        
        $curr_lang_id = trim(ParamHolder::get('l_id', '0'));
        if (intval($curr_lang_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_result';
        }
        try {
            $curr_lang = new Language($curr_lang_id);
            if ($curr_lang->locale == SessionHolder::get('_LOCALE')) {
                $this->assign('json', Toolkit::jsonERR(__('Cannot delete default language!')));
                return '_result';
            } else {
            	if (!Toolkit::rmdir_locale($curr_lang->locale)) {
	                $this->assign('json', Toolkit::jsonERR(__('Deleting language failed!')));
	                return '_result';
            	}
                $curr_lang->delete();
            }
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return ('_result');
        }
        
        $this->assign('json', Toolkit::jsonOK());
        return '_result';
    }
    
    public function admin_make_default() {
        
        $curr_lang_id = trim(ParamHolder::get('l_id', '0'));
        if (intval($curr_lang_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_result';
        }
        try {
            $curr_lang = new Language($curr_lang_id);
            $o_param = new Parameter();
            $locale_param =& $o_param->find("`key`='DEFAULT_LOCALE'");
            $locale_param->val = $curr_lang->locale;
            $locale_param->save();
            SessionHolder::set('_LOCALE', $curr_lang->locale);
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return ('_result');
        }
        
        $this->assign('json', Toolkit::jsonOK());
        return '_result';
    }
    
    public function langbar() {
        $o_lang = new Language();
        $langs = $o_lang->findAll();
        $this->assign('langs', $langs);
    }
    
    private function _saveLangFile($locale, $struct_file) {
    	$lang_file_path = Toolkit::mkdir_locale($locale);
        if (!$lang_file_path) {
            return false;
        }
        return move_uploaded_file($struct_file['tmp_name'], $lang_file_path.'/messages.mo');
    }
}
?>