<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class publicOModel extends X {

    function list_to_tree($list, $root = 0, $pk = 'id', $pid = 'pid', $child = '_child') {
	// 创建Tree
	$tree = array();
	if (is_array($list)) {
		// 创建基于主键的数组引用

		$refer = array();
		foreach ($list as $key => $data) {
			$refer[$data[$pk]] = &$list[$key];
		}

		foreach ($list as $key => $data) {
			// 判断是否存在parent
			$parentId = 0;
			if (isset($data[$pid])) {
				$parentId = $data[$pid];
			}
			if ((string)$root == $parentId) {
				$tree[] = &$list[$key];
			} else {
				if (isset($refer[$parentId])) {
					$parent = &$refer[$parentId];
					$parent[$child][] = &$list[$key];
				}
			}
		}
	}
	return $tree;
}
    function list_to_tree1($list, $root = 0, $pk = 'id', $pid = 'pid', $child = '_child') {
  //      print_r($list);
	// 创建Tree
	$tree = array();
	if (is_array($list)) {
		// 创建基于主键的数组引用

		$refer = array();
		foreach ($list as $key => $data) {
			$refer[$data[$pk]] = &$list[$key];
		}
               
		foreach ($list as $key => $data) {
			// 判断是否存在parent
			$parentId = 0;
			if (isset($data[$pid])) {
				$parentId = $data[$pid];
			}
			if ((string)$root == $parentId) {
				$tree[] = &$list[$key];
			} else {
				if (isset($refer[$parentId])) {
					$parent = &$refer[$parentId];
					$parent[$child][] = &$list[$key];
				}
			}
                       
		}
                 
	}
	return $tree;
}
function tree_to_list($tree, $level = 0, $pk = 'id', $pid = 'pid', $child = '_child') {
	$list = array();
	if (is_array($tree)) {
		foreach ($tree as $val) {
			$val['level'] = $level;
			if (isset($val['_child'])) {
				$child = $val['_child'];
				if (is_array($child)) {
					unset($val['_child']);
					$list[] = $val;
					$list = array_merge($list, $this->tree_to_list($child, $level + 1));
				}
			} else {
				$list[] = $val;
			}
		}
		return $list;
	}
}
function tree_to_list1($tree, $level = 0, $pk = 'id', $pid = 'pid', $child = '_child') {
	$list = array();
	if (is_array($tree)) {
		foreach ($tree as $val) {
			$val['level'] = $level;
			if (isset($val['_child'])) {
				$child = $val['_child'];
				if (is_array($child)) {
					unset($val['_child']);
					$list[] = $val;
					$list = array_merge($list, $this->tree_to_list($child, $level + 1));
				}
			} else {
				$list[] = $val;
			}
		}
		return $list;
	}
}
function popup_tree_menu($tree, $level = 0) {
	$level++;
	$html = "";
	if (is_array($tree)) {
		$html = "<ul class=\"tree_menu\">\r\n";

		foreach ($tree as $val) {
			if (isset($val["name"])) {
				$title = $val["name"];
				$id = $val["id"];
				if (empty($val["id"])) {
					$id = $val["name"];
				}
				if (!empty($val["is_del"])) {
					$del_class = "is_del";
				} else {
					$del_class = "normal";
				}
				if (isset($val['_child'])) {
					$html = $html . "<li>\r\n<a class=\"$del_class\" node=\"$id\" ><i class=\"fa fa-angle-right level$level\"></i><span>$title</span></a>\r\n";
					$html = $html . $this->popup_tree_menu($val['_child'], $level);
					$html = $html . "</li>\r\n";
				} else {
					$html = $html . "<li>\r\n<a class=\"$del_class\" node=\"$id\" ><i class=\"fa fa-angle-right level$level\"></i><span>$title</span></a>\r\n</li>\r\n";
				}
			}
		}
		$html = $html . "</ul>\r\n";
	}
	return $html;
}
function addfile($arr){

    return parent::$obj->insert(DB_PREFIXOA.'file', $arr); 
}
function rotate($a) {
	$b = array();
	if (is_array($a)) {
		foreach ($a as $val) {
			foreach ($val as $k => $v) {
				$b[$k][] = $v;
			}
		}
	}
	return $b;
}

        public function get_notice(){
            $sql=' SELECT * FROM `oa_notice`  ORDER BY time desc ';
            $list = parent::$obj->getall($sql);
            return $list;
        }
          
        public function get_notice_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."notice WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}
 /**
 * 求两个已知经纬度之间的距离,单位为米
 * 
 * @param lng1 $ ,lng2 经度
 * @param lat1 $ ,lat2 纬度
 * @return float 距离，单位米
 * @author www.Alixixi.com 
 */
function getdistance($lng1, $lat1, $lng2, $lat2) {
    // 将角度转为狐度
    $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
    return $s;
} 
        /**
     * 判断一个坐标是否在圆内
     * 思路：判断此点的经纬度到圆心的距离  然后和半径做比较
     * 如果此点刚好在圆上 则返回true
     * @param $point ['lng'=>'','lat'=>''] array指定点的坐标
     * @param $circle array ['lng'=>'','lat'=>'','radius'=>'']  中心点和半径
     */
    function is_point_in_circle($point, $circle){
        $distance = $this -> distance($point['lat'],$point['lang'],$circle['lat'],$circle['lang']);
        if($distance <= $circle['radius']){
            return 1;
        }else{
            return 0;
        }
    }
    
 
    /**
     *  计算两个点之间的距离
     * @param $latA  第一个点的纬度
     * @param $lonA  第一个点的经度
     * @param $latB  第二个点的纬度
     * @param $lonB  第二个点的经度
     * @return float
     */
    function distance($latA, $lonA, $latB, $lonB)
    {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $earthR = 6371000.;
        $x = cos($latA * $x_pi / 180.) * cos($latB * $x_pi / 180.) * cos(($lonA - $lonB) * $x_pi / 180);
        $y = sin($latA * $x_pi / 180.) * sin($latB * $x_pi / 180.);
        $s = $x + $y;
        if ($s > 1){
            $s = 1;
        } 
        if ($s < -1) {
            $s = -1;
        }
      //  echo $s;die;
        $alpha = acos($s);
       // print_r($alpha);
        $distance = $alpha * $earthR;
        
        return $distance;
    }
    function select_tree_menu($tree,$eq_dept_id) {
                $html = "";
                if (is_array($tree)) {
                        $list = $this->tree_to_list($tree);
                        foreach ($list as $val) {
                                if($eq_dept_id==$val['id']){
                                    $html = $html . "<option value='{$val['id']}' selected='selected'>" . str_pad("", $val['level'] * 3, "│") . "├─" . "{$val['name']}</option>";
                                } else {
                                    $html = $html . "<option value='{$val['id']}'>" . str_pad("", $val['level'] * 3, "│") . "├─" . "{$val['name']}</option>";
                                }
                                
                        }
                }
                return $html;
        }
}
     


?>
