PartType.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class PartType {
  3. public static function manifest(){
  4. return array(
  5. 'uid' => 'part',
  6. 'label' => 'Set de composants',
  7. 'description' => 'Ensemble de composants éléctroniques (ou autres)',
  8. 'fromExtension' => array('part'),
  9. 'toExtension' => 'txt'
  10. );
  11. }
  12. //Import depuis un glisser déposé du fichier
  13. public static function fromFile($resource){
  14. //TODO
  15. return $resource;
  16. }
  17. //Import depuis un flux json compressé de la ressource
  18. public static function fromJson($resource){
  19. global $myUser;
  20. $parts = $resource->content;
  21. $resource->content = '';
  22. foreach($parts as $p):
  23. $part = new Part();
  24. $part->fromArray($p['part']);
  25. $part->id = null;
  26. $stream = base64_decode($part->image);
  27. $part->owner = $myUser->id;
  28. $part->save();
  29. $name = $part->id.'.png';
  30. file_put_contents(PART_PATH.$name,$stream);
  31. $part->image = $name;
  32. $part->save();
  33. $resourcePart = new ResourcePart();
  34. $resourcePart->fromArray($p['resourcePart']);
  35. $resourcePart->id = null;
  36. $resourcePart->part = $part->id;
  37. $resourcePart->resource = $resource->id;
  38. $resourcePart->save();
  39. endforeach;
  40. $resource->content = '';
  41. return $resource;
  42. }
  43. //export en fichier JSON compressé de la ressource
  44. public static function toJson($resource){
  45. $resource = $resource->toArray();
  46. $resource['content'] = array();
  47. foreach(ResourcePart::loadAll(array('resource'=>$resource['id'])) as $resourcePart):
  48. $part = $resourcePart->part_object;
  49. $part = $part->toArray();
  50. $resourcePart = $resourcePart->toArray();
  51. if($part['image']!='') $part['image'] = base64_encode(file_get_contents(PART_PATH.$part['image']));
  52. $resource['content'][] = array('resourcePart'=>$resourcePart,'part'=>$part);
  53. endforeach;
  54. return $resource;
  55. }
  56. public static function toFile($resource){
  57. global $myUser;
  58. $infos = self::manifest();
  59. $content = '> '.strtoupper($resource->label).PHP_EOL;
  60. $content .= str_repeat('=',strlen($resource->label)+2).PHP_EOL;
  61. foreach(ResourcePart::loadAll(array('resource'=>$resource->id)) as $resourcePart):
  62. $part = $resourcePart->part_object;
  63. $content .= $part->label."\t";
  64. if(isset($part->link) && !empty($part->link)) $content .= $part->link."\t";
  65. if(isset($part->brand) && !empty($part->brand)) $content .= '('.$part->brand.")\t";
  66. if(isset($part->price) && !empty($part->price)) $content .= $part->price.'€';
  67. $content .= PHP_EOL;
  68. endforeach;
  69. return array(
  70. 'name'=> slugify($resource->label).'.'.$infos['toExtension'],
  71. 'content' => $content
  72. );
  73. }
  74. public static function toHtml($resource,$sketch){
  75. global $myUser;
  76. $response['callback'] = 'init_part();';
  77. $response['content'] = '<table class="table table-stripped table-bordered" id="parts"><thead>
  78. <tr>
  79. <th>Libellé</th>
  80. <!--<th>Lien</th>
  81. <th>Prix</th>-->';
  82. if($myUser->id == $sketch->owner)
  83. $response['content'] .= '<th></th>';
  84. $response['content'] .= '</tr>';
  85. if($myUser->id == $sketch->owner){
  86. $response['content'] .= '<tr id="partForm" data-action="save_part" data-id="">
  87. <td><input type="text" id="label" class="form-control"></td>
  88. <!--<td><input type="url" id="link" class="form-control"></td>
  89. <td><input type="text" id="price" class="form-control input-mini"></td>-->
  90. <td><div class="btn btn-success" onclick="save_part();"><i class="fa fa-plus"></i></div></td>
  91. </tr>';
  92. }
  93. $response['content'] .= '</thead><tbody>';
  94. $response['content'] .= '<tr style="display:none" data-id="{{id}}">
  95. <td ><a href="{{link}}"><div class="componentImage"><img src="{{image}}"/></div> {{label}}</a> {{#price}}<code>{{price}} €</code>{{/price}}{{#brand}} <small>{{brand}}</small>{{/brand}}</td>';
  96. if($myUser->id == $sketch->owner)
  97. $response['content'] .= '<td><div class="btn btn-danger btn-mini btn-rounded pulse" onclick="delete_part(this);"><i class="fa fa-times" ></i></div></td>';
  98. $response['content'] .= '</tr>';
  99. $response['content'] .='</tbody></table>';
  100. return $response;
  101. }
  102. }
  103. ?>