123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Admin\Repositories;
- use App\Models\DistAppearanceTemplate as Model;
- use Dcat\Admin\Repositories\EloquentRepository;
- class DistAppearanceTemplate extends EloquentRepository
- {
- /**
- * Model.
- *
- * @var string
- */
- protected $eloquentClass = Model::class;
- public static function getContent($id)
- {
- $data = Model::where('id', $id)->first();
- if ($data) {
- return $data->content;
- }
- return '';
- }
- public static function getTemplateTree($appearance_id,$dist_id)
- {
- $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']);
- return self::buildTree($data->toArray(),0);
- }
- public static function buildTree(array $elements, $parentId = 0) {
- $branch = [];
- foreach ($elements as $element) {
- if ($element['parent_id'] == $parentId) {
- $children = self::buildTree($elements, $element['id']);
- if ($children) {
- $element['children'] = $children;
- }
- $branch[] = $element;
- }
- }
- return $branch;
- }
- /*
- * 保存模板内容
- */
- public static function saveContent($id, $content)
- {
- $data = Model::where('id', $id)->first();
- if ($data) {
- $data->content = $content;
- $data->save();
- return true;
- }
- return false;
- }
- /*
- * 请空指定模板
- */
- public static function deleteTemplates($distId,$appearanceId) {
- $self = new self();
- $self->model()->where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
- return true;
- }
- /*
- * 插入模版文件夹
- */
- public static function insertTemplateFolder($distId,$appearanceId,$filePath,$fileName,$parentId) {
- $self = new self();
- $model = $self->model();
- $model->dist_id = $distId;
- $model->appearance_id = $appearanceId;
- $model->file_name = $fileName;
- $model->parent_id = $parentId;
- $model->file_type = 0;
- $model->file_path = $filePath;
- $model->save();
- // 获取插入数据的 ID
- $insertedId = $model->id;
- return $insertedId;
- }
- /*
- * 插入模版文件内容
- */
- public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$parentId,$content) {
- $self = new self();
- $model = $self->model();
- $model->dist_id = $distId;
- $model->appearance_id = $appearanceId;
- $model->file_name = $fileName;
- $model->parent_id = $parentId;
- $model->file_type = 1;
- $model->file_path = $filePath;
- $model->content = $content;
- $model->save();
- // 获取插入数据的 ID
- $insertedId = $model->id;
- return $insertedId;
- }
- }
|