index.php 3.7 KB

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