template.lib.php 14 KB

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