DistAppearanceTemplate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\DistAppearanceTemplate as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class DistAppearanceTemplate extends EloquentRepository
  6. {
  7. /**
  8. * Model.
  9. *
  10. * @var string
  11. */
  12. protected $eloquentClass = Model::class;
  13. public static function getContent($id)
  14. {
  15. $data = Model::where('id', $id)->first();
  16. if ($data) {
  17. return $data->content;
  18. }
  19. return '';
  20. }
  21. public static function getTemplateTree($appearance_id,$dist_id)
  22. {
  23. $data = Model::where('appearance_id', $appearance_id)->where('dist_id', $dist_id)->orderBy('file_type', 'desc')->orderBy('file_path', 'asc')->get(['id', 'file_name', 'parent_id','file_type','file_path']);
  24. return self::buildTree($data->toArray(),0);
  25. }
  26. public static function buildTree(array $elements, $parentId = 0) {
  27. $branch = [];
  28. foreach ($elements as $element) {
  29. if ($element['parent_id'] == $parentId) {
  30. $children = self::buildTree($elements, $element['id']);
  31. if ($children) {
  32. $element['children'] = $children;
  33. }
  34. $branch[] = $element;
  35. }
  36. }
  37. return $branch;
  38. }
  39. /*
  40. * 保存模板内容
  41. */
  42. public static function saveContent($id, $content)
  43. {
  44. $data = Model::where('id', $id)->first();
  45. if ($data) {
  46. $data->content = $content;
  47. $data->save();
  48. return true;
  49. }
  50. return false;
  51. }
  52. /*
  53. * 请空指定模板
  54. */
  55. public static function deleteTemplates($distId,$appearanceId) {
  56. $self = new self();
  57. $self->model()->where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
  58. return true;
  59. }
  60. /*
  61. * 插入模版文件夹
  62. */
  63. public static function insertTemplateFolder($distId,$appearanceId,$filePath,$fileName,$parentId) {
  64. $self = new self();
  65. $model = $self->model();
  66. $model->dist_id = $distId;
  67. $model->appearance_id = $appearanceId;
  68. $model->file_name = $fileName;
  69. $model->parent_id = $parentId;
  70. $model->file_type = 0;
  71. $model->file_path = $filePath;
  72. $model->save();
  73. // 获取插入数据的 ID
  74. $insertedId = $model->id;
  75. return $insertedId;
  76. }
  77. /*
  78. * 插入模版文件内容
  79. */
  80. public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$parentId,$content) {
  81. $self = new self();
  82. $model = $self->model();
  83. $model->dist_id = $distId;
  84. $model->appearance_id = $appearanceId;
  85. $model->file_name = $fileName;
  86. $model->parent_id = $parentId;
  87. $model->file_type = 1;
  88. $model->file_path = $filePath;
  89. $model->content = $content;
  90. $model->save();
  91. // 获取插入数据的 ID
  92. $insertedId = $model->id;
  93. return $insertedId;
  94. }
  95. }