<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_page_cab_esmo_magnit_pdd_upload_napravlenie_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() ;
      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._UPLOADS_DIR.$fname ;
               if (is_uploaded_file($file_info['tmp_name']))
                   if (move_uploaded_file($file_info['tmp_name'],$target_fname)) echo '<div class="green">Файл успешно сохранен</div>' ;
                   else                                                          echo '<div class="red">Не удалось сохранить файл</div>' ;

               if ($file_info['type']=='application/pdf')
                 {   echo 'Преобразовываем PDF в JPG<br>' ;
                     $saved_file_dir=$target_fname ;
                     $saved_file_name=basename($target_fname) ;
                     $arr_images=array() ;

                     $images = new Imagick();
                     $images->setResolution(300,300);
                     $images->readImage($saved_file_dir);
                     if (_sizeof($images)) foreach($images as $i=>$image)
                     {  $saved_img_file_name=$saved_file_name.'_page'.$i.'.jpg' ;
                        $saved_img_file_dir=_TEMP_DIR.'uploads/'.$saved_file_name.'_page'.$i.'.jpg' ;
                         $image->setColorspace(imagick::IMGTYPE_GRAYSCALE);
                         $image->setCompression(Imagick::COMPRESSION_JPEG);
                         $image->setCompressionQuality(80);
                         $image->setImageFormat("jpg");
                        $image->writeImage($saved_img_file_dir);
                        if (file_exists($saved_img_file_dir))
                        { $arr_info=getimagesize($saved_img_file_dir,$arr_info2) ;
                          echo 'Преобразование выполнено успешно, тип файла '.$arr_info['mime'].', разрешение '.$arr_info[0].'x'.$arr_info[1].', размер '.show_size(filesize($saved_img_file_dir)).'<br>' ;
                          $finfo['saved_images'][]=array('file'=>$saved_img_file_dir,'preview'=>'<img src="'._UPLOADS_DIR.$saved_img_file_name.'" width="100">') ;
                          $arr_images[]=_UPLOADS_DIR.$saved_img_file_name ;
                        }
                        $result['file_name'][]=$saved_img_file_name ;
                        //$target_fname=_DIR_TO_ROOT._UPLOADS_DIR.$saved_img_file_name ;

                     } else echo 'Не удалось выполнить открыть PDF для преобразования<br>' ;
                 }
               else $result['file_name'][]=$fname  ;

               // регистрируем скан в таблице сканов
               if (_sizeof($result['file_name'])) foreach($result['file_name'] as $saved_img_file_name)
               { $data=array('obj_name'=>hide_server_dir(_UPLOADS_DIR.$saved_img_file_name),'status'=>1,'account_upload'=>MEMBER()->id,'group_upload'=>MEMBER()->cur_group_id) ;
                 if ($result['scan_id'][0]) $data['parent']=$result['scan_id'][0] ;
                 $data['indx']=_sizeof($result['scan_id'])+1 ;
                 $reg_result=DOCS()->reg_scan($data) ;
                 $scan_reffer=$reg_result['doc_reffer'] ;
                 list($id,$tkey)=explode('.',$scan_reffer) ;
                 $result['scan_id'][]=$id  ;
                 echo 'Скан успешно зарегистрирован в системе, ID='.$id.'<br>' ;
               }

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



}
