index.php 3.8 KB

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