<?php


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

class ModStatic extends Module {
    protected $_filters = array(
        'check_login' => '{view}{custom_html}{company_intro}'
    );
    
    public function view() {
        $this->_layout = 'frontpage';
    	
        $sc_id = ParamHolder::get('sc_id', '0');
        $curr_locale = trim(SessionHolder::get('_LOCALE'));
        if (intval($sc_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_error';
        }
        $user_role = trim(SessionHolder::get('user/s_role', '{guest}'));
        try {
            $o_scontent = new StaticContent();
            $menu_items = new MenuItem();// for show title
            if ($user_role == '{admin}') {
                $curr_scontent =& $o_scontent->find("`id`=? AND "
                            ."published='1' AND s_locale=?", 
                        array($sc_id, $curr_locale));
                // for show title
//                $curr_menuitem =& $menu_items->find("`link`='_m=mod_static&_a=view&sc_id={$sc_id}' AND "
//                            ."published='1' AND s_locale=?", 
//                        array($curr_locale));
            } else {
                $curr_scontent =& $o_scontent->find("`id`=? AND "
                            ."published='1' AND for_roles LIKE ? AND s_locale=?", 
                        array($sc_id, '%'.$user_role.'%', $curr_locale));
               	// for show title
//               	$curr_menuitem =& $menu_items->find("`link`='_m=mod_static&_a=view&sc_id={$sc_id}' AND "
//                            ."published='1' AND for_roles LIKE ? AND s_locale=?", 
//                        array('%'.$user_role.'%', $curr_locale));
            }
//            $this->assign('cur_title', $curr_menuitem->name);
            $this->assign('page_title', $curr_scontent->title);
            $this->assign('curr_scontent', $curr_scontent);
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_error';
        }
    }
    
    public function custom_html() {
    	//echo ParamHolder::get('block_id', '');
        $this->setVar('html', ParamHolder::get('html', ''));
    }
    
    public function company_intro() {
    	$curr_locale = trim(SessionHolder::get('_LOCALE'));
    	$user_role = trim(SessionHolder::get('user/s_role', '{guest}'));
    	
    	if (preg_match('/^\d+$/i', trim(ParamHolder::get('cpy_intro_number')))) {
    		$cpy_intro_number = trim(ParamHolder::get('cpy_intro_number'));
    	} else {
    		$cpy_intro_number = 150;
    	}
    	try {
            $o_scontent = new StaticContent();
    	    if ($user_role == '{admin}') {
    	    	if($curr_locale == 'en') {
    	    		$curr_scontent =& $o_scontent->find("`id`=4 AND published='1'");
    	    	} elseif($curr_locale == 'zh_CN') {
	                $curr_scontent =& $o_scontent->find("`id`=2 AND published='1'");
    	    	}  
            } else {
            	if($curr_locale == 'en') {
	                $curr_scontent =& $o_scontent->find("`id`=? AND "
	                            ."published='1' AND for_roles LIKE ?", 
	                        array(4, '%'.$user_role.'%'));
	            } elseif($curr_locale == 'zh_CN') {
	            	$curr_scontent =& $o_scontent->find("`id`=? AND "
	                            ."published='1' AND for_roles LIKE ?", 
	                        array(2, '%'.$user_role.'%'));
	            }
            }
            $this->assign('curr_scontent', $curr_scontent);
            $this->assign('cpy_intro_number', $cpy_intro_number);
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_error';
        }
    }
}
?>