<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 系统基础函数文件
 *
 * @package     CodeIgniter
 * @subpackage  Helpers
 * @category    Helpers
 * @author      HOUSE365 ESF Dev Team
 * @link        http://nj.sell.house365.com/
 */


/**
 * 自动加载model类库
 * @param   string $model model类名
 * @update  2014/6/6 esf dev team
 */
if( !function_exists('load_m') )
{
    function load_m($model)
    {   
        $model = $model != '' ? strtolower($model) : '';
        $model_file_url = PUBLIC_MODEL_PATH.$model.'.php';
        if($model != '' && file_exists($model_file_url))
        {
            require_once($model_file_url);
        }
        else
        {
            echo 111;
			exit;
        }
    }
}

if( !function_exists('get_ip') )
{
    function get_ip(){
       
        $ip=$_SERVER["REMOTE_ADDR"];
        if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
            $tip=split(",",$_SERVER["HTTP_X_FORWARDED_FOR"]);
            $ip=$tip[0];
        }
        return(trim($ip));
    }
}

//通过框架路由类定位
if( !function_exists('where_am_i') )
{
    function where_am_i(){
       
        global $RTR;
        //通过框架路由类定位
        $class  = $RTR->fetch_class();
        $method = $RTR->fetch_method();

        return array('class'=>$class, 'method'=>$method);
    }
}

/**
 * 个人用户中心分页
 * @param int $page_now 当前页
 * @param int $pages 总页数
 * @param int $show_num 分页显示的页数
 * @param string  $form 表单名
 * @return string  分页字符串
 * @author esf dev team 2014-02-26
 */
if( !function_exists('helper_pagination') )
{
    function helper_pagination( $page_now , $pages , $numrows , $show_mum = 7 , $form = "search_form")
    {
        $form = 'document.'.$form;
        $ss = '';

        //如果页数小于1则返回空
        if( $pages <= 1 || !is_numeric($page_now) )
        {
            return '';
        } 
        else
        {
            $pre = $page_now == 1   ? $page_now : $page_now - 1; //前一页
            $end = $page_now == $pages  ? $pages : $page_now + 1; //下一页

            //首页、前一页
            if( $page_now == 1 )
            {
                $ss .= "<p class='pag'><a class='lin prev'>首 页</a></p>";
                $ss .= "<p class='pag'><a class='lin prev'>&lt;上一页</a></p>";
            }
            else
            {
                $first_page = 1; //第一页
                $ss .= "<p class='pag'><a class='lin prev' onclick=\"$form.page.value=".$first_page.";$form.submit();return false;\">首 页</a></p>";
                $ss .= "<p class='pag'><a class='lin prev' onclick=\"$form.page.value=".$pre.";$form.submit();return false;\">&lt;上一页</a></p>";
            }

            //前三页
            if( $page_now > 3 )
            {
                if( $pages - $page_now <= 3 )
                {
                    $num = $page_now - ( $show_mum - ( $pages + 1 - $page_now ) ) ;
                    $num = $num >= 1 ? $num : 1;

                    for( $i = $num ; $i < $page_now ; $i ++)
                    {
                        $ss .= "<p class='pag'><a class='lin' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a></p>";
                    }
                }
                else
                {
                    for($i = $page_now - 3 ; $i < $page_now ; $i ++)
                    {
                        $ss .= "<p class='pag'><a class='lin' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a></p>";
                    }
                }
            }
            else
            {
                $show_num   = $pages > $page_now ? $page_now : $pages;
                for($i = 1 ; $i < $show_num ; $i ++)
                {
                    $ss .= "<p class='pag'><a class='lin' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a></p>";
                }
            }

            //当前页面
            $ss .= "<p class='pag'><a  onclick=\"$form.page.value=".$page_now.";$form.submit();return false;\" class='lin current'>{$page_now}</a></p>";

            //后三页数
            if( $pages - $page_now > 5 )
            {
                //当前页数小于等于5时，强制显示到第7页
                for($i = $page_now + 1 ; $i <= $page_now + 5; $i++)
                {
                    $ss .= "<p class='pag'><a  class='lin' class='lin' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a></p>";
                }
            }
            else
            {
                for($i = ($page_now + 1) ; $i <= $pages; $i ++)
                {
                    $ss .= "<p class='pag'><a class='lin' onclick=\"$form.page.value=".$i.";$form.submit();return false;\" >{$i}</a></p>";
                }
            }

            //下一页、尾页
            if( ($page_now == $pages) || ($pages == 0) )
            {
                $last_page = $pages;
                $ss .= "<p class='pag'><a class='lin next'>下一页&gt;</a></p>";
                //$ss .= "<p class='pag'><a class='p-next'>末  页</a></p>";
            } 
            else 
            {
                $next_page = $page_now + 1;
                $last_page = $pages;
                $ss .= "<p class='pag'><a  onclick=\"$form.page.value=".$next_page.";$form.submit();return false;\" class='lin next'>下一页&gt;</a></p>";
                //$ss .= "<p class='pag'><a  onclick=\"$form.page.value=".$last_page.";$form.submit();return false;\" class='lin next'>末 页</a></p>";
            }
        }

        return $ss;
    }
}

/**
 * 内页ajax新分页
 * @param int $page 当前页
 * @param int $pages 总页数
 * @return mixed
 */
function zsb_pagenavi_ajax($page, $pages, $func_callback)
{
	$form ='document.search_form';

	$ss = "";
	if( $pages <= 1 || !is_numeric($page) ){//如果页数小于1则返回NULL
		return '';
	} else{
        $pre = $page==1 ? $page : $page-1;//前一页
		$end = $page==$pages ? $pages : $page+1;////下一页
        if( $page == 1 ){
			$ss .= '<p class="pag"><a class="lin prev" href="###">&lt;上一页</a></p>';
		} else {
			$first_page = 1;//第一页
			$ss .= '<p class="pag"><a class="lin prev" onclick="'.$func_callback.'(1)" href="###">&lt;上一页</a></p>';
		}

        for($i=1;$i<=$pages;$i++){
            if($i==$page)
            {
                $css='style="color:#FF8106;"';
            }else{
                $css='';
            }
            $ss .= '<p class="pag"> <a '.$css.' class="lin" href="###" onclick="'.$func_callback.'('.$i.')">0'.$i.'</a></p>';
        }

    	if( ($page == $pages) || ($pages == 0) ){
			$last_page = $pages;
			$ss .= '<p class="pag"><a class="lin next" href="###">下一页&gt;</a></p>';
		} else {
			$next_page = $page +1;
			$ss .= "<p class='pag'><a class='lin next' href='###' onclick='$func_callback(".$next_page.")'>下一页&gt;</a></p>";
		}
        }

	return $ss;
}

if( !function_exists('changeToKeyArray') )
{
    function changeToKeyArray($array,$key) 
    {
        $newArray = array();
        if($array) {
            foreach ($array as $value) {
                if(key_exists($key, $value)) {
                    $newArray[$value[$key]] = $value;
                }
            }
        }
        return $newArray;
    }
}





/**
* 发送短消息
* @param string $mobile 电话号码
* @param string $msg 消息内容
* @return $msg
*/
if(!function_exists('send_sms'))
{


	function send_sms($mobile,$msg,$type='tz',$yzm=""){
		global $conf_city;
                $conf_city = $conf_city == '' ? 'nj' : $conf_city; 
		$type = $type ? $type : 'tz';

        $url = "http://api.house365.com/esf/web/sendsms.php?conf_cityflag=".$conf_city."&mobile=".$mobile."&msg=".urlencode($msg)."&type=".$type."&ymz=".$yzm;
		curl_get_contents($url);
	}
}


/**
*生成随机字符串
*/
function getRandChar($length){
   $str = null;
   $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
   $max = strlen($strPol)-1;

   for($i=0;$i<$length;$i++){
	  $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
   }

   return $str;
}


/**
 *部署GA统计代码
 *@param string $ga_page GA统计代码
 *@param string $ga_city 页面所属城市
 *@param string $ga_channel 页面所属频道
 *@return string  ga统计代码
*/
function get_ga_js_code($ga_page , $ga_city = 'nj' , $ga_channel = 'zsb')
{	
	$ga_js_code = '';

	if( $ga_page != '')
	{	
		$last_sign = substr($ga_page , -1 , 1);
		if( $last_sign == '/')
		{
			$ga_page = substr($ga_page , 0 , -1);
		}
	   // if($ga_page=='/登陆页'){
		//	$t2_ga_page = "/登陆页";
		//}else{
		//   $t2_ga_page = "/经纪人版";
		//}
        $t2_ga_page = $ga_page=='/登陆页' ? "/登陆页" : "/经纪人版";
		$ga_js_code = "<script type='text/javascript'>
		var ga_city = '".$ga_city."';
		var ga_channel = '".$ga_channel."';
		var ga_page= '".$ga_page."';
		var t2_ga_channel = 'zsb';
        var t2_ga_page= '".$t2_ga_page."';
		</script>
		<script language='javascript'>var website = 2;</script>
		<script language='javascript' src='http://stat.house365.com/365count.js'></script>";
	}

	return $ga_js_code;
}
/* End of file base_helper.php */
/* Location: ./applications/helpers/base_helper.php */