Edit via SFTP
  1. <?php
  2. /*::::
  3.   USAGE: use in whatever element should contain the Login Widget node but:
  4.   * ONLY USE IN ONE ELEMENT, else you will get multiple LWs, resulting
  5.   in stuff happening twice whenever it's supposed to happen once.
  6.   * DO NOT CALL EVENTS (including Render()) directly, otherwise the above will happen.
  7.   Render_LoginWidget() is an exception, because it probably needs to display
  8.   within a different element than the one where the login widget proper lives.
  9. */
  10.  
  11. interface fiEventAware {
  12. function DoEvent(int $nEvent) : void;
  13. function Render() : string;
  14. }
  15.  
  16. // event codes:
  17. define('KI_NODE_EVENT_DO_BUILDING',1); // set up node structure
  18. define('KI_NODE_EVENT_DO_FIGURING',2); // do any necessary communication between nodes
  19. // PURPOSE: So an ExecutableTwig node will pass events down to its subnodes
  20. trait ftExecutableTree {
  21. use ftExecutableTwig;
  22.  
  23. // ++ OVERRIDE ++ //
  24.  
  25. public function DoEvent(int $nEvent) : void {
  26. $this->OnEventBefore($nEvent);
  27. if ($this->HasNodes()) {
  28. $ar = $this->GetNodes();
  29. foreach ($ar as $sName => $oNode) {
  30. //echo "DOING EVENT [$nEvent] IN [$sName]<br>";
  31. $oNode->DoEvent($nEvent);
  32. }
  33. }
  34. $this->OnEventAfter($nEvent);
  35. }
  36.  
  37. // -- OVERRIDE -- //
  38. // ++ NEW ++ //
  39.  
  40. // PURPOSE: by default, Self handles event before passing it down
  41. protected function OnEventBefore(int $nEvent) : void { $this->OnEventDispatch($nEvent); }
  42. protected function OnEventAfter(int $nEvent) : void {} // stub
  43.  
  44. // -- NEW -- //
  45.  
  46. }
  47. trait ftRenderableTree {
  48. protected function RenderNodes() : string {
  49. $out = '';
  50. if ($this->HasNodes()) {
  51. $out = $this->RenderBeforeNodes();
  52. $ar = $this->GetNodes();
  53. foreach ($ar as $name => $oNode) {
  54. $out .= $oNode->Render();
  55. }
  56. $out .= $this->RenderAfterNodes();
  57. }
  58. return $out;
  59. }
  60. protected function RenderBeforeNodes() : string { return ''; }
  61. protected function RenderAfterNodes() : string { return ''; }
  62. }
  63. // PURPOSE: So a node can respond to events (does not pass them down to subnodes)
  64. trait ftExecutableTwig {
  65. public function DoEvent(int $nEvent) : void { $this->OnEventDispatch($nEvent); }
  66. protected function OnEventDispatch(int $nEvent) : void {
  67. switch ($nEvent) {
  68. case KI_NODE_EVENT_DO_BUILDING:
  69. $this->OnCreateElements();
  70. break;
  71. case KI_NODE_EVENT_DO_FIGURING:
  72. $this->OnRunCalculations();
  73. break;
  74. }
  75. }
  76. abstract protected function OnCreateElements() : void;
  77. abstract protected function OnRunCalculations() : void;
  78. }
  79.