<?php
if(!defined('IN_DEMOSOSO')) {
  exit('this is wrong page,please back to homepage');
}


 
function dmjtblock($folder,$file,$topv=10){ 
  
     //dmjtblock('jtpage01/part/part1.php');
      $datafile = jtdatafilexist($folder,$file);
     // $file_field = $datafile[0];
      $file_cur = $datafile[0];
    //  if(!is_file($file_field)){
     //   echo '<p class="errorfont">字段文件：......'.substr($file_field,-60).'  not exist</p>';
      //  return '';
    //  }
      if(!is_file($file_cur)){
        echo '<p class="errorfont">数据文件：......'.substr($file_cur,-60).'  not exist</p>';
        return '';
      }


             
             //-----------
       if(dmlogin()){      
          $type = 'jtmb';
          $text = '编辑文本';
            $linkv = BASEURL.'dmpostform.php?type=editjtmbtext&pidname='.$folder.'&pidname2='.$file.'&lang='.LANGPATH;
       
            echo  '<div style="position:relative">';
            echo '<a style="display:none;top:'.$topv.'px !important;right:45% !important" target="_blank" 
            data-fancybox data-type="iframe" data-src="'.$linkv.'" href="javascript:void(0)" class="dmedit dmeditjttext" >'.$text.'</a>';
            echo '</div>'; 

           }

           //---------------

           // $arr_field = require $file_field; 
            // pre($arr_field);



            
            $arr_data = require $file_cur;
          //  pre($arr_data);

           
            if(count($arr_data)==0) return []; 



            $arrNew = [];
            $arrNew['cateid']='';


             $i=-1;
            foreach ($arr_data as $k => $v) {
              $i++;

                 $datav = '';

            //  if(array_key_exists($i,$arr_data))  $datav = $arr_data[$i];

                $datav =  dmjtinput2($v);
               // array_push($arrNew, $v);
                $arrNew[$k]= $datav;
            }

             // pre($arrNew); 
            
            if($arrNew['cateid']<>'') echo edit_node_inblock($arrNew['cateid'],''); 
           
 
              return $arrNew; 
                 
         

             
}

function jtdatafilexist($folder,$file){
   $file_field =  $file_cn = $file_cur = '';
   $filename_cn = 'data_'.$file.'_'.JTTEXTLANG.'.php';
   $filename_cur = 'data_'.$file.'_'.LANG.'.php';
   $hascurfile = 1;

      if(substr($folder,0,6)=='jtpage')  {
           $file_cn = BLOCKROOT.'jtpage/'.$folder.'data/'.$filename_cn;
           $file_cur = BLOCKROOT.'jtpage/'.$folder.'data/'.$filename_cur;
      }
      if(substr($folder,0,6)=='jthome')  {
        //$fileself =BLOCKROOT.'jthome/'.$folder.'data/'.LANG.'_'.$file.'.php';
       // $filecn = BLOCKROOT.'jthome/'.$folder.'data/cn_'.$file.'.php';

       //  $file_field =BLOCKROOT.'jthome/'.$folder.'data/'.'data_'.$file.'.php';
         $file_cn = BLOCKROOT.'jthome/'.$folder.'data/'.$filename_cn;
         $file_cur = BLOCKROOT.'jthome/'.$folder.'data/'.$filename_cur;

      }

      // echo $file_cn;

      if(is_file($file_cn)) {
            if(JTTEXTLANG<>LANG){
                     if(!is_file($file_cur)) {
                       $hascurfile = 0;

                          //copy($file_cn,$file_cur); //不在这里复制，另外手动复制
                         $file_cur = $file_cn;
                         $filename_cur = $filename_cn;
                     } 

                 }
      } 
      else{
        $str = "<?php return ['title'=>'','subtitle'=>'','linktitle'=>'','linkurl'=>'','img'=>''];"; 
       file_put_contents($file_cn,$str);


      }


 
  //    $datafile = $fileself;
     // if(!is_file($datafile)){
      //   $datafile = $filecn;
    //  } 
     // return [$file_field,$file_cur,$filename_cur,$hascurfile];     
     return [$file_cur,$filename_cur,$hascurfile];     

}



function mbjtnode($pidname,$maxline=20,$excerpt=80,$order='',$where=''){

	  $sqlarr = array(                 
                'start'=>0,
                'maxline'=>$maxline,
                'excerpt'=>$excerpt,
                'order'=>$order,
                'where'=>$where
                );
 
 $arr = getarr_node($pidname,$sqlarr); 
 
return  $arr;

}


 


function dmjttext($arr,$topv=10){


   $datafilearr = jttextexist($arr); 
   $datafile = $datafilearr[0];

  if(!is_file($datafile)) { 
      
        echo '<div style="padding:20px;background:red;color:#fff">';
      echo '...'.substr($datafile,-50).'   -- file not exist.';
      echo '</div>';

      return [];  
     }    
    // edit_data_injttext($arr,$topv); 
     //---------------

      $arr = require $datafile;
      if(count($arr)==0) return []; 
      $arrNew = [];
      $arrNew['cateid']='';

      foreach ($arr as $k => $v) {
          $v =  dmjtinput2($v);
         // array_push($arrNew, $v);
          $arrNew[$k]= $v;
      }

      //pre($arrNew);
 
      
      
      if($arrNew['cateid']<>'') echo edit_node_inblock($arrNew['cateid'],''); 
     

      return $arrNew; 
}

function jttextexist($arr){
      if(substr($arr[0],0,6)=='jtpage')   $folder = 'jtpage/';
      else $folder = 'jthome/';
      
        $file =BLOCKROOT.$folder.$arr[0].'/data/'.LANG.'/data_'.$arr[1].'.php';
       // $filebak =  BLOCKROOT.'jtpage/'.$arr[0].'/data/'.LANG.'/bak/data_'.$arr[1].'.php';

        $filebak=str_replace('\\','/',$arr[0].$arr[1]);

         $filebak2=str_replace('/','_',$filebak).'_'.LANG.'.php';


        if(!is_file($file)) {
            $file =BLOCKROOT.$folder.$arr[0].'/data/cn/data_'.$arr[1].'.php';
            $filebak2=str_replace('/','_',$filebak).'_cn.php';
        } 

      //  $filebak =  BLOCKROOT.'jtpage/'.$arr[0].'/bak/'.$pidname.'-bak.php';
       
       $filebak3 = WEB_ROOT.'cache/jttextbak/'.$filebak2;
      //echo  $filebak3;
      return [$file,$filebak3];
       

}

function jttext_toarr($v){//-----no use...........
   $arr = [];
  $arr1 =  explode('##:_-_##::', $v);
  //pre($arr1);
  $arr3= [];
  foreach ($arr1 as $k => $v) {
        $arr2 =  explode('**:_-_**::', $v);
        //pre($arr2);
        $key = $arr2[0];
      //  echo $key;
        $arr3 = [$key=>$arr2[1]];

        array_push($arr, $arr3);

  }


   return $arr;

    
      
       

}

 
function dmjttoarrtwo($arr){
 // when use slider
  
     $arrNew = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],];
     $i=0;
     foreach ($arr as $k => $v) {
     // echo $v;
      if($v=='#:line:#'){
        $i = $i+1;
      }else{
         
         array_push($arrNew[$i], $v); 
      }
      
       
     }
     return $arrNew;
}



 function dmget_curfolderandfile($filearr,$level){ 
     $pidfile = $filearr['filename'];
       $pidtemp = str_replace('\\','/',$filearr['dirname']);
       $arr = explode('/', $pidtemp);
       $count = count($arr);
       if($level == 1)  $v = $arr[$count-1].'/';
       else if($level == 2)  $v =  $arr[$count-2].'/'.$arr[$count-1].'/';
       else  $v = $arr[$count-3].'/'.$arr[$count-2].'/'.$arr[$count-1].'/';
       return [$v,$pidfile];

}
