<?php

//查找作者名称，根据参数类型判断查作者表还是翰特表
function get_author_name($author_code_id)
{
    $CI = & get_instance();
	if(is_numeric($author_code_id))
	{
		$CI->load->model('Infoauthors_model');
		$user=$CI->Infoauthors_model->detail_by_id($author_code_id);
		if($user)
		{
			return $user->a_name;
		}
	}
	else
	{
		$CI->load->model('Operator_model');
		$user=$CI->Operator_model->get_user($author_code_id);
		if($user)
		{
			return $user['OPI_Name'];
		}
	}
	return false;
}

//检查是否有短消息
function have_unread_sms($m_object_type,$m_object_id)
{
	$CI = & get_instance();
	$CI->load->model('InfoSMS_model');
	$sms_list=$CI->InfoSMS_model->unread_sms($m_object_type,$m_object_id);
	$admin_info=$CI->session->userdata('session_admin');
	if(empty($sms_list))
	{
		return false;
	}
	else
	{
		$sender=get_author_name($sms_list[0]->m_sender);
		if($admin_info->a_name==$sender){
			return false;
		}else{
			return true;
		}
	}
}

//截取字符串
function get_text_short($str, $length = 40, $ext = '') {
        $str    =   strip_tags($str);
        $str    =   htmlspecialchars_decode($str);
        $strlenth   =   0;
        $out        =   '';
        $output     =   '';
        $is_length  = false;
        preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/", $str, $match);
        foreach($match[0] as $v){
            preg_match("/[\xe0-\xef][\x80-\xbf]{2}/",$v, $matchs);
            if(!empty($matchs[0])){
                $strlenth   +=  1;
            }elseif(is_numeric($v)){
                $strlenth   +=  0.5;    // 字符字节长度比例 汉字为1
            }else{
                $strlenth   +=  0.5;    // 字符字节长度比例 汉字为1
            }
            if ($strlenth > $length) {
                $output .= $ext;
                $is_length=true;
                break;
            }
            $output .=  $v;
        }
        $data['content']=$output;
        $data['length']=$is_length;
        return $data;
    }
//获取头像
function get_author_photo($author_code_id=false)
{
    $CI = & get_instance();
    $root_url='';//http://data.chtcdn.com';
    $avatar=$root_url.'/css/images/avatar.jpg';
	if($author_code_id && is_numeric($author_code_id))
	{
		$CI->load->model('Infoauthors_model');
		$user=$CI->Infoauthors_model->detail_by_id($author_code_id);
		if(isset($user->a_photo) && $user->a_photo!='')
		{
			$avatar = $root_url.'/author/document/profile_photo/'.$user->a_photo;
		}
	}
	elseif(!$author_code_id)
	{
		$admin_info=$CI->session->userdata('session_admin');
		if($admin_info->a_photo!=''){
			$avatar = $root_url.'/author/document/profile_photo/'.$admin_info->a_photo;
		}
	}
	return $avatar;
}

//补全图片路径
function get_photo_url($photo){
    $root_url='';//'http://data.chtcdn.com';
    if ($photo=='') {
		$avatar = $root_url.'/css/images/uploadPic.jpg';
	}else{
		$avatar = $root_url.'/author/document/profile_photo/'.$photo;
	}
    return $avatar;
}

//获取任务大厅任务数量
function get_task_hall_count()
{
    $CI = & get_instance();
	$CI->load->model('Infotasks_model');
	$num=$CI->Infotasks_model->task_hall_count();
	return $num;
}

//获取不同任务状态的任务数量
function get_task_count_by_status(){
    $CI = & get_instance();
    $CI->load->model('Infotasks_model');
    $task_count_by_status=$CI->Infotasks_model->get_task_count_by_status();
    return $task_count_by_status;
}

//获取平台头部未读信息数量
function get_all_unread_sms(){
	$CI = & get_instance();
	$CI->load->model('InfoSMS_model');
	$admin_info=$CI->session->userdata('session_admin');
	$all_unread_sms=$CI->InfoSMS_model->all_unread_sms($admin_info->a_id);
	return $all_unread_sms;
}

//获取作者网前url
function get_user_web_url($userid=false){
	$CI = & get_instance();
	$CI->load->model('Infoauthors_model');
	if (!$userid) {
		$admin_info=$CI->session->userdata('session_admin');
		$userid=$admin_info->a_id;
	}
	$weburl='#';
	if (is_numeric($userid)) {
		$result=$CI->Infoauthors_model->get_user_weburl($userid);
		if (!empty($result)) {
			$a_name=$result[0]->a_name;
			$suer_weburl=trim($a_name);
			$suer_weburl=str_replace(" ","-",$suer_weburl);
			$weburl='http://www.chinahighlights.com/author/'.$suer_weburl.'/';
		}
	}
	return $weburl;
}

//分页函数
function show_page($page){
    $pageSize = $page['pageSize'];
    $total = $page['total'];
    $url = $page['url'];
    $pageNumber = ceil($total / $pageSize);
    $currentPage = $page['current'] ? $page['current'] : 1;
    $startPage = ($currentPage - 1) * $pageSize;

    $befor = $after = 0;
    $nowview=ceil(($currentPage+1)/10);
    $befor = $nowview==1?($nowview-1)*10+1:($nowview-1)*10;
    if($pageNumber>=$nowview*10){
        $after = $nowview*10;
    }else{
        $after = $pageNumber;
    }

    $html='';
    if ($pageSize <= $total) { 
       for ($i = $befor; $i <= $after; $i++) {
          if ($i == $currentPage) {
                $html .= "<li class=\"active\"><a href=\"javascript:void(0)\">{$i}</a></li>";
           }else {
                $html .= "<li><a href=\"{$url}/{$i}\">$i</a></li>";
           }
       }
    }
    $res['htmls']=$html;
    $res['prepage']=$currentPage - 1;
    $res['nextpage']=$currentPage + 1;
    $res['totalpage']=$pageNumber;
    return  $res;
}

function get_meta($im_ic_id, $im_key) {
    $CI = & get_instance();
    $CI->load->model('InfoMetas_model');
    return $CI->InfoMetas_model->get($im_ic_id, $im_key);
}