index.php 3.8 KB

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