ProjectTreeController.php 4.4 KB

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