<?

	require_once "xslt/codegen/agents/_base.php";
	define('PREFIX','info');

	class PubGenAgent extends BaseGenAgent
	{
		function PubGenAgent()
		{}

		//
	 	// Process
		//
		
		function process($r)
		{
		        global $cat_db;
			
			$s = '<pub>';
			$s .= "<title>$r[title]</title>";
		
			$res = $cat_db->query("select * from p_contain where doc_fn='$r[id_p_docum]' order by pos");

			foreach($res as $rs) {
				$s .= "<block>";
				if(strlen($rs[photo]) > 2 && file_exists('../'.$rs[photo])) $s .= "<photo>$rs[photo]</photo>";
				$s .= "<info>".xmlParseToHTML($rs[info])."</info>";
				$s .= "</block>";	
			};		

			$res = $cat_db->query("select * from p_docum,p_link where doc_base='$r[id_p_docum]' and doc_sub=id_p_docum order by grp,pos");			
			
			$s .= "<publinks>";
		
			$s .= "<pubgroup>";
			$grp = $res[0][grp];
			$p = false;

			foreach($res as $rs) {
				if($rs[grp] != $grp) {
					$s .= "</pubgroup><pubgroup>";										
					$grp = $rs[grp];
				};
				if(strlen($rs[link]) > 2)  
					$s .= "<publink title='$rs[title]' location='$rs[link]'>$rs[comment]</publink>";
				else
					$s .= "<publink title='$rs[title]' location='".PREFIX."$rs[id_p_docum].html'>$rs[comment]</publink>";
			};		

                        $s .= "</pubgroup>";
			$s .= "</publinks>";			
			$s .= '</pub>';
			return $s;
		}	

		function execute()
		{
			$d = array();
			$s = '';
			$filename = '';

			global $cat_db;
	
			$res = $cat_db->query("select * from p_docum order by id_p_docum");

			$b = true;
			$i = 0;
			foreach($res as $r) {
				if(strlen($r[link]) > 1) continue;
				if($b && strlen($r[link]) < 2) {
					$filename = "info.html";
					$s = $this->preprocess($filename,'info','Информация');
					$s .= $this->process($r);
					$s .= $this->postprocess();
		        		$d[$i] = new GenPage($filename);
					$d[$i]->applyXML($s);
					$i++;
					$b = false;	
				};
				$filename = PREFIX.$r[id_p_docum].".html";
                        

				$s = $this->preprocess($filename,'info',$r[title]);
				$s .= $this->process($r);
				$s .= $this->postprocess();

				// print $s;
					
		       		$d[$i] = new GenPage($filename);
				$d[$i]->applyXML($s);
				$i++;
			};
			return $d;				
		}	

	};

?>