2
0

ProjectTreeController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Project;
  4. use Illuminate\Http\Request;
  5. use App\Http\Api\AuthApi;
  6. use Illuminate\Support\Str;
  7. use App\Http\Api\StudioApi;
  8. use Illuminate\Support\Facades\Log;
  9. class ProjectTreeController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. //
  19. }
  20. /**
  21. * Store a newly created resource in storage.
  22. *
  23. * @param \Illuminate\Http\Request $request
  24. * @return \Illuminate\Http\Response
  25. */
  26. public function store(Request $request)
  27. {
  28. //
  29. $user = AuthApi::current($request);
  30. if (!$user) {
  31. Log::error('notification auth failed {request}', ['request' => $request]);
  32. return $this->error(__('auth.failed'), 401, 401);
  33. }
  34. $studioId = StudioApi::getIdByName($request->get('studio_name'));
  35. if (!ProjectController::canEdit($user['user_uid'], $studioId)) {
  36. return $this->error(__('auth.failed'), 403, 403);
  37. }
  38. $newData = [];
  39. foreach ($request->get('data') as $key => $value) {
  40. $newData[] = [
  41. 'id' => Str::uuid(),
  42. 'old_id' => $value['id'],
  43. 'title' => $value['title'],
  44. 'type' => 'normal',
  45. 'parent_id' => $value['parent_id'],
  46. 'path' => null,
  47. 'owner_id' => $studioId,
  48. 'editor_id' => $user['user_uid'],
  49. 'created_at' => now(),
  50. 'updated_at' => now(),
  51. ];
  52. }
  53. foreach ($newData as $key => $value) {
  54. if ($value['parent_id']) {
  55. $found = array_filter($newData, function ($element) use ($value) {
  56. return $element['old_id'] == $value['parent_id'];
  57. });
  58. if (count($found) > 0) {
  59. $newData[$key]['parent_id'] = $found[0]['id'];
  60. $parentPath = $found[0]['path'] ? json_decode($found[0]['path']) : [];
  61. $newData[$key]['path'] = json_encode([...$parentPath, $found[0]['id']], JSON_UNESCAPED_UNICODE);
  62. } else {
  63. $newData[$key]['parent_id'] = null;
  64. }
  65. }
  66. }
  67. foreach ($newData as $key => $value) {
  68. unset($newData[$key]['old_id']);
  69. }
  70. $leafs = [];
  71. foreach ($newData as $key => $value) {
  72. $children = array_filter($newData, function ($element) use ($value) {
  73. return $element['parent_id'] === $value['id'];
  74. });
  75. if (count($children) === 0) {
  76. $leafs[] = $value['id'];
  77. }
  78. }
  79. $ok = Project::insert($newData);
  80. if ($ok) {
  81. return $this->ok(['leafs' => $leafs]);
  82. } else {
  83. return $this->error('error', 200, 200);
  84. }
  85. }
  86. /**
  87. * Display the specified resource.
  88. *
  89. * @param \App\Models\Project $project
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function show(Project $project)
  93. {
  94. //
  95. }
  96. /**
  97. * Update the specified resource in storage.
  98. *
  99. * @param \Illuminate\Http\Request $request
  100. * @param \App\Models\Project $project
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function update(Request $request, Project $project)
  104. {
  105. //
  106. }
  107. /**
  108. * Remove the specified resource from storage.
  109. *
  110. * @param \App\Models\Project $project
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function destroy(Project $project)
  114. {
  115. //
  116. }
  117. }