<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2013.11.19
*/

class control extends adminbase {
    
    
	private $_comeurl = NUll;
    private $_urlitem = NULL;
    
    private $stitle = NULL;

    private function _getItems() {
        $this->stitle = XRequest::getGpc('stitle');
        $this->_urlitem = "stitle=".urlencode($this->stitle)."";
        $this->_comeurl = $this->_urlitem."&page=".$this->page."";
    }

	
    public function action_run() {
        $this->checkAuth('guestbool_volist');
        $this->_getItems();
        
        $searchsql = "";
        if (!empty($this->stitle)) {
            $searchsql .= " AND lower(v.title) LIKE '%".strtolower($this->stitle)."%'";
        }
        $model = parent::model('guestbool', 'am');
        list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
        
        $url = XRequest::getPhpSelf();
        $url .= '?c=guestbool&'.$this->_urlitem;
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::admin($total, $this->pagesize, $this->page, $url, 10),
            'urlitem'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'stitle'=>$this->stitle,
            'guestbool'=>$data,
        );
        unset($model);
		TPL::assign($var_array);
		TPL::display($this->cptpl.'guestbool.tpl');
	}
    
    
    public function action_add() {
        $this->checkAuth('guestbool_add');
        $this->_getItems();
           
        $var_array = array(
            'timeline'=>time(),
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'guestbool.tpl');
    }
    
    
    public function action_edit() {
        $this->checkAuth('guestbool_edit');
        $this->_getItems();
        
        $id = XRequest::getInt('id');
        $this->_validID($id);
        $model = parent::model('guestbool', 'am');
        list($data) = $model->getData($id);
        if (empty($data)) {
            XHandle::halt('对不起，读取数据不存在！', '', 1);
        }
        else {

            $var_array = array(
                'guestbool'=>$data,
                'id'=>$id,
                'page'=>$this->page,
                'comeurl'=>$this->_comeurl,
                'timeline'=>time(),
            );
            TPL::assign($var_array);
            TPL::display($this->cptpl.'guestbool.tpl');
        }
        unset($model);
    }
    
    
    public function action_saveadd() {
        $this->checkAuth('guestbool_add');
        $args = $this->_validAdd();
        $model = parent::model('guestbool', 'am');
        $result = $model->doAdd($args);
        unset($model);
        if (true === $result) {
            $this->log('guestbool_add', '', 1);
            XHandle::halt('添加成功', $this->cpfile.'?c=guestbool', 0);
        }
        else {
            $this->log('guestbool_add', '', 0);
            XHandle::halt('添加失败', '', 1);
        }
        
	}
    
    
    public function action_saveedit() {
        $this->checkAuth('guestbool_edit');
        $this->_getItems();
        
        list($id, $args) = $this->_validEdit();
        $model = parent::model('guestbool', 'am');
        $result = $model->doEdit($id, $args);
		unset($model);
        if (true === $result) {
            $this->log('guestbool_edit', "id={$id}", 1);
			XHandle::halt('编辑成功', $this->cpfile.'?c=guestbool&'.$this->_comeurl.'', 0);
		}
        else {
            $this->log('guestbool_edit', "id={$id}", 0);
			XHandle::halt('编辑失败', '', 1);
		}
        
    }
    
    
    public function action_del() {
        $this->checkAuth('guestbool_del');
        $this->_getItems();
        
        $array_id = XRequest::getArray('id');
        $this->_validID($array_id);
        $model = parent::model('guestbool', 'am');
        for($ii=0; $ii<count($array_id); $ii++){
            $id = intval($array_id[$ii]);
            $result = $model->doDel($id);            
        }
        unset($model);
        
        if (true === $result) {
            $this->log('guestbool_del', "id={$array_id}", 1);
            XHandle::halt('删除成功', $this->cpfile.'?c=guestbool', 0);
        }
        else {
            $this->log('guestbool_del', "id={$array_id}", 0);
            XHandle::halt('删除失败', '', 1);
        }
         
    }
    
    
    public function action_update() {
        $this->checkAuth('guestbool_edit');
        $args = XRequest::getGpc(array('id', 'type'));
        $model = parent::model('guestbool', 'am');
        $model->doUpdate(intval($args['id']), trim($args['type']));
        unset($model);
    }
    
    
    private function _validAdd() {
        $args = XRequest::getGpc(array(
            'title', 'username', 'email', 'content',
            'flag', 'qq', 'msn', 'address', 'telephone', 'mobile',
			'addtime',
        ));
        if (empty($args['title'])) {
            XHandle::halt('请填写标题', '', 1);
        }
        if (empty($args['username'])) {
            XHandle::halt('请填写姓名', '', 1);
        }
        if (empty($args['email'])) {
            XHandle::halt('请填写邮箱', '', 1);
        }
		else {
			if (false === XValid::isEmail($args['email'])) {
				XHandle::halt('邮箱格式不正确', '', 1);
			}
		}
        if (empty($args['content'])) {
            XHandle::halt('请填写留言内容', '', 1);
        }
        $args['flag'] = intval($args['flag']);
		$args['addtime'] = strtotime(trim(str_replace('&nbsp;', ' ', $args['addtime'])));
        return $args;
    }
    
    
    private function _validEdit() {
        $id = XRequest::getInt('id');
        $args = XRequest::getGpc(array(
            'title', 'username', 'email', 'content', 'flag',
			'addtime', 'replycontent', 'replyuser',
        ));
        if ($id<1) {
            XHandle::halt('ID丢失', '', 1);
        }
        if (empty($args['title'])) {
            XHandle::halt('请填写标题', '', 1);
        }
        if (empty($args['username'])) {
            XHandle::halt('请填写姓名', '', 1);
        }
        if (empty($args['email'])) {
            XHandle::halt('请填写邮箱', '', 1);
        }
		else {
			if (false === XValid::isEmail($args['email'])) {
				XHandle::halt('邮箱格式不正确', '', 1);
			}
		}
        if (empty($args['content'])) {
            XHandle::halt('请填写留言内容', '', 1);
        }
        $args['flag'] = intval($args['flag']);
		$args['addtime'] = strtotime(trim(str_replace('&nbsp;', ' ', $args['addtime'])));
		if (!empty($args['replycontent'])) {
			$args = array_merge($args, array('replyflag'=>1, 'replytime'=>time(),));
		}
        return array($id, $args);
    }
    
    
    private function _validID($id) {
        if (empty($id)) {
            XHandle::halt('ID丢失，请选择要操作的ID', '', 1);
        }
    }
         public function action_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }
    
}
?>
