template.lib.php 15 KB

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