AssetController.php 4.9 KB

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