ajaxwebpage.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Simple web page with no includes, header or fancy formatting, useful to
  18. * generate HTML fragments when called by an AJAX method
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. require_once('../approot.inc.php');
  26. require_once(APPROOT."/application/webpage.class.inc.php");
  27. class ajax_page extends WebPage
  28. {
  29. /**
  30. * Jquery style ready script
  31. * @var Hash
  32. */
  33. protected $m_sReadyScript;
  34. protected $m_sCurrentTab;
  35. protected $m_sCurrentTabContainer;
  36. protected $m_aTabs;
  37. /**
  38. * constructor for the web page
  39. * @param string $s_title Not used
  40. */
  41. function __construct($s_title)
  42. {
  43. parent::__construct($s_title);
  44. $this->m_sReadyScript = "";
  45. $this->add_header("Content-type: text/html; charset=utf-8");
  46. $this->add_header("Cache-control: no-cache");
  47. $this->m_sCurrentTabContainer = '';
  48. $this->m_sCurrentTab = '';
  49. $this->m_aTabs = array();
  50. }
  51. public function AddTabContainer($sTabContainer, $sPrefix = '')
  52. {
  53. $this->m_aTabs[$sTabContainer] = array('content' =>'', 'prefix' => $sPrefix);
  54. $this->add("\$Tabs:$sTabContainer\$");
  55. }
  56. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  57. {
  58. if (!isset($this->m_aTabs[$sTabContainer]['content'][$sTabLabel]))
  59. {
  60. // Set the content of the tab
  61. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] = $sHtml;
  62. }
  63. else
  64. {
  65. // Append to the content of the tab
  66. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] .= $sHtml;
  67. }
  68. }
  69. public function SetCurrentTabContainer($sTabContainer = '')
  70. {
  71. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  72. $this->m_sCurrentTabContainer = $sTabContainer;
  73. return $sPreviousTabContainer;
  74. }
  75. public function SetCurrentTab($sTabLabel = '')
  76. {
  77. $sPreviousTab = $this->m_sCurrentTab;
  78. $this->m_sCurrentTab = $sTabLabel;
  79. return $sPreviousTab;
  80. }
  81. /**
  82. * Echoes the content of the whole page
  83. * @return void
  84. */
  85. public function output()
  86. {
  87. foreach($this->a_headers as $s_header)
  88. {
  89. header($s_header);
  90. }
  91. if (count($this->m_aTabs) > 0)
  92. {
  93. $this->add_ready_script(
  94. <<<EOF
  95. // The "tab widgets" to handle.
  96. var tabs = $('div[id^=tabbedContent]');
  97. // This selector will be reused when selecting actual tab widget A elements.
  98. var tab_a_selector = 'ul.ui-tabs-nav a';
  99. // Enable tabs on all tab widgets. The `event` property must be overridden so
  100. // that the tabs aren't changed on click, and any custom event name can be
  101. // specified. Note that if you define a callback for the 'select' event, it
  102. // will be executed for the selected tab whenever the hash changes.
  103. tabs.tabs({ event: 'change' });
  104. // Define our own click handler for the tabs, overriding the default.
  105. tabs.find( tab_a_selector ).click(function()
  106. {
  107. var state = {};
  108. // Get the id of this tab widget.
  109. var id = $(this).closest( 'div[id^=tabbedContent]' ).attr( 'id' );
  110. // Get the index of this tab.
  111. var idx = $(this).parent().prevAll().length;
  112. // Set the state!
  113. state[ id ] = idx;
  114. $.bbq.pushState( state );
  115. });
  116. EOF
  117. );
  118. }
  119. // Render the tabs in the page (if any)
  120. foreach($this->m_aTabs as $sTabContainerName => $aTabContainer)
  121. {
  122. $sTabs = '';
  123. $m_aTabs = $aTabContainer['content'];
  124. $sPrefix = $aTabContainer['prefix'];
  125. $container_index = 0;
  126. if (count($m_aTabs) > 0)
  127. {
  128. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  129. $sTabs .= "<ul>\n";
  130. // Display the unordered list that will be rendered as the tabs
  131. $i = 0;
  132. foreach($m_aTabs as $sTabName => $sTabContent)
  133. {
  134. $sTabs .= "<li><a href=\"#tab_{$sPrefix}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  135. $i++;
  136. }
  137. $sTabs .= "</ul>\n";
  138. // Now add the content of the tabs themselves
  139. $i = 0;
  140. foreach($m_aTabs as $sTabName => $sTabContent)
  141. {
  142. $sTabs .= "<div id=\"tab_{$sPrefix}$i\">".$sTabContent."</div>\n";
  143. $i++;
  144. }
  145. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  146. }
  147. $this->s_content = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $this->s_content);
  148. $container_index++;
  149. }
  150. $s_captured_output = ob_get_contents();
  151. ob_end_clean();
  152. echo $this->s_content;
  153. echo $this->s_deferred_content;
  154. if (count($this->a_scripts) > 0)
  155. {
  156. echo "<script type=\"text/javascript\">\n";
  157. echo implode("\n", $this->a_scripts);
  158. echo "\n</script>\n";
  159. }
  160. if (!empty($this->m_sReadyScript))
  161. {
  162. echo "<script type=\"text/javascript\">\n";
  163. echo $this->m_sReadyScript; // Ready Scripts are output as simple scripts
  164. echo "\n</script>\n";
  165. }
  166. if (trim($s_captured_output) != "")
  167. {
  168. echo $s_captured_output;
  169. }
  170. }
  171. /**
  172. * Adds a paragraph with a smaller font into the page
  173. * NOT implemented (i.e does nothing)
  174. * @param string $sText Content of the (small) paragraph
  175. * @return void
  176. */
  177. public function small_p($sText)
  178. {
  179. }
  180. public function add($sHtml)
  181. {
  182. if (!empty($this->m_sCurrentTabContainer) && !empty($this->m_sCurrentTab))
  183. {
  184. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  185. }
  186. else
  187. {
  188. parent::add($sHtml);
  189. }
  190. }
  191. /**
  192. * Adds a script to be executed when the DOM is ready (typical JQuery use)
  193. * NOT implemented in this version of the class.
  194. * @return void
  195. */
  196. public function add_ready_script($sScript)
  197. {
  198. // Does nothing in ajax rendered content.. for now...
  199. // Maybe we should add this as a simple <script> tag at the end of the output
  200. // considering that at this time everything in the page is "ready"...
  201. $this->m_sReadyScript .= $sScript;
  202. }
  203. /**
  204. * Cannot be called in this context, since Ajax pages do not share
  205. * any context with the calling page !!
  206. */
  207. public function GetUniqueId()
  208. {
  209. assert(false);
  210. return 0;
  211. }
  212. }
  213. ?>