Location: PHPKode > scripts > phpchartPlus > phpchartplus/Sources/TXTData_Strategy.php
<?php

/*
**************************************************
Class: TXTData_Strategy.php
**************************************************
Author: Tsiavos Chris <hide@address.com>
Date: October 2004
**************************************************/

/**
*Includes the DataStrategy Interface
*/
require_once("DataStrategy_Interface.php");

/**
*Reads chart data from text files
*This class should be used when you want to import chart data from text files. The indicated file should 
*have the following structure (per line) for parsing to be successfull: group=$group_name//$Item,$Value:$Item,$Value
*@author Tsiavos Chris <hide@address.com>
*@license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class TXTData_Strategy implements DataStrategy_Interface {
	
	/**
	*Holds the file handler
	*@access private
	*/
	private $File_Handler;
	
	/**
	*Initializes the strategy object
	*@access public
	*@params string[assoc] $params["filename"] 
	*@throws DataStrategy_Exception
	*/
	public function initialize($params=NULL) 
	{
		$filename=$params["filename"];
		
		if (!file_exists($filename))
			throw new DataStrategy_Exception("TXTData_Strategy: File $filename not found");
			
		$this->File_Handler=@fopen($filename,"r");
		
	}
	
	/**
	*Performs the required action for parsing the data from the text file
	*@access public
	*@return Group[] Common communication structure between DataStrategy objects and DataParser
	*@param NULL $params
	*/
	public function perform($params=NULL) 
	{
		$line=NULL;
		$line_num=0;
		$Groups=array();
		
		while(!feof($this->File_Handler)) {
			
			$line=fgets($this->File_Handler,255);
			
			if (strlen(trim($line))==0) continue;
			
			$line_data=split("=",trim($line));
			$line_data_contents=split("//",$line_data[1]);
			
			$group_name=$line_data_contents[0];
			
			$Groups[$line_num]=new Group();
			$Groups[$line_num]->GroupName=$group_name;
				
			$Items=split(":",$line_data_contents[1]);
		
			for ($i=0;$i<count($Items);$i++) {
				$Item=split(",",$Items[$i]);
				
				$Groups[$line_num]->GroupItems[$i]=new GroupItem();
				
				$Groups[$line_num]->GroupItems[$i]->ItemName=$Item[0];
				$Groups[$line_num]->GroupItems[$i]->ItemValue=$Item[1];
			}
			
		$line_num++;		
			
		}
	
	return $Groups;		
	
	}
	
	/**
	*Finalizes the DataStrategy object.
	*@access public
	*@return void
	*/
	public function finalize() 
	{
		fclose($this->File_Handler);
	}
	
}

?>
Return current item: phpchartPlus