template.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // Load Smarty library
  4. require_once api_get_path(LIBRARY_PATH).'smarty/Smarty.class.php';
  5. class Template extends Smarty {
  6. var $style = 'default'; //see the template folder
  7. var $show_header;
  8. var $show_footer;
  9. function __construct($title = '', $show_header = true, $show_footer = true) {
  10. $this->title = $title;
  11. //Smarty 3 configuration
  12. $this->setPluginsDir(api_get_path(LIBRARY_PATH).'smarty/plugins');
  13. $this->setCacheDir(api_get_path(SYS_ARCHIVE_PATH));
  14. $this->setCompileDir(api_get_path(SYS_ARCHIVE_PATH));
  15. $this->setTemplateDir(api_get_path(SYS_CODE_PATH).'template/');
  16. $this->setConfigDir(api_get_path(SYS_ARCHIVE_PATH));
  17. //Caching settings
  18. $this->caching = false;
  19. //$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  20. $this->cache_lifetime = Smarty::CACHING_OFF; // no caching
  21. //$this->cache_lifetime = 120;
  22. //Setting system variables
  23. $this->set_system_parameters();
  24. //Setting user variables
  25. $this->set_user_parameters();
  26. //header and footer are showed by default
  27. $this->set_footer($show_footer);
  28. $this->set_header($show_header);
  29. //Creating a Smarty modifier - Now we can call the get_lang from a template!!! Just use {"MyString"|get_lang}
  30. $this->registerPlugin("modifier","get_lang", "get_lang");
  31. //Not recomended to use get_path, use {$_p.'xxx'} see the set_system_parameters()
  32. $this->registerPlugin("modifier","get_path", "api_get_path");
  33. $this->registerPlugin("modifier","get_setting", "api_get_setting");
  34. //To load a smarty plugin
  35. //$this->loadPlugin('smarty_function_get_lang');
  36. //To the the smarty installation
  37. //$this->testInstall();
  38. $this->set_header_parameters();
  39. $this->set_footer_parameters();
  40. $this->assign('style', $this->style);
  41. }
  42. /*
  43. * Use smarty to parse the actions menu
  44. * @todo finish it!
  45. * */
  46. function set_actions($actions) {
  47. $action_string = '';
  48. if (!empty($actions)) {
  49. foreach($actions as $action) {
  50. }
  51. }
  52. $this->assign('actions', $actions);
  53. }
  54. /**
  55. * Shortcut to display a 1 col layout (index.php)
  56. * */
  57. function display_one_col_template() {
  58. $tpl = $this->get_template('layout/layout_1_col.tpl');
  59. $this->display($tpl);
  60. }
  61. /**
  62. * Shortcut to display a 2 col layout (userportal.php)
  63. * */
  64. function display_two_col_template() {
  65. $tpl = $this->get_template('layout/layout_2_col.tpl');
  66. $this->display($tpl);
  67. }
  68. /**
  69. * Displays an empty template
  70. */
  71. function display_blank_template() {
  72. $tpl = $this->get_template('layout/blank.tpl');
  73. $this->display($tpl);
  74. }
  75. /**
  76. * Displays an empty template
  77. */
  78. function display_no_layout_template() {
  79. $tpl = $this->get_template('layout/no_layout.tpl');
  80. $this->display($tpl);
  81. }
  82. /**
  83. * Sets the footer visibility
  84. * @param bool true if we show the footer
  85. */
  86. function set_footer($status) {
  87. $this->show_footer = $status;
  88. $this->assign('show_footer', $status);
  89. }
  90. /**
  91. * Sets the header visibility
  92. * @param bool true if we show the header
  93. */
  94. function set_header($status) {
  95. $this->show_header = $status;
  96. $this->assign('show_header', $status);
  97. $show_admin_toolbar = api_get_setting('show_admin_toolbar');
  98. $show_toolbar = 0;
  99. switch($show_admin_toolbar) {
  100. case 'do_not_show':
  101. break;
  102. case 'show_to_admin':
  103. if (api_is_platform_admin()) {
  104. $show_toolbar = 1;
  105. }
  106. break;
  107. case 'show_to_admin_and_teachers':
  108. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  109. $show_toolbar = 1;
  110. }
  111. break;
  112. case 'show_to_all':
  113. $show_toolbar = 1;
  114. break;
  115. }
  116. $this->assign('show_toolbar', $show_toolbar);
  117. }
  118. function get_template($name) {
  119. return $this->style.'/'.$name;
  120. }
  121. private function set_user_parameters() {
  122. $user_info = array();
  123. $user_info['logged'] = 0;
  124. if (api_get_user_id() && !api_is_anonymous()) {
  125. $user_info = api_get_user_info();
  126. $user_info['logged'] = 1;
  127. $user_info['is_admin'] = 0;
  128. if (api_is_platform_admin()) {
  129. $user_info['is_admin'] = 1;
  130. }
  131. $user_info['messages_count'] = MessageManager::get_new_messages();
  132. }
  133. //Setting the $_u array that could be use in any template
  134. $this->assign('_u', $user_info);
  135. }
  136. private function set_system_parameters() {
  137. global $_configuration;
  138. //Setting app paths
  139. $_p = array('web' => api_get_path(WEB_PATH),
  140. 'web_course' => api_get_path(WEB_COURSE_PATH),
  141. 'web_main' => api_get_path(WEB_CODE_PATH),
  142. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  143. );
  144. $this->assign('_p', $_p);
  145. //Here we can add system parameters that can be use in any template
  146. $_s = array(
  147. 'software_name' => $_configuration['software_name'],
  148. 'system_version' => $_configuration['system_version'],
  149. 'site_name' => api_get_setting('siteName'),
  150. 'institution' => api_get_setting('Institution'),
  151. );
  152. $this->assign('_s', $_s);
  153. }
  154. private function set_header_parameters($help = null) {
  155. $nameTools = $this->title;
  156. global $_plugins, $lp_theme_css, $mycoursetheme, $user_theme, $platform_theme;
  157. global $httpHeadXtra, $htmlHeadXtra, $_course, $_user, $text_dir, $plugins, $_user,
  158. $_cid, $interbreadcrumb, $charset, $language_file, $noPHP_SELF;
  159. global $menu_navigation;
  160. global $_configuration, $show_learn_path;
  161. $this->assign('system_charset', api_get_system_encoding());
  162. if (isset($httpHeadXtra) && $httpHeadXtra) {
  163. foreach ($httpHeadXtra as & $thisHttpHead) {
  164. header($thisHttpHead);
  165. }
  166. }
  167. $this->assign('online_button', Security::remove_XSS(Display::return_icon('online.png')));
  168. $this->assign('offline_button', Security::remove_XSS(Display::return_icon('offline.png')));
  169. // Get language iso-code for this page - ignore errors
  170. $this->assign('document_language', api_get_language_isocode());
  171. $course_title = $_course['name'];
  172. $title_list[] = api_get_setting('Institution');
  173. $title_list[] = api_get_setting('siteName');
  174. if (!empty($course_title)) {
  175. $title_list[] = $course_title;
  176. }
  177. if ($nameTools != '') {
  178. $title_list[] = $nameTools;
  179. }
  180. $title_string = '';
  181. for($i=0; $i<count($title_list);$i++) {
  182. $title_string .=$title_list[$i];
  183. if (isset($title_list[$i+1])) {
  184. $item = trim($title_list[$i+1]);
  185. if (!empty($item))
  186. $title_string .=' - ';
  187. }
  188. }
  189. $this->assign('title_string', $title_string);
  190. $platform_theme = api_get_setting('stylesheets');
  191. $my_style = api_get_visual_theme();
  192. $style = '';
  193. //Base CSS
  194. $style = '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  195. //Default theme CSS
  196. $style .= '@import "'.api_get_path(WEB_CSS_PATH).$my_style.'/default.css";';
  197. //Course theme CSS
  198. $style .= '@import "'.api_get_path(WEB_CSS_PATH).$my_style.'/course.css";';
  199. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  200. $style .= 'img, div { behavior: url('.api_get_path(WEB_LIBRARY_PATH).'javascript/iepngfix/iepngfix.htc) } ';
  201. }
  202. $this->assign('css_style', $style);
  203. $style_print = '@import "'.api_get_path(WEB_CSS_PATH).$my_style.'/print.css";';
  204. $this->assign('css_style_print', $style_print);
  205. //Extra JS files
  206. $js_files = array(
  207. 'jquery.min.js',
  208. 'chosen/chosen.jquery.min.js',
  209. 'thickbox.js',
  210. 'jquery.menu.js',
  211. 'dtree/dtree.js',
  212. 'email_links.lib.js.php',
  213. 'bootstrap/bootstrap-dropdown.js'
  214. );
  215. if (1) {
  216. $js_files[] = 'chat/js/chat.js';
  217. }
  218. if (api_get_setting('accessibility_font_resize') == 'true') {
  219. $js_files[] = 'fontresize.js';
  220. }
  221. if (api_get_setting('include_asciimathml_script') == 'true') {
  222. $js_files[] = 'asciimath/ASCIIMathML.js';
  223. }
  224. $js_file_to_string = '';
  225. foreach($js_files as $js_file) {
  226. $js_file_to_string .= api_get_js($js_file);
  227. }
  228. //Extra CSS files
  229. $css_files = array (
  230. api_get_path(WEB_LIBRARY_PATH).'javascript/thickbox.css',
  231. api_get_path(WEB_LIBRARY_PATH).'javascript/chosen/chosen.css',
  232. api_get_path(WEB_LIBRARY_PATH).'javascript/dtree/dtree.css',
  233. );
  234. if ($show_learn_path) {
  235. $css_files[] = api_get_path(WEB_CSS_PATH).$my_style.'/learnpath.css';
  236. }
  237. if (1) {
  238. $css_files[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chat/css/chat.css';
  239. }
  240. $css_file_to_string = '';
  241. foreach($css_files as $css_file) {
  242. $css_file_to_string .= api_get_css($css_file);
  243. }
  244. global $this_section, $nameTools;
  245. $this->assign('css_file_to_string', $css_file_to_string);
  246. $this->assign('js_file_to_string', $js_file_to_string);
  247. $this->assign('text_direction', api_get_text_direction());
  248. $this->assign('style_print', $style_print);
  249. $this->assign('section_name', 'section-'.$this_section);
  250. $extra_headers = '';
  251. if (isset($htmlHeadXtra) && $htmlHeadXtra) {
  252. foreach ($htmlHeadXtra as & $this_html_head) {
  253. $extra_headers .= $this_html_head;
  254. }
  255. }
  256. $this->assign('extra_headers', $extra_headers);
  257. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  258. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  259. $access_url_id = api_get_current_access_url_id();
  260. if ($access_url_id != -1) {
  261. $url_info = api_get_access_url($access_url_id);
  262. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  263. $clean_url = replace_dangerous_char($url);
  264. $clean_url = str_replace('/', '-', $clean_url);
  265. $clean_url .= '/';
  266. $homep = api_get_path(REL_PATH).'home/'.$clean_url; //homep for Home Path
  267. //we create the new dir for the new sites
  268. if (is_file($homep.'favicon.ico')) {
  269. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  270. }
  271. }
  272. }
  273. $this->assign('favico', $favico);
  274. //old banner.inc.php
  275. require_once api_get_path(LIBRARY_PATH).'banner.lib.php';
  276. global $my_session_id;
  277. $session_id = api_get_session_id();
  278. $session_name = api_get_session_name($my_session_id);
  279. $help_content = '';
  280. if (api_get_setting('enable_help_link') == 'true') {
  281. if (!empty($help)) {
  282. $help = Security::remove_XSS($help);
  283. $help_content = '<li class="help">';
  284. $help_content .= '<a href="'.api_get_path(WEB_CODE_PATH).'help/help.php?open='.$help.'&height=400&width=600" class="thickbox" title="'.get_lang('Help').'">';
  285. $help_content .= '<img src="'.api_get_path(WEB_IMG_PATH).'help.large.png" alt="'.get_lang('Help').'" title="'.get_lang('Help').'" />';
  286. $help_content .= '</a></li>';
  287. }
  288. }
  289. $this->assign('help_content', $help_content);
  290. $bug_notification_link = '';
  291. if (api_get_setting('show_link_bug_notification') == 'true') {
  292. $bug_notification_link = '<li class="report">
  293. <a href="http://support.chamilo.org/projects/chamilo-18/wiki/How_to_report_bugs" target="_blank">
  294. <img src="'.api_get_path(WEB_IMG_PATH).'bug.large.png" style="vertical-align: middle;" alt="'.get_lang('ReportABug').'" title="'.get_lang('ReportABug').'"/></a>
  295. </li>';
  296. }
  297. $this->assign('bug_notification_link', $bug_notification_link);
  298. if (isset($database_connection)) {
  299. // connect to the main database.
  300. Database::select_db($_configuration['main_database'], $database_connection);
  301. }
  302. ob_start();
  303. show_header_1($language_file, $nameTools);
  304. $header1 = ob_get_contents();
  305. ob_clean();
  306. ob_start();
  307. show_header_2();
  308. $header2 = ob_get_contents();
  309. ob_clean();
  310. ob_start();
  311. $menu_navigation = show_header_3();
  312. $header3 = ob_get_contents();
  313. ob_clean();
  314. $header4 = show_header_4($interbreadcrumb, $language_file, $nameTools);
  315. $this->assign('header1', $header1);
  316. $this->assign('header2', $header2);
  317. $this->assign('header3', $header3);
  318. $this->assign('header4', $header4);
  319. if (!api_is_platform_admin()) {
  320. $extra_header = trim(api_get_setting('header_extra_content'));
  321. if (!empty($extra_header)) {
  322. $this->assign('header_extra_content', $extra_header);
  323. }
  324. }
  325. if ($this->show_header == 1) {
  326. header('Content-Type: text/html; charset='.api_get_system_encoding());
  327. header('X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'],0,1));
  328. }
  329. }
  330. private function set_footer_parameters() {
  331. //Footer plugin
  332. global $_plugins, $_configuration;
  333. ob_start();
  334. api_plugin('footer');
  335. $plugin_footer = ob_get_contents();
  336. ob_clean();
  337. $this->assign('plugin_footer', $plugin_footer);
  338. $this->assign('show_administrator_data', api_get_setting('show_administrator_data'));
  339. //$platform = get_lang('Platform').' <a href="'.$_configuration['software_url'].'" target="_blank">'.$_configuration['software_name'].' '.$_configuration['system_version'].'</a> &copy; '.date('Y');
  340. //$this->assign('platform_name', $platform);
  341. if (!api_is_platform_admin()) {
  342. $extra_footer = trim(api_get_setting('footer_extra_content'));
  343. if (!empty($extra_footer)) {
  344. $this->assign('footer_extra_content', $extra_footer);
  345. }
  346. }
  347. $administrator_data = get_lang('Manager'). ' : '. Display::encrypted_mailto_link(api_get_setting('emailAdministrator'), api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname')));
  348. $this->assign('administrator_name', $administrator_data);
  349. $stats = '';
  350. $this->assign('execution_stats', $stats);
  351. }
  352. }