ArticleCategoryRepository.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * Class ArticleCategoryRepository
  23. * @package app\common\repositories\article
  24. * @author xaboy
  25. * @day 2020-04-20
  26. * @mixin ArticleCategoryDao
  27. */
  28. class ArticleCategoryRepository extends BaseRepository
  29. {
  30. /**
  31. * ArticleCategoryRepository constructor.
  32. * @param ArticleCategoryDao $dao
  33. */
  34. public function __construct(ArticleCategoryDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * @param int $merId
  40. * @return array
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. * @author xaboy
  45. * @day 2020-04-20
  46. */
  47. public function getFormatList($merId = 0,$status = null)
  48. {
  49. return formatCategory($this->dao->getAll($merId,$status)->toArray(), 'article_category_id');
  50. }
  51. /**
  52. * @return \think\Collection
  53. * @throws DataNotFoundException
  54. * @throws DbException
  55. * @throws ModelNotFoundException
  56. * @author xaboy
  57. * @day 2020/9/18
  58. */
  59. public function apiGetArticleCategory()
  60. {
  61. return $this->dao->search(['status' => 1, 'pid' => 0])->select();
  62. }
  63. /**
  64. * @param int $merId
  65. * @param int|null $id
  66. * @param array $formData
  67. * @return Form
  68. * @throws FormBuilderException
  69. * @author xaboy
  70. * @day 2020-04-20
  71. */
  72. public function form(int $merId, ?int $id = null, array $formData = [])
  73. {
  74. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemArticleCategoryCreate')->build() : Route::buildUrl('systemArticleCategoryUpdate', ['id' => $id])->build());
  75. $form->setRule([
  76. // Elm::cascader('pid', '上级分类')->options(function () use ($id, $merId) {
  77. // $menus = $this->dao->getAllOptions($merId);
  78. // if ($id && isset($menus[$id])) unset($menus[$id]);
  79. // $menus = formatCascaderData($menus, 'title');
  80. // array_unshift($menus, ['label' => '顶级分类', 'value' => 0]);
  81. // return $menus;
  82. // })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  83. Elm::input('title', '分类名称')->required(),
  84. Elm::input('info', '分类简介'),
  85. Elm::frameImage('image', '分类图片', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=image&type=1')->width('896px')->height('480px')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  86. Elm::switches('status', '状态', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  87. Elm::number('sort', '排序', 0),
  88. ]);
  89. return $form->setTitle(is_null($id) ? '添加文章配置' : '编辑文章分类')->formData($formData);
  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. }