ProjectTreeController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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' => $value['type'],
  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. } else if (!empty($request->get('parent_id'))) {
  66. $parentPath = json_decode(Project::find($request->get('parent_id'))->value('path'));
  67. if (!is_array($parentPath)) {
  68. $parentPath = [];
  69. }
  70. $newData[$key]['path'] = json_encode([...$parentPath, $request->get('parent_id')], JSON_UNESCAPED_UNICODE);
  71. $newData[$key]['parent_id'] = $request->get('parent_id');
  72. }
  73. }
  74. foreach ($newData as $key => $value) {
  75. unset($newData[$key]['old_id']);
  76. }
  77. $leafs = [];
  78. foreach ($newData as $key => $value) {
  79. $children = array_filter($newData, function ($element) use ($value) {
  80. return $element['parent_id'] === $value['id'];
  81. });
  82. if (count($children) === 0) {
  83. $leafs[] = $value['id'];
  84. }
  85. }
  86. $ok = Project::insert($newData);
  87. if ($ok) {
  88. return $this->ok(['leafs' => $leafs]);
  89. } else {
  90. return $this->error('error', 200, 200);
  91. }
  92. }
  93. /**
  94. * Display the specified resource.
  95. *
  96. * @param \App\Models\Project $project
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function show(Project $project)
  100. {
  101. //
  102. }
  103. /**
  104. * Update the specified resource in storage.
  105. *
  106. * @param \Illuminate\Http\Request $request
  107. * @param \App\Models\Project $project
  108. * @return \Illuminate\Http\Response
  109. */
  110. public function update(Request $request, Project $project)
  111. {
  112. //
  113. }
  114. /**
  115. * Remove the specified resource from storage.
  116. *
  117. * @param \App\Models\Project $project
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function destroy(Project $project)
  121. {
  122. //
  123. }
  124. }