<?php

require_once('Documents/DocumentBuilder.class.php');
/**
 * Support class for portfolio management. This class is used to export documents inside portfolios to 
 * excel format.
 */
class ExportData
{
	/**
	 * This function exports the documents to an excel sheet. 
	 *
	 * @param array $documents array that contain document numbers.
	 * @param string $file_name file name of the exporting file.
	 */
	public function printRows($documents, $file_name="portfolio.xls")
	{
		// Include PEAR::Spreadsheet_Excel_Writer
 
		require_once "Spreadsheet/Excel/Writer.php";
 
		$row = 0;
		$column = 0;
		$server_name = $_SERVER['HTTP_HOST'];
		$doc_num = 0;
		$doc_type = '';
		$pub_date = '';
		$title = '';
		$abstract = '';
		$inventor_name = '';
  		$assignee = '';
		$app_number = '';
		$filing_date = '';
		$current_class = '';
		$other_class = '';
		$arr_obj_document = array();		
		$doc_count = 0;
			
		foreach ($documents as $document)
		{
			$document_id = $document[0];
			$collection_id = $document[1];

			$document_file = '';
			
			if ($collection_id == 900)
			{
				$document_file = JpPatentsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);	
			    $doc_type = 'JP';
			}
			elseif ($collection_id == 800)
			{
				$document_file = WipoPatentsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);	
			    $doc_type = 'WIPO';		
			}
			elseif ($collection_id == 250)
			{
				$document_file = FamilyPatentsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);			
			}
			elseif ($collection_id == 210)
			{
				$document_file = EpPatentsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);	
			    $doc_type = 'EP';
			}
			elseif ($collection_id == 111)
			{
				$document_file = UsApplicationsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);	
			    $doc_type = 'USAPP';		
			}
			else 
			{
				$document_file = UsPatentsPeer::getPhpDocument($document_id);
			    $document_builder = new DocumentBuilder();
			    $obj_document = $document_builder->buildDocumentObject($document_file);
			    $doc_type = 'US';
			}


			if ($obj_document != null && $obj_document != 'file error')
			{
				$arr_obj_document[] = $obj_document;
				$arr_document_type[] = 	$doc_type;
				$doc_count++;
			}							
			
			if ($doc_count == 250)
			{
				break;
			}
				
		}
		// Create an instance
		$xls =& new Spreadsheet_Excel_Writer();
		$xls->setVersion(8);
		// Send HTTP headers to tell the browser what's coming
		$file_name = str_replace("/","_",$file_name);
		$xls->send($file_name.'.xls');
		 
		// Add a worksheet to the file, returning an object to add data to
		$sheet =& $xls->addWorksheet('Document Details');
		$sheet->setInputEncoding('utf-8');
		$format_title =& $xls->addFormat();
		$format_title->setBold();
		$format_title->setColor('yellow');
		$format_title->setPattern(1);
		$format_title->setFgColor('blue');
					
		$sheet->write($row, $column++, 'Document Number',$format_title);
		$sheet->write($row, $column++, 'Document Type',$format_title);
		$sheet->write($row, $column++, 'Publication Date',$format_title);
		$sheet->write($row, $column++, 'Title',$format_title);
		$sheet->write($row, $column++, 'Abstract',$format_title);
		$sheet->write($row, $column++, 'Inventor Name',$format_title);
		$sheet->write($row, $column++, 'Assignee',$format_title);
		$sheet->write($row, $column++, 'Application Number',$format_title);
		$sheet->write($row, $column++, 'Filing Date',$format_title);
		$sheet->write($row, $column++, 'Primary Class',$format_title);
		$sheet->write($row, $column++, 'Other Class',$format_title);
		
		$row = $row + 1;
		$column = 0;
				
		foreach ($arr_obj_document as $obj_document)
		{	
			if (in_array($arr_document_type[$row-1], array('WIPO', 'EP')))
		   	{
		   	    $kind_code = $obj_document->getKindCode();
		   	    $doc_num = $obj_document->getDocumentNum().$kind_code;
		   	}
		   	else 
		   	{
		   	    $doc_num = $obj_document->getDocumentNum();
		   	}
		   
		   	$doc_num_strip = str_replace("/","",$doc_num);
			$doc_num_link = "http://$server_name/$doc_num_strip.html";
			$pub_date = strip_tags(html_entity_decode($obj_document->getPubDate()));
			$title = strip_tags(html_entity_decode($obj_document->getTitle()));
			$abstract = strip_tags(html_entity_decode($obj_document->getAbstractData());
			$arr_inventor_name = $obj_document->getInventors();
			$inventor_names = ExportData::retriveAllNames($arr_inventor_name);
			$inventor_name = strip_tags(html_entity_decode($inventor_names));
			$arr_assignee = $obj_document->getAssignee();
          		$assignee_names = ExportData::retriveAllNames($arr_assignee);
	  		$assignee = strip_tags(html_entity_decode($assignee_names));
			$app_number = strip_tags(html_entity_decode($obj_document->getAppNumber()));
			$filing_date = strip_tags(html_entity_decode($obj_document->getFilingDate()));			
			$arr_current_class = $obj_document->getCurrentClass();
			
			if ($arr_current_class['subclass'])
			{
			    $current_class = $arr_current_class['main_class'].'/'.$arr_current_class['subclass'];
			}
			else 
			{
			    $current_class = $arr_current_class['main_class'];
			}
			
			$current_class = strip_tags(html_entity_decode($current_class));
			$other_class = strip_tags(html_entity_decode($obj_document->getOtherClass()));
			
			$sheet->writeUrl($row, $column++,$doc_num_link,$doc_num);
			$sheet->write($row, $column++, $arr_document_type[$row-1]);
			$sheet->write($row, $column++, $pub_date);
			$sheet->write($row, $column++,$title);
			$sheet->write($row, $column++, $abstract);
			$sheet->write($row, $column++, $inventor_name);
			$sheet->write($row, $column++,$assignee);
			$sheet->write($row, $column++, $app_number);
			$sheet->write($row, $column++, $filing_date);
			$sheet->write($row, $column++,$current_class);
			$sheet->write($row, $column++, $other_class);	
			$row = $row + 1;		
			$column = 0;
		}

		$xls->close();
		exit;
	}
	
	/**
	 * This function is used Join array elements with a string. Here the string is ';'.
	 *
	 * @param Array $arr_name whose elements needs to join.
	 * @return  Returns a string containing a string representation of all the array elements, with ';' between each element.
	 */
	private function retriveAllNames($arr_name)
	{
		$name_string = '';
		$name_string = implode("; ", $arr_name);
	
		return $name_string;
	}
}
?>
