ArticleCategoryRepository.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\article;
  12. use app\common\dao\article\ArticleCategoryDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Exception\FormBuilderException;
  15. use FormBuilder\Factory\Elm;
  16. use FormBuilder\Form;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\facade\Route;
  21. /**
  22. * 文章内容分类
  23. */
  24. class ArticleCategoryRepository extends BaseRepository
  25. {
  26. /**
  27. * ArticleCategoryRepository constructor.
  28. * @param ArticleCategoryDao $dao
  29. */
  30. public function __construct(ArticleCategoryDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 文章分类列表
  36. * @param int $merId
  37. * @return array
  38. * @throws DataNotFoundException
  39. * @throws DbException
  40. * @throws ModelNotFoundException
  41. * @author xaboy
  42. * @day 2020-04-20
  43. */
  44. public function getFormatList($merId = 0,$status = null)
  45. {
  46. return formatCategory($this->dao->getAll($merId,$status)->toArray(), 'article_category_id');
  47. }
  48. /**
  49. * 开启的文章分类列表
  50. * @return \think\Collection
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @throws ModelNotFoundException
  54. * @author xaboy
  55. * @day 2020/9/18
  56. */
  57. public function apiGetArticleCategory()
  58. {
  59. return $this->dao->search(['status' => 1, 'pid' => 0])->select();
  60. }
  61. /**
  62. * 文章分类表单
  63. * @param int $merId
  64. * @param int|null $id
  65. * @param array $formData
  66. * @return Form
  67. * @throws FormBuilderException
  68. * @author xaboy
  69. * @day 2020-04-20
  70. */
  71. public function form(int $merId, ?int $id = null, array $formData = [])
  72. {
  73. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemArticleCategoryCreate')->build() : Route::buildUrl('systemArticleCategoryUpdate', ['id' => $id])->build());
  74. $form->setRule([
  75. // Elm::cascader('pid', '上级分类')->options(function () use ($id, $merId) {
  76. // $menus = $this->dao->getAllOptions($merId);
  77. // if ($id && isset($menus[$id])) unset($menus[$id]);
  78. // $menus = formatCascaderData($menus, 'title');
  79. // array_unshift($menus, ['label' => '顶级分类', 'value' => 0]);
  80. // return $menus;
  81. // })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  82. Elm::input('title', '分类名称:')->placeholder('请输入分类名称')->required(),
  83. Elm::input('info', '分类简介:')->placeholder('请输入分类简介'),
  84. Elm::frameImage('image', '分类图片:', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=image&type=1')->width('1000px')->height('600px')->props(['footer' => false])->icon('el-icon-camera')->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  85. Elm::switches('status', '状态:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  86. Elm::number('sort', '排序:', 0)->precision(0)->max(99999),
  87. ]);
  88. return $form->setTitle(is_null($id) ? '添加文章配置' : '编辑文章分类')->formData($formData);
  89. }
  90. /**
  91. * 编辑表单
  92. * @param int $merId
  93. * @param $id
  94. * @return Form
  95. * @throws DataNotFoundException
  96. * @throws DbException
  97. * @throws ModelNotFoundException
  98. * @throws FormBuilderException
  99. * @author xaboy
  100. * @day 2020-04-20
  101. */
  102. public function updateForm(int $merId, $id)
  103. {
  104. return $this->form($merId, $id, $this->dao->get($id, $merId)->toArray());
  105. }
  106. }