api.zenflow.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Just dynamic content update abstraction layer
  4. */
  5. class ZenFlow {
  6. /**
  7. * Container refresh timeout in ms.
  8. *
  9. * @var int
  10. */
  11. protected $timeout = 3000;
  12. /**
  13. * Contains current zen-flow ID string
  14. *
  15. * @var string
  16. */
  17. protected $flowId = '';
  18. /**
  19. * Content string to render in container area
  20. *
  21. * @var string
  22. */
  23. protected $content = '';
  24. /**
  25. * Contains some predefined routes
  26. */
  27. const ROUTE_ZENFLOW = 'zenflow';
  28. /**
  29. * Creates new Zen-flow instance
  30. *
  31. * @param string $flowId unique identifier of zen-flow instance
  32. * @param string $content some string or function which will be updated into container
  33. * @param int $timeout timeout in ms.
  34. */
  35. public function __construct($flowId, $content = '', $timeout = '') {
  36. $this->setFlowId($flowId); //set flow ID
  37. $this->setContent($content);
  38. if (!empty($timeout)) {
  39. $this->setTimeout($timeout);
  40. }
  41. $this->listener();
  42. }
  43. /**
  44. * Sets current instance flow ID
  45. *
  46. * @param string $flowId
  47. *
  48. * @return void
  49. */
  50. protected function setFlowId($flowId) {
  51. if (!empty($flowId)) {
  52. $this->flowId = 'zen' . $flowId;
  53. } else {
  54. throw new Exception('EX_EMPTY_FLOWID');
  55. }
  56. }
  57. /**
  58. * Sets instance refresh rate in ms.
  59. *
  60. * @param int $timeout
  61. *
  62. * @return void
  63. */
  64. protected function setTimeout($timeout) {
  65. $this->timeout = $timeout;
  66. }
  67. /**
  68. * Puts content data from constructor into protected property.
  69. *
  70. * @param string $content
  71. *
  72. * @return void
  73. */
  74. protected function setContent($content = '') {
  75. $this->content = $content;
  76. }
  77. /**
  78. * Renders initial zen-container with some prefilled content.
  79. *
  80. * @return string
  81. */
  82. public function render() {
  83. $result = '';
  84. if (!empty($this->flowId)) {
  85. $container = 'zencontainer_' . $this->flowId;
  86. $requestUrl = $_SERVER['REQUEST_URI'];
  87. if (!empty($requestUrl)) {
  88. $result .= wf_AjaxContainer($container, '', $this->content);
  89. $dataUrl = $requestUrl;
  90. if (!ubRouting::checkGet(self::ROUTE_ZENFLOW)) {
  91. $dataUrl .= '&' . self::ROUTE_ZENFLOW . '=' . $this->flowId;
  92. }
  93. $result .= wf_tag('script');
  94. $result .= '$(document).ready(function() {
  95. var prevData= "";
  96. setInterval(function(){
  97. $.get("' . $dataUrl . '", function(data) {
  98. //update zen-container only if data is changed
  99. if (prevData!=data) {
  100. $("#' . $container . '").html(data);
  101. prevData=data;
  102. }
  103. });
  104. }, ' . $this->timeout . ');
  105. });
  106. ';
  107. $result .= wf_tag('script', true);
  108. }
  109. }
  110. return($result);
  111. }
  112. /**
  113. * Listens for some flow callback, checks is this current instance flow and renders content.
  114. *
  115. * @return bool
  116. */
  117. protected function listener() {
  118. $result = false;
  119. if (ubRouting::checkGet(self::ROUTE_ZENFLOW)) {
  120. $requestFlow = ubRouting::get(self::ROUTE_ZENFLOW);
  121. //its my flow?
  122. if ($requestFlow == $this->flowId) {
  123. print($this->content);
  124. die();
  125. }
  126. }
  127. /**
  128. * If you heard the loud big bang
  129. * You can see it with your eyes
  130. * The eternal night breaks when
  131. * The mushroom grows into the sky
  132. */
  133. return($result);
  134. }
  135. }