template.lib.php 13 KB

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