<?php

function get_cond_arr() {
    //年月日数组
    GLOBAL $year_arr, $month_arr, $day_arr;
    $year_arr = array();
    $month_arr = array();
    $day_arr = array();
    for ($i = date("Y"); $i >= 2012; $i--)
        $year_arr[$i] = $i;
    for ($i = 1; $i <= 12; $i++)
        $month_arr[$i] = $i;
    for ($i = 1; $i <= 31; $i++)
        $day_arr[$i] = $i;

    //查询条件1
    GLOBAL $cond_arr_1;
    $cond_arr_1 = array();
    $cond_arr_1["none"] = "* 请选择 *";
    $cond_arr_1["a.bi_add_time"] = "采集日期";

    //查询条件2
    GLOBAL $cond_arr_2;
    $cond_arr_2 = array();
    $cond_arr_2["none"] = "* 请选择 *";
    $cond_arr_2["a.id"] = "图片编号";
    $cond_arr_2["a.bi_title"] = "图片名称";
	$cond_arr_2["b.id"] = "小区编号";
    $cond_arr_2["b.blockname"] = "小区名称";
	$cond_arr_2["brokeruname"] = "经纪人登录名";


    //排序条件
    GLOBAL $orderby_arr;
    $orderby_arr = array();
	$orderby_arr["none"] = "* 请选择 *";
    $orderby_arr["bi_add_time"] = "采集日期";

    GLOBAL $asc_desc_arr;
    $asc_desc_arr = array();
    $asc_desc_arr["desc"] = "&nbsp;倒序排列&nbsp;";
    $asc_desc_arr["asc"] = "&nbsp;正序排列&nbsp;"; //方向
}

function get_orderby_str($d) {
    GLOBAL $orderby_arr;
    GLOBAL $asc_desc_arr;

	$ss = '';
    if (isset($orderby_arr[$d["orderby"]]) && $d["orderby"] != "none")
        $ss = " order by " . $d["orderby"];

    if (isset($asc_desc_arr[$d["asc_desc"]]) && $ss != '')
        $ss.=" " . $d["asc_desc"];

    return $ss;
}

function get_query_str(&$d, $q_ext = "") {//$cond_arr只做检测用
    global $db,$cond_arr_1, $cond_arr_2, $set_arr, $set_flag_arr;

    if ($q_ext != "") {
        if ($ss != "")
            $ss.=" and ";
        $ss.=$q_ext;
    }

	if (isset($d["cond3"]) && $d["cond3"] >= 0 ){
		if ($ss != "")
            $ss.=" and ";
		if($d["cond3"] == 0)
		{
			$ss .= " (a.bi_type = '".$d["cond3"]."' or a.bi_type = '9') ";
		}
		else if($d["cond3"] == 1)
		{
			$ss .= " (a.bi_type = '".$d["cond3"]."' or a.bi_type = '7') ";
		}
		else
		{
			$ss .= " a.bi_type = '".$d["cond3"]."' ";
		}
		
	}

    //字串条件
    if ((isset($cond_arr_2[$d["cond2"]])) and ($d["cond2"] != "none") and ($d["cond2"] != "0")) {
        if ($ss != "")
            $ss.=" and ";
		if($d["cond2"] == 'brokeruname')
		{
			$buidstr = '';
			$barr = $db->GetAll("SELECT uid FROM broker WHERE username LIKE '%".qstr($d["cond2_val"])."%' LIMIT 20 ");
			if(!empty($barr))
			{
				foreach($barr as $b)
				{
					$b['uid'] = intval($b['uid']);
					if($b['uid'] > 0)
					{
						$buidstr .= $buidstr == '' ? $b['uid'] : ','.$b['uid'];
					}
				}
			}
			$ss .= $buidstr != '' ? " a.brokeruid IN (".$buidstr.") " : " a.brokeruid = -1 ";
		}
		else
		{
			$d["cond2_val"] = trim($d["cond2_val"]);
			$ss.=" " . $d["cond2"] . " like '%" . qstr($d["cond2_val"]) . "%' ";
		}
    }

	if(isset($d["cond1"]) && $d["cond1"] != 'none' && $d["cond1"] != '0')
	{
		if ($ss != "")
            $ss.=" and ";

		$time1 = strtotime($d["year1"].'-'.$d["month1"].'-'.$d["day1"].' 00:00:00');
		$time2 = strtotime($d["year2"].'-'.$d["month2"].'-'.$d["day2"].' 00:00:00');
		if($time1 <= $time2)
		{
			$time2 = $time2 + 86399;
		}
		else
		{
			$temp = $time2;
			$time1 = $time1 + 86399;
			$time2 = $time1;
			$time1 = $temp;
		}
		$ss.=" (bi_add_time > $time1 and bi_add_time < $time2) ";
	}

    if (isset($d["bi_type"]) and $d["bi_type"] != "none" and $d["bi_type"] != "") {
        if ($ss != "")
            $ss.=" and ";
        $ss.=" a.bi_type='" . $d["bi_type"] . "' ";
    }

    if (isset($d["isestimate"]) and $d["isestimate"] != "none" and $d["isestimate"] != "") {
        if ($ss != "")
            $ss.=" and ";
        $ss.=" block.isestimate='" . $d["isestimate"] . "' ";
    }

    //组合
    if ($ss != "")
        $ss = " where " . $ss;

    return $ss;
}


//删除处理
function msg_del($id) {
    GLOBAL $db, $tbl;

    //删除记录
    $q = "delete from $tbl where id=" . intval($id);

    $db->Execute($q);
}

function doact($type, $id) {
    GLOBAL $db, $tbl;
    $id2 = explode(",,", $id);
    $aa = explode(",", $id2[0]);
    $bb = explode(",", $id2[1]);

    switch ($type) {
        case 'del':

			$id = intval($id);
			$blockid = $db->GetOne("SELECT bi_blockid FROM block_img WHERE id = ".$id);
			$delarr[$blockid] = $blockid;
          
            msg_del($id);
            break;
        case 'addsort0':
            foreach ($aa as $v) {
				$id = intval($v);
				 $q = "update $tbl set bi_type=0 where id=$v";
              
                 $db->Execute($q);
                //echo "$v<BR>";
            }
            break;
        case 'multdel':
            foreach ($aa as $v) {
				$id = intval($v);
				$blockid = $db->GetOne("SELECT bi_blockid FROM block_img WHERE id = ".$id);
				$delarr[$blockid] = $blockid;
                msg_del($v);
                //echo "$v<BR>";
            }
            break;
        case 'top':
            if ($args == 0 || $args = '') {
                $q = "update $tbl set bi_top='1' where id in ($id2[0])";
            } else {
                $q = "update $tbl set bi_top='0' where id in ($id2[0])";
            }
            $db->Execute($q);
            break;
        case 'altorder':
            foreach ($aa as $k => $v) {
                $q = "update $tbl set displayorder=$bb[$k] where id=$v";
                $db->Execute($q);
            }
            break;
		case 'setface':
			$id = intval($id);
			$q = "SELECT bi_blockid,bi_img FROM block_img WHERE id = '".$id."'";
			$bi = $db->GetRow($q);
			if($bi['bi_img'] != '')
			{
				$q = "update block set b_img = '".$bi['bi_img']."' where id= '".$bi['bi_blockid']."'";
               
                $db->Execute($q);
			}
			break;

        case 'setface1':
			$id = intval($id);
			$q = "SELECT bi_blockid,bi_img FROM block_img WHERE id = '".$id."'";
			$bi = $db->GetRow($q);
			if($bi['bi_img'] != '')
			{
				$q = "update block set b_img2 = '".$bi['bi_img']."' where id= '".$bi['bi_blockid']."'";
               
                $db->Execute($q);
			}
			break;
        default:
            return "";
    }

	//更新删除图片的小区图片数
	if(!empty($delarr))
	{
		foreach($delarr as $blockid)
		{
			if($blockid > 0)
			{
				$sql = "select count(*) from block_img where bi_blockid = '".$blockid."' AND bi_type = 0";
				$num0 = $db->GetOne($sql);
				$sql = "select count(*) from block_img where bi_blockid = '".$blockid."' AND bi_type = 1";
				$num1 = $db->GetOne($sql);

				$sql = "update block_img_num set sort0_num = '".$num0."', sort1_num = '".$num1."' where blockid = '".$blockid."' ";
				$db->Execute($sql);
			}
		}
	}
  
}

function get_status($args, $id) {
    GLOBAL $conf_default_tplpath;
    //$args=$args==1?1:0;
    $src = $args == 1 ? "status_green.gif" : "status_red.gif";
    $alt = $args == 1 ? "点击关闭" : "点击打开";
    $ss = "<a href='' onclick='return clicktop($id,$args)'><img src=\"$conf_default_tplpath/images/$src\" border=0 alt=$alt></a>";
    return $ss;
}

?>