Advertisement
rhandom

TControl.php

Jun 27th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.01 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "vihv/interface/IControl.php";
  4. require_once "vihv/interface/IControlConfig.php";
  5. require_once "vihv/misc/TXml.php";
  6. require_once "vihv/event/TEventManager.php";
  7. require_once "vihv/config/TConfigManager.php";
  8. /**
  9. base class for all controls
  10. control abstracts functional part of aplication
  11. */
  12. class TControl implements IControl {
  13.  
  14.     /**
  15.     this variable store associative array that will be used in GetXml and GetHtml
  16.     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
  17.     */
  18.     var $Data;
  19.  
  20.     var $Container;
  21.     var $Config;
  22.     var $Enabled;
  23.  
  24.     function __construct() {
  25.         $Manager = TEventManager::getInstance();
  26.         $Manager->AddListener($this);
  27.         $this->SetEvent('OnGet', array($this,'OnGetEvent'));
  28.         $this->SetEvent('OnPost', array($this,'OnPostEvent'));
  29.         $this->SetEvent('OnDisplay', array($this,'OnDisplayEvent'));
  30.         $this->SetEvent('OnBeforeDisplay', array($this,'OnBeforeDisplayEvent'));
  31.         $this->SetEvent('OnEnable', array($this,'OnEnableEvent'));
  32.         $this->SetEvent('OnDisable', array($this,'OnDisableEvent'));
  33.         $this->SetEvent('OnCreate', array($this,'OnCreateEvent'));
  34.         $this->SetEvent('OnRootTemplateLoad', array($this,'OnRootTemplateLoadEvent'));
  35.         $this->SetEvent('OnDefaultPage', array($this,'OnDefaultPageEvent'));
  36.         $this->OnCreate($this);
  37.         }
  38.  
  39.     function Enable() {
  40.         $this->Enabled = true;
  41.         $this->OnEnable();
  42.         }
  43.  
  44.     function Disable() {
  45.         $this->Enabled = false;
  46.         $this->OnDisable();
  47.         }
  48.  
  49.     function IsEnabled() {
  50.         return $this->Enabled;
  51.         }
  52.  
  53.     function GetResourceId() {
  54.         return get_class($this);
  55.         }
  56.  
  57.     function GetTemplate() {
  58.         return TConfigManager::GetTemplate(get_class($this));
  59.         }
  60.  
  61.     function GetHtml() {
  62.         $oldval = ini_get('track_errors');
  63.         ini_set('track_errors', true);
  64.         global $php_errormsg;
  65.        
  66.        
  67.         $XslProcessor = new XSLTProcessor();
  68.         $this->DOM = new DOMDocument();
  69.         if(@$this->DOM->load(TFile::SearchIncludePath($this->GetTemplate()))===false){
  70.             throw new ETemplateNotFound();
  71.         }
  72.        
  73.         $this->OnRootTemplateLoad($this->DOM);
  74.         if(@$XslProcessor->importStylesheet($this->DOM) === false) {
  75.             throw new EXslError($php_errormsg);
  76.         }
  77.         $this->DOM->loadXML($this->GetXml());
  78.         @$Doc = $XslProcessor->transformToDoc($this->DOM);
  79.         if($Doc === false) {
  80.             throw new EXslError($php_errormsg);
  81.         }
  82.         ini_set('track_errors', $oldval);
  83.         //$Doc->formatOutput = true;
  84.         //$Doc->preserveWhiteSpace = false;
  85.         //$Doc->normalizeDocument();
  86.         return  str_replace("<br></br>","<br/>",$Doc->saveHTML()); // ugly DOMDocument bugfix
  87.         }
  88.  
  89.     function GetXml() {
  90.             try {
  91.                 $Xml = new SimpleXmlElement(TXml::MakeTree($this->GetData(), get_class($this)));
  92.                 return $Xml->asXml();
  93.             } catch(Exception $e) {
  94.                 //print_r($this->GetData());
  95.             }
  96.         return "<".get_class($this)."/>";
  97.         }
  98.  
  99.     function GetData() {
  100.         return $this->Data;
  101.         }
  102.  
  103.     function SetParent($Container) {
  104.         $this->Container = $Container;
  105.         }
  106.  
  107.     function GetParent() {
  108.         return $this->Container;
  109.         }
  110.  
  111.     function __call($EventName, $Args) {
  112.        
  113.         $Allowed = TEventManager::CheckPermission($this->GetResourceId(),$EventName);
  114.         //var_dump($Allowed);
  115.         if( !$this->Provide($EventName) ) {
  116.             throw new EControlEventNotFound($EventName);
  117.             }
  118.         if($Allowed && $this->Provide($EventName)) {
  119.             return call_user_func($this->Event[$EventName], $this, $Args[0]); // only first element we need
  120.             }
  121.         }
  122.  
  123.     /**
  124.     Example $Sender->SetEvent('OnEnable', array($this, 'OnEnableEvent'));
  125.     @param $Name  name of event, event will be called like $Sender->OnEnable()
  126.     @param $Handler array(object what contains method, method name) where "method" is a fuction what does all real work
  127.     */
  128.     function SetEvent($Name, $Handler) {
  129.         $this->Event[$Name] = $Handler;
  130.         }
  131.  
  132.     function Provide($Name) {
  133.         if(!empty($this->Event[$Name])) {
  134.             return true;
  135.             }
  136.         return false;
  137.         }
  138.  
  139.     /**
  140.     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
  141.     */
  142.     function GoBack() {
  143.         Header("Location: ".$_SERVER['REQUEST_URI']);
  144.         }
  145.  
  146.     /**
  147.     this is default (does nothing) event handler, you can set it to be any function, even from another class
  148.     @param $Input  - same as global $_GET, but after passing validation, should be safe for interaction with database
  149.     @param $Sender - as for $this wont work if handler will be defined outside control, use $Sender instead. $Sender points to the control.
  150.     Common usage - modify $Sender->Data
  151.     */
  152.     function OnGetEvent($Sender,$Input) {}
  153.     function OnPostEvent($Sender,$Input) {}
  154.     function OnCreateEvent($Sender) {}
  155.     function OnRootTemplateLoadEvent($Sender, $DOM) {}
  156.     function OnDisplayEvent($Sender) {}
  157.     function OnEnableEvent($Sender) {}
  158.     function OnDisableEvent($Sender) {}
  159.     function OnDefaultPageEvent($Sender) {}
  160.     function OnBeforeDisplayEvent($Sender) {}
  161.     }
  162.    
  163. class ETemplateNotFound extends Exception {}
  164. class EXslError extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement