Login   Register  
PHP Classes
elePHPant
Icontem

File: clsDataProducer.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of chris  >  DataProducer  >  clsDataProducer.php  >  Download  
File: clsDataProducer.php
Role: ???
Content type: text/plain
Description: The source
Class: DataProducer
Author: By
Last change:
Date: 12 years ago
Size: 2,925 bytes
 

Contents

Class file image Download
<?
/*
************************************************************************
*  Sloppycode.net All rights reserved.
*
* This is a standard copyright header for all source code appearing
* at sloppycode.net. This application/class/script may be redistributed,
* as long as the above copyright remains intact. 
* Comments to sloppycode@sloppycode.net
************************************************************************
*/

/**
 * @title DataProducer class - data tag replacement class
 * @author C.Small
 * @version 2.0 - Overhaul of original system
 * @version 1.0 - Original idea from Delphi Data Producers.
 */
class DataProducer
{
	
		function doDataProducer($startTag,$endTag,$data,$contents)
		{
			return $this->privateDataProducer($startTag,$endTag,$data,$contents);
		}
		function doSingleDataProducer($data,$contents)
		{
			return $this->privateSingleDataProducer($data,$contents);
		}
		function openTemplate($filename)
		{
			return $this->privateopenTemplate($filename);
		}

		function privateDataProducer($startTag,$endTag,$data,$contents)
		{
				// Get start and end points
				$start = strpos($contents,$startTag);
				$end   = strpos($contents,$endTag,$startTag);
				
				// Retrieve everything before start tag
				$prefix = substr($contents,0,$start);
				$prefix = rtrim($prefix);
				
				// Retrieve everything after end tag. Make it starts at the end of end-tag
				$suffix = substr($contents,$end + strlen($endTag),strlen($contents) - ($end + strlen($endTag)));
				$suffix = ltrim($suffix);
				
				// Retrieve data template. make sure it starts at the end of the start-tag.
				$dataTemplate = substr($contents,$start + strlen($startTag),$end - ($start + strlen($startTag)));
				
				// New method implemented here
				for ($i=0; $i <= sizeof($data) -1;$i++)
				{
					$tempReplace = $dataTemplate;
					$tempReplace = rtrim($tempReplace);
					
					$keys = array_keys($data[$i]);
					foreach($keys as $keyname)
					{
						if (!empty($data[$i][$keyname]))
						{
							$tempReplace = str_replace("<".$keyname.">",$data[$i][$keyname],$tempReplace);
						} else{
							$tempReplace = str_replace("<".$keyname.">","",$tempReplace);
						}
					}
					$build .= $tempReplace;
				}
				
				return $prefix . $build . $suffix;
		}
		/**
		 *
		 */
		function privateSingleDataProducer($data,$contents)
		{
			$result = $contents;
			foreach ($data as $tagname => $value){
				$result = str_replace("<".$tagname.">",$value,$result);
			}
			return $result;
		}
		/**
		 *
		 */
		function privateOpenTemplate($filename)
		{		
				$fHnd = @fopen($filename,"r") or die("<br><b>Unable to open template: ".$filename."</b>");
				$contents = @fread($fHnd,filesize($filename)) or die("<br><b>Unable to open template: ".$filename."</b>");
				fclose($fHnd);
				
				return $contents;
		}
}
?>