AssetController.php 5.7 KB

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