<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_page_cab_ot_upload_docs_json extends c_page_TXT
{ public $terminal_id ;
  public $personal_id ;
  public $mo_id ;

  function c_page_json() {}
  function body()
  { $res=array() ; ob_start() ;
    if (_sizeof($_FILES)) $res=$this->upload_doc() ;
    $res['report']=ob_get_clean() ;
    $text_json_out=json_encode($res,JSON_UNESCAPED_UNICODE) ;
    echo $text_json_out ;
  }

  function upload_doc()
  {   $result=array() ;
      //damp_array($_POST) ;
      if (_sizeof($_FILES))
       { foreach($_FILES as $file_info)
           {   //if ($file_info['type']=='application/vnd.ms-excel')
               //damp_array($file_info,1,-1)  ;
               ?><h2>Загружаем данные из файла "<?echo $file_info['name']?>"</h2><?
               $res=array() ;
               $fname=safe_file_names($file_info['name']) ;
               $target_fname=_DIR_TO_ROOT.'/public/ot_docs/'.$fname ;
               if (is_uploaded_file($file_info['tmp_name']))
                   if (move_uploaded_file($file_info['tmp_name'],$target_fname)) echo '<div class="green">Файл успешно сохранен '.hide_server_dir($target_fname).'</div>' ;
                   else                                                          echo '<div class="red">Не удалось сохранить файл в '.$target_fname.'</div>' ;

               $result['file_name'][$target_fname]=$fname  ;

               $data=array('obj_name'=>$file_info['name'],
                           'file_name'=>hide_server_dir($target_fname),
                           'type'=>$file_info['type'],
                           'size'=>$file_info['size'],
                           'obj_reffer'=>$_POST['obj_reffer'],
                            ) ;
               //damp_array($data) ;
               $id=ENGINE()->reg_file($data) ;
               $result['file_id'][]=$id  ;
               echo '<div class="green">Документ успешно зарегистрирован, ID='.$id.'</div>' ;
               ?><iframe src="<?echo _MAIN_PROTOCOL._MAIN_DOMAIN?>/<? echo $data['file_name']?>" style="width:100%; height:700px;" frameborder="0" /></iframe><?


           }
           $result['status']='ok'  ;
       }
       else $result['status']='error'  ;
       return($result) ;
    }



}
