<?php
	require_once "pageBase.php";
	require_once "../dal/autokeywordlinkDAL.php";
	require_once "../bin/system/conver.php";

	class _autokeywordAddLink extends pageBase
	{
		private $dal;

		//覆蓋父類load()方法
		public function load()
		{
			$this->dal = new autokeywordlinkDAL();
			$this->loadData();
			parent::load();
		}
		public function loadData()
		{
			$this->dal->KeywordID = ctype_digit($_POST["KeywordID"])?$_POST["KeywordID"]:"0";
			
			//$testhttp = explode($_POST["LinkURL"],"http://"); 
			$testhttp = explode("http://",$_POST["LinkURL"]);
			if(count($testhttp) > 1){
				$this->dal->LinkURL = conver::toDbString(trim($_POST["LinkURL"]));
			}else{
				$this->dal->LinkURL = "http://".conver::toDbString(trim($_POST["LinkURL"]));
			}
				
			if($this->dal->KeywordID == "0" || $this->dal->LinkURL == ""){
				parent::alert('輸入資料不完整');
			}else{
				$SameLink = $this->dal->countRecordset("a.KeywordID='".$this->dal->KeywordID."' AND a.LinkURL='".$this->dal->LinkURL."'");
				if($SameLink > 0){
					parent::alert("關鍵字已經建立相同超連結");
				}else{
					$orderNO = $this->dal->countRecordset("a.KeywordID='".$this->dal->KeywordID."'");
					$this->dal->OrderNO = ($orderNO + 1) * 10;
					$this->dal->insertRecord();
					parent::redirect("autokeywordList.php");
				}
			}
		}
	}

	$page = new _autokeywordAddLink();
	$page->load();
	$page->render();
?>