index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /* Integrate svg-edit libraries with Chamilo default documents
  3. * @author Juan Carlos Raña Trabado
  4. * @since 25/september/2010
  5. */
  6. //Chamilo load libraries
  7. require_once '../../../../../inc/global.inc.php';
  8. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  9. //Add security from Chamilo
  10. api_protect_course_script();
  11. api_block_anonymous_users();
  12. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  13. $curdirpath='/images/gallery'; //path of library directory
  14. $course_info = api_get_course_info();
  15. //get all files and folders
  16. $docs_and_folders = DocumentManager::get_all_document_data($course_info, $curdirpath, 0, null, $is_allowed_to_edit, false);
  17. //get all filenames
  18. $array_to_search = is_array($docs_and_folders) ? $docs_and_folders : array();
  19. if (count($array_to_search) > 0) {
  20. while (list($key) = each($array_to_search)) {
  21. $all_files[] = basename($array_to_search[$key]['path']);
  22. }
  23. }
  24. //get all svg and png files
  25. $accepted_extensions = array('.svg', '.png');
  26. if (is_array($all_files) && count($all_files) > 0) {
  27. foreach ($all_files as & $file) {
  28. $slideshow_extension = strrchr($file, '.');
  29. $slideshow_extension = strtolower($slideshow_extension);
  30. if (in_array($slideshow_extension, $accepted_extensions)) {
  31. $png_svg_files[] =$file;
  32. }
  33. }
  34. }
  35. $disk_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/images/gallery/';
  36. $web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document/images/gallery/';
  37. $style = '<style>';
  38. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  39. $style .= '@import "'.api_get_path(WEB_CSS_PATH).api_get_visual_theme().'/default.css";';
  40. $style .='</style>';
  41. ?>
  42. <!doctype html>
  43. <?php echo api_get_js('jquery.min.js'); ?>
  44. <?php echo $style ?>
  45. <body>
  46. <?php
  47. echo '<h2>'.get_lang('Course').': '.$course_info['name'].'</h2>';
  48. if (!empty($png_svg_files)) {
  49. echo '<h3>'.get_lang('SelectSVGEditImage').'</h3>';
  50. echo '<ul>';
  51. foreach($png_svg_files as $filename) {
  52. $image=$disk_path.$filename;
  53. if (strpos($filename, "svg")){
  54. $new_sizes['width'] = 60;
  55. $new_sizes['height'] = 60;
  56. }
  57. else {
  58. $new_sizes = api_resize_image($image, 60, 60);
  59. }
  60. echo '<li style="display:inline; padding:8px;"><a href="'.$web_path.$filename.'" alt "'.$filename.'" title="'.$filename.'"><img src="'.$web_path.$filename.'" width="'.$new_sizes['width'].'" height="'.$new_sizes['height'].'" border="0"></a></li>';
  61. }
  62. echo '</ul>';
  63. } else {
  64. Display::display_warning_message(get_lang('NoSVGImagesInImagesGalleryPath'));
  65. }
  66. ?>
  67. </body>
  68. <script>
  69. $('a').click(function() {
  70. var href = this.href;
  71. // Convert Non-SVG images to data URL first
  72. // (this could also have been done server-side by the library)
  73. if(this.href.indexOf('.svg') === -1) {
  74. var meta_str = JSON.stringify({
  75. name: $(this).text(),
  76. id: href
  77. });
  78. window.top.postMessage(meta_str, "*");
  79. var img = new Image();
  80. img.onload = function() {
  81. var canvas = document.createElement("canvas");
  82. canvas.width = this.width;
  83. canvas.height = this.height;
  84. // load the raster image into the canvas
  85. canvas.getContext("2d").drawImage(this,0,0);
  86. // retrieve the data: URL
  87. try {
  88. var dataurl = canvas.toDataURL();
  89. } catch(err) {
  90. // This fails in Firefox with file:// URLs :(
  91. alert("Data URL conversion failed: " + err);
  92. var dataurl = "";
  93. }
  94. window.top.postMessage('|' + href + '|' + dataurl, "*");
  95. }
  96. img.src = href;
  97. } else {
  98. // Send metadata (also indicates file is about to be sent)
  99. var meta_str = JSON.stringify({
  100. name: $(this).text(),
  101. id: href
  102. });
  103. window.top.postMessage(meta_str, "*");
  104. // Do ajax request for image's href value
  105. $.get(href, function(data) {
  106. data = '|' + href + '|' + data;
  107. // This is where the magic happens!
  108. window.top.postMessage(data, "*");
  109. }, 'html'); // 'html' is necessary to keep returned data as a string
  110. }
  111. return false;
  112. });
  113. </script>