FritzingType.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. class FritzingType {
  3. public static function manifest(){
  4. return array(
  5. 'uid' => 'fritzing',
  6. 'label' => 'Schéma fritzing',
  7. 'description' => 'Fichier pour le logiciel fritzing',
  8. 'fromExtension' => array('fzz'),
  9. 'toExtension' => 'fzz',
  10. 'upload' => array(
  11. 'url' => 'action.php?action=upload_resource_file',
  12. 'element' => '#dropZoneFiles',
  13. 'callback' => "refresh_fritzing();",
  14. )
  15. );
  16. }
  17. //Import depuis un glisser déposé du fichier
  18. public static function fromFile($resource){
  19. $resource->save();
  20. $ext = getExt($resource->label);
  21. $name = $resource->id.'.'.$ext;
  22. file_put_contents(SKETCH_PATH.$name,$resource->content);
  23. $resource->content = $name;
  24. return $resource;
  25. }
  26. //Import depuis un flux json compressé de la ressource
  27. public static function fromJson($resource){
  28. $resource->save();
  29. $stream = base64_decode($resource->content);
  30. $resource->content = $resource->id.'.png';
  31. file_put_contents(SKETCH_PATH.$resource->content,$stream);
  32. return $resource;
  33. }
  34. //export en fichier JSON compressé de la ressource
  35. public static function toJson($resource){
  36. $resource = $resource->toArray();
  37. $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
  38. return $resource;
  39. }
  40. public static function toFile($resource){
  41. $ext = getExt($resource->content);
  42. $infos = self::manifest();
  43. return array(
  44. 'name'=> slugify($resource->label).'.'.$ext,
  45. 'content' => file_exists(SKETCH_PATH.$resource->content) ? file_get_contents(SKETCH_PATH.$resource->content) : ''
  46. );
  47. }
  48. public static function toHtml($resource,$sketch){
  49. global $myUser;
  50. $infos = self::manifest();
  51. require_once(__DIR__.SLASH."Fritzing.class.php");
  52. if(!file_exists(SKETCH_PATH.$resource->id)){
  53. $content = '<div id="dropZoneFiles" style="width:100%;height:auto;" class="dropzone">Envoyer un fichier</div>';
  54. }else{
  55. $file = glob(SKETCH_PATH.$resource->id.'/*.fzz');
  56. $breadboard = new Fritzing($file[0]);
  57. $content = '<div id="dropZoneFiles" style="width:100%;height:auto;" class="dropzone"><a href="action.php?action=get_resource&id='.$resource->id.'">Télécharger</a></div>';
  58. $content .= '<h1>Schéma</h1>'.$breadboard->toHtml();
  59. if($breadboard->ino!=null)$content .= '<h1>Code associé</h1><textarea>'.$breadboard->ino.'</textarea>';
  60. if($breadboard->comment!=null)$content .= $breadboard->comment;
  61. }
  62. $response['content'] = $content;
  63. $response['codemirror'] = array(
  64. 'mode'=>'text/x-carduino',
  65. 'theme'=>'monokai',
  66. 'lineNumbers' => true,
  67. 'readOnly' => true
  68. );
  69. if($myUser->id == $sketch->owner)
  70. $response['upload'] = $infos['upload'];
  71. return $response;
  72. }
  73. }
  74. ?>