<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class bookControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
			 
		}
		
		public function onDefault(){
			$where=" status=1 AND isprivate=0 ";
			$url="/module.php?m=book";
			$limit=24;
			$start=get("per_page","i");
			$option=array(
				"start"=>$start,
				"limit"=>$limit,
				"order"=>" bookid DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=MM("book","book")->Dselect($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$v['imgurl']=images_site($v['imgurl']);
					$data[$k]=$v;
				}
			}
			$reclist=MM("book","book")->Dselect(array(
				"where"=>" isrecommend=1 AND status=1 AND isprivate=0",
				"limit"=>24
			));
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$per_page=$start+$limit;
			$per_page=$per_page>$rscount?0:$per_page;
			$this->smarty->goassign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"per_page"=>$per_page,
					"reclist"=>$reclist
				)
			);
			$this->smarty->display("book/index.html");
		}
		
		public function onList(){
			$where=" status=1 AND isprivate=0 ";
			$url="/module.php?m=book&a=list";
			$catid=get("catid","i");
			$cat=[];
			if($catid){
				$where.=" AND catid=".$catid;
				$url.="&catid=".$catid;
				$cat=M("mod_book_category")->selectRow("catid=".$catid);
			}
			$limit=24;
			$start=get("per_page","i");
			$option=array(
				"start"=>$start,
				"limit"=>$limit,
				"order"=>" bookid DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=MM("book","book")->Dselect($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$v['imgurl']=images_site($v['imgurl']);
					$data[$k]=$v;
				}
			}
			$reclist=MM("book","book")->Dselect(array(
				"where"=>" isrecommend=1 AND status=1 AND isprivate=0",
				"limit"=>24
			));
			$catList=M("mod_book_category")->select(array(
				"where"=>" status=1 ",
				"order"=>" orderindex ASC"
			));
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$per_page=$start+$limit;
			$per_page=$per_page>$rscount?0:$per_page;
			$this->smarty->goassign(
				array(
					"catid"=>$catid,
					"cat"=>$cat,
					"catList"=>$catList,
					"list"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"per_page"=>$per_page,
					"reclist"=>$reclist
				)
			);
			$this->smarty->display("book/list.html");
		}
		
		public function onShow(){
			$bookid=get_post("bookid","i");
			$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid} AND status=1 "));
			if(empty($data)){
				$this->goAll("课程已下架",1);
			}
			
			$userid=M("login")->userid;
			$isbuy=1;
			if($data['ispay']){
				$isbuy=0;
				$order=M("mod_book_order")->selectRow("bookid=".$bookid." AND isdelete=0 AND userid=".$userid);
				if($order){
					$isbuy=1;
				}
			}
			//是否收藏
			$isfav=0;
			$fav=M("fav")->selectRow("tablename='mod_book' AND userid=".$userid." AND objectid=".$bookid);
			if($fav){
				$isfav=1;
			}
			if($data['isprivate'] && !$isbuy){
				$this->goAll("该书不公开",1);
			}
			$reclist=MM("book","book")->Dselect(array(
				"where"=>" status=1 AND  isrecommend=1 AND isprivate=0 ",
				"limit"=>24
			)); 
			$seo=array(
				"title"=>$data["title"],
				"description"=>$data["description"]
			);
			$this->smarty->assign(
				array("seo"=>$seo)
			);
			$this->smarty->goassign(array(
				"data"=>$data,
				"isbuy"=>$isbuy,
				"reclist"=>$reclist,
				"isfav"=>$isfav
			));
			$this->smarty->display("book/show.html");
		}
		
		 
		public function onView(){
			$bookid=get_post("bookid","i");
			$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid} AND status=1 "));
			if(empty($data)){
				$this->goAll("课程已下架",1);
			}
			$userid=M("login")->userid;
			if($data['ispay'] || $data['isprivate']){
				$order=M("mod_book_order")->selectRow("bookid=".$bookid." AND isdelete=0 AND userid=".$userid);
				if(empty($order)){
					$this->goAll("请先购买图书",1);
				}
				
			}
			if($data['isprivate'] && !$order){
				$this->goAll("该书不公开",1);
			} 
			$artlist=MM("book","book_article")->getFamily($bookid);
			 
			$this->smarty->goassign(array(
				"book"=>$data,
				"artlist"=>$artlist,
				 
			));
			$this->smarty->display("book/view.html");
			
		}
		public function onViewContent(){
			$bookid=get_post("bookid","i");
			if($bookid){
				$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid}"));				
			}
			 
			 
			$this->smarty->goassign(array(
				"book"=>$data,
				 
				 
			));
			$this->smarty->display("book/view_content.html");
		}
		
		public function onMy(){
			M("login")->checkLogin(true);
			$userid=M("login")->userid;
			$where="  userid=".$userid." AND status in(0,1,2) ";
			$url="/module.php?m=book&a=my";
			$limit=24;
			$start=get("per_page","i");
			$option=array(
				"start"=>$start,
				"limit"=>$limit,
				"order"=>" bookid DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=MM("book","book")->Dselect($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$v['imgurl']=images_site($v['imgurl']);
					$data[$k]=$v;
				}
			}
			 
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$per_page=$start+$limit;
			$per_page=$per_page>$rscount?0:$per_page;
			$this->smarty->goassign(
				array(
					"list"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"per_page"=>$per_page,
				 
				)
			);
			$this->smarty->display("book/my.html");
		}
		
		public function onAdd(){
			$bookid=get_post("bookid","i");
			if($bookid){
				$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid}"));
				
			}
			$catList=M("mod_book_category")->select(array(
				"where"=>" status=1 ",
				"order"=>" orderindex ASC"
			));
			$this->smarty->goassign(array(
				"data"=>$data,
				"catList"=>$catList
			));
			$this->smarty->display("book/add.html");
		}
		
		public function onSave(){
			M("login")->checkLogin();
			$bookid=get_post("bookid","i");
			$userid=M("login")->userid;
			$data=M("mod_book")->postData();
			if($bookid){
				$row=M("mod_book")->selectRow("bookid=".$bookid);
				if($row["userid"]!=$userid){
					$this->goAll("暂无权限",1);
				}
				M("mod_book")->update($data,"bookid='$bookid'");
			}else{
				 
				$data["userid"]=intval($userid);
				$data["createtime"]=date("Y-m-d H:i:s");
				M("mod_book")->insert($data);
			}
			$this->goall("保存成功");
		}
		public function onWrite(){
			$bookid=get_post("bookid","i");
			if($bookid){
				$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid}"));				
			}
			$artlist=MM("book","book_article")->getFamily($bookid);
			$this->smarty->goassign(array(
				"book"=>$data,
				"artlist"=>$artlist
			));
			$this->smarty->display("book/write.html");
		}
		
		public function onMenu(){
			$bookid=get_post("bookid","i");
			if($bookid){
				$data=M("mod_book")->selectRow(array("where"=>"bookid={$bookid}"));				
			}
			$artlist=MM("book","book_article")->getFamily($bookid);
			$html="";
			if($artlist){
				
				foreach($artlist as $a){
					$html.="*"."[".$a['id']."]".$a['title']."\r\n";
					if($a['child']){
						foreach($a['child'] as $b){
							$html.="**"."[".$b['id']."]".$b['title']."\r\n";
							if($b['child']){
								foreach($b['child'] as $c){
									$html.="****"."[".$c['id']."]".$c['title']."\r\n";
								}
							}
						}
					}
				}
			}
			$this->smarty->goassign(array(
				"book"=>$data,
				"artlist"=>$artlist,
				"artMenu"=>$html
			));
			$this->smarty->display("book/menu.html");
		}
		
		public function onMenuSave(){
			$bookid=get_post('bookid','i');
			$content=post("content");
			$arr=explode("\n",$content);
			if($arr){
				foreach($arr as $k=>$v){
					if(empty($v)){
						unset($arr[$k]);
					}
				}
				$step=1;
				$fms_key_1=-1;
				$fms_key_2=-1;
				$fms_key_3=-1;
				$oldids=array();
				$colids=M("mod_book_article")->selectCols(array(
					"where"=>"bookid=".$bookid." AND status in(0,1,2) " ,
					"order"=>"id asc"
				));
				
				foreach($arr as $v){
					//解析每行
					$dots=substr($v,0,strpos($v,"["));
					if(!preg_match("/\*+/",$dots,$dota)){
						$this->goALl("*格式出错",1);
					}
					$dot=$dota[0];
					$ids=substr($v,0,strpos($v,"]"));
					if(!preg_match("/\d+/",$ids,$ida)){
						$this->goALl("id出错",1);
					}
					$title=substr($v,strpos($v,"]")+1);
					$id=$ida[0];
					if($id!=0){
						$oldids[]=$id;
					}
					
					$row=array(
						"dot"=>$dot,
						"id"=>$id,
						"title"=>$title
					);
					 
					if(strlen($dot)==4){
						
						$fms_key_3++;
						$fms[$fms_key_1]['child'][$fms_key_2]['child'][$fms_key_3]=$row; 
					}elseif(strlen($dot)==2){
						$fms_key_3=-1;
				 		$fms_key_2++;
				 		$fms[$fms_key_1]['child'][$fms_key_2]=$row; 
				 	}else{
				 		$fms_key_1++;
				 		$fms_key_2=-1;
						$fms_key_3=-1;
				 		$fms[$fms_key_1]=$row; 
				 	}
					
				}
				sort($oldids);
				$addIds=array_diff($oldids,$colids);
				$delIds=array_diff($colids,$oldids);
				 
				if($oldids!=$colids){
					$this->goAll("原有文章出错，请刷新",1);
				}
				if(!empty($delIds)){
					foreach($delIds as $articleid){
						M("mod_book_article")->update(array(
							"status"=>11
						),"id=".$articleid);
					}
				}
				
				//处理新增文章
				$aindex=$bindex=$cindex=0;
				$updata=array();
				foreach($fms as $ak=>$a){
					if($a['id']==0){
						$a['id']=M("mod_book_article")->insert(array(
							"pid"=>0,
							"bookid"=>$bookid,
							"title"=>$a['title'],
							"createtime"=>date("Y-m-d H:i:s")
						));
						M("mod_book_article_data")->insert(array(
							"id"=>$a['id']
						));
										
					}
					$a['orderindex']=$aindex;
					$aindex++;
					$bindex=0;
					$a['pid']=0;
					unset($a['dot']);
					$indata=array(
						"id"=>$a['id'],
						"pid"=>$a['pid'],
						"title"=>$a['title'],
						"orderindex"=>$a['orderindex']
					);
					//M("mod_book_article")->update($indata,"id=".$a['id']);
					//批量更新
					$updata[]=$indata;
					$fms[$ak]=$a;	
					if(isset($a['child'])){
						foreach($a['child'] as $bk=>$b){
							if($b['id']==0){
								$b['id']=M("mod_book_article")->insert(array(
									"pid"=>$a['id'],
									"bookid"=>$bookid,
									"title"=>$b['title'],
									"createtime"=>date("Y-m-d H:i:s")
								));
								M("mod_book_article_data")->insert(array(
									"id"=>$b['id']
								));
							}
							$b['pid']=$a['id'];
							$b['orderindex']=$bindex;
							$bindex++;
							$cindex=0;
							unset($b['dot']);
							$indata=array(
								"id"=>$b['id'],
								"pid"=>$b['pid'],
							 
								"title"=>$b['title'],
								"orderindex"=>$b['orderindex']
							);
							//M("mod_book_article")->update($indata,"id=".$b['id']);
							//批量更新
							$updata[]=$indata;
							$fms[$ak]['child'][$bk]=$b;
							if(isset($b['child'])){
								foreach($b['child'] as $ck=>$c){
									if($c['id']==0){
										$c['id']=M("mod_book_article")->insert(array(
											"pid"=>$b['id'],
											"title"=>$c['title'],
											"bookid"=>$bookid,
											"createtime"=>date("Y-m-d H:i:s")
										));
										M("mod_book_article_data")->insert(array(
											"id"=>$c['id']
										));
										
									}
									$c['orderindex']=$cindex;
									$cindex++;
									$c['pid']=$b['id'];
									unset($c['dot']);
									$indata=array(
										"id"=>$c['id'],
										"pid"=>$c['pid'],
										 
										"title"=>$c['title'],
										"orderindex"=>$c['orderindex']
									);
									
									//M("mod_book_article")->update($indata,"id=".$c['id']);
									//批量更新
									$updata[]=$indata;
									$fms[$ak]['child'][$bk]['child'][$ck]=$c;
								}
							}
							
						}
					}
					
				}
				M("mod_book_article")->updateMore($updata);
				
				  
			}
			
			$this->goAll("处理成功");
		}
		
	}

?>