AssetController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $fullAsset=[];
  21. $paths = Asset::filter($request->all())
  22. ->where('company_id',Auth::user()->company_id)
  23. ->allowed()->pluck('path')->toArray();
  24. foreach ($paths as $path){
  25. $parentAssets = explode(',', substr($path, 1, -1));
  26. $fullAsset= array_merge($parentAssets,$fullAsset);
  27. }
  28. $resultAssets = Asset::whereIn('id', $fullAsset)->get()->each(function ($asset) {
  29. $asset->requirement_total = $asset->total_requirements_count;
  30. $asset->plan_total = $asset->total_plans_count;
  31. });
  32. return $this->success([
  33. 'data' => make_tree($resultAssets->toArray())
  34. ]);
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. */
  39. public function store(CreateOrUpdateRequest $request)
  40. {
  41. $asset = Asset::create([
  42. ...$request->all(),
  43. 'company_id' => Auth::user()->company_id,
  44. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  45. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  46. 'created_by' => Auth::id(),
  47. ]);
  48. $parentAsset = $request->parent_id > 0
  49. ? Asset::query()->findOrFail($request->parent_id)
  50. : null;
  51. $asset->path = $parentAsset ? $parentAsset?->path . $asset->id . "," : sprintf(",%s,", $asset->id);
  52. $asset->save();
  53. return $this->created();
  54. }
  55. /**
  56. * Display the specified resource.
  57. */
  58. public function show(string $id)
  59. {
  60. $asset = Asset::allowed()->findOrFail($id);
  61. return new AssetResource($asset);
  62. }
  63. /**
  64. * Update the specified resource in storage.
  65. */
  66. public function update(Request $request, string $id)
  67. {
  68. $asset = Asset::allowed()->findOrFail($id);
  69. $parentAsset = $request->parent_id > 0
  70. ? Asset::query()->findOrFail($request->parent_id)
  71. : null;
  72. $isUpdateParent = $parentAsset?->id != $asset->parent_id;
  73. $path = $isUpdateParent ? $parentAsset->path . $asset->id . "," : $asset->path;
  74. $formData = [
  75. ...$request->all(),
  76. 'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
  77. 'description' => $request->description? (new \App\Services\File\ImageUrlService)->interceptImageUrl($request->description) : null,
  78. 'path' => $path
  79. ];
  80. if ($isUpdateParent) {
  81. $children = Asset::query()
  82. ->where("id", "!=", $asset->id)
  83. ->where("path", "like", "%," . $asset->id . ",%")
  84. ->get();
  85. foreach ($children as $child) {
  86. $child->fill([
  87. 'path' => str_replace($asset->path, $path, $child->path),
  88. ]);
  89. $child->save();
  90. }
  91. }
  92. $asset->fill($formData);
  93. $asset->save();
  94. return $this->noContent();
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. */
  99. public function destroy(string $id)
  100. {
  101. $asset = Asset::allowed()->findOrFail($id);
  102. if(!empty($asset->children()->first())){
  103. throw new \Exception("Please remove the sub-level assets.");
  104. }
  105. $asset->delete();
  106. return $this->noContent();
  107. }
  108. public function report(string $id){
  109. $asset = Asset::allowed()->with(['requirements','plans','projects','children'])->findOrFail($id);
  110. //$asset = Asset::find($id);
  111. $assets=Asset::query()->where("path","like","%,".$id.",%")->pluck("id");
  112. $asset->setAttribute('child_id',$assets);
  113. return new AssetReportResource($asset);
  114. }
  115. public function tree()
  116. {
  117. $attachmentArray = [];
  118. $assets = Asset::allowed()->get(['id', 'name', 'parent_id'])->each(function ($assets)use (&$attachmentArray){
  119. $attachmentArray[]=[
  120. 'parent_id'=>$assets->id,
  121. 'type'=>'attachment',
  122. 'name'=>'attachment',
  123. 'uuId'=>'attachment_'.$assets->id,
  124. 'id'=>'asset_id_'.$assets->id,
  125. ];
  126. $assets->type='asset';
  127. $assets->uniId=$assets->type.'_'.$assets->id;
  128. });
  129. return $this->success([
  130. 'data' => make_tree(array_merge($assets->toArray(),$attachmentArray))
  131. ]);
  132. }
  133. }