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

class ModQq extends Module {
    
	protected $_filters = array(
        'check_admin' => ''
    );
	
    public function admin_list() {
    	$this->_layout = 'content';
    	
        $o_qq = new OnlineQq();
        $qqs =& $o_qq->findAll();
        
        $this->assign('content_title', __('service'));
        $this->assign('next_action', 'admin_service_create');
        $this->assign('roles', Toolkit::loadAllRoles());
        $o_param = new Parameter();
        $s_param =& $o_param->find("`key`='SERVICE53'");
        $this->assign('service', $s_param->val);
        $this->assign('qqs', $qqs);
        
        // for sitestarv1.3 online customer service
        $site_param =& ParamHolder::get('sparam', array());
        $str_temp = trim(ParamHolder::get('hidqq_online', ''));
        if (isset($site_param['QQ_ONLINE']) || !empty($str_temp)) {
        	$o_param = new Parameter();
        	$site_param['QQ_ONLINE'] = !empty($str_temp) ? '0' : $site_param['QQ_ONLINE'];
        	foreach ($site_param as $key => $val) {
        	    $param =& $o_param->find('`key`=?', array($key));
        	    if ($param) {
        	        $param->val = $val;
        	        $param->save();
        	    }
        	}
        	@header('Location: '.Html::uriquery('mod_qq','admin_list'));
        }
    }
    
    public function admin_add() {
    	$this->_layout = 'content';
    	
		$select_categories = array('QQ','MSN', __('WangWang'),'Skype');
        $this->assign('select_categories', $select_categories);
        $this->assign('content_title', __('New IM Account'));
        $this->assign('next_action', 'admin_create');
        
        return '_form';
    }
    
    public function admin_create() {
        
        $qq_info =& ParamHolder::get('qq', array());
        if (sizeof($qq_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing Account information!')));
            return '_result';
        }
        try {
        	// Re-arrange publish status
//            if ($qq_info['published'] == '1') {
//                $qq_info['published'] = '1';
//            } else {
//                $qq_info['published'] = '0';
//            }
            $qq_info['published'] = '1';
            // Data operation
            $o_qq = new OnlineQq();
            $o_qq->set($qq_info);
            $o_qq->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_qq', 'admin_list'))));
        return '_result';
    }
    
    public function admin_edit() {
    	$this->_layout = 'content';
    	
        $q_id = ParamHolder::get('q_id', '0');
        if (intval($q_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_error';
        }
        
        try {
            $curr_qq = new OnlineQq($q_id);
            $this->assign('curr_qq', $curr_qq);
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_error';
        }
        $select_categories = array('QQ','MSN', __('WangWang'),'Skype');
        $this->assign('select_categories', $select_categories);
        $this->assign('content_title', __('Edit IM Account'));
        $this->assign('next_action', 'admin_update');
        
        return '_form';
    }
    
    public function admin_update() {
        
        $qq_info =& ParamHolder::get('qq', array());
        if (sizeof($qq_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing Account information!')));
            return '_result';
        }
        try {
        	// Re-arrange publish status
//            if ($qq_info['published'] == '1') {
//                $qq_info['published'] = '1';
//            } else {
//                $qq_info['published'] = '0';
//            }
            
            // Data operation
            $o_qq = new OnlineQq($qq_info['id']);
            $o_qq->set($qq_info);
            $o_qq->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_qq', 'admin_list'))));
        return '_result';
    }
    
    public function admin_delete() {
        
        $q_id = trim(ParamHolder::get('q_id', '0'));
        if (intval($q_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_result';
        }
        try {
			$tmp_arr = explode('_', $q_id);
			$len = sizeof($tmp_arr);
			for ($i = 0; $i< $len; $i++){
				$curr_qq = new OnlineQq($tmp_arr[$i]);
				$curr_qq->delete();
			}
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return ('_result');
        }
        
        $this->assign('json', Toolkit::jsonOK());
        return '_result';
    }
    
	public function admin_pic()
    {
    	$qq_info = array();
    	$qq_id = trim(ParamHolder::get('_id', ''));
    	if(!empty($qq_id))
    	{
    		$o_qq = new OnlineQq($qq_id);
            if($o_qq->published == 1)
            {
            	$qq_info['published'] = '0';
            	$o_qq->set($qq_info);
            	$o_qq->save();
				die('0');
            }
            elseif($o_qq->published == 0)
            {
            	$qq_info['published'] = '1';
            	$o_qq->set($qq_info);
            	$o_qq->save();
				die('1');
            }
    	}
    }
}
?>
