<?
	class Banner {
		var $DB;
		var $_LIST_NUM = 20;
		var $_PDS = _PDS;
		var $_PDS_SUB = "/banner/";


		# 생성자
		function Banner($db) {
			$this->DB = $db;
			$this->_PDS .= $this->_PDS_SUB;
		}

		#등록#
		function set_insert($argu){

			$order_num = $this->DB->getOne("select coalesce(max(order_num),0)+1 from ".TABLE_BANNER." where (division = '".$argu["division"]."')");
			if (DB::isError($res)) {
			    go_url("","등록에 실패했습니다!\\n[".$res->getMessage()."]");
			    exit;
			}

			//파일 업로드
			/*if($argu["division"] == "T"){
				$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);
			}
			else if($argu["division"] == "R"){
				$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);
			}*/
			$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);


			$data = array(
				"division" => $argu["division"],
				"image"	=> $_banner_thumb,
				"order_num"	=> $order_num,
				"url"	=> $argu["url"],
				"target" => $argu["target"],
				"published"		=> $argu["published"],
				"title"	=> $argu["title"],
				"description"	=> $argu["description"],
				"url_title"	=> $argu["url_title"],
				"text_use"	=> $argu["text_use"],
				"gubun" => $argu["gubun"]
			);
			
			$sth = $this->DB->autoPrepare(TABLE_BANNER, array_keys($data), DB_AUTOQUERY_INSERT);
			$res = $this->DB->execute($sth, array_values($data));

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
				exit;
			}
			
			$b_id=mysql_insert_id();
			
			$data=array_merge($data,array("b_id"=>$b_id));

			return $data;

		}


		#수정#
		function set_modify($argu){

			//파일 업로드
			/*if($argu["division"] == "T"){
				if($_FILES['image']['tmp_name']){
					$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);
					unlink($_SERVER["DOCUMENT_ROOT"]."/pds/banner/".$argu["tmp_image"]);
				}
				else{
					$_banner_thumb = $argu["tmp_image"];
				}
			}
			else if($argu["division"] == "R"){
				if($_FILES['image']['tmp_name']){
					$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);
					unlink($_SERVER["DOCUMENT_ROOT"]."/pds/banner/".$argu["tmp_image"]);
				}
				else{
					$_banner_thumb = $argu["tmp_image"];
				}
			}*/
			if($_FILES['image']['tmp_name']){
				$_banner_thumb = thumb_upload($_FILES['image'],'/pds/banner/','O',$argu["division"]);
				unlink($_SERVER["DOCUMENT_ROOT"]."/pds/banner/".$argu["tmp_image"]);
			}
			else{
				$_banner_thumb = $argu["tmp_image"];
			}

			$data = array(
				"division" => $argu["division"],
				"image"	=> $_banner_thumb,
				"published"		=> $argu["published"],
				"url"	=> $argu["url"],
				"target" => $argu["target"],
				"title"	=> $argu["title"],
				"description"	=> $argu["description"],
				"url_title"	=> $argu["url_title"],
				"text_use"	=> $argu["text_use"],
				"gubun" => $argu["gubun"]
			);

			$res = $result = $this->DB->autoExecute(TABLE_BANNER, $data, DB_AUTOQUERY_UPDATE, " b_id = '{$argu['b_id']}' ");

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
				exit;
			}
			return true;
		}


		#삭제#
		function set_delete($b_id){
			$_row = $this -> get_view($b_id);

			if($_row["image"]){
				unlink($_SERVER["DOCUMENT_ROOT"]."/pds/banner/".$_row["image"]);
			}

			$query = "DELETE FROM ".TABLE_BANNER." WHERE b_id = '".$b_id."'";
			
			$res = $this->DB->query($query);
			if (DB::isError($res)) {
			    go_url("", $res->getMessage());
			    exit;
			}

			return true;
		}


		#리스트삭제#
		function set_delete_list($argu){
			if(count($argu["chk"]) > 0){
				for($i = 0; $i < count($argu["chk"]); $i++){
					$b_id = $argu["chk"][$i];
					$_list = $this -> set_delete($b_id);
				}
				return true;
			}
			else{
			    go_url("","삭제할데이터가 없습니다.");
				exit;
			}
		}


		#리스트#
		function get_list($argu,&$total,$limit = true){
			global $_adminpage;

			$addwhere = " where (1=1) ";
			if($argu["division"]){$addwhere .= " and (division = '".$argu["division"]."')";}

			/// 검색쿼리
			if($argu['s_string']){	$addwhere .= " and title like '%{$argu['s_string']}%' ";	}

			if($argu['gubun']){	$addwhere .= " and gubun = '{$argu['gubun']}' ";	}

			$query = "
				select
					count(*)
				from
					".TABLE_BANNER."
				".$addwhere;

			$res = $total = $this->DB->getOne($query);
			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			$query = "
				select
					*
				from
					".TABLE_BANNER."
				".$addwhere."
				order by
					order_num asc
			";
			if($limit)
			$query .= "
				limit ".($argu['p']-1)*$this->_LIST_NUM.",".$this->_LIST_NUM;

			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;

		}


		#상세보기
		function get_view($b_id){
			$query = "SELECT 
						* 
					  FROM ".TABLE_BANNER." 
					  where b_id = '".$b_id."'";

			$res = $row = $this->DB->getRow($query,DB_FETCHMODE_ASSOC);
			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $row;
			
		}

		
		#메인비주얼리스트#
		function get_main($division,$lang){
			$addwhere = "WHERE (published = 'Y') and (division = '".$division."') and (gubun = '".$lang."')";

			$query = "SELECT 
						*
					  FROM ".TABLE_BANNER." 
					  ".$addwhere." 
					  ORDER BY order_num asc";

			$res = $list =& $this->DB->getAll($query, array(), DB_FETCHMODE_ASSOC);

			if (DB::isError($res)) {
				go_url("", $res->getMessage());
			    exit;
			}

			return $list;
		}


	

		/*****
		 *정렬값을 수정한다.
		 *****/
		function set_order($argu){

			if($argu["s_mode"] == "up"){

				$res = $b_id = $this->DB->getOne("select b_id from ".TABLE_BANNER." where (order_num < '".$argu["order_num"]."') and (division = '".$argu["division"]."') order by order_num desc limit 1");

				if($b_id){
					
					$query = "
						update ".TABLE_BANNER."
						set
							order_num = order_num - 1
						where
							b_id = '".$argu["b_id"]."'
					";
					$res = $this->DB->query($query);
					if (DB::isError($res)) {
						go_url("", $res->getMessage());
						exit;
					}
					
					$query1 = "
						update ".TABLE_BANNER."
						set
							order_num = order_num + 1
						where
							b_id = '".$b_id."'
					";

					$res1 = $this->DB->query($query1);
					if (DB::isError($res1)) {
						go_url("", $res1->getMessage());
						exit;
					}

				}
				else{
					go_url("", "정열값이 최상위 입니다.");
					exit;
				}
			
			}
			else if($argu["s_mode"] == "down"){
				$res = $b_id = $this->DB->getOne("select b_id from ".TABLE_BANNER." where (order_num > '".$argu["order_num"]."') and (division = '".$argu["division"]."') order by order_num asc limit 1");

				if($b_id){
					
					$query = "
						update ".TABLE_BANNER."
						set
							order_num = order_num + 1
						where
							b_id = '".$argu["b_id"]."'
					";
					$res = $this->DB->query($query);
					if (DB::isError($res)) {
						go_url("", $res->getMessage());
						exit;
					}
					
					$query1 = "
						update ".TABLE_BANNER."
						set
							order_num = order_num - 1
						where
							b_id = '".$b_id."'
					";

					$res1 = $this->DB->query($query1);
					if (DB::isError($res1)) {
						go_url("", $res1->getMessage());
						exit;
					}

				}
				else{
					go_url("", "정열값이 최하위 입니다.");
					exit;
				}
			}
			
			return true;
		}



	}
?>