<?php
class baseModel extends Model{

   	 function getSet(){
			global $styleList,$pictureSize,$thumbSize;
			$varList = array("selectStyle"		=> $styleList,
					 "appStyle"		=> APP_STYLE,
					 "articlePageSize"	=> ARTICLE_PAGE_SIZE,
					 "productPageSize"	=> PRODUCT_PAGE_SIZE,
					 "appDescription"	=> APP_DESCRIPTION,
					 "appKeywords"		=> APP_KEYWORDS,
					 "waterpath"		=> ATTCHMENT_PATH."product/",
					 "water"		=> WATER,
					 "position"		=> POSITION,
					 "seoType"		=> SEO_TYPE,
					 "htmlDir"		=> HTML_DIR,
					 "width"		=> $thumbSize["width"],
					 "height"		=> $thumbSize["height"],
					 "maxWidth"		=> $pictureSize["maxWidth"],
					 "maxHeight"	=> $pictureSize["maxHeight"]
				 );
			return $varList;
		}



		function validateForm($post) {
			$result=true;
			//载入全局变量
			if(!Validate::isNumber($post['articlePageSize'])){
				$this->messList[] = "文章每页显示数目不是数字.";
				$result=false;
			}
			if(!Validate::isNumber($post['productPageSize'])){
				$this->messList[] = "产品每页显示数目不是数字.";
				$result=false;
			}
			if($post['articlePageSize'] <= 0) {
				$this->messList[] = "文章每页显示数目必须大于零.";
				$result=false;
			}
			if($post['productPageSize'] <= 0){
				$this->messList[] = "产品每页显示数目必须大于零.";
				$result=false;
			}

			if(!Validate::isNumber($post['width']) || !Validate::isNumber($post['height'])) {
				$this->messList[] =  "缩略图尺寸不是数字.";
				$result=false;
			}
			if(!Validate::isNumber($post['maxWidth']) || !Validate::isNumber($post['maxHeight']))	{
				$errorList[] =  "图片上传后的最大尺寸不是数字.";
				$result=false;
			}
			if($post['width'] <= 0 || $post['height'] <= 0){
				$this->messList[] =  "图缩略图尺寸必须大于零.";
				$result=false;
			}
			if($post['maxWidth'] <= 0 || $post['maxHeight'] <= 0) {
				$this->messList[] = "图片上传后的最大尺寸必须大于零.";
				$result=false;
			}
			return  $result;
		}


		private function upload(){
			$up=new upload(array('filepath'=>ATTCHMENT_REAL_PATH."product"));
			if($up->uploadFile('water')){
				return $up->getNewFileName();
			}else{
				$this->messList[]=$up->getErrorMsg();
				return false;
			}
		}


		function writeConfig($fileName,$post){
			$confile=CMS_ROOT.$fileName;
			if(file_exists($confile)){
				$configText = file_get_contents($confile);
			}else{
				$this->messList[]="配置文件不存在.";
				return false;
			}

			if(!empty($post)){
				$configArray['APP_STYLE'] = $post['appStyle'];
				$configArray['ARTICLE_PAGE_SIZE'] = $post['articlePageSize'];
				$configArray['PRODUCT_PAGE_SIZE'] = $post['productPageSize'];
				$configArray['APP_DESCRIPTION'] = $post['appDescription'];
				$configArray['APP_KEYWORDS'] = $post['appKeywords'];
				$configArray['POSITION']=$post["position"];
				$configArray['SEO_TYPE']=$post["seoType"];
				$configArray['HTML_DIR']=$post["htmlDir"];
				if(isset($_FILES["water"])){
					if($newwater=$this->upload()){
						$configArray["WATER"]=$newwater;
						@unlink(ATTCHMENT_REAL_PATH."product/".$post['unlink']);

					}else{
						return false;
					}
				}
				$configArray["pictureSize"] = "array('maxWidth' => {$post['maxWidth']}, 'maxHeight' => {$post['maxHeight']});";
				$configArray["thumbSize"] = "array('width' => {$post['width']}, 'height' => {$post['height']});";

			}else{
				$this->messList[]="表单数据传递为空.";
				return false;
			}


			//循环修改配置
			foreach($configArray as $key => $val) {
				$regV = "/$key\s*=\s*.+;/";		//变量配置参数
				$regC = "/define\(\"$key\".+;/";	//常量配置参数

				if(preg_match($regV, $configText)) {
					$configText = preg_replace($regV, $key." = ".$val, $configText);
				}else{
					if(!Validate::isNumber($val)) {	//字符串加上引号
						$val = "\"$val\"";
					}
					$configText = preg_replace($regC, "define(\"$key\", $val);", $configText);
				}
			}

			if($fp = fopen($confile, "w")) {
				fwrite($fp, $configText);
				fclose($fp);
				return true;
			}else{
				$this->messList[]="基本设置修改失败,请重试.";
				return false;
			}
		}
}
?>