fcktemplates.xml.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * Chamilo LMS
  4. *
  5. * For a full list of contributors, see "credits.txt".
  6. * The full license can be read in "license.txt".
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * See the GNU General Public License for more details.
  14. */
  15. // Setting the encoding to UTF-8.
  16. header('Content-Type: text/xml; charset=utf-8');
  17. // Name of the language file that needs to be included.
  18. $language_file = 'document';
  19. // Including the global initialization file.
  20. require_once '../../global.inc.php';
  21. // Outputting the opening tag of the xml file.
  22. echo '<?xml version="1.0" encoding="utf-8" ?>';
  23. // Load a stylesheet.
  24. $css = loadCSS(api_get_setting('stylesheets'));
  25. // Setting some paths.
  26. $img_dir = api_get_path(REL_CODE_PATH).'img/';
  27. $default_course_dir = api_get_path(REL_CODE_PATH).'default_course_document/';
  28. // Setting templates for teachers or for students
  29. $is_allowed_to_edit = api_is_allowed_to_edit(false,true);
  30. // Start the templates node.
  31. echo '<Templates imagesBasePath="">';
  32. // Load empty template.
  33. load_empty_template();
  34. if($is_allowed_to_edit){
  35. // Load the templates that were defined by the platform admin.
  36. load_platform_templates();
  37. }
  38. else{
  39. // Load student templates.
  40. load_student_templates();
  41. }
  42. // Load the personal templates.
  43. load_personal_templates(api_get_user_id());
  44. // End the templates node.
  45. echo '</Templates>';
  46. exit;
  47. /**
  48. * Loads a given css style (default.css).
  49. *
  50. * @param string $css_name the folder name of the style
  51. * @return html code for adding a css style <style ...
  52. */
  53. function loadCSS($css_name) {
  54. $template_css = file_get_contents(api_get_path(SYS_PATH).'main/css/'.$css_name.'/default.css');
  55. $template_css = str_replace('../../img/', api_get_path(REL_CODE_PATH).'img/', $template_css);
  56. $template_css = str_replace('images/', api_get_path(REL_CODE_PATH).'css/'.$css_name.'/images/', $template_css);
  57. // Reseting the body's background color to be in white, see Task #1885 and http://www.chamilo.org/en/node/713
  58. $template_css .= "\n".'body { background: #fff; } /* Resetting the background. */'."\n";
  59. // Removing system-specific styles and cleaning, see task #1282.
  60. $regex1 = array(
  61. '/\/\*(.+?)\*\//sm' => '', // Removing comments.
  62. '/\r\n/m' => "\n", // New lines in Unix style.
  63. '/\r/m' => "\n" // New lines in Unix style.
  64. );
  65. $template_css = preg_replace(array_keys($regex1), $regex1, $template_css);
  66. $template_css = preg_replace('/behavior[^;\{\}]*;/ism', '', $template_css); // Removing behavior-definition, it is IE-specific.
  67. $template_css_array = explode('}', $template_css);
  68. if (!empty($template_css_array)) {
  69. $deleters = array(
  70. '/.*\#.*\{[^\}]*\}/sm', // Removing css definitions bound to system secific elements (identified by id).
  71. '/.*\..*\{[^\}]*\}/sm', // Removing css definitions bound to classes, we assume them as system secific.
  72. // Removing css definitions bound to intractive types of elements that teachers most probably don't need.
  73. '/.*input.*\{[^\}]*\}/ism',
  74. '/.*textarea.*\{[^\}]*\}/ism',
  75. '/.*select.*\{[^\}]*\}/ism',
  76. '/.*form.*\{[^\}]*\}/ism',
  77. '/.*button.*\{[^\}]*\}/ism'
  78. );
  79. foreach ($template_css_array as $key => & $css_definition) {
  80. if (trim($css_definition) == '') {
  81. unset($template_css_array[$key]);
  82. continue;
  83. }
  84. $css_definition = trim($css_definition.'}');
  85. foreach ($deleters as & $deleter) {
  86. if (preg_match($deleter, $css_definition)) {
  87. unset($template_css_array[$key]);
  88. }
  89. }
  90. }
  91. $template_css = implode("\n\n", $template_css_array);
  92. }
  93. $regex2 = array(
  94. '/[ \t]*\n/m' => "\n", // Removing trailing whitespace.
  95. '/\n{3,}/m' => "\n\n" // Removing extra empty lines.
  96. );
  97. $template_css = preg_replace(array_keys($regex2), $regex2, $template_css);
  98. if (trim($template_css) == '') {
  99. return '';
  100. }
  101. return "\n".'<style type="text/css">'."\n".$template_css."\n".'</style>'."\n";
  102. }
  103. /**
  104. * Transforms a language variable into XML-usable code
  105. *
  106. * @param unknown_type $var
  107. * @return unknown
  108. */
  109. function s($var) {
  110. static $search = array('&', '<', '>');
  111. static $replace = array('&amp;',' &amp;lt;', '&amp;gt;');
  112. return str_replace($search, $replace, api_utf8_encode(get_lang($var, '')));
  113. }
  114. /**
  115. * Transforms some text into XML-usable code
  116. *
  117. * @param unknown_type $var
  118. * @return unknown
  119. */
  120. function s2($var) {
  121. static $search = array('&', '<', '>');
  122. static $replace = array('&amp;', '&amp;lt;', '&amp;gt;');
  123. return str_replace($search, $replace, api_utf8_encode($var));
  124. }
  125. /**
  126. * Loads the platform templates as defined by the platform administrator in
  127. * "Administration > Configuration settings > Templates"
  128. *
  129. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  130. * @version March 2009
  131. * @since Dokeos 1.8.6
  132. */
  133. function load_platform_templates() {
  134. global $css, $img_dir, $default_course_dir, $js;
  135. $table_template = Database::get_main_table('system_template');
  136. $sql = "SELECT title, image, comment, content FROM $table_template";
  137. $result = Database::query($sql);
  138. $search = array('{CSS}', '{IMG_DIR}', '{REL_PATH}', '{COURSE_DIR}');
  139. $replace = array($css.$js, $img_dir, api_get_path(REL_PATH), $default_course_dir);
  140. $template_thumb = api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/';
  141. while ($row = Database::fetch_array($result)) {
  142. $image = empty($row['image']) ? $template_thumb.'empty.gif' : $template_thumb.$row['image'];
  143. $row['content'] = str_replace($search, $replace, $row['content']);
  144. echo '
  145. <Template title="'.s($row['title']).'" image="'.$image.'">
  146. <Description>'.s($row['comment']).'</Description>
  147. <Html>
  148. <![CDATA[
  149. '.$row['content'].'
  150. ]]>
  151. </Html>
  152. </Template>';
  153. }
  154. }
  155. /**
  156. * Loads all the personal templates of the user when
  157. *
  158. * @param integer $user_id the id of the user
  159. * @return xml node
  160. *
  161. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  162. * @version March 2009
  163. * @since Dokeos 1.8.6 The code already existed but not in a function and a lot less performant.
  164. */
  165. function load_personal_templates($user_id = 0) {
  166. global $_course;
  167. // the templates that the user has defined are only available inside the course itself
  168. if (empty($_course)) {
  169. return false;
  170. }
  171. // For which user are we getting the templates?
  172. if ($user_id == 0) {
  173. $user_id = api_get_user_id();
  174. }
  175. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  176. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  177. $course_id = api_get_course_int_id();
  178. // The sql statement for getting all the user defined templates
  179. $sql = "SELECT template.id, template.title, template.description, template.image, template.ref_doc, document.path
  180. FROM ".$table_template." template, ".$table_document." document
  181. WHERE
  182. user_id='".Database::escape_string($user_id)."' AND
  183. course_code='".Database::escape_string(api_get_course_id())."' AND
  184. document.c_id = $course_id AND
  185. document.id = template.ref_doc";
  186. $result_template = Database::query($sql);
  187. while ($row = Database::fetch_array($result_template)) {
  188. $row['content'] = file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row['path']);
  189. //$row['content'] = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row['path'];
  190. if (!empty($row['image'])) {
  191. $image = api_get_path(WEB_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/'.$row['image'];
  192. } else {
  193. $image = api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif';
  194. }
  195. echo '
  196. <Template title="'.s2($row['title']).'" image="'.$image.'">
  197. <Description>'.s2($row['Description']).'</Description>
  198. <Html>
  199. <![CDATA[
  200. '.$row['content'].'
  201. ]]>
  202. </Html>
  203. </Template>';
  204. }
  205. }
  206. function load_empty_template() {
  207. global $css, $js;
  208. /* <?php echo $css; ?>
  209. <?php echo $js; ?> */
  210. ?>
  211. <Template title="<?php echo s2('Empty'); ?>" image="<?php echo api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/empty.gif'; ?>">
  212. <Description></Description>
  213. <Html>
  214. <![CDATA[
  215. <html>
  216. <head>
  217. <meta charset="<?php echo api_get_system_encoding(); ?>" />
  218. </head>
  219. <body dir="<?php echo api_get_text_direction(); ?>">
  220. </body>
  221. </html>
  222. ]]>
  223. </Html>
  224. </Template>
  225. <?php
  226. }
  227. /**
  228. * Loads the student templates
  229. */
  230. function load_student_templates() {
  231. $fckeditor_template_path='/main/inc/lib/fckeditor/editor/dialog/fck_template/images/';
  232. ?>
  233. <Template title="Image and Title" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template1.gif';?>">
  234. <Description>One main image with a title and text that surround the image.</Description>
  235. <Html>
  236. <![CDATA[
  237. <img style="MARGIN-RIGHT: 10px" height="100" alt="" width="100" align="left"/>
  238. <h3>Type the title here</h3>
  239. Type the text here
  240. ]]>
  241. </Html>
  242. </Template>
  243. <Template title="Strange Template" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template2.gif';?>">
  244. <Description>A template that defines two colums, each one with a title, and some text.</Description>
  245. <Html>
  246. <![CDATA[
  247. <table cellspacing="0" cellpadding="0" width="100%" border="0">
  248. <tbody>
  249. <tr>
  250. <td width="50%">
  251. <h3>Title 1</h3>
  252. </td>
  253. <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
  254. <td width="50%">
  255. <h3>Title 2</h3>
  256. </td>
  257. </tr>
  258. <tr>
  259. <td>Text 1</td>
  260. <td>&nbsp;</td>
  261. <td>Text 2</td>
  262. </tr>
  263. </tbody>
  264. </table>
  265. More text goes here.
  266. ]]>
  267. </Html>
  268. </Template>
  269. <Template title="Text and Table" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template3.gif';?>">
  270. <Description>A title with some text and a table.</Description>
  271. <Html>
  272. <![CDATA[
  273. <table align="left" width="80%" border="0" cellspacing="0" cellpadding="0"><tr><td>
  274. <h3>Title goes here</h3>
  275. <p>
  276. <table style="FLOAT: right" cellspacing="0" cellpadding="0" width="150" border="1">
  277. <tbody>
  278. <tr>
  279. <td align="center" colspan="3"><strong>Table title</strong></td>
  280. </tr>
  281. <tr>
  282. <td>&nbsp;</td>
  283. <td>&nbsp;</td>
  284. <td>&nbsp;</td>
  285. </tr>
  286. <tr>
  287. <td>&nbsp;</td>
  288. <td>&nbsp;</td>
  289. <td>&nbsp;</td>
  290. </tr>
  291. <tr>
  292. <td>&nbsp;</td>
  293. <td>&nbsp;</td>
  294. <td>&nbsp;</td>
  295. </tr>
  296. <tr>
  297. <td>&nbsp;</td>
  298. <td>&nbsp;</td>
  299. <td>&nbsp;</td>
  300. </tr>
  301. </tbody>
  302. </table>
  303. Type the text here</p>
  304. </td></tr></table>
  305. ]]>
  306. </Html>
  307. </Template>
  308. <?php
  309. }