PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/php/main/inc/lib/svg-edit/extensions/imagelib/groups.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 141 lines | 102 code | 22 blank | 17 comment | 17 complexity | 595343cee585c7ecc0b20ff9c1186a2a MD5 | raw file
  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. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  10. //Add security from Chamilo
  11. api_protect_course_script();
  12. api_block_anonymous_users();
  13. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  14. $course_info = api_get_course_info();
  15. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  16. $groupdirpath = $group_properties['directory'];
  17. $group_disk_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  18. $group_web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document'.$groupdirpath.'/';
  19. //get all group files and folders
  20. $docs_and_folders = DocumentManager::get_all_document_data($course_info, $groupdirpath, api_get_group_id(), null, $is_allowed_to_edit, false);
  21. //get all group filenames
  22. $array_to_search = is_array($docs_and_folders) ? $docs_and_folders : array();
  23. if (count($array_to_search) > 0) {
  24. while (list($key) = each($array_to_search)) {
  25. $all_files[] = basename($array_to_search[$key]['path']);
  26. }
  27. }
  28. //get all svg and png group files
  29. $accepted_extensions = array('.svg', '.png');
  30. if (is_array($all_files) && count($all_files) > 0) {
  31. foreach ($all_files as & $file) {
  32. $slideshow_extension = strrchr($file, '.');
  33. $slideshow_extension = strtolower($slideshow_extension);
  34. if (in_array($slideshow_extension, $accepted_extensions)) {
  35. $png_svg_files[] =$file;
  36. }
  37. }
  38. }
  39. $style = '<style>';
  40. $style .= '@import "'.api_get_path(WEB_CSS_PATH).'base.css";';
  41. $style .= '@import "'.api_get_path(WEB_CSS_PATH).api_get_visual_theme().'/default.css";';
  42. $style .='</style>';
  43. ?>
  44. <!doctype html>
  45. <?php echo api_get_js('jquery.min.js'); ?>
  46. <?php echo $style ?>
  47. <body>
  48. <?php
  49. echo '<h2>'.get_lang('GroupSingle').': '.$group_properties['name'].'</h2>';
  50. if (($group_properties['doc_state'] == 2 && ($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'], $_SESSION['_gid']))) || $group_properties['doc_state'] == 1){
  51. if (!empty($png_svg_files)) {
  52. echo '<h3>'.get_lang('SelectSVGEditImage').'</h3>';
  53. echo '<ul>';
  54. foreach($png_svg_files as $filename) {
  55. $image = $group_disk_path.$filename;
  56. if (strpos($filename, "svg")){
  57. $new_sizes['width'] = 60;
  58. $new_sizes['height'] = 60;
  59. }
  60. else {
  61. $new_sizes = api_resize_image($image, 60, 60);
  62. }
  63. echo '<li style="display:inline; padding:8px;">';
  64. echo '<a href = "'.$group_web_path.$filename.'" alt="'.$filename.'" title="'.$filename.'">';
  65. echo '<img src = "'.$group_web_path.$filename.'" width = "'.$new_sizes['width'].'" height="'.$new_sizes['height'].'" border="0"></a></li>';
  66. }
  67. echo '</ul>';
  68. }
  69. } else {
  70. echo Display::display_warning_message(get_lang('OnlyAccessFromYourGroup'));
  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>