Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once "vihv/interface/IControl.php";
- require_once "vihv/interface/IControlConfig.php";
- require_once "vihv/misc/TXml.php";
- require_once "vihv/event/TEventManager.php";
- require_once "vihv/config/TConfigManager.php";
- /**
- base class for all controls
- control abstracts functional part of aplication
- */
- class TControl implements IControl {
- /**
- this variable store associative array that will be used in GetXml and GetHtml
- you should shange this variable in event handlers (by modifying $Sender->Data) if you want control to display result of your calculations, or result of sql request or whatever
- */
- var $Data;
- var $Container;
- var $Config;
- var $Enabled;
- function __construct() {
- $Manager = TEventManager::getInstance();
- $Manager->AddListener($this);
- $this->SetEvent('OnGet', array($this,'OnGetEvent'));
- $this->SetEvent('OnPost', array($this,'OnPostEvent'));
- $this->SetEvent('OnDisplay', array($this,'OnDisplayEvent'));
- $this->SetEvent('OnBeforeDisplay', array($this,'OnBeforeDisplayEvent'));
- $this->SetEvent('OnEnable', array($this,'OnEnableEvent'));
- $this->SetEvent('OnDisable', array($this,'OnDisableEvent'));
- $this->SetEvent('OnCreate', array($this,'OnCreateEvent'));
- $this->SetEvent('OnRootTemplateLoad', array($this,'OnRootTemplateLoadEvent'));
- $this->SetEvent('OnDefaultPage', array($this,'OnDefaultPageEvent'));
- $this->OnCreate($this);
- }
- function Enable() {
- $this->Enabled = true;
- $this->OnEnable();
- }
- function Disable() {
- $this->Enabled = false;
- $this->OnDisable();
- }
- function IsEnabled() {
- return $this->Enabled;
- }
- function GetResourceId() {
- return get_class($this);
- }
- function GetTemplate() {
- return TConfigManager::GetTemplate(get_class($this));
- }
- function GetHtml() {
- $oldval = ini_get('track_errors');
- ini_set('track_errors', true);
- global $php_errormsg;
- $XslProcessor = new XSLTProcessor();
- $this->DOM = new DOMDocument();
- if(@$this->DOM->load(TFile::SearchIncludePath($this->GetTemplate()))===false){
- throw new ETemplateNotFound();
- }
- $this->OnRootTemplateLoad($this->DOM);
- if(@$XslProcessor->importStylesheet($this->DOM) === false) {
- throw new EXslError($php_errormsg);
- }
- $this->DOM->loadXML($this->GetXml());
- @$Doc = $XslProcessor->transformToDoc($this->DOM);
- if($Doc === false) {
- throw new EXslError($php_errormsg);
- }
- ini_set('track_errors', $oldval);
- //$Doc->formatOutput = true;
- //$Doc->preserveWhiteSpace = false;
- //$Doc->normalizeDocument();
- return str_replace("<br></br>","<br/>",$Doc->saveHTML()); // ugly DOMDocument bugfix
- }
- function GetXml() {
- try {
- $Xml = new SimpleXmlElement(TXml::MakeTree($this->GetData(), get_class($this)));
- return $Xml->asXml();
- } catch(Exception $e) {
- //print_r($this->GetData());
- }
- return "<".get_class($this)."/>";
- }
- function GetData() {
- return $this->Data;
- }
- function SetParent($Container) {
- $this->Container = $Container;
- }
- function GetParent() {
- return $this->Container;
- }
- function __call($EventName, $Args) {
- $Allowed = TEventManager::CheckPermission($this->GetResourceId(),$EventName);
- //var_dump($Allowed);
- if( !$this->Provide($EventName) ) {
- throw new EControlEventNotFound($EventName);
- }
- if($Allowed && $this->Provide($EventName)) {
- return call_user_func($this->Event[$EventName], $this, $Args[0]); // only first element we need
- }
- }
- /**
- Example $Sender->SetEvent('OnEnable', array($this, 'OnEnableEvent'));
- @param $Name name of event, event will be called like $Sender->OnEnable()
- @param $Handler array(object what contains method, method name) where "method" is a fuction what does all real work
- */
- function SetEvent($Name, $Handler) {
- $this->Event[$Name] = $Handler;
- }
- function Provide($Name) {
- if(!empty($this->Event[$Name])) {
- return true;
- }
- return false;
- }
- /**
- redirect to THE SAME page but only with GET method, this almost be the page with form you submit, should be used in OnPostEvent or Events raised by OnPostEvent
- */
- function GoBack() {
- Header("Location: ".$_SERVER['REQUEST_URI']);
- }
- /**
- this is default (does nothing) event handler, you can set it to be any function, even from another class
- @param $Input - same as global $_GET, but after passing validation, should be safe for interaction with database
- @param $Sender - as for $this wont work if handler will be defined outside control, use $Sender instead. $Sender points to the control.
- Common usage - modify $Sender->Data
- */
- function OnGetEvent($Sender,$Input) {}
- function OnPostEvent($Sender,$Input) {}
- function OnCreateEvent($Sender) {}
- function OnRootTemplateLoadEvent($Sender, $DOM) {}
- function OnDisplayEvent($Sender) {}
- function OnEnableEvent($Sender) {}
- function OnDisableEvent($Sender) {}
- function OnDefaultPageEvent($Sender) {}
- function OnBeforeDisplayEvent($Sender) {}
- }
- class ETemplateNotFound extends Exception {}
- class EXslError extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement