| 1: | <?php |
| 2: | |
| 3: | namespace Zippy\Html; |
| 4: | |
| 5: | use Zippy\WebApplication; |
| 6: | use Zippy\Interfaces\EventReceiver; |
| 7: | use Zippy\Interfaces\AjaxRender; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | abstract class WebPage extends HtmlContainer implements EventReceiver |
| 15: | { |
| 16: | public $args = ''; |
| 17: | public $_title = ''; |
| 18: | public $_description = ''; |
| 19: | public $_keywords = ''; |
| 20: | private $beforeRequestEvents = array(); |
| 21: | private $afterRequestEvents = array(); |
| 22: | private $_ajax; |
| 23: | private $_ankor = ''; |
| 24: | public $_tvars = array(); |
| 25: | protected $_ajaxblocks = []; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public function __construct() { |
| 34: | |
| 35: | |
| 36: | } |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function RequestHandle() { |
| 45: | $this->_ajaxblock = []; |
| 46: | $this->beforeRequest(); |
| 47: | |
| 48: | parent::RequestHandle(); |
| 49: | |
| 50: | $this->afterRequest(); |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | final public function getURLNode() { |
| 57: | return WebApplication::$app->getResponse()->getBaseUrl(); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function beforeSaveToSession() { |
| 64: | |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function afterRestoreFromSession() { |
| 71: | |
| 72: | } |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | protected function updateAjax($components, $js = null) { |
| 84: | $this->addAjaxResponse($js) ; |
| 85: | return; |
| 86: | if (!is_array($components)) { |
| 87: | $components = array($components); |
| 88: | } |
| 89: | if (is_array($components)) { |
| 90: | foreach ($components as $item) { |
| 91: | $this->_ajax[] = $item; |
| 92: | } |
| 93: | } |
| 94: | if (strlen($js) > 0) { |
| 95: | WebApplication::$app->getResponse()->addAjaxResponse($js); |
| 96: | } |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | public function Render() { |
| 137: | if (strlen($this->_ankor) > 0) { |
| 138: | WebApplication::$app->getResponse()->addJavaScript("window.location='#" . $this->_ankor . "'", true); |
| 139: | $this->_ankor = ''; |
| 140: | } |
| 141: | $this->beforeRender(); |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | $this->RenderImpl(); |
| 148: | $this->afterRender(); |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | $_baseurl = $this->getURLNode() ; |
| 154: | $this->_tvars['_baseurl'] = $_baseurl ; |
| 155: | WebApplication::$app->addJavaScript(" window._baseurl= '{$_baseurl}'") ; |
| 156: | |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | public function addBeforeRequestEvent($obj, $func) { |
| 181: | $this->beforeRequestEvents[] = new \Zippy\Event($obj, $func); |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | public function addAfterRequestEvent($obj, $func) { |
| 190: | $this->afterRequestEvents[] = new \Zippy\Event($obj, $func); |
| 191: | } |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | public function beforeRequest() { |
| 198: | $this->_ajax = array(); |
| 199: | |
| 200: | if (count($this->beforeRequestEvents) > 0) { |
| 201: | foreach ($this->beforeRequestEvents as $event) { |
| 202: | $event->onEvent($this); |
| 203: | } |
| 204: | } |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | public function afterRequest() { |
| 212: | |
| 213: | if (count($this->afterRequestEvents) > 0) { |
| 214: | foreach ($this->afterRequestEvents as $event) { |
| 215: | $event->OnEvent($this); |
| 216: | } |
| 217: | } |
| 218: | } |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | protected function goAnkor($name) { |
| 226: | $this->_ankor = $name; |
| 227: | } |
| 228: | |
| 229: | public function setTitle($title) { |
| 230: | $this->_title = $title; |
| 231: | } |
| 232: | |
| 233: | public function setDescription($description) { |
| 234: | $this->_description = $description; |
| 235: | } |
| 236: | |
| 237: | public function setKeywords($keywords) { |
| 238: | $this->_keywords = $keywords; |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | public function OnBackgroundUpdate($sender) { |
| 247: | |
| 248: | } |
| 249: | |
| 250: | |
| 251: | final public function RequestMethod($method) { |
| 252: | $p = WebApplication::$app->getRequest()->request_params[$method]; |
| 253: | $post=null; |
| 254: | if($_SERVER["REQUEST_METHOD"]=='POST') { |
| 255: | |
| 256: | if(count($_POST)>0) { |
| 257: | $post = $_POST; |
| 258: | } else { |
| 259: | $post = file_get_contents('php://input'); |
| 260: | } |
| 261: | } |
| 262: | $answer = $this->{$method}($p, $post); |
| 263: | if(strlen($answer)) { |
| 264: | WebApplication::$app->getResponse()->addAjaxResponse($answer) ; |
| 265: | } |
| 266: | |
| 267: | } |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | protected function addAjaxResponse($js) { |
| 275: | if (strlen($js) > 0) { |
| 276: | WebApplication::$app->getResponse()->addAjaxResponse($js); |
| 277: | } |
| 278: | } |
| 279: | |
| 280: | |
| 281: | final public function updateAjaXBlocks($a) { |
| 282: | $this->_ajaxblock = $a; |
| 283: | |
| 284: | } |
| 285: | |
| 286: | final public function updateAjaxHTML() { |
| 287: | if($this->hasAB()) { |
| 288: | |
| 289: | foreach($this->_ajaxblock as $id) { |
| 290: | $c = $this->getComponent($id,true) ; |
| 291: | if($c ==null){ |
| 292: | continue; |
| 293: | } |
| 294: | $html = $c->getHTML() ; |
| 295: | if($html == null){ |
| 296: | continue; |
| 297: | } |
| 298: | $html = str_replace("'","`",$html) ; |
| 299: | |
| 300: | $b = json_encode($html, JSON_UNESCAPED_UNICODE); |
| 301: | |
| 302: | |
| 303: | $js .= "$('#{$id}').replaceWith({$b})"; |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | $this->addAjaxResponse($js) ; |
| 309: | |
| 310: | } |
| 311: | |
| 312: | } |
| 313: | } |
| 314: | |
| 315: | final public function hasAB() { |
| 316: | return count($this->_ajaxblock); |
| 317: | } |
| 318: | |
| 319: | } |
| 320: | |