FritzingType.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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=fritzing_upload_resource',
  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. $file =SKETCH_PATH.$resource->id.'.fzz';
  53. if(!file_exists($file)){
  54. $content = '<div id="dropZoneFiles" style="width:100%;height:auto;" class="dropzone"> <img src="plugin/fritzing/img/fritzing.png" /><br>Envoyer un fritzing</div>';
  55. }else{
  56. $breadboard = new Fritzing($file);
  57. $content = '<div id="dropZoneFiles" style="width:100%;height:auto;" class="dropzone"></div>';
  58. $content .= '<h1>Schéma</h1>'.$breadboard->toHtml();
  59. if($breadboard->ino!=null){
  60. $content .= '<h1>Code associé</h1><textarea>'.$breadboard->ino.'</textarea>';
  61. $response['codemirror'] = array(
  62. 'mode'=>'text/x-carduino',
  63. 'theme'=>'monokai',
  64. 'lineNumbers' => true,
  65. 'readOnly' => true
  66. );
  67. }
  68. $content .= '<h1>Composants</h1><ul class="list-group">';
  69. foreach ($breadboard->parts as $part) {
  70. if($part['type'] == 'component' && isset($part['component']['name'])){
  71. $content .= '<li class="list-group-item"><strong>'.$part['sigle'].' :</strong> '. $part['component']['name'].' ('. $part['component']['description'].')</li>';
  72. }
  73. }
  74. $content .= '</ul>';
  75. }
  76. $response['content'] = $content;
  77. if($myUser->id == $sketch->owner)
  78. $response['upload'] = $infos['upload'];
  79. return $response;
  80. }
  81. }
  82. ?>