<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function debug($debugged){
	echo '<pre style="text-align:left; font-family: tahoma; font-size: 13px;">';
	print_r($debugged);
	echo '</pre>';
    }

    function content($key='')
    {
        return Modules::run("content/bykey",$key);
    }

    function img_check($url = ''){
        if(is_array(getimagesize($url)))
        {
            return $url;
        }
        else{
            return base_url()."assets/images/default-placeholder.png";
        }
    }
    
    function alertmsg($msg='',$url='',$code='200'){
        $status[200] = ' success';
        $status[100] = ' error';
        
        $chk_status =(isset($status[$code])) ?   $status[$code] : 'error' ;
        $html = '<html>';
            $html.='<header>
                             <script src="'.base_url().'assets/js/jquery-1.8.3.min.js"></script>
 
                               <meta charset="utf-8" /> 
                             <script type="text/javascript" src="'.base_url().'assets/js/jquery.blockui.js"></script> 
                             <style>
                                 .titleMsg{
                                    background-size: 40px 40px;
                                    background-image: linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
                                                        transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
                                                        transparent 75%, transparent);                                        
                                     box-shadow: inset 0 -1px 0 rgba(255,255,255,.4);
                                      
                                     border: 1px solid;
                                     color: #fff;
                                     padding: 10px;
                                     
                                     text-shadow: 0 1px 0 rgba(0,0,0,.5);
                                     animation: animate-bg 5s linear infinite;
                                     z-index: 100000;  
                                 }
                                 .error{
                                         background-color: #de4343;
                                         border-color: #c43d3d;
                                }
                                         
                                .warning{
                                         background-color: #eaaf51;
                                         border-color: #d99a36;
                                }

                                .success{
                                         background-color: #61b832;
                                         border-color: #55a12c;
                                }
                                @keyframes animate-bg {
                                    from {
                                        background-position: 0 0;
                                    }
                                    to {
                                       background-position: -80px 0;
                                    }
                                }
                                .code_error{font-size:11px;margin-top:5px;color:#E5BCBC}   
                                .head_msg{font-weight:bold;font-size15px;}                          
                             </style>
                        ';
            $html .="<script>
                        $(document).ready(function() { 
                           
                                $.blockUI({ 
                                    theme:false, 
                                    title:    '', 
                                    message:  '<div align=\"center\"  ><div class=\"titleMsg ".$chk_status."\"><b>MESSAGE</b><br/> <br/><p>".$msg."</p><input type=\"button\" id=\"OK\" value=\"OK\" class=\"btn btn-primary btn-mini\"></div></div>', 
                                     
                                } ); 
                                
                                   
                                $('#OK').click(function(){

                                    
                                      var url ='".$url."' ; 
                                      if(url !='') {window.location.href='".$url."';  }
                                      else{ setTimeout($.unblockUI, 1);   }
                                });
                             
                        }); 
                                    </script></header>";
        $html .='</html>';
        
        echo $html ;
        
    }
 
    function showDate($dateDb=''){
        if($dateDb =="" || $dateDb =='0000-00-00 00:00:00' || $dateDb =='0000-00-00' ) return NULL ;
        return date(FORMATDATE_SHOW,strtotime($dateDb));
    }
    
    function showDateTime($dateDb=''){
        if($dateDb =="" || $dateDb =='0000-00-00 00:00:00' || $dateDb =='0000-00-00' ) return NULL ;
        return date(FORMATDATETIME_SHOW,strtotime($dateDb));
    }
    
    function setFormatDateINDB($date='',$str='-'){
 
        if(empty($date)) return null ;
        list($day,$month,$year) = explode($str,$date);    
        if($date =="" || $date =='0000'.$str.'00'.$str.'00' ){
            return NULL ;
        }else{ return date(FORMATDATE_INDB,strtotime($year."-".$month."-".$day));   }
    }
    function setFormatDateTimeINDB($date=''){
 
        if(empty($date)) return null ;
        list($day,$month,$year) = explode($str,$date);    
        if($date =="" || $date =='0000-00-00' ){
            return NULL ;
        }else{ return date(FORMATDATETIME_INDB,strtotime($year."-".$month."-".$day));   }
    }
     function setFormatDate2INDB($date=''){
 
        if(empty($date)) return null ;
        list($day,$month,$year) = explode('/',$date);    
        if($date =="" || $date =='0000/00/00' ){
            return NULL ;
        }else{ return date(FORMATDATE_INDB,strtotime($year."-".$month."-".$day));   }
    }
    
    function  ErrorPicUser(){
        return ' onerror=\'this.src="'.base_url().'assets/img/nopic.jpg"\'     ' ;
    }
    
    function  ErrorPicImg(){
        return ' onerror=\'this.src="'.base_url().'assets/img/nopic.jpeg"\'     ' ;
    }

    function checkImg($img=''){
       if(empty($img)) return  base_url().'assets/images/nopic.jpeg' ;
       else return $img ;
   }
    
    function zeroDefault($value){
       return @$value ? $value : '00' ;
   }
   
   function getImageLoad(){
       return '<img src="'.base_url().'assets/img/ajax-loader.gif" >';
   }
 
   function resetNumberformat($number=''){
       return str_replace(',','',$number);
   }
   
   function setFloatformat($number=''){
       return number_format($number,2);
   }
   
   function setFormatNamecat($namecat= '',$listnamecat=''){
        
        if(isset($listnamecat) && $listnamecat !=""){
           if(strstr($listnamecat,'-'))  $listarray =  explode('-',$listnamecat)  ;
              else $listarray[0] = $listnamecat ;
               $listarray[] =  $namecat ;
               
               return join('-',$listarray) ; 
            }else{
                 return $namecat;
            }
         
   }
   
   function unset_post($post=''){
         foreach($post as $key =>$value){
                       $post[$key] = "" ;
                  }  
   }
    
   function getlang(){
       return  isset($_SESSION['site_lang']) ? $_SESSION['site_lang'] : "thai" ;
    } 
   
   function getlangDB(){
       $lang = getlang();
       
       switch($lang){
           case "thai":  return 'th' ; break ;
           case "english":  return 'eng' ; break ;
           case "chinese": return 'cn' ; break ;
       }
        
    } 
    
   function substrText($msg='',$leng='100'){
        if(empty($msg)) return NULL ;
        if(strlen($msg) > $leng){
                return iconv_substr($msg,0,$leng, "UTF-8")."...";
        }  return $msg ;
        
    }
   
   function thai_date($time='',$array_format='',$lang='thai'){  
        if(empty($time)) return null ; 
          $thai_day_arr_th=array("อาทิตย์","จันทร์","อังคาร","พุธ","พฤหัสบดี","ศุกร์","เสาร์");  
          
              $thai_month_arr_th=array("0"=>"", "1"=>"มกราคม",  "2"=>"กุมภาพันธ์",  "3"=>"มีนาคม",  "4"=>"เมษายน",  "5"=>"พฤษภาคม",  "6"=>"มิถุนายน",   "7"=>"กรกฎาคม",  "8"=>"สิงหาคม",  "9"=>"กันยายน",  "10"=>"ตุลาคม",  "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม" );  
             $thai_month_mini_th=array("0"=>"","1"=>"ม.ค.","2"=>"ก.พ.","3"=>"มี.ค.",  "4"=>"เม.ย.",  "5"=>"พ.ค.",  "6"=>"มิ.ย.",   "7"=>"ก.ค.",  "8"=>"ส.ค.",  "9"=>"ก.ย.",  "10"=>"ต.ค.",  "11"=>"พ.ย.",  "12"=>"ธ.ค." );  
             $thai_day_arr_eng=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");  
             $thai_month_arr_eng=array( "0"=>"", "1"=>"January",  "2"=>"February",  "3"=>"March",  "4"=>"April",  "5"=>"May",  "6"=>"June",   "7"=>"July",  "8"=>"August",  "9"=>"September",  "10"=>"October",  "11"=>"November",  "12"=>"December" );  
             $thai_month_mini_eng=array( "0"=>"","1"=>"Jan","2"=>"Feb","3"=>"Mar",  "4"=>"Apr",  "5"=>"May",  "6"=>"Jun",   "7"=>"Jul",  "8"=>"Aug",  "9"=>"Sep",  "10"=>"Oct",  "11"=>"Nov",  "12"=>"Dec" );  
  
        $day = $lang=="thai" ?  "วัน".$thai_day_arr_th[date("w",$time)]."ที่ " :    " ".$thai_day_arr_eng[date("w",$time)]." " ;  
        $date  =  date("j",$time);
        $thai_y = (date("Y",$time)+543) ;
        $month = $lang=="thai" ? " ". $thai_month_arr_th[date("n",$time)] :  " ".$thai_month_arr_eng[date("n",$time)];  
        $month_mini = $lang=="thai" ?  " ".$thai_month_mini_th[date("n",$time)] : " ".$thai_month_mini_eng[date("n",$time)];  
      //  $year =   $lang=="thai" ?  " พ.ศ. ".(date("Y",$time)+543) : date("Y",$time) ;  
        $year =   $lang=="thai" ?  " ".(date("Y",$time)+543) : date("Y",$time) ;  
        $time =   "  ".date("H:i",$time).""; 
        
      
        $array['D'] = $day ;
        $array['d'] = $date ;
        $array['M'] = $month ;
        $array['m'] = $month_mini ;
        $array['Y'] = $year ;
        $array['y'] = substr($thai_y,2) ;
        $array['t'] = $time ;
    
        $return ="";
        if(is_array($array_format)){
            foreach($array_format as $key=>$value){
                if(isset($array[$value])) 
                $return .= $array[$value] ;
            }
        }
 
        
        return $return; 
      //  return $thai_date_return;  
    } 
    
    function thai_month($monthInt='',$mini=''){
        $thai_month=array("0"=>"", "1"=>"มกราคม",  "2"=>"กุมภาพันธ์",  "3"=>"มีนาคม",  "4"=>"เมษายน",  "5"=>"พฤษภาคม",  "6"=>"มิถุนายน",   "7"=>"กรกฎาคม",  "8"=>"สิงหาคม",  "9"=>"กันยายน",  "10"=>"ตุลาคม",  "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม" );  
        $thai_month_mini=array("0"=>"","1"=>"ม.ค.","2"=>"ก.พ.","3"=>"มี.ค.",  "4"=>"เม.ย.",  "5"=>"พ.ค.",  "6"=>"มิ.ย.",   "7"=>"ก.ค.",  "8"=>"ส.ค.",  "9"=>"ก.ย.",  "10"=>"ต.ค.",  "11"=>"พ.ย.",  "12"=>"ธ.ค." );  
       if($mini) {
           if(isset($thai_month_mini[$monthInt])) return $thai_month_mini[$monthInt] ; 
       }else{
           if(isset($thai_month[$monthInt])) return $thai_month[$monthInt] ; 
       } 
       
    }
    
    function thaiyear($year=''){
        return $year+543 ;
    }
    
   
/* End of file common_helper.php */
/* Location: ./system/helpers/common_helper.php */
