<?php
	require_once "pageBase.php";
	require_once "../dal/htaccessDAL.php";
	require_once "../dal/registryDAL.php";
	require_once "../function/htaccess.php";
	class _webbaseSet extends pageBase {
		public function load() {
			$this->limitID = "47";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/webbaseSet.html");
			parent::load();
		}
		
		public function loadData() {
			$webConfig = registryDAL::getRegistryValues();
			foreach ($webConfig as $key => &$value) {
				$value = $value ?? "";
				parent::addParameter($key, $value);
			}
			parent::loadData();
		}
		
		public function saveData() {
			$aryR = [
				"webName" => parent::getPost("webName"),
				"webEmail" => parent::getPost("webEmail"),
				"webEmailisShow" => parent::getPost("webEmailisShow", "0"),
				"webAddress" => parent::getPost("webAddress"),
				"webAddressisShow" => parent::getPost("webAddressisShow", "0"),
				"webPhone" => parent::getPost("webPhone"),
				"webPhoneisShow" => parent::getPost("webPhoneisShow", "0"),
				"webMobile" => parent::getPost("webMobile"),
				"webMobileShow" => parent::getPost("webMobileShow", "0"),
				"webFax" => parent::getPost("webFax"),
				"webFaxisShow" => parent::getPost("webFaxisShow", "0"),
				"systemEmail1" => parent::getPost("systemEmail1"),
				"systemEmail2" => parent::getPost("systemEmail2"),
				"systemEmail3" => parent::getPost("systemEmail3"),
				"systemEmail4" => parent::getPost("systemEmail4"),
				"systemEmail5" => parent::getPost("systemEmail5"),
				"LineOpen" => parent::getPost("LineOpen", "0"),
				"OGTitle" => parent::getPost("OGTitle"),
				"OGDescription" => preg_replace("/\s+/", " ", parent::getPost("OGDescription")),
				"OGImage" => parent::getPost("OGImage"),
				"lineTitle" => parent::getPost("lineTitle"),
				"lineTitleShow" => parent::getPost("lineTitleShow", "0"),
				"lineQRCode" => parent::getPost("lineQRCode"),
				"ModifiedTime" => time(),
			];
			
			foreach ($aryR as $key => $value) {
				if ($key === "OGImage" || $key === "lineQRCode") {
					if (!empty($value) && !parent::image_exists($value)) {
						$this->alertMsg = "找不到指定的" . ($key === "OGImage" ? "社群分享縮圖" : "QRCode") . "!";
						$this->goBack = "Y";
						return false;
					}
				}

				registryDAL::saveRegistry($key, $value);
			}
			
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "webbaseSet.php";
		}
	}
	$page = new _webbaseSet();
	$page->load();
	$page->render();
?>