You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.5 KiB

15 years ago
15 years ago
15 years ago
15 years ago
12 years ago
12 years ago
15 years ago
15 years ago
12 years ago
12 years ago
15 years ago
  1. <?php
  2. //doc
  3. // classname: PageBuilder
  4. // scope: PUBLIC
  5. //
  6. ///doc
  7. /**
  8. * \class PageBuilder
  9. * \brief Templating engine for Movim
  10. *
  11. * This class is the templating engine for movim. It determines what page to
  12. * load based on the context and fills in placeholder values ('%' delimited).
  13. *
  14. * It also handles themes.
  15. */
  16. class TplPageBuilder
  17. {
  18. // internal variables
  19. private $theme = 'movim';
  20. private $title = '';
  21. private $menu = array();
  22. private $content = '';
  23. private $user;
  24. private $css = array();
  25. private $scripts = array();
  26. private $polling = true;
  27. /**
  28. * Constructor. Determines whether to show the login page to the user or the
  29. * Movim interface.
  30. */
  31. function __construct(&$user = NULL)
  32. {
  33. $this->user = $user;
  34. $conf = new Conf();
  35. $this->theme = $conf->getServerConfElement('theme');
  36. }
  37. function views_path($file)
  38. {
  39. return VIEWS_PATH . '/' . $file;
  40. }
  41. /**
  42. * Returns or prints the link to a file.
  43. * @param file is the path to the file relative to the theme's root
  44. * @param return optionally returns the link instead of printing it if set to true
  45. */
  46. function linkFile($file, $return = false)
  47. {
  48. $path = BASE_URI . 'themes/' . $this->theme . '/' . $file;
  49. if($return) {
  50. return $path;
  51. } else {
  52. echo $path;
  53. }
  54. }
  55. /**
  56. * Inserts the link tag for a css file.
  57. */
  58. function themeCss($file)
  59. {
  60. echo '<link rel="stylesheet" href="'
  61. . $this->linkFile($file, true) .
  62. "\" type=\"text/css\" />\n";
  63. }
  64. /**
  65. * Actually generates the page from templates.
  66. */
  67. function build($template)
  68. {
  69. if (ENVIRONMENT === 'production')ob_clean();
  70. ob_start();
  71. require($this->views_path($template));
  72. $outp = ob_get_clean();
  73. $outp = str_replace('<%scripts%>',
  74. $this->printCss() . $this->printScripts(),
  75. $outp);
  76. return $outp;
  77. }
  78. /**
  79. * Sets the page's title.
  80. */
  81. function setTitle($name)
  82. {
  83. $this->title = $name;
  84. }
  85. /**
  86. * Displays the current title.
  87. */
  88. function title()
  89. {
  90. echo $this->title;
  91. }
  92. /**
  93. * Adds a link to the menu with the displayed label.
  94. */
  95. function menuAddLink($label, $href, $active = false, $mobile = false)
  96. {
  97. $this->menu[] = array(
  98. 'type' => 'link',
  99. 'label' => $label,
  100. 'href' => $href,
  101. 'active' => $active,
  102. 'mobile' => $mobile
  103. );
  104. }
  105. function menuAddVerbatim($html)
  106. {
  107. $this->menu[] = array(
  108. 'type' => 'verbatim',
  109. 'html' => $html,
  110. );
  111. }
  112. /** shows up the menu. */
  113. function menu()
  114. {
  115. echo '<ul class="menu">' . "\n";
  116. foreach($this->menu as $link) {
  117. if($link['type'] == 'link') {
  118. echo "\t\t".'<li><a
  119. href="'.Route::urlize($link['href']).'"
  120. title="'.$link['label'].'"
  121. class="'.$link['href'].'' ;
  122. if($link['active'] == true) {
  123. echo ' active ';
  124. }
  125. // If we display only the link on desktop
  126. if($link['mobile'] == true) {
  127. echo ' on_desktop ';
  128. }
  129. echo '"';
  130. echo "><span class=\"mobile\">".$link['label'] . "</span></a></li>\n";
  131. } else {
  132. echo $link['html'];
  133. }
  134. }
  135. echo "\t</ul>\n";
  136. }
  137. function addScript($script)
  138. {
  139. $this->scripts[] = BASE_URI . 'app/assets/js/' . $script;
  140. }
  141. /**
  142. * Inserts the link tag for a css file.
  143. */
  144. function addCss($file)
  145. {
  146. $this->css[] = $this->linkFile($file, true);
  147. }
  148. function scripts()
  149. {
  150. echo '<%scripts%>';
  151. }
  152. function printScripts() {
  153. $out = '';
  154. $widgets = WidgetWrapper::getInstance();
  155. $scripts = array_merge($this->scripts, $widgets->loadjs());
  156. foreach($scripts as $script) {
  157. $out .= '<script type="text/javascript" src="'
  158. . $script .
  159. '"></script>'."\n";
  160. }
  161. $ajaxer = AjaxController::getInstance();
  162. $out .= $ajaxer->genJs();
  163. return $out;
  164. }
  165. function printCss() {
  166. $out = '';
  167. $widgets = WidgetWrapper::getInstance();
  168. $csss = array_merge($this->css, $widgets->loadcss()); // Note the 3rd s, there are many.
  169. foreach($csss as $css_path) {
  170. $out .= '<link rel="stylesheet" href="'
  171. . $css_path .
  172. "\" type=\"text/css\" />\n";
  173. }
  174. return $out;
  175. }
  176. function setContent($data)
  177. {
  178. $this->content .= $data;
  179. }
  180. function addContent($data, $append = true)
  181. {
  182. if($append) {
  183. $this->content .= $data;
  184. } else {
  185. $this->content = $data . $this->content;
  186. }
  187. }
  188. function content()
  189. {
  190. echo $this->content;
  191. }
  192. /**
  193. * Loads up a widget and prints it at the current place.
  194. */
  195. function widget($name, $register = true)
  196. {
  197. $widgets = WidgetWrapper::getInstance($register);
  198. $widgets->runWidget($name, 'build');
  199. }
  200. function displayFooterDebug()
  201. {
  202. //\system\Logs\Logger::displayFooterDebug();
  203. }
  204. }