AssetController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Asset\CreateOrUpdateRequest;
  5. use App\Http\Resources\API\AssetReportResource;
  6. use App\Http\Resources\API\AssetResource;
  7. use App\Models\Asset;
  8. use App\Models\File;
  9. use App\Models\User;
  10. use App\Services\File\ImageUrlService;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. class AssetController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. */
  18. public function index(Request $request)
  19. {
  20. $pageSize=$request->get('page_size') ?? 10;
  21. $assets = Asset::filter($request->all())
  22. ->where("parent_id", 0)
  23. ->where('company_id',Auth::user()->company_id)
  24. ->allowed()
  25. ->with(['children','children.children'])
  26. ->paginate($pageSize);
  27. return AssetResource::collection($assets);
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. */
  32. public function store(CreateOrUpdateRequest $request)
  33. {
  34. $asset = Asset::create([
  35. ...$request->all(),
  36. 'company_id' => Auth::user()->company_id,
  37. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  38. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  39. 'created_by' => Auth::id(),
  40. ]);
  41. $parentAsset = $request->parent_id > 0
  42. ? Asset::query()->findOrFail($request->parent_id)
  43. : null;
  44. $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id);
  45. $asset->save();
  46. return $this->created();
  47. }
  48. /**
  49. * Display the specified resource.
  50. */
  51. public function show(string $id)
  52. {
  53. $asset = Asset::allowed()->findOrFail($id);
  54. return new AssetResource($asset);
  55. }
  56. /**
  57. * Update the specified resource in storage.
  58. */
  59. public function update(Request $request, string $id)
  60. {
  61. $asset = Asset::allowed()->findOrFail($id);
  62. $parentAsset = $request->parent_id > 0
  63. ? Asset::query()->findOrFail($request->parent_id)
  64. : null;
  65. $isUpdateParent = $parentAsset?->id != $asset->parent_id;
  66. $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path;
  67. $formData = [
  68. ...$request->all(),
  69. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  70. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  71. 'path' => $path
  72. ];
  73. if ($isUpdateParent) {
  74. $children = Asset::query()
  75. ->where("id", "!=", $asset->id)
  76. ->where("path", "like", "%," . $asset->id . ",%")
  77. ->get();
  78. foreach ($children as $child) {
  79. $child->fill([
  80. 'path' => str_replace($asset->path, $path, $child->path),
  81. ]);
  82. $child->save();
  83. }
  84. }
  85. $asset->fill($formData);
  86. $asset->save();
  87. return $this->noContent();
  88. }
  89. /**
  90. * Remove the specified resource from storage.
  91. */
  92. public function destroy(string $id)
  93. {
  94. $asset = Asset::allowed()->findOrFail($id);
  95. if(!empty($asset->children()->first())){
  96. throw new \Exception("Please remove the sub-level assets.");
  97. }
  98. $asset->delete();
  99. return $this->noContent();
  100. }
  101. public function report(string $id){
  102. $asset = Asset::allowed()->with(['requirements','plans','projects','children'])->findOrFail($id);
  103. //$asset = Asset::find($id);
  104. $assets=Asset::query()->where("path","like","%,".$id.",%")->pluck("id");
  105. $asset->setAttribute('child_id',$assets);
  106. return new AssetReportResource($asset);
  107. }
  108. public function tree()
  109. {
  110. $attachmentArray = [];
  111. $assets = Asset::allowed()->get(['id', 'name', 'parent_id'])->each(function ($assets)use (&$attachmentArray){
  112. $attachmentArray[]=[
  113. 'parent_id'=>$assets->id,
  114. 'type'=>'attachment',
  115. 'name'=>'attachment',
  116. 'uuId'=>'attachment_'.$assets->id,
  117. 'id'=>'asset_id_'.$assets->id,
  118. ];
  119. $assets->type='asset';
  120. $assets->uniId=$assets->type.'_'.$assets->id;
  121. });
  122. return $this->success([
  123. 'data' => make_tree(array_merge($assets->toArray(),$attachmentArray))
  124. ]);
  125. }
  126. }