<?php
/**
 * fishow共通
 * $Id: CompassHandler.inc,v 1.1 2015/10/08 13:34:45 g_wang Exp $
 * @author $Author: g_wang $
 * @access public
 * @package jp.fishow.handler
 */
class CompassHandler{
	/**
	 * 图片显示URL取得
	 * child_path(不包含"/")
	 */
	public static function getImageUrl($file_name, $type, $child_path) {
		$file_path = IMAGES_PATH;
		
		if ($type == "theme") {
			$file_path = THEME_IMAGES_PATH;
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		} else if ($type == "article") {
			$file_path = ARTICLE_IMAGES_PATH;
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		} else if ($type == "category") {
			$file_path = ARTICLE_IMAGES_PATH;
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		} else if ($type == "course") {
			$file_path = COURSE_IMAGES_PATH;
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		} else if ($type == "video") {
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		} else {
			if(!empty($child_path)) {
				$file_path .= "/" . $child_path;
			}
			$file_path .= "/" . $file_name;
		}
		$url = "display_image.php?type={type}&child_path={child_path}&file_name={file_name}";
		$url = str_replace("{child_path}", $child_path, $url);
		$url = str_replace("{file_name}", $file_name, $url);
		$url = str_replace("{type}", $type, $url);
		return $url;
	}
	
	/**
	 * 获取图集的目录下文件列表
	 */
	public static function getArticleImages($articleDat) {
		$resultArray = array();
		if(empty($articleDat) || empty($articleDat->image_path)) {
			return $resultArray;
		}
		$image_full_path = IMAGES_PATH . "/" . $articleDat->image_path;
		if(!is_dir($image_full_path)) {
			return $resultArray;
		}
		//遍历读取列表
		if($dh = @opendir($image_full_path)){
			//读取
			while(($file = readdir($dh)) !== false){
				if($file != '.' && $file != '..'){
					$resultArray[] = $file;
				}
			}
			//关闭
			closedir($dh);
		}
		return $resultArray;
	}
	
	/**
	 * 获取图集的目录下文件列表图片数量
	 */
	public static function getArticleImagesCount($articleDat) {
		$resultCount = 0;
		if(empty($articleDat) || empty($articleDat->image_path)) {
			return $resultCount;
		}
		$image_full_path = IMAGES_PATH . "/" . $articleDat->image_path;
		if(!is_dir($image_full_path)) {
			return $resultCount;
		}
		//遍历读取列表
		if($dh = @opendir($image_full_path)){
			//读取
			while(($file = readdir($dh)) !== false){
				if($file != '.' && $file != '..'){
					$resultCount++;
				}
			}
			//关闭
			closedir($dh);
		}
		return $resultCount;
	}	
	
	/**
	 * 获取网络图片并保存
	*/
	public static function grabImage($url, $filename = "") {
	 //如果$url地址为空,直接退出
	 if(empty($url)){
		return false;
	 }
	 //如果没有指定新的文件名
	 if(empty($filename)){
		 return false;
	 } 
	 ob_start();//打开输出
	 //输出图片文件
	 if(!readfile($url)) {
		return false;
	 }
	 $img = ob_get_contents();//得到浏览器输出
	 ob_end_clean();//清除输出并关闭
	 $size = strlen($img);//得到图片大小
	 $fp2 = @fopen($filename, "a");
	 fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
	 fclose($fp2);	 
	 return true;
	} 	
	
	/**
	 * 获取去重后的解梦关键词列表
	 */
	public static function getUniqueDreamListByCategoryId($categoty_id, $offset, $limit) {
		$db = &DivinationsDBManager::getInstance();
		$sql = "select distinct category_id, dream_id, title from dream_dat where delete_flg=false";
		if(isset($categoty_id)) {
			$sql .= " and category_id=".$categoty_id;
		}
		if(isset($limit)) {
			$sql .= " limit ".$limit;
		}
		if(isset($offset)) {
			$sql .= " offset ".$offset;
		}

		$result = $db->executeQuery($sql);
		if(count($result) > 0){
			return $result;
		}
		return array();
	}

	/**
	 * 获取单个广告合计后的去重点击数
	 */
	public static function getSumUniqueAdClickCount($adid, $object_date) {
		$db = &DivinationsDBManager::getInstance();
		$sql = "select sum(unique_click_count) as count from miniplay_ad_click_dat where delete_flg=false and channel_id<>'0' and adid='" . $adid . "' and object_date='" . $object_date . "'";

		$result = $db->executeQuery($sql);
		if(count($result) > 0){
			return $result[0]["count"];
		}
		return 0;
	}	
	
	/**
	 * 插入用户点数获得数据【openid】
	*/
	public static function insertPointLog($openid, $action_type, $point, $status) {
		if(empty($openid) || $openid=="undefined") return;
		$db = &DivinationsDBManager::getInstance();
		$insert_sql = "insert into miniplay_user_point_log (openid, action_type, point, status) VALUES ('{$openid}', '{$action_type}','{$point}','{$status}')";
		$db->executeQuery($insert_sql);
		
		//更新该用户的累计点数
		if($status=="SUCCESS") {
			$point_val = (int)$point;
			if($point_val > 0) {
				$update_sql = "update user_mst set point=point+{$point_val}, sum_point=sum_point+{$point} where openid='{$openid}' and delete_flg=false";
			} else {
				$point_val2 = abs($point_val);
				$update_sql = "update user_mst set point=point-{$point_val2} where openid='{$openid}' and delete_flg=false";
			}
			$db->executeQuery($update_sql);
		}
		return;
	}
	
	/**
	 * 插入用户点数获得数据【openid】
	*/
	public static function insertPointLogByUID($uid, $action_type, $point, $status) {
		if(empty($uid) || $uid=="undefined") return;
		$db = &DivinationsDBManager::getInstance();
		$insert_sql = "insert into miniplay_user_point_log (openid, action_type, point, status) VALUES ('{$uid}', '{$action_type}','{$point}','{$status}')";
		$db->executeQuery($insert_sql);
		
		//更新该用户的累计点数
		if($status=="SUCCESS") {
			//判断记录是否存在
			$param = array();
			$param["union_id"] = $uid;
			$param["delete_flg"] = false;
			$tmp_list = UnionUserDat::getList($param, "id", "desc", 0 , 1);
			if(empty($tmp_list)) {
				$union_user_dat = new UnionUserDat();
				$union_user_dat->union_id = $uid;
				$union_user_dat->point = 0;
				$union_user_dat->sum_point = 0;
			} else {
				$union_user_dat = $tmp_list[0];
			}
			$point_val = (int)$point;
			if($point_val > 0) {
				$union_user_dat->point += $point_val;
				$union_user_dat->sum_point += $point_val;
			} else {
				$point_val2 = abs($point_val);
				$union_user_dat->point -= $point_val;
			}
			$union_user_dat->save();
		}
		return;
	}
	
	/**
	 * 插入用户点数获得数据
	*/
	public static function insertOctopusPointLog($openid, $action_type, $group_id, $point) {
		if(empty($openid) || $openid=="undefined") return;
		$db = &DivinationsDBManager::getInstance();
		$insert_sql = "insert into octopus_user_point_log (openid, action_type, group_id, point) VALUES ('{$openid}', '{$action_type}','{$group_id}','{$point}')";
		$db->executeQuery($insert_sql);
		
		//更新该用户的累计点数
		$point_val = (int)$point;
		if($point_val > 0) {
			$update_sql = "update user_mst set point=point+{$point_val}, sum_point=sum_point+{$point} where openid='{$openid}' and delete_flg=false";
		} else {
			$point_val2 = abs($point_val);
			$update_sql = "update user_mst set point=point-{$point_val2} where openid='{$openid}' and delete_flg=false";
		}
		$db->executeQuery($update_sql);

		return;
	}
	
	/**
	 * 渠道广告点击记录
	*/
	public static function insertTrafficLog($channel_id, $openid, $adid) {
		if(empty($openid) || $openid=="undefined") return;
		$db = &DivinationsDBManager::getInstance();
		$insert_sql = "insert into miniplay_traffic_click_log (channel_id, openid, adid) VALUES ('{$channel_id}', '{$openid}','{$adid}')";
		$db->executeQuery($insert_sql);
		return;
	}

	/**
	 * 下线广告处理
	*/
	public static function offlineAd($adDat) {
		if(empty($adDat)) return;
		
		//该广告对应的ad_plan下线处理
		$param = array();
		$param["delete_flg"] = false;
		$param["adid"] = $adDat->id;
		$param["is_finish"] = false;
		$ad_plan_list = MiniplayAdPlan::getList($param);
		if(!empty($ad_plan_list)) {
			foreach($ad_plan_list as $ad_plan) {
				$ad_plan->is_finish = true;
				$ad_plan->save();
			}
		}
		//渠道广告配置下线
		$param = array();
		$param["delete_flg"] = false;
		$param["adid"] = $adDat->id;
		$promotion_dat_list = MiniplayPromotionDat::getList($param);
		if(!empty($promotion_dat_list)) {
			foreach($promotion_dat_list as $promotion_dat) {
				$promotion_dat->delete_flg = true;
				$promotion_dat->save();
			}
		}
		//广告下线
		$adDat->is_top = false;
		if($adDat->status == "OK") {
			$adDat->status = "NG";
			$adDat->save();
		}
		
		//渠道配置广告下线处理
		return;
	}	
	
	
	/**
	 * Cookies设定
	 * @param string $name
	 * @param string $value
	 * @param int $expire
	 * @return empty
	 */
	public static function setCookies($name, $value, $expire = 0) {
		setcookie($name, $value, $expire, "/");
	}

	/**
	 * Cookies取得
	 * @param string $name
	 * @return string
	 */
	public static function getCookies($name) {
		$cookies_value = null;
		if (isset($_COOKIE[$name]) && $_COOKIE[$name] != null) {
			$cookies_value = $_COOKIE[$name];
		}
		return $cookies_value;
	}

	/**
	 * Cookies清除
	 * @param string $name
	 * @return null
	 */
	public static function clearCookies($name) {
		if (isset($_COOKIE[$name]) && $_COOKIE[$name] != null) {
			setcookie($name, "", time() - 3600, "/");
		}
		return null;
	}
	
	/**
	 * CURLでHttp請求
	 * @param unknown $url
	*/
	public static function getCurl($url) {
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$file_contents = curl_exec($ch);
		curl_close($ch);
	
		return $file_contents;
	}

	  
	/**
	* 对象转数组
	*/
	public static function object2array($object) {
	  if (is_object($object)) {
	    foreach ($object as $key => $value) {
	      $array[$key] = $value;
	    }
	  }
	  else {
	    $array = $object;
	  }
	  return $array;
	}

	/**
	 * 排序专用callback
	 */
	public static function callbackCmpPriority($a, $b) {
		return $a["idx"] < $b["idx"] ? true : false;
	}
}

?>