AssetController.php 5.8 KB

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