File.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class File {
  3. public static function manifest(){
  4. return array(
  5. 'uid' => 'files',
  6. 'label' => 'Set de fichiers',
  7. 'description' => 'Ensemble de fichiers attachés',
  8. 'fromExtension' => array('zip','7z'),
  9. 'toExtension' => 'zip',
  10. 'upload' => array(
  11. 'url'=>'action.php?action=upload_resource_file',
  12. 'element' => '#dropZoneFiles',
  13. 'callback' => "search_file();",
  14. )
  15. );
  16. }
  17. //Import depuis un glisser déposé du fichier
  18. public static function fromFile($resource){
  19. $resource->save();
  20. $path = SKETCH_PATH.'/'.$resource->id;
  21. if(!file_exists($path)) mkdir($path);
  22. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$resource->label.'-'.time();
  23. if(file_exists($filepath))unlink($filepath);
  24. file_put_contents($filepath, $resource->content);
  25. $zip = new ZipArchive;
  26. $res = $zip->open($filepath);
  27. if ($res === TRUE) {
  28. $zip->extractTo($path);
  29. $zip->close();
  30. }
  31. $resource->content = '';
  32. return $resource;
  33. }
  34. //Import depuis un flux json compressé de la ressource
  35. public static function fromJson($resource){
  36. $files = $resource->content;
  37. $resource->content = '';
  38. $resource->save();
  39. $folder = SKETCH_PATH.'/'.$resource->id;
  40. if(!file_exists($folder)) mkdir($folder);
  41. foreach($files as $file):
  42. $stream = base64_decode($file['stream']);
  43. file_put_contents($folder.'/'.$file['label'],$stream);
  44. endforeach;
  45. return $resource;
  46. }
  47. //export en fichier JSON compressé de la ressource
  48. public static function toJson($resource){
  49. $resource = $resource->toArray();
  50. $resource->content = array();
  51. $folder = SKETCH_PATH.'/'.$resource->id;
  52. foreach(glob($folder.'/*') as $file):
  53. $resource->content[] = array('label'=>basename($file),'stream'=>base64_encode(file_get_contents($file)));
  54. endforeach;
  55. return $resource;
  56. }
  57. public static function toFile($resource){
  58. global $myUser;
  59. $infos = self::manifest();
  60. $path = SKETCH_PATH.'/'.$resource->id;
  61. $filename = $resource->label.'-'.time().'.'.$infos['toExtension'];
  62. $filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename;
  63. $zip = new ZipArchive;
  64. if(file_exists($filepath))unlink($filepath);
  65. $res = $zip->open($filepath, ZipArchive::CREATE);
  66. if ($res === TRUE) {
  67. foreach(glob($path.'/*') as $f)
  68. $zip->addFile($f,basename($f));
  69. $zip->close();
  70. }
  71. return array(
  72. 'name'=> slugify($resource->label).'.'.$infos['toExtension'],
  73. 'content' => file_get_contents($filepath)
  74. );
  75. }
  76. public static function toHtml($resource,$sketch){
  77. global $myUser;
  78. $infos = self::manifest();
  79. if($myUser->id == $sketch->owner){
  80. $response['upload'] = $infos['upload'];
  81. $response['content'] = '<div id="dropZoneFiles"><i class="fa fa-file-text-o"></i> Faites glisser vos fichiers ici</div>';
  82. }
  83. $response['callback'] = 'init_file();';
  84. $response['content'] .= '<table class="table table-stripped table-bordered" id="files"><thead>
  85. <tr>
  86. <th>Fichiers disponibles <a class="btn btn-primary" href="action.php?action=download_file&resource='.$resource->id.'" style="float:right;" title="Télécharger les fichiers"><i class="fa fa-file-zip-o"></i> Télécharger</a></th>';
  87. $response['content'] .= '<th style="width:50px;"></th>';
  88. $response['content'] .= '</tr></thead><tbody>';
  89. $response['content'] .= '<tr style="display:none" data-id="{{id}}">
  90. <td ><a href="action.php?action=get_resource_file&id={{resource}}&file={{label}}"><i class="fa {{icon}}"></i> {{label}}</a></td>';
  91. $response['content'] .= '<td>';
  92. if($myUser->id == $sketch->owner)
  93. $response['content'] .= '<div class="btn btn-danger btn-mini btn-rounded pulse" onclick="delete_file(this);"><i class="fa fa-times" ></i></div>';
  94. $response['content'] .= '</td>';
  95. $response['content'] .= '</tr>';
  96. $response['content'] .='</tbody></table>';
  97. return $response;
  98. }
  99. }
  100. ?>