ShowHelperTrait.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Module\Article\AdminControllers\Helper;
  3. use App\Module\Article\Enums\STATUS;
  4. use App\Module\Article\Models\ArticleCate;
  5. use Dcat\Admin\Show;
  6. /**
  7. * 详情页辅助特性
  8. *
  9. * 提供文章模块后台控制器的详情页构建功能的具体实现
  10. */
  11. trait ShowHelperTrait
  12. {
  13. /**
  14. * 显示文章标题
  15. *
  16. * @param string $field 字段名
  17. * @param string $label 标签名
  18. * @return Show\Field
  19. */
  20. public function fieldTitle(string $field = 'title', string $label = '标题'): Show\Field
  21. {
  22. return $this->show->field($field, $label);
  23. }
  24. /**
  25. * 显示文章描述
  26. *
  27. * @param string $field 字段名
  28. * @param string $label 标签名
  29. * @return Show\Field
  30. */
  31. public function fieldDescription(string $field = 'description', string $label = '简述'): Show\Field
  32. {
  33. return $this->show->field($field, $label);
  34. }
  35. /**
  36. * 显示文章分类
  37. *
  38. * @param string $field 字段名
  39. * @param string $label 标签名
  40. * @return Show\Field
  41. */
  42. public function fieldCategory(string $field = 'category_id', string $label = '分类'): Show\Field
  43. {
  44. return $this->show->field($field, $label)->as(function ($value) {
  45. return ArticleCate::getCate()[$value] ?? '未知分类';
  46. });
  47. }
  48. /**
  49. * 显示文章状态
  50. *
  51. * @param string $field 字段名
  52. * @param string $label 标签名
  53. * @return Show\Field
  54. */
  55. public function fieldStatus(string $field = 'status', string $label = '状态'): Show\Field
  56. {
  57. return $this->show->field($field, $label)->as(function ($value) {
  58. return STATUS::getValueDescription()[$value] ?? '未知状态';
  59. });
  60. }
  61. /**
  62. * 显示文章内容
  63. *
  64. * @param string $field 字段名
  65. * @param string $label 标签名
  66. * @return Show\Field
  67. */
  68. public function fieldContent(string $field = 'content', string $label = '内容'): Show\Field
  69. {
  70. return $this->show->field($field, $label)->unescape();
  71. }
  72. /**
  73. * 显示文章排序
  74. *
  75. * @param string $field 字段名
  76. * @param string $label 标签名
  77. * @return Show\Field
  78. */
  79. public function fieldSortOrder(string $field = 'sort_order', string $label = '排序'): Show\Field
  80. {
  81. return $this->show->field($field, $label);
  82. }
  83. /**
  84. * 显示文章是否置顶
  85. *
  86. * @param string $field 字段名
  87. * @param string $label 标签名
  88. * @return Show\Field
  89. */
  90. public function fieldIsTop(string $field = 'is_top', string $label = '是否置顶'): Show\Field
  91. {
  92. return $this->show->field($field, $label)->as(function ($value) {
  93. return $value ? '是' : '否';
  94. });
  95. }
  96. /**
  97. * 显示文章是否推荐
  98. *
  99. * @param string $field 字段名
  100. * @param string $label 标签名
  101. * @return Show\Field
  102. */
  103. public function fieldIsRecommend(string $field = 'is_recommend', string $label = '是否推荐'): Show\Field
  104. {
  105. return $this->show->field($field, $label)->as(function ($value) {
  106. return $value ? '是' : '否';
  107. });
  108. }
  109. /**
  110. * 显示文章浏览量
  111. *
  112. * @param string $field 字段名
  113. * @param string $label 标签名
  114. * @return Show\Field
  115. */
  116. public function fieldViewsCount(string $field = 'views_count', string $label = '浏览量'): Show\Field
  117. {
  118. return $this->show->field($field, $label);
  119. }
  120. /**
  121. * 显示文章创建者
  122. *
  123. * @param string $field 字段名
  124. * @param string $label 标签名
  125. * @return Show\Field
  126. */
  127. public function fieldCreatedBy(string $field = 'created_by', string $label = '创建者'): Show\Field
  128. {
  129. return $this->show->field($field, $label);
  130. }
  131. }