Menu.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\DB;
  6. use Kalnoy\Nestedset\NodeTrait;
  7. class Menu extends Model
  8. {
  9. use NodeTrait;
  10. protected $table = 'tb_menu';
  11. protected $primaryKey = 'id';
  12. public $keyType = 'int';
  13. public $incrementing = true;
  14. public $timestamps = true;
  15. public string $cacheName_1 = 'menu-config';
  16. public string $cacheName_2 = 'menu-front';
  17. public int $cacheTime = 31536000;
  18. const CREATED_AT = 'created_at';
  19. const UPDATED_AT = 'updated_at';
  20. const DELETED_AT = null;
  21. protected $guarded = [];
  22. public function getLftName(): string
  23. {
  24. return 'lft';
  25. }
  26. public function getRgtName(): string
  27. {
  28. return 'rgt';
  29. }
  30. public function getParentIdName(): string
  31. {
  32. return 'parent_id';
  33. }
  34. /**
  35. * 메뉴 DB 조회
  36. */
  37. public function data(): ?object
  38. {
  39. if (!$ret = Cache::get($this->cacheName_1)) {
  40. $list = DB::select("CALL SP_MENU_LIST();");
  41. $rows = count($list);
  42. $total = $this->count();
  43. $ret = (object)[
  44. 'total' => $total,
  45. 'rows' => $rows,
  46. 'list' => $list
  47. ];
  48. Cache::put($this->cacheName_1, $ret, $this->cacheTime);
  49. }
  50. return $ret;
  51. }
  52. /**
  53. * 메뉴 저장
  54. */
  55. public function register(array $insertData): int
  56. {
  57. $insertData = [
  58. $insertData['menu_id'],
  59. $insertData['board_id'],
  60. $insertData['name'],
  61. $insertData['link'],
  62. $insertData['target'],
  63. $insertData['desktop'],
  64. $insertData['mobile'],
  65. $insertData['custom'],
  66. $insertData['icon'],
  67. $insertData['depth']
  68. ];
  69. $menuID = DB::selectOne("CALL SP_MENU_SAVE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", $insertData)->id;
  70. $this->reload();
  71. return $menuID;
  72. }
  73. /**
  74. * 메뉴 수정
  75. */
  76. public function updater(array $updateData): void
  77. {
  78. DB::select("CALL SP_MENU_UPDATE(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", [
  79. $updateData['menu_id'],
  80. $updateData['parent_id'],
  81. $updateData['board_id'],
  82. $updateData['name'],
  83. $updateData['link'],
  84. $updateData['target'],
  85. $updateData['desktop'],
  86. $updateData['mobile'],
  87. $updateData['custom'],
  88. $updateData['icon'],
  89. $updateData['depth']
  90. ]);
  91. $this->reload();
  92. }
  93. /**
  94. * 메뉴 삭제
  95. */
  96. public function remove(int $menuID): void
  97. {
  98. DB::select("CALL SP_MENU_DELETE(?);", [$menuID]);
  99. $this->reload();
  100. }
  101. /**
  102. * 메뉴 캐시 생성
  103. */
  104. private function reload(): void
  105. {
  106. Cache::forget($this->cacheName_1);
  107. Cache::forget($this->cacheName_2);
  108. $this->data();
  109. }
  110. /**
  111. * 메뉴 한 단계 위로
  112. */
  113. public function orderUp(int $menuID): void
  114. {
  115. $this->findCategory($menuID)->up();
  116. $this->reload();
  117. }
  118. /**
  119. * 메뉴 한 단계 아래로
  120. */
  121. public function orderDown(int $menuID): void
  122. {
  123. $this->findCategory($menuID)->down();
  124. $this->reload();
  125. }
  126. /**
  127. * 메뉴 조회
  128. */
  129. public function findCategory(int $menuID): Menu
  130. {
  131. return (new Menu)->newQuery()->findOrNew($menuID);
  132. }
  133. }