<?php
	require_once "pageBase.php";
	require_once "../../bin/system/conver.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";
	require_once "../../dal/faqDAL.php";
	class _autokeyword extends pageBase {
		public function load() {
			$action = parent::getPost("do");
			switch ($action) {
				case "delk":	// 刪除關鍵字
					$this->deleteKey();
					break;
				case "dell":	// 刪除超連結
					$this->deleteLink();
					break;
				case "imp":		// 變更重要關鍵字
					$this->changeImportant();
					break;
				case "plugin":	// 在文章中插入關鍵字
					$this->plugin();
					break;
				default:
					break;
			}
			
			parent::load();
		}

		private function performAction($dal, $id, $successMsg = "1", $failureMsg = "-1") {
			echo ($dal->deleteRecord($id)) ? $successMsg : $failureMsg;
		}

		private function processDescription($description, $data, $re) {
			$ary = [];
			$aryY = [];
		
			for ($i = 0; $i < mb_strlen($description, "utf-8"); $i++) {
				$word = mb_substr($description, $i, 1, "utf-8");
				$ary[] = $word;
		
				if (preg_match($re, $word)) $aryY[] = $i;
			}
		
			if (count($aryY) < count($data)) {
				foreach ($data as $row) {
					$description .= "<a href=\"" . $row["LinkURL"] . "\" target=\"\">" . $row["KeywordTitle"] . "</a>";
				}
			} else {
				shuffle($aryY);
				for ($i = 0; $i < count($data); $i++) {
					$ary[$aryY[$i]] .= "<a href=\"" . $data[$i]["LinkURL"] . "\" target=\"\">" . $data[$i]["KeywordTitle"] . "</a>";
				}
				$description = implode("", $ary);
			}
		
			return $description;
		}
		
		private function deleteKey() {
			$dal = new autokeywordDAL();
			$id = parent::getPost("id");
			$this->performAction($dal, $id);
		}

		private function deleteLink() {
			$dal = new autokeywordlinkDAL();
			$id = parent::getPost("id");
			$this->performAction($dal, $id);
		}
		
		private function changeImportant() {
			$dal = new autokeywordDAL();
			$id = parent::getPost("id");
			$this->performAction($dal, $id);
		}
		
		private function plugin() {
			$dal = new autokeywordDAL();
			$dalL = new autokeywordlinkDAL();
			$type = parent::getPost("typ");
			$start = (int) parent::getPost("no");
			$pd = parent::getPost("pd");
			$over = "0";
			$re = "/[^A-Za-z0-9\-_=!@#$\?\.\,\^\\%&:;\(\)\{\}\[\]<>\/'\"\s]/";

			if ($pd != "7277") {
				$over = "1";
				$return = "確認碼錯誤!";
			} else {
				$data = $dal->executeRecordset([], "a.`Visible`='1' AND a.`Important`='1'", "", 0, 1);
				if (count($data) == 0) {
					$data2 = $dal->executeRecordset([], "a.`Visible`='1' AND a.`Important`='0'", "RAND()", 0, 3);
					if (count($data2) > 0 && count($data2) < 3) {
						for ($i = count($data2); $i < 3; $i++) {
							$data2[] = $data2[0];
						}
					}
				} else {
					$data2 = $dal->executeRecordset([], "a.`Visible`='1' AND a.`Important`='0'", "RAND()", 0, 2);
					if (count($data2) < 2) {
						for ($i = count($data2); $i < 2; $i++) {
							$data2[] = $data[0];
						}
					}
				}

				foreach ($data2 as $row) {
					$data[] = $row;
				}
				
				if ($type == "A" || $type == "N" || $type == "P" || $type == "F") {
					$dalA = new dalA();
					$dalN = new newsDAL();
					$dalP = new productDAL();
					$dalF = new faqDAL();

					switch ($type) {
						case "A":
							$dalA->loadRecord();
							$description = $dalA->Description;
							$dalA->ArticleID = $dalA->ArticleID;
							break;
						case "N":
							$dalN->loadRecord();
							$description = $dalN->Description;
							$dalN->NewsID = $dalN->NewsID;
							break;
						case "P":
							$dalP->loadRecord();
							$description = $dalP->Description1;
							$dalP->ProductID = $dalP->ProductID;
							break;
						case "F":
							$dalF->loadRecord();
							$description = $dalF->Description;
							$dalF->FaqID = $dalF->FaqID;
							break;
					}

					$description = preg_replace("/<a.*>/iU", "", $description);
					$description = preg_replace("/<\/a>/iU", "", $description);
					$description = preg_replace("/\r/iU", "", $description);
					$description = preg_replace("/\n/iU", "", $description);
					$description = preg_replace("/\t/iU", " ", $description);
		
					$description = $this->processDescription($description, $data, $re);

					switch ($type) {
						case "A":
							$dalA->Description = $description;
							$dalA->ModifiedUserID = parent::getCurrentUserID();
							$saved = $dalA->updateRecord();
							$return = ($saved === false) ? "文章「" . $dalA->Title . "」插入關鍵字失敗!" : "<p>文章「" . $dalA->Title . "」</p><textarea style=\"width:95%; margin:0 auto;\">" . $description . "</textarea>";
							break;
						case "N":
							$dalN->Description = $description;
							$dalN->ModifiedUserID = parent::getCurrentUserID();
							$saved = $dalN->updateRecord();
							$return = ($saved === false) ? "最新消息「" . $dalN->Title . "」插入關鍵字失敗!" : "<p>最新消息「" . $dalN->Title . "」</p><textarea style=\"width:95%; margin:0 auto;\">" . $description . "</textarea>";
							break;
						case "P":
							$dalP->Description1 = $description;
							$dalP->ModifiedUserID = parent::getCurrentUserID();
							$saved = $dalP->updateRecord();
							$return = ($saved === false) ? "產品「" . $dalP->ProductName . "」插入關鍵字失敗!" : "<p>產品「" . $dalP->ProductName . "」</p><textarea style=\"width:95%; margin:0 auto;\">" . $description . "</textarea>";
							break;
						case "F":
							$dalF->Description = $description;
							$dalF->ModifiedUserID = parent::getCurrentUserID();
							$saved = $dalF->updateRecord();
							$return = ($saved === false) ? "問與答「" . $dalF->Title . "」插入關鍵字失敗!" : "<p>問與答「" . $dalF->Title . "」</p><textarea style=\"width:95%; margin:0 auto;\">" . $description . "</textarea>";
							break;
					}

					$start = ((int) $start) + 1;
				} else {
					$over = "1";
				}

				$aryJ = array();
				$aryJ["type"] = $type;
				$aryJ["start"] = $start;
				$aryJ["over"] = $over;
				$aryJ["return"] = urlencode(str_replace('"', '\"', $return));
				echo urldecode(json_encode($aryJ));
			}
		}
	}

	$page = new _autokeyword();
	$page->load();
?>