Location: PHPKode > scripts > NiftyCube > niftycube/niftycubed.class.php
<?php

/*
The NiftyCube class was made by Jon Gjengset (hide@address.com), but all credit goes to Alessandro Fulciniti
and his amazing Nifty Corners Cube, without which this would not have been possible!

To get the original script, please visit the NCC homepage @
http://www.html.it/articoli/niftycube/index.html

The methods of this class are as follows (* are obligatory, ! should not be run by user)
* configureBorder	(  width_of_border_with_unit		,border_color_code,	size_of_corners(big | normal | small)  )	//Configures the border basically
* addToBody		(  content_to_be_added_to_body  )												//Has to be run at least once, adds content to the page
* setPage			(  width_of_the_page,			[, spacing_from_top	[, padding_in_content_box]]  )			//Sets page measures
setColors			(  outside_color_code			,inside_color_code,	[, border_color_code]]  )			//Sets various colors on the page
setTitle			(  page_title  )															//Sets the page title (<title> tag)
addToHead			(  lines_of_code  )														//Adds something inside the <head> tag
addToFooter		(  lines_of_code  )														//Adds content underneath the page itself
addAbove			(  lines_of_code  )														//Adds content above the page itself
setTransparency		(  true(on)_or_false(off)  )													//Turns on and off transparency for the corners of the Cube (Handy for background images etc.)
show				(  return_true_false  )														//Prints or returns the complete HTML
obShow			(  content  )																//Made for use with ob_start, see ob_example file!
!draw				(    )																	//Draws all elements (sets up the styles)
!makePage			(    )																	//Sets up all HTML code

*/

class NiftyCube {
	private $borderWidth	= '1px';
	private $borderColor	= '336699';
	private $outsideColor	= 'FFFFFF';
	private $insideColor	= 'FFFFFF';
	private $cornerSize		= 'normal';
	private $transparent	= TRUE;
	private $head			= '';
	private $body			= '';
	private $footer			= '';
	private $header			= '';
	private $finalOutput	= '';
	private $pageWidth		= '';
	private $fromTop		= '0';
	private $insidePadding	= '10px 10px 10px 10px';
	private $step			= 0;
	
	function __construct()
	{
		$this->head =  '<script type="text/javascript" src="niftycube.js"></script>';
		$this->head .= "\n";
	}
	
	function configureBorder($width, $color, $corners)
	{
		if (!is_numeric($width))
		{
			trigger_error("The width specified was not a valid number! Trying to remove unit", E_USER_WARNING);
			$width = preg_replace("/^(\d+).*$/", "$1", $width);
			if (!is_numeric($width))
			{
				trigger_error("Width parsing failed! Please use a plain number!", E_USER_ERROR);
			}
		}
		
		if ($corners != 'big' && $corners != 'small' && $corners != 'normal')
		{
			trigger_error("Unknown corner size specified (big normal small), using normal", E_USER_NOTICE);
		}
		
		$this->borderWidth = $width;
		$this->borderColor = $color;
		$this->cornerSize  = $corners;	
		$this->step++;
	}
	
	function setColors($outside, $inside, $border = FALSE)
	{
		$this->outsideColor = $outside;
		$this->insideColor  = $inside;
		if ($border)
		{
			$this->borderColor = $border;
		}
	}
	
	function setTitle($title)
	{
		$this->head .= '<title>'.$title.'</title>'."\n";
	}
	
	function addToHead($lines)
	{
		$this->head .= $lines;
	}
	
	function addToBody($content)
	{
		$this->body .= $content;
		$this->step++;
	}
	
	function addToFooter($lines)
	{
		$this->footer .= $lines;
	}
	
	function addAbove($lines)
	{
		$this->header .= $lines;
	}
	
	function setTransparency($onoff)
	{
		if (!is_bool($onoff))
		{
			trigger_error("Transparency can only be on or off (True or False)!", E_USER_ERROR);
		}
		$this->transparent = $onoff;
	}
	
	function setPage($pageWidth, $fromTop = '0', $insidePadding = '10px 10px 10px 10px')
	{
		$this->pageWidth		= $pageWidth;
		$this->fromTop			= $fromTop;
		$this->insidePadding	= $insidePadding;
		$this->step++;
	}
	
	function draw()
	{
		if ($this->step < 3)
		{
			trigger_error("Not all required operations were run! (configureBorder addToBody setPage)", E_USER_ERROR);
		}
		
		$this->head .= "\n";
		$this->head .= '<style type="text/css">';
		$this->head .= "
BODY		{
	margin:			0 0 0 0;
	padding:		0 0 0 0;
	text-align:		center;
	background:		{$this->outsideColor};
}

#cover		{
	position:		absolute;
	width:			{$this->pageWidth};
";
		if (strstr($this->pageWidth, '%'))
		{
			$sides = (int)((100-rtrim($this->pageWidth,'%'))/2);
			$this->head .= "\tleft:			$sides%;\n";
			$this->head .= "\tright:			$sides%;\n";
			$this->head .= "\tmargin:			{$this->fromTop} 0 0 0;";
		}
		else
		{
			$this->head .= "\tmargin:		{$this->fromTop} auto 0 auto;";
		}
		$this->head .= "
	padding:		0 0 0 0;
}

#bord		{
	margin:			0 0 0 0;
	padding:		{$this->borderWidth}px;
	background:		#{$this->borderColor};
}

#inside		{
	position:		relative;
	margin:			0 0 0 0;
	padding:		{$this->insidePadding};
	background:		#{$this->insideColor};
	text-align:		left;
}\n";
		$this->head .= '</style>';
		$this->head .= "\n";

		$this->makePage();
	}
	
	function makePage()
	{
		$this->finalOutput  = "<html>\n";
		$this->finalOutput .= "<head>\n";
		$this->finalOutput .= $this->head;
		$this->finalOutput .= "</head>\n";
		$this->finalOutput .= "<body>\n";
		$this->finalOutput .= '<script type="text/javascript">window.onload=function(){';
		$this->finalOutput .= 'Nifty("div#bord", "'.$this->cornerSize.($this->transparent ? ' transparent' : '').'");';
		$this->finalOutput .= 'Nifty("div#inside", "'.$this->cornerSize.' transparent");';
		$this->finalOutput .= "}</script>\n";
		$this->finalOutput .= '<div id="cover">'."\n";
		$this->finalOutput .= $this->header;
		$this->finalOutput .= '<div id="bord">'."\n";
		$this->finalOutput .= '<div id="inside">'."\n";
		$this->finalOutput .= $this->body;
		$this->finalOutput .= "\n".'</div>'."\n";
		$this->finalOutput .= "\n".'</div>'."\n";
		$this->finalOutput .= $this->footer;
		$this->finalOutput .= "\n".'</div>'."\n";
		$this->finalOutput .= "</body>\n";
		$this->finalOutput .= "</html>\n";
	}
	
	function show($return = FALSE)
	{
		$this->draw();
		if ($return)
		{
			return $this->finalOutput;
		}
		echo $this->finalOutput;
	}
	
	function obShow($all)
	{
		$this->addToBody($all);
		$this->draw();
		return $this->show(TRUE);
	}
};

?>
Return current item: NiftyCube