FolderController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Folder\CreateRequest;
  5. use App\Http\Requests\API\Folder\UpdateRequest;
  6. use App\Http\Resources\API\FileByObjectResource;
  7. use App\Http\Resources\API\FolderDetailResource;
  8. use App\Models\Enums\FolderObjectType;
  9. use App\Models\File;
  10. use App\Models\Folder;
  11. use App\Models\Library;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\DB;
  15. class FolderController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. */
  20. public function tree(string $objectType, string $objectId)
  21. {
  22. $folderObjectType = FolderObjectType::from($objectType);
  23. $folderObjectType->modelBuilderAllowed()->findOrFail($objectId);
  24. $folders = Folder::query()
  25. ->where([
  26. 'object_type' => $objectType,
  27. 'object_id' => $objectId,
  28. ])
  29. ->when(request("parent_id", 0) > 0, function ($query) {
  30. return $query->where("path", "like", "%," . \request("parent_id") . ",%")->where("id", "!=", \request("parent_id"));
  31. })
  32. ->orderByDesc("sequence")
  33. ->get([
  34. 'id',
  35. 'name',
  36. 'parent_id'
  37. ])
  38. ->each(function ($folders){
  39. $folders->type='folder';
  40. });
  41. return $this->success([
  42. 'data' => make_tree($folders->toArray(), \request("parent_id", 0)),
  43. ]);
  44. }
  45. /**
  46. * Store a newly created resource in storage.
  47. */
  48. public function store(CreateRequest $request)
  49. {
  50. $folderObjectType = FolderObjectType::from($request->object_type);
  51. $object = $folderObjectType->modelBuilderAllowed()->findOrFail($request->object_id);
  52. $objectWhere = [
  53. 'object_type' => $folderObjectType->value, 'object_id' => $object->id
  54. ];
  55. $parentFolder = $request->parent_id > 0
  56. ? Folder::query()->where($objectWhere)->findOrFail($request->parent_id)
  57. : null;
  58. $names = [];
  59. $updateFolders = [];
  60. foreach ($request->items as $item) {
  61. if (! isset($item['name']) || !$item['name']) {
  62. return $this->badRequest("Folder name cannot be empty");
  63. }
  64. if (in_array($item['name'], $names)) {
  65. return $this->badRequest("Folder names must be unique");
  66. }
  67. $names[] = $item['name'];
  68. $isUpdate = isset($item['id']) && $item['id'];
  69. $count = Folder::query()
  70. ->where($objectWhere)
  71. ->where("parent_id", $request->parent_id)
  72. ->when($isUpdate, function ($query) use ($item) {
  73. return $query->where("id", "!=", $item['id']);
  74. })
  75. ->where("name", $item['name'])
  76. ->count();
  77. if ($count > 0) {
  78. return $this->badRequest(sprintf("Folder '%s' must be unique", $item['name']));
  79. }
  80. if ($isUpdate) {
  81. $folder = Folder::query()
  82. ->where($objectWhere)
  83. ->where("parent_id", $request->parent_id)
  84. ->find($item['id']);
  85. if (! $folder) {
  86. return $this->badRequest("Illegal parameters or the file relationship that needs to be updated has changed.");
  87. }
  88. $updateFolders[$item['id']] = $folder;
  89. }
  90. }
  91. foreach ($request->items as $item) {
  92. $isUpdate = isset($item['id']) && $item['id'];
  93. $data = [
  94. 'name' => $item['name'],
  95. 'sequence' => data_get($item, "sequence", 0),
  96. ];
  97. if ($isUpdate) {
  98. $folder = $updateFolders[$item['id']];
  99. $folder->fill($data);
  100. $folder->save();
  101. } else {
  102. $folder = Folder::query()->create([
  103. 'company_id' => Auth::user()->company_id,
  104. ...$objectWhere,
  105. 'parent_id' => $request->parent_id,
  106. ...$data
  107. ]);
  108. $folder->path = $parentFolder ? $parentFolder?->path . $folder->id . "," : sprintf(",%s,", $folder->id);
  109. $folder->save();
  110. }
  111. }
  112. return $this->created();
  113. }
  114. /**
  115. * Display the specified resource.
  116. */
  117. public function show(string $id)
  118. {
  119. $folder = Folder::query()->findOrFail($id);
  120. return new FolderDetailResource($folder);
  121. }
  122. /**
  123. * Update the specified resource in storage.
  124. */
  125. public function update(UpdateRequest $request, string $id)
  126. {
  127. $folder = Folder::query()->findOrFail($id);
  128. $folderObjectType = FolderObjectType::from($folder->object_type);
  129. $object = $folderObjectType->modelBuilderAllowed()->findOrFail($folder->object_id);
  130. $objectWhere = [
  131. 'object_type' => $folderObjectType->value, 'object_id' => $object->id
  132. ];
  133. $parentFolder = Folder::query()->where($objectWhere)->findOrFail($request->parent_id ?? $folder->id);
  134. if (! $parentFolder) {
  135. return $this->badRequest("Parent folder does not exist");
  136. }
  137. $count = Folder::query()
  138. ->where($objectWhere)
  139. ->where("parent_id", $request->parent_id)
  140. ->where("name", $request->name)
  141. ->where("id", "!=", $folder->id)
  142. ->count();
  143. if ($count > 0) {
  144. return $this->badRequest(sprintf("Folder '%s' must be unique", $request->name));
  145. }
  146. $fields = ['name', 'sequence'];
  147. if ($parentFolder?->id == $folder->parent_id) {
  148. $folder->fill($request->only($fields));
  149. $folder->save();
  150. } else {
  151. $path = $parentFolder->path . $folder->id . ",";
  152. $folderData = [
  153. ...$request->only($fields),
  154. 'path' => $path,
  155. 'parent_id' => $request->parent_id,
  156. ];
  157. $children = Folder::query()
  158. ->where($objectWhere)
  159. ->where("id", "!=", $folder->id)
  160. ->where("path", "like", "%," . $folder->id . ",%")
  161. ->get();
  162. foreach ($children as $child) {
  163. $child->fill([
  164. 'path' => str_replace($folder->path, $path, $child->path),
  165. ]);
  166. $child->save();
  167. }
  168. $folder->fill($folderData);
  169. $folder->save();
  170. }
  171. return $this->noContent();
  172. }
  173. /**
  174. * Remove the specified resource from storage.
  175. */
  176. public function destroy(string $id)
  177. {
  178. $folder = Folder::query()->findOrFail($id);
  179. $folderObjectType = FolderObjectType::from($folder->object_type);
  180. $folderObjectType->modelBuilderAllowed()->findOrFail($folder->object_id);
  181. $children = Folder::query()->where("parent_id", $folder->id)->count();
  182. if ($children > 0) {
  183. return $this->badRequest("Subordinate folders or containers exist and are not allowed to be deleted.");
  184. }
  185. $folder->delete();
  186. return $this->noContent();
  187. }
  188. public function open(Request $request)
  189. {
  190. $folderId = $request->get("id", 0);
  191. if ($folderId > 0) {
  192. $folder = Folder::query()->findOrFail($folderId);
  193. $objectType = $folder->object_type;
  194. $objectId = $folder->object_id;
  195. } else {
  196. $objectType = $request->get("object_type");
  197. $objectId = $request->get("object_id");
  198. }
  199. $folderObjectType = FolderObjectType::from($request->object_type);
  200. $folderObjectType->modelBuilderAllowed()->findOrFail($request->object_id);
  201. $objectWhere = ['object_type' => $objectType, 'object_id' => $objectId,];
  202. $folders = Folder::query()
  203. ->where($objectWhere)
  204. ->when($folderId, fn($query) => $query->where("parent_id", $folderId))
  205. ->when(! $folderId, fn($query) => $query->where("parent_id", 0))
  206. ->get(['id', 'name']);
  207. $files = File::query()->where($objectWhere)
  208. ->where("folder_id", $folderId)
  209. ->where("is_latest_version", 1)
  210. ->get();
  211. return $this->success([
  212. 'data' => [
  213. 'folders' => $folders,
  214. 'files' => FileByObjectResource::collection($files),
  215. ]
  216. ]);
  217. }
  218. }