fcktemplates.xml.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_DATA_PATH).'document_templates/';
  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 '<Template title="'.s($row['title']).'" image="'.$image.'">
  145. <Description>'.s($row['comment']).'</Description>
  146. <Html>
  147. <![CDATA[
  148. '.$row['content'].'
  149. ]]>
  150. </Html>
  151. </Template>';
  152. }
  153. }
  154. /**
  155. * Loads all the personal templates of the user when
  156. *
  157. * @param integer $user_id the id of the user
  158. * @return xml node
  159. *
  160. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  161. * @version March 2009
  162. * @since Dokeos 1.8.6 The code already existed but not in a function and a lot less performant.
  163. */
  164. function load_personal_templates($user_id = 0) {
  165. $_course = api_get_course_info();
  166. // the templates that the user has defined are only available inside the course itself
  167. if (empty($_course)) {
  168. return false;
  169. }
  170. // For which user are we getting the templates?
  171. if ($user_id == 0) {
  172. $user_id = api_get_user_id();
  173. }
  174. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  175. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  176. $course_id = api_get_course_int_id();
  177. // The sql statement for getting all the user defined templates
  178. $sql = "SELECT template.id, template.title, template.description, template.image, template.ref_doc, document.path
  179. FROM ".$table_template." template, ".$table_document." document
  180. WHERE
  181. user_id='".Database::escape_string($user_id)."' AND
  182. course_code='".Database::escape_string(api_get_course_id())."' AND
  183. document.c_id = $course_id AND
  184. document.id = template.ref_doc";
  185. $result_template = Database::query($sql);
  186. while ($row = Database::fetch_array($result_template)) {
  187. $row['content'] = file_get_contents(api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$row['path']);
  188. if (!empty($row['image'])) {
  189. $image = api_get_path(WEB_DATA_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/'.$row['image'];
  190. } else {
  191. $image = api_get_path(WEB_DATA_PATH).'document_templates/noimage.gif';
  192. }
  193. echo '
  194. <Template title="'.s2($row['title']).'" image="'.$image.'">
  195. <Description>'.s2($row['Description']).'</Description>
  196. <Html>
  197. <![CDATA[
  198. '.$row['content'].'
  199. ]]>
  200. </Html>
  201. </Template>';
  202. }
  203. }
  204. function load_empty_template() {
  205. global $css, $js;
  206. /* <?php echo $css; ?>
  207. <?php echo $js; ?> */
  208. ?>
  209. <Template title="<?php echo s2('Empty'); ?>" image="<?php echo api_get_path(WEB_DATA_PATH).'document_templates/empty.gif'; ?>">
  210. <Description></Description>
  211. <Html>
  212. <![CDATA[
  213. <html>
  214. <head>
  215. <meta charset="<?php echo api_get_system_encoding(); ?>" />
  216. </head>
  217. <body dir="<?php echo api_get_text_direction(); ?>">
  218. </body>
  219. </html>
  220. ]]>
  221. </Html>
  222. </Template>
  223. <?php
  224. }
  225. /**
  226. * Loads the student templates
  227. */
  228. function load_student_templates() {
  229. $fckeditor_template_path='/main/inc/lib/fckeditor/editor/dialog/fck_template/images/';
  230. ?>
  231. <Template title="Image and Title" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template1.gif';?>">
  232. <Description>One main image with a title and text that surround the image.</Description>
  233. <Html>
  234. <![CDATA[
  235. <img style="MARGIN-RIGHT: 10px" height="100" alt="" width="100" align="left"/>
  236. <h3>Type the title here</h3>
  237. Type the text here
  238. ]]>
  239. </Html>
  240. </Template>
  241. <Template title="Strange Template" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template2.gif';?>">
  242. <Description>A template that defines two colums, each one with a title, and some text.</Description>
  243. <Html>
  244. <![CDATA[
  245. <table cellspacing="0" cellpadding="0" width="100%" border="0">
  246. <tbody>
  247. <tr>
  248. <td width="50%">
  249. <h3>Title 1</h3>
  250. </td>
  251. <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
  252. <td width="50%">
  253. <h3>Title 2</h3>
  254. </td>
  255. </tr>
  256. <tr>
  257. <td>Text 1</td>
  258. <td>&nbsp;</td>
  259. <td>Text 2</td>
  260. </tr>
  261. </tbody>
  262. </table>
  263. More text goes here.
  264. ]]>
  265. </Html>
  266. </Template>
  267. <Template title="Text and Table" image="<?php echo api_get_path(WEB_PATH).$fckeditor_template_path.'template3.gif';?>">
  268. <Description>A title with some text and a table.</Description>
  269. <Html>
  270. <![CDATA[
  271. <table align="left" width="80%" border="0" cellspacing="0" cellpadding="0"><tr><td>
  272. <h3>Title goes here</h3>
  273. <p>
  274. <table style="FLOAT: right" cellspacing="0" cellpadding="0" width="150" border="1">
  275. <tbody>
  276. <tr>
  277. <td align="center" colspan="3"><strong>Table title</strong></td>
  278. </tr>
  279. <tr>
  280. <td>&nbsp;</td>
  281. <td>&nbsp;</td>
  282. <td>&nbsp;</td>
  283. </tr>
  284. <tr>
  285. <td>&nbsp;</td>
  286. <td>&nbsp;</td>
  287. <td>&nbsp;</td>
  288. </tr>
  289. <tr>
  290. <td>&nbsp;</td>
  291. <td>&nbsp;</td>
  292. <td>&nbsp;</td>
  293. </tr>
  294. <tr>
  295. <td>&nbsp;</td>
  296. <td>&nbsp;</td>
  297. <td>&nbsp;</td>
  298. </tr>
  299. </tbody>
  300. </table>
  301. Type the text here</p>
  302. </td></tr></table>
  303. ]]>
  304. </Html>
  305. </Template>
  306. <?php
  307. }