<?php
	require_once "../dal/autokeywordDAL.php";
	require_once "../dal/autokeywordlinkDAL.php";
	require_once "../dal/articleDAL.php";
	require_once "../dal/newsDAL.php";
	require_once "../dal/productDAL.php";
	
	echo "自動插入關鍵字<br />\r\n";
	echo "執行結果<br />\r\n";
	echo "─────────────────────────────────────────────────────────────────<br />\r\n";

	$dal = new autokeywordDAL();
	$dalL = new autokeywordlinkDAL();
	
	$dataI = $dal->executeRecordset("a.Important='1' AND a.Visible='1'","",0,1);
	$dataNI = $dal->executeRecordset("a.Important='0' AND a.Visible='1'");
	$data = array();
	$data = $dataNI;
	
	$stopflag = 0;
	
	if(count($dataI) == 0){
		if(count($dataNI) == 0){
			echo "沒有任何關鍵字，自動插入關鍵字失敗!!!<br />";
			$stopflag = 1;
		}
	}else{
		if(count($dataNI) < 2){
			$data = array_merge($data,$dataI);
		}
	}
	
	/*$testText = "這是測試用字串<p>這是測試用字串,</p>這是測試用字串，<br />這是測試用字串&nbsp;這是測試用字串這是測試用字串";
	echo "[測試字串]<br />".$testText."[/測試字串]<br /><br />字串長度：".mb_strlen($testText,"utf-8");
	$testArray = array();
	for($i=0;$i<mb_strlen($testText,"utf-8");$i++){
		$testArray[] = mb_substr($testText,$i,1,"utf-8");
	}
	echo "<br /><br />逐字元顯示：<br />";
	for($i=0;$i<count($testArray);$i++){
		echo "[".($i + 1)."]".$testArray[$i]."<br />";
	}*/
	
	
	$pattern = "/^[\x{4e00}-\x{9fff}]+$/u";
	/*for($i=0;$i<3;$i++){
		$randNum = mt_rand(0,count($testArray) - 1);
		$randStr = $testArray[$randNum];
		if(preg_match($pattern,$randStr,$match)){
			$sameflag = 0;
			for($j=0;$j<count($addNum);$j++){
				if($randNum == $addNum[$j]){
					$sameflag = 1;
					$j = 100;
				}
			}
			if($sameflag == 0){
				$addNum[] = $randNum;
				if(count($addNum) == 1 && count($dataI) == 1){
					$keywordTitle = $dataI[0]["KeywordTitle"];
					unset($dataL);
					$dataL = $dalL->executeRecordset("a.KeywordID='".$dataI[0]["KeywordID"]."' AND a.Visible='1'");
					if(count($dataL) == 0){
						$linkURL = "http://".$_SERVER['HTTP_HOST'];
					}else{
						$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
					}
				}else{
					$dataNum = mt_rand(0,count($data) - 1);
					$keywordTitle = $data[$dataNum]["KeywordTitle"];
					unset($dataL);
					$dataL = $dalL->executeRecordset("a.KeywordID='".$data[$dataNum]["KeywordID"]."' AND a.Visible='1'");
					if(count($dataL) == 0){
						$linkURL = "http://".$_SERVER['HTTP_HOST'];
					}else{
						$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
					}
				}
				$testArray[$randNum] .= "<a href=\"".$linkURL."\" target=\"_blank\">".$keywordTitle."</a>";
			}else{
				$i--;
				echo "<br />[".$randNum."]重複位置插入<br />";
			}
		}else{
			$i--;
			echo "<br />[".$randNum."][".$randStr."]插入位置非中文字元<br />";
		}
	}
	
	$NewText = implode("",$testArray);
	echo "<br /><br />[插入後字串]<br />".$NewText."<br />[/插入後字串]";*/
	
	if($stopflag == 0){
		$dalA = new articleDAL();
		$dataA = $dalA->executeRecordset("","a.CategoryID,a.OrderNO,a.ArticleID",0,-1);
		for($k=0;$k<count($dataA);$k++){
			if(isset($addNum))
				unset($addNum);
			$addNum = array();
		
			$contentTxt = strip_tags($dataA[$k]["Content"],"<p><br><img>");
			$contentTxt = str_replace("\r\n","",$contentTxt);
			$contentTxt = str_replace("\r","",$contentTxt);
			$contentTxt = str_replace("\n","",$contentTxt);
			$contentTxt = str_replace("\t","",$contentTxt);
			$contentTxt = str_replace(" ","",$contentTxt);
		
			if(isset($contentArray))
				unset($contentArray);
			$contentArray = array();
		
			for($i=0;$i<mb_strlen($contentTxt,"utf-8");$i++){
				$contentArray[] = mb_substr($contentTxt,$i,1,"utf-8");
			}
		
			for($i=0;$i<3;$i++){
				$randNum = mt_rand(0,count($contentArray) - 1);
				$randStr = $contentArray[$randNum];
				if(preg_match($pattern,$randStr)){
					$sameflag = 0;
					for($j=0;$j<count($addNum);$j++){
						if($randNum == $addNum[$j]){
							$sameflag = 1;
							$j = 100;
						}
					}
					if($sameflag == 0){
						$addNum[] = $randNum;
						if(count($addNum) == 1 && count($dataI) == 1){
							$keywordTitle = $dataI[0]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$dataI[0]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}else{
							$dataNum = mt_rand(0,count($data) - 1);
							$keywordTitle = $data[$dataNum]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$data[$dataNum]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}
						$contentArray[$randNum] .= "<a href=\"".$linkURL."\" target=\"_blank\">".$keywordTitle."</a>";
					}else{
						$i--;
					}
				}else{
					$i--;
				}
			}
		
			$contentNew = implode("",$contentArray);
		
			$dalA->PathName = $dataA[$k]["PathName"];
			$dalA->loadRecord();
			$dalA->Content = $contentNew;
			if($dalA->updateRecord()){
				echo "[True]<br />\r\n";
			}else{
				echo "[False]<br />\r\n";
			}
			echo "文章標題：".$dalA->Title."<br />\r\n";
			echo "文章內容：<br />\r\n<textarea style=\"width:800px; height:500px;\">".$contentNew."</textarea><br />\r\n";
			echo "─────────────────────────────────────────────────────────────────<br />\r\n";
		}
	
		$dalN = new newsDAL();
		$dataN = $dalN->executeRecordset("","a.CategoryID, a.OrderNO DESC, a.PubTime DESC",0,-1);
		for($k=0;$k<count($dataN);$k++){
			if(isset($addNum))
				unset($addNum);
			$addNum = array();
			
			$contentTxt = strip_tags($dataN[$k]["Content"],"<p><br><img>");
			$contentTxt = str_replace("\r\n","",$contentTxt);
			$contentTxt = str_replace("\r","",$contentTxt);
			$contentTxt = str_replace("\n","",$contentTxt);
			$contentTxt = str_replace("\t","",$contentTxt);
		
			if(isset($contentArray))
				unset($contentArray);
			$contentArray = array();
			for($i=0;$i<mb_strlen($contentTxt,"utf-8");$i++){
				$contentArray[] = mb_substr($contentTxt,$i,1,"utf-8");
			}
		
			for($i=0;$i<3;$i++){
				$randNum = mt_rand(0,count($contentArray) - 1);
				$randStr = $contentArray[$randNum];
				if(preg_match($pattern,$randStr)){
					$sameflag = 0;
					for($j=0;$j<count($addNum);$j++){
						if($randNum == $addNum[$j]){
							$sameflag = 1;
							$j = 100;
						}
					}
					if($sameflag == 0){
						$addNum[] = $randNum;
						if(count($addNum) == 1 && count($dataI) == 1){
							$keywordTitle = $dataI[0]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$dataI[0]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}else{
							$dataNum = mt_rand(0,count($data) - 1);
							$keywordTitle = $data[$dataNum]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$data[$dataNum]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}
						$contentArray[$randNum] .= "<a href=\"".$linkURL."\" target=\"_blank\">".$keywordTitle."</a>";
					}else{
						$i--;
					}
				}else{
					$i--;
				}
			}
		
			$contentNew = implode("",$contentArray);
		
			$dalN->ArticleID = $dataN[$k]["ArticleID"];
			$dalN->loadRecord();
			$dalN->Content = $contentNew;
			if($dalN->updateRecord()){
				echo "[True]<br />\r\n";
			}else{
				echo "[False]<br />\r\n";
			}
			echo "消息標題：".$dataN[$k]["Title"]."<br />\r\n";
			echo "消息內容：<br />\r\n<textarea style=\"width:800px; height:500px;\">".$contentNew."</textarea><br />\r\n";
			echo "─────────────────────────────────────────────────────────────────<br />\r\n";
		}
	
		$dalP = new productDAL();
		$dataP = $dalP->executeRecordset("","a.ProductID",0,-1);
		for($k=0;$k<count($dataP);$k++){
			if(isset($addNum))
				unset($addNum);
			$addNum = array();
			
			$contentTxt = strip_tags($dataP[$k]["Description"],"<p><br><img>");
			$contentTxt = str_replace("\r\n","",$contentTxt);
			$contentTxt = str_replace("\r","",$contentTxt);
			$contentTxt = str_replace("\n","",$contentTxt);
			$contentTxt = str_replace("\t","",$contentTxt);
		
			if(isset($contentArray))
				unset($contentArray);
			$contentArray = array();
			for($i=0;$i<mb_strlen($contentTxt,"utf-8");$i++){
				$contentArray[] = mb_substr($contentTxt,$i,1,"utf-8");
			}
		
			for($i=0;$i<3;$i++){
				$randNum = mt_rand(0,count($contentArray) - 1);
				$randStr = $contentArray[$randNum];
				if(preg_match($pattern,$randStr)){
					$sameflag = 0;
					for($j=0;$j<count($addNum);$j++){
						if($randNum == $addNum[$j]){
							$sameflag = 1;
							$j = 100;
						}
					}
					if($sameflag == 0){
						$addNum[] = $randNum;
						if(count($addNum) == 1 && count($dataI) == 1){
							$keywordTitle = $dataI[0]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$dataI[0]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}else{
							$dataNum = mt_rand(0,count($data) - 1);
							$keywordTitle = $data[$dataNum]["KeywordTitle"];
							unset($dataL);
							$dataL = $dalL->executeRecordset("a.KeywordID='".$data[$dataNum]["KeywordID"]."' AND a.Visible='1'");
							if(count($dataL) == 0){
								$linkURL = "http://".$_SERVER['HTTP_HOST'];
							}else{
								$linkURL = $dataL[mt_rand(0,count($dataL) - 1)]["LinkURL"];
							}
						}
						$contentArray[$randNum] .= "<a href=\"".$linkURL."\" target=\"_blank\">".$keywordTitle."</a>";
					}else{
						$i--;
					}
				}else{
					$i--;
				}
			}
		
			$contentNew = implode("",$contentArray);
		
			$dalP->ProductID = $dataP[$k]["ProductID"];
			$dalP->loadRecord();
			$dalP->Description = $contentNew;
			if($dalP->updateRecord()){
				echo "[True]<br />\r\n";
			}else{
				echo "[False]<br />\r\n";
			}
			echo "商品名稱：".$dataP[$k]["Name"]."<br />\r\n";
			echo "商品說明：<br />\r\n<textarea style=\"width:800px; height:500px;\">".$contentNew."</textarea><br />\r\n";
			echo "─────────────────────────────────────────────────────────────────<br />\r\n";
		}
	}
?>
