ajaxwebpage.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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."/application/webpage.class.inc.php");
  26. class ajax_page extends WebPage
  27. {
  28. /**
  29. * Jquery style ready script
  30. * @var Hash
  31. */
  32. protected $m_sReadyScript;
  33. protected $m_sCurrentTab;
  34. protected $m_sCurrentTabContainer;
  35. protected $m_aTabs;
  36. /**
  37. * constructor for the web page
  38. * @param string $s_title Not used
  39. */
  40. function __construct($s_title)
  41. {
  42. parent::__construct($s_title);
  43. $this->m_sReadyScript = "";
  44. $this->add_header("Content-type: text/html; charset=utf-8");
  45. $this->add_header("Cache-control: no-cache");
  46. $this->m_sCurrentTabContainer = '';
  47. $this->m_sCurrentTab = '';
  48. $this->m_aTabs = array();
  49. }
  50. public function AddTabContainer($sTabContainer, $sPrefix = '')
  51. {
  52. $this->m_aTabs[$sTabContainer] = array('content' =>'', 'prefix' => $sPrefix);
  53. $this->add("\$Tabs:$sTabContainer\$");
  54. }
  55. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  56. {
  57. if (!isset($this->m_aTabs[$sTabContainer]['content'][$sTabLabel]))
  58. {
  59. // Set the content of the tab
  60. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] = $sHtml;
  61. }
  62. else
  63. {
  64. // Append to the content of the tab
  65. $this->m_aTabs[$sTabContainer]['content'][$sTabLabel] .= $sHtml;
  66. }
  67. }
  68. public function SetCurrentTabContainer($sTabContainer = '')
  69. {
  70. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  71. $this->m_sCurrentTabContainer = $sTabContainer;
  72. return $sPreviousTabContainer;
  73. }
  74. public function SetCurrentTab($sTabLabel = '')
  75. {
  76. $sPreviousTab = $this->m_sCurrentTab;
  77. $this->m_sCurrentTab = $sTabLabel;
  78. return $sPreviousTab;
  79. }
  80. /**
  81. * Echoes the content of the whole page
  82. * @return void
  83. */
  84. public function output()
  85. {
  86. foreach($this->a_headers as $s_header)
  87. {
  88. header($s_header);
  89. }
  90. if (count($this->m_aTabs) > 0)
  91. {
  92. $this->add_ready_script(
  93. <<<EOF
  94. // The "tab widgets" to handle.
  95. var tabs = $('div[id^=tabbedContent]');
  96. // This selector will be reused when selecting actual tab widget A elements.
  97. var tab_a_selector = 'ul.ui-tabs-nav a';
  98. // Enable tabs on all tab widgets. The `event` property must be overridden so
  99. // that the tabs aren't changed on click, and any custom event name can be
  100. // specified. Note that if you define a callback for the 'select' event, it
  101. // will be executed for the selected tab whenever the hash changes.
  102. tabs.tabs({ event: 'change' });
  103. // Define our own click handler for the tabs, overriding the default.
  104. tabs.find( tab_a_selector ).click(function()
  105. {
  106. var state = {};
  107. // Get the id of this tab widget.
  108. var id = $(this).closest( 'div[id^=tabbedContent]' ).attr( 'id' );
  109. // Get the index of this tab.
  110. var idx = $(this).parent().prevAll().length;
  111. // Set the state!
  112. state[ id ] = idx;
  113. $.bbq.pushState( state );
  114. });
  115. EOF
  116. );
  117. }
  118. // Render the tabs in the page (if any)
  119. foreach($this->m_aTabs as $sTabContainerName => $aTabContainer)
  120. {
  121. $sTabs = '';
  122. $m_aTabs = $aTabContainer['content'];
  123. $sPrefix = $aTabContainer['prefix'];
  124. $container_index = 0;
  125. if (count($m_aTabs) > 0)
  126. {
  127. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  128. $sTabs .= "<ul>\n";
  129. // Display the unordered list that will be rendered as the tabs
  130. $i = 0;
  131. foreach($m_aTabs as $sTabName => $sTabContent)
  132. {
  133. $sTabs .= "<li><a href=\"#tab_{$sPrefix}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  134. $i++;
  135. }
  136. $sTabs .= "</ul>\n";
  137. // Now add the content of the tabs themselves
  138. $i = 0;
  139. foreach($m_aTabs as $sTabName => $sTabContent)
  140. {
  141. $sTabs .= "<div id=\"tab_{$sPrefix}$i\">".$sTabContent."</div>\n";
  142. $i++;
  143. }
  144. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  145. }
  146. $this->s_content = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $this->s_content);
  147. $container_index++;
  148. }
  149. $s_captured_output = ob_get_contents();
  150. ob_end_clean();
  151. echo $this->s_content;
  152. echo $this->s_deferred_content;
  153. if (count($this->a_scripts) > 0)
  154. {
  155. echo "<script type=\"text/javascript\">\n";
  156. echo implode("\n", $this->a_scripts);
  157. echo "\n</script>\n";
  158. }
  159. if (!empty($this->m_sReadyScript))
  160. {
  161. echo "<script type=\"text/javascript\">\n";
  162. echo $this->m_sReadyScript; // Ready Scripts are output as simple scripts
  163. echo "\n</script>\n";
  164. }
  165. if (trim($s_captured_output) != "")
  166. {
  167. echo $s_captured_output;
  168. }
  169. }
  170. /**
  171. * Adds a paragraph with a smaller font into the page
  172. * NOT implemented (i.e does nothing)
  173. * @param string $sText Content of the (small) paragraph
  174. * @return void
  175. */
  176. public function small_p($sText)
  177. {
  178. }
  179. public function add($sHtml)
  180. {
  181. if (!empty($this->m_sCurrentTabContainer) && !empty($this->m_sCurrentTab))
  182. {
  183. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  184. }
  185. else
  186. {
  187. parent::add($sHtml);
  188. }
  189. }
  190. /**
  191. * Adds a script to be executed when the DOM is ready (typical JQuery use)
  192. * NOT implemented in this version of the class.
  193. * @return void
  194. */
  195. public function add_ready_script($sScript)
  196. {
  197. // Does nothing in ajax rendered content.. for now...
  198. // Maybe we should add this as a simple <script> tag at the end of the output
  199. // considering that at this time everything in the page is "ready"...
  200. $this->m_sReadyScript .= $sScript;
  201. }
  202. /**
  203. * Cannot be called in this context, since Ajax pages do not share
  204. * any context with the calling page !!
  205. */
  206. public function GetUniqueId()
  207. {
  208. assert(false);
  209. return 0;
  210. }
  211. }
  212. ?>