<?php
	require_once "pageBase.php";
	require_once "../dal/registryDAL.php";
	class _facebookSet extends pageBase {
		public function load() {
			$this->limitID = "69";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/facebookSet.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() {
			$aryFS = parent::getPostArray("fbFansSidePageShow");
		
			$aryR = [
				"fbFansURL" => parent::getPost("fbFansURL"),
				"fbFansFriendFace" => parent::getPost("fbFansFriendFace", "0"),
				"fbFansCoverPhoto" => parent::getPost("fbFansCoverPhoto", "0"),
				"fbFansPagePost" => parent::getPost("fbFansPagePost", "0"),
				"fbFansSidePageShow" => $aryFS !== null ? implode(",", $aryFS) : "",
				"fbFansSidePageType" => parent::getPost("fbFansSidePageType"),
				"fbFansLOGO" => parent::getPost("fbFansLOGO"),
				"ModifiedTime" => time(),
			];

			$fbFansSidePageType = $aryR["fbFansSidePageType"];
			$fbFansLOGO = $aryR["fbFansLOGO"];
		
			if ($fbFansSidePageType == "2") {
				if (empty($fbFansLOGO)) {
					$this->alertMsg = "請上傳自訂的臉書粉絲團LOGO!";
					$this->goBack = "Y";
					return false;
				} else if (!parent::image_exists($fbFansLOGO)) {
					$this->alertMsg = "找不到指定的自訂臉書粉絲團LOGO!";
					$this->goBack = "Y";
					return false;
				}
			}
			
			foreach ($aryR as $key => $value) {
				registryDAL::saveRegistry($key, $value);
			}

			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "facebookSet.php";
		}
	}
	$page = new _facebookSet();
	$page->load();
	$page->render();
?>