DistAppearanceTemplate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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($appearanceId,$distId) {
  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->template_code = uniqueCode('');
  73. $model->save();
  74. // 获取插入数据的 ID
  75. $insertedId = $model->id;
  76. return $insertedId;
  77. }
  78. /*
  79. * 插入模版文件内容
  80. */
  81. public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$parentId,$content) {
  82. $self = new self();
  83. $model = $self->model();
  84. $model->dist_id = $distId;
  85. $model->appearance_id = $appearanceId;
  86. $model->file_name = $fileName;
  87. $model->parent_id = $parentId;
  88. $model->file_type = 1;
  89. $model->file_path = $filePath;
  90. $model->content = $content;
  91. $model->template_code = uniqueCode('');
  92. $model->save();
  93. // 获取插入数据的 ID
  94. $insertedId = $model->id;
  95. return $insertedId;
  96. }
  97. /*
  98. * 把原始模板复制给分销商
  99. */
  100. public static function copyTemplateToDist($appearanceId,$distId) {
  101. return Model::copyTemplateToDist($appearanceId, $distId);
  102. }
  103. }