<?php
// +----------------------------------------------------------------------
// | 微信关键词管理
// +----------------------------------------------------------------------
// | ThinkOAO （Online And Offline）
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.thinkoao.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xiaogg <xiaogg@sina.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
class WxKeywordsController extends BaseController{
    public function _initialize() {
        parent::_initialize ();
        
        $this->typearr=array('完全匹配','左边匹配','右边匹配','模糊匹配');
        $this->assign('typearr', $this->typearr);
        $this->keytypearray=array('text'=>'文字回复','news'=>'图文回复');
        $this->assign('keytypearray', $this->keytypearray);
        $this->model = D (CONTROLLER_NAME);
        $this->uid=1;
    }
    public function index(){
        $name = CONTROLLER_NAME;
        $map=array();
        $keytype=I('keytype');
        $keyword=I('keyword');
        if($keytype)$map['keytype']=$keytype;
        if($keyword)$map['keyword']=array('like',"%".$keyword."%");
        $list = $this->lists($name,$map);
        $this->assign('list', $list);
        /*$big_menu = array('添加文本回复' => U('add',array('keytype'=>'text')),'添加图文回复' => U('add',array('keytype'=>'news')));
        $this->assign('big_menu', $big_menu);*/$this->assign('keytype', $keytype);
        $this->display();
    }
    public function _before_add(){
        $keytype=I('get.keytype');
        if(empty($keytype))$this->error('参数错误');
        $this->assign('keytype', $keytype);
    }
    public function insert() {
        if (false === $this->model->create ()) {$this->error ( $this->model->getError () );}
        $id = $this->model->add();
        if ($id !==false) {
            $keytype=I('post.keytype');
            $model_content=M('Wx'.ucfirst($keytype));
            $postdata=I('post.config');
            $postdata['id']=$id;
            $postdata['jump_url'] = return_urlrules($data['jump_url']);
            $model_content->add($postdata);
            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(CONTROLLER_NAME.'/index');
            $this->assign ( 'jumpUrl',$jumpUrl );
            $this->success (L('ADD_OK'));
        } else {
            $this->error (L('ADD_ERROR').': '.$this->model->getDbError());
        }
    }
    public function _before_edit(){
        $keytype=I('get.keytype');
        $id=I('get.id');
        if(empty($keytype) || empty($id))$this->error('参数错误');
        $model_content=M('Wx'.ucfirst(I('keytype')));
        $key_content=$model_content->field(true)->find($id);
        $this->assign ($key_content);
    }
    public function update (){
        $data=$this->model->create();
        $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME.'/index');
        $where['id'] =I('post.id');
        $msg   = array_merge( array('success'=>L('edit_ok'), 'error'=>L('edit_error'), 'url'=>$jumpUrl ,'ajax'=>IS_AJAX) , (array)$msg );
        $res=$this->model->where($where)->save($data);
        if( $res!==false ) {            
            $keytype=I('post.keytype');
            $model_content=M('Wx'.ucfirst($keytype));
            $postdata=I('post.config');
            $postdata['jump_url'] = return_urlrules($postdata['jump_url']);
            $model_content->where($where)->save($postdata);
            $this->success($msg['success'],$msg['url'],$msg['ajax']);
        }else{
            $this->error($msg['error'],$msg['url'],$msg['ajax']);
        }
    }
    public function first(){//首次关注回复
        $info=M('WxFirst')->field(true)->find($this->uid);
        if($info['keyword']){
            $status['status'] = 2;
        }else{
            $status['status'] = 1;
        }
        $this->assign ($status);
        $map['status'] = 1;
        $wx_keywords = M('WxKeywords')->where($map)->field('id,keyword')->select();
        $this->assign('wx_keywords',$wx_keywords);
        if(!$info)$info['id']=$this->uid;
        $this->assign ($info);
        $this->display();
    }
    public function updatefirst(){
        $firstmod=M('WxFirst');
        $info=$firstmod->field(true)->find($this->uid);
        $postdata = I('post.');
        if($postdata['status'] == 2){
            $data['keyword'] = $postdata['keyword'];
            $data['content'] = '';
        }else{
            $data['keyword'] = '';
            $data['content'] = $postdata['content'];
        }
        if($info){
            $map = array();
            $map['id'] = $postdata['id'];
            $result=$firstmod->where($map)->save($data);
        }else{
            $data['id'] = $postdata['id'];
            $result=$firstmod->add($data);
        }
        if($result){ $this->success(L('DO_OK'));
        }else{
            $this->error(L('DO_ERROR'));
        }
    }
}