2
0

ProjectTreeController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 'uid' => Str::uuid(),
  42. 'old_id' => $value['id'],
  43. 'title' => $value['title'],
  44. 'type' => $value['type'],
  45. 'res_id' => $value['res_id'],
  46. 'parent_id' => $value['parent_id'],
  47. 'path' => null,
  48. 'owner_id' => $studioId,
  49. 'editor_id' => $user['user_uid'],
  50. 'created_at' => now(),
  51. 'updated_at' => now(),
  52. ];
  53. }
  54. foreach ($newData as $key => $value) {
  55. if ($value['parent_id']) {
  56. $found = array_filter($newData, function ($element) use ($value) {
  57. return $element['old_id'] == $value['parent_id'];
  58. });
  59. if (count($found) > 0) {
  60. $newData[$key]['parent_id'] = $found[0]['uid'];
  61. $parentPath = $found[0]['path'] ? json_decode($found[0]['path']) : [];
  62. $newData[$key]['path'] = json_encode([...$parentPath, $found[0]['uid']], JSON_UNESCAPED_UNICODE);
  63. } else {
  64. $newData[$key]['parent_id'] = null;
  65. }
  66. } else if (!empty($request->get('parent_id'))) {
  67. $pPath = Project::where('uid', $request->get('parent_id'))->value('path');
  68. $parentPath = json_decode($pPath);
  69. if (!is_array($parentPath)) {
  70. $parentPath = [];
  71. }
  72. $newData[$key]['path'] = json_encode([...$parentPath, $request->get('parent_id')], JSON_UNESCAPED_UNICODE);
  73. $newData[$key]['parent_id'] = $request->get('parent_id');
  74. }
  75. }
  76. $output = [];
  77. foreach ($newData as $key => $value) {
  78. $children = array_filter($newData, function ($element) use ($value) {
  79. return $element['parent_id'] === $value['uid'];
  80. });
  81. $output[] = [
  82. 'id' => $value['uid'],
  83. 'resId' => $value['res_id'],
  84. 'isLeaf' => count($children) === 0,
  85. ];
  86. unset($newData[$key]['old_id']);
  87. unset($newData[$key]['res_id']);
  88. }
  89. $ok = Project::insert($newData);
  90. if ($ok) {
  91. return $this->ok(['rows' => $output, count($output)]);
  92. } else {
  93. return $this->error('error', 200, 200);
  94. }
  95. }
  96. /**
  97. * Display the specified resource.
  98. *
  99. * @param \App\Models\Project $project
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function show(Project $project)
  103. {
  104. //
  105. }
  106. /**
  107. * Update the specified resource in storage.
  108. *
  109. * @param \Illuminate\Http\Request $request
  110. * @param \App\Models\Project $project
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function update(Request $request, Project $project)
  114. {
  115. //
  116. }
  117. /**
  118. * Remove the specified resource from storage.
  119. *
  120. * @param \App\Models\Project $project
  121. * @return \Illuminate\Http\Response
  122. */
  123. public function destroy(Project $project)
  124. {
  125. //
  126. }
  127. }