<?php
/**
 *TextEditor
 *
 * @date 21.03.2007
 * @name View
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2006 Margo Poolak. All Rights Reserved.
 * @version 2.0
 * @package System.Web.UI.WebControls
 * @category 
 * 
 * This is UI view 
 */
class TextEditor
{
	protected $Page;
	protected $text;
	protected $file;
	/*
	*
	*/
	function __construct(TPage $parent)
	{
		$this->Page = $parent;
	}
	/*
	* Opens the file
	* @param $file destination
	* @returns null
	*/
	public function open($file)
	{		
		if($this->Page->Request->isPostBack()==true)
		{
			$this->text = stripslashes(html_entity_decode(stripslashes($this->Page->Request->getPOST('source',null))));
			$this->save($file);
		}
		elseif(is_file($file))
		{
			$this->file = $file;
			$this->text = htmlspecialchars(get_file_contents($this->file));
		}else
			exit("This is not file");
	}
	/*
	* Saves the source into file
	*/
	public function save($file)
	{
		set_file_contents($file,$this->text);	
	}
	/*
	* Render source and return the HTML
	*/
	public function render()
	{
		return '<form method="post" action=""><input type="submit" value="Save"><br/><textarea rows="30" cols="80" name="source">'.$this->text.'</textarea></form>';
	}
}
?>