<?php
// сервис для сохранения файла титула 1 на диск и регистрации пакета
// более ничего с пакетом не делается
// обработка пакета будет выполняться отдельным заданием планировщика

include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_services_pl_get_titul extends c_page_txt
{

 function check_access()
  { /*if (!$this->setting['no_check_access']) $result=ESMO_GATEWAY()->api_check_access() ;
    else $result=['status'=>'success','comment'=>''] ;

    //if ($result['client_id']) $this->client_id=$result['client_id'] ;
		if ($result['status']!='success') { $this->response_error($mode,array('code'=>$result['comment'],'ip'=>$_SERVER['REMOTE_ADDR'])) ; }
*/
		$result=['status'=>'success','comment'=>''] ;
    return($result) ;
  }

  function body()
  {	  //ob_start() ;
	  // packet_type=2 - титул 2
	  // status=3 - подписан
	  if (!$_GET['driver_id'])
	  $recs_packet = execSQL('select t2.*,
	  						 t1.file_uid as titul1_file_uid,
	  						 pl.uid as pl_uid,
	  						 t4.uid as driver_uid
	  						 from pl_packets t2 
	  						 left join pl_packets t1 on t1.pkey=t2.titul1_id 
	  						 left join pl_put_list pl on pl.pkey=t2.pl_id 
	  						 left join pl_packet_driver t3 on t3.packet_id=t2.pkey 
	  						 left join obj_site_personals t4 on t4.pkey=t3.driver_id 
	  						 where t2.packet_type in (2,6) and t2.status=3');
	  else
		  $recs_packet = execSQL('select t2.*,
		  						 t1.file_uid as titul1_file_uid,
		  						 pl.uid as pl_uid,
		  						 t4.uid as driver_uid
		  						 from pl_packets t2 
		  						 left join pl_packets t1 on t1.pkey=t2.titul1_id 
		  						 left join pl_put_list pl on pl.pkey=t2.pl_id 
		  						 left join pl_packet_driver t3 on t3.packet_id=t2.pkey 
		  						 left join obj_site_personals t4 on t4.pkey=t3.driver_id 
		  						 where t2.packet_type in (2,6) and t2.status in (3,4) and t3.driver_id='.(int)$_GET['driver_id']);
	  //$text = ob_get_clean();

	  header("Content-type: text/xml;windows-1251");
	  $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	  $xml_doc = $imp->createDocument("", ""); // Creates a DOMDocument instance
	  $xml_doc->encoding = 'windows-1251';
	  $xml_doc->formatOutput = true;
	  $xml = add_element($xml_doc, $xml_doc, 'packets', '', array('date' => date('Y-m-d H:i')));
	  if (sizeof($recs_packet)) foreach ($recs_packet as $rec)
	  { $xml_packet = add_element($xml_doc, $xml, 'packet', '', array('id' => $rec['pkey']));
		 add_element($xml_doc, $xml_packet, 'pl_uid', $rec['pl_uid']);
		 add_element($xml_doc, $xml_packet, 'file_uid', $rec['file_uid']);
		 add_element($xml_doc, $xml_packet, 'type', $rec['packet_type']);
		 add_element($xml_doc, $xml_packet, 'link_id', $rec['titul1_id']);
		 add_element($xml_doc, $xml_packet, 'driver_uid', $rec['driver_uid']);
		 //add_element($xml_doc, $xml_packet, 'file_uid', $rec['titul1_file_uid']);
		 add_element($xml_doc, $xml_packet, 'download_packet_url', _PATH_TO_SITE.'/services/pl/get_packet/'.$rec['pkey'].'/');
		 add_element($xml_doc, $xml_packet, 'download_sign_url', _PATH_TO_SITE.'/services/pl/get_packet_sign/'.$rec['pkey'].'/');
	  }

	  //if ($options['debug']) add_element($xml_doc, $xml, 'debug', $options['debug']);
	  echo $xml_doc->saveXML();


  }



  function response($result,$options=[])
  {  header("Content-type: text/xml;charset=UTF-8") ;
	  //var_dump($result);
	   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
		 $xml_doc->encoding = 'utf-8';
		 $xml_doc->formatOutput = true;
		 $xml = add_element($xml_doc,$xml_doc,'results','',array('date'=>date('Y-m-d H:i'))) ;
		 if (_sizeof($result)) foreach ($result as $item_name=>$item_attr)
		 {
			 if (is_array($item_attr))
			 { $item_value=($item_attr['value'])?:'' ;
			   unset($item_attr['value']) ;
			 }
			 else $item_value=$item_attr ;
			 $item = add_element($xml_doc,$xml,$item_name,$item_value,$item_attr) ;
			 //if (_sizeof($arr)) foreach($arr as $name=>$value)  if ($name!='uid') add_element($xml_doc,$item,$name,$value) ;
		 }
		 if ($options['debug']) add_element($xml_doc,$xml,'debug',$options['debug']) ;
		 echo $xml_doc->saveXML() ;
  }

  function response_success($arr,$options=[])
  {  header("Content-type: text/xml;charset=UTF-8") ;
	   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
		 $xml_doc->encoding = 'utf-8';
		 $xml_doc->formatOutput = true;
		 $xml = add_element($xml_doc,$xml_doc,'results','',array('date'=>date('Y-m-d H:i'))) ;
		 $result = add_element($xml_doc,$xml,'result','success',array('uid'=>$arr['uid'])) ;
		 if (_sizeof($arr)) foreach($arr as $name=>$value)  if ($name!='uid') add_element($xml_doc,$result,$name,$value) ;
		 if ($options['debug']) add_element($xml_doc,$xml,'debug',$options['debug']) ;
		 echo $xml_doc->saveXML() ;
  }

	function response_error($mode,$arr)
  {  header("Content-type: text/xml;charset=UTF-8") ;
	   $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
	   $xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
		 $xml_doc->encoding = 'utf-8';
		 $xml_doc->formatOutput = true;
		 $xml = add_element($xml_doc,$xml_doc,'xml_doc','',array('date'=>date('Y-m-d H:i'))) ;
		 $error=add_element($xml_doc,$xml,'error','',array()) ;
		 if (_sizeof($arr)) foreach($arr as $name=>$value) add_element($xml_doc,$error,$name,$value) ;
		 echo $xml_doc->saveXML() ;
  }

}



?>
