<?php
/**
 * 飲食店業態向けシステムmcs_combo 共通関数定義クラス
 * @name common.php
 * @category  web function
 * @since 2012-11-21
 * @version 1.0.1
 * @update 2013-07-09
 * @copyright almex.inc
 * @author r.yoshinaga
 */


class common{
	const FILESIZE_MAX = 100000000;
	const HISTORY_COUNT = 10;
	/**/
	//const PATH = 'c:\\xampp\\htdocs\\logs\\';
	/**/
	//エラーログ出力
	static public function sql_error_log($file, $line, $sql, $errno, $error){
		require_once 'const.mcs.php';

		date_default_timezone_set('Asia/Tokyo');
		$now =  date('Y/m/d H:i:s');
		//$path = $_SERVER['DOCUMENT_ROOT'] ."/logs/mainte.log";
		//$path = $_SERVER['DOCUMENT_ROOT'] ."/logs/mainte.log";
		//$path = "../log/setting.log";

		$path = $_SERVER['DOCUMENT_ROOT']."/mcs_combo/log/".CONST_MCS_DEFINE::COMMON_LOG_FILE;
		self::checkfilesize($path);
		$text = $now ." errno=". $errno ." message=".$error." ".$file ."(". $line .")". " sql=". $sql . "\n";
		mb_convert_variables("SJIS", "UTF-8", $text);
		error_log( $text, 3, $path );
	}

	static public function sql_error_logex($logfile, $file, $line, $sql, $errno, $error){
		date_default_timezone_set('Asia/Tokyo');
		$now =  date('Y/m/d H:i:s');
		//$path = $_SERVER['DOCUMENT_ROOT'] ."/logs/".$logfile;
		//$path = "../log/".$logfile;
		//$path = $_SERVER['DOCUMENT_ROOT']."/mcs_combo/log/".CONST_MCS_DEFINE::COMMON_LOG_FILE;
		$path = $_SERVER['DOCUMENT_ROOT']."/mcs_combo/log/".$logfile;
		//$path = self::PATH.$logfile;
		self::checkfilesize($path);
		$text = $now ." errno=". $errno ." message=". $error . $file ."(". $line .")". " sql=". $sql . "\n";
		mb_convert_variables("SJIS", "UTF-8", $text);
		error_log( $text, 3, $path );
	}

	//ファイルサイズチェック
	static public function checkfilesize($filename, $increment = true){
		if (!file_exists($filename)) {
			$size = 0;
			return False;
		}
		$size = filesize($filename);
		if ($size > self::FILESIZE_MAX){
			if ($increment) {
				self::incfilerename($filename);  //指定ファイルサイズを超えた場合ファイル名を更新する
				return false;
			} else {
				return true;
			}
		}
	}
	//ファイル世代更新処理
	static public function incfilerename($filename){
		for ($i = self::HISTORY_COUNT; $i >= 1; $i--){
			if (file_exists($filename.$i)) {
				if ($i == self::HISTORY_COUNT) {
					unlink($filename.$i);
				} else {
					rename($filename.$i, $filename.($i+1));
				}
			}
		}
		rename($filename, $filename.'1');
	}

	/**
	 * 共通ライブラ読み込み
	 * @param  なし
	 * @return なし
	 **/
	static public function require_common_files() {
		require_once 'sysinfo.php';
		require_once 'SQL.mySQL.php';
		require_once 'timeutility.php';
		require_once 'const.mcsdefine.php';
		require_once 'const.mcs.php';

	}

	/**
	 * jsonの作成
	 * @param string $status  ステータス
	 * @param string $message  メッセージ
	 * @return $json json形式
	 **/
	static public function make_json($status,$message){
		require_once 'timeutility.php';

		$json = null;
		$json['status'] = $status;
		$json['message'] = $message;
		$json['res_time'] = getnowstr_YMDhms('/',':');

		return json_encode($json);
	}

	/**
	 * resultArray 返却JSON用ひな形配列生成関数
	 * @param $statu	処理結果ステータス
	 * @param $message	処理結果メッセージ
	 * @param $result	返却データがある場合は、配列を格納する
	 */
	static public function resultArray($status=0, $message='', $result=''){
		$arr = array();
		$arr['status'] = $status;
		$arr['message'] = $message;
		$arr['result'] = $result;
		return $arr;
	}

	/**
	 * echoJSON 処理結果JSON返却関数
	 * @param $arr 返却JSON用配列（resultArrayにて生成すること）
	 */
	static public function echoJSON($arr){
		$json = json_encode($arr);
		header('Content-type: application/json; charset=UTF-8');
		echo $json;
	}

}
?>