<?php
namespace Teacher\Controller;
use Common\Controller\HomebaseController;
class MarketController extends HomebaseController {
    function _initialize() {
		parent::_initialize();
		$this->check_teacher();
		$this->user_obj = D("Common/Users");
		$this->coursetype_obj = D("Common/Coursetype");
		//$this->market_obj = M('Market');
		$this->course_obj = D("Common/Course");
		$userid=sp_get_current_userid();
		$user=$this->user_obj->where(array('id'=>$userid))->find();
		$avatar=$user['avatar'];
		$this->assign('avatar',$avatar);
		$this->assign('user',$user);
	}
	public function index(){
		$this->display();
	}
	public function purchased(){
		$this->display();
	}
	public function bangding(){
		$this->display();
	}
	public function bangdingPost(){
		$data=I("post.");
		$userid=sp_get_current_userid();
		if($data['states']=='error'){
			$this->error($data['message']);
		}
		if($data['states']=='success'){
			$udata['marketcount']=$data['marketcount'];
			$udata['marketpass']=$data['marketpass'];
			if($this->user_obj->where(array('id'=>$userid))->save($udata)){
				$this->success("绑定成功"); 
			}else{
				$this->error("绑定失败"); 
			}
		}
	}
	function import(){
		$MarketId=I('get.id');
		$user=$this->user_obj->where(array('id'=>sp_get_current_userid()))->find();
		$this->_getTree();
		$this->assign('marketId',$MarketId);
		$this->assign('user',$user);
		$this->display();
	}
	function checkimport(){
		 $markedid=I("post.markedid");
		 if($daye=$this->course_obj->where(array('markedid'=>$markedid))->find()){
			 $result['states']=true;
		 }else{
			 $result['states']=false;
		 }
		 $json_string = json_encode($result);
		 echo $json_string;
	}
	 private function _getTree($term_id){
		$result = $this->coursetype_obj->order(array("listorder"=>"asc"))->select();
		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminCoursetype/add", array("parent" => $r['term_id'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$r['visit'] = "<a href='#'>访问</a>";
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id']=$r['term_id'];
			$r['parentid']=$r['parent'];
			$r['selected']=$term_id==$r['term_id']?"selected":"";
			$array[] = $r;
		}
		$tree->init($array);
		$str="<option value='\$id' \$selected>\$spacer\$name</option>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
	}
}