fcktemplates.xml.php 13 KB

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