PartType.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. class PartType {
  3. public static function manifest(){
  4. return array(
  5. 'uid' => 'part',
  6. 'label' => 'Composants',
  7. 'description' => 'Composants électroniques',
  8. 'fromExtension' => array('part'),
  9. 'toExtension' => 'md',
  10. 'icon' => 'fas fa-microchip',
  11. 'color' => '#ffffff',
  12. 'background' => '#2ecc71'
  13. );
  14. }
  15. //Import depuis un glisser déposé du fichier
  16. public static function fromFile($resource){
  17. // $resource->save();
  18. // $ext = getExt($resource->label);
  19. // $name = $resource->id.'.'.$ext;
  20. // file_put_contents(SKETCH_PATH.$name,$resource->content);
  21. // $resource->content = $name;
  22. // return $resource;
  23. }
  24. //Import depuis un flux json compressé de la ressource
  25. public static function fromJson($resource){
  26. // $resource->save();
  27. // $stream = base64_decode($resource->content);
  28. // $resource->content = $resource->id.'.png';
  29. // file_put_contents(SKETCH_PATH.$resource->content,$stream);
  30. // return $resource;
  31. }
  32. //export en fichier JSON compressé de la ressource
  33. public static function toJson($resource){
  34. // $resource = $resource->toArray();
  35. // $resource['content'] = base64_encode(file_get_contents(SKETCH_PATH.$resource['content']));
  36. // return $resource;
  37. }
  38. public static function toFile($resource){
  39. require_once(__DIR__.SLASH.'ResourcePart.class.php');
  40. require_once(__DIR__.SLASH.'Part.class.php');
  41. global $myUser;
  42. $infos = self::manifest();
  43. $content = '# '.strtoupper($resource->label).PHP_EOL.PHP_EOL;
  44. foreach(ResourcePart::loadAll(array('resource'=>$resource->id), null, null, array('*'), 1) as $resourcePart):
  45. $part = $resourcePart->join('part');
  46. $content .= '## '.$part->label.PHP_EOL;
  47. if(isset($part->link) && !empty($part->link)) $content .= "\t".'* Url: ' .$part->link.PHP_EOL;
  48. if(isset($part->brand) && !empty($part->brand)) $content .="\t".'* Marque: ' .$part->brand.PHP_EOL;
  49. if(isset($part->price) && !empty($part->price)) $content .="\t".'* Prix moyen: '.$part->price.'€'.PHP_EOL;
  50. $content .= '---';
  51. $content .= PHP_EOL;
  52. endforeach;
  53. return array(
  54. 'name'=> slugify($resource->label).'.'.$infos['toExtension'],
  55. 'content' => $content
  56. );
  57. }
  58. /* EDITION */
  59. public static function toHtml($resource,$sketch=null){
  60. $infos = self::manifest();
  61. $response = array();
  62. //.$resource->id
  63. $images = array();
  64. foreach(glob($resource->directory().SLASH.'*.*') as $file){
  65. $images[] = array(
  66. 'path' => 'hackpoint'.SLASH.'sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.basename($file),
  67. 'url' => 'action.php?action=hackpoint_download_file&file='.base64_encode('sketch'.SLASH.$resource->sketch.SLASH.$resource->id.SLASH.rawurlencode(basename($file))),
  68. 'name' => basename($file),
  69. 'icon' => getExtIcon(getExt($file))
  70. );
  71. }
  72. $response['html'] = '
  73. <div class="row">
  74. <div class="col-md-12">
  75. <div class="btn btn-dark btn-add-part" onclick="part_part_add()"><i class="far fa-plus-square pr-1"></i> Ajouter un composant</div>
  76. <ul id="parts" data-entity-search="part_resource_part_search">
  77. <li data-id="{{id}}" data-part="{{part}}" class="hidden">
  78. <div class="part">
  79. <input type="text" data-type="part" data-force="false" placeholder="Nom du composant" value="{{label}}" class="form-control label">
  80. <div class="part-image" data-label="Image" data-stream="{{picture}}"></div>
  81. <input class="hidden" type="file">
  82. <div class="price w-100 mt-2">
  83. <div class="input-group">
  84. <div class="input-group-prepend">
  85. <span class="input-group-text">Prix</span>
  86. </div>
  87. <input class="form-control" type="text" value="{{price}}">
  88. <div class="input-group-append">
  89. <span class="input-group-text">€</span>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="brand w-100 mt-2 mb-3">
  94. <div class="input-group">
  95. <div class="input-group-prepend">
  96. <span class="input-group-text">Marque</span>
  97. </div>
  98. <input class="form-control" type="text" value="{{brand}}"/>
  99. </div>
  100. </div>
  101. <a onclick="part_part_find(this)" title="Chercher sur le net" class="link"><i class="fas fa-globe-americas pointer"></i></a>
  102. <div class="btn btn-delete" onclick="part_resource_part_delete(this);"><i class="far fa-trash-alt"></i></div>
  103. <div class="clear"></div>
  104. <div class="btn btn-dark w-100 mb-3 btn-save-part" onclick="part_part_save(this)"><i class="far fa-check-circle"></i>Valider</div>
  105. </div>
  106. </li>
  107. </ul>
  108. </div>
  109. </div>
  110. ';
  111. $response['javascript'] = 'part_resource_part_search();';
  112. return $response;
  113. }
  114. }
  115. ?>