123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- /**
- * 模版库管理(弃用)
- * @copyright 2021-浙江引擎力营销策划有限公司
- * @author Lc<sunshinecc1@163.com>
- * @since 2021-08-01
- */
- namespace App\Http\Controllers\Admin\Stencil;
- use App\Http\Controllers\Controller;
- use App\Http\Models\Site;
- use App\Http\Models\TemplateLibrary;
- use App\Http\Models\TemplateLibraryRelation;
- use App\Http\Models\TemplateLibraryVar;
- use App\Http\Services\TemplateLibraryApiService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Contracts\View\Factory;
- use Illuminate\View\View;
- use Illuminate\Http\JsonResponse;
- class TemplateLibraryController extends Controller
- {
- private $templateLibraryApiService;
- private $siteId;
- /**
- * 模版站服务类
- * TplController constructor.
- * @param TemplateLibraryApiService $templateLibraryApiService
- */
- public function __construct(TemplateLibraryApiService $templateLibraryApiService)
- {
- $this->templateLibraryApiService = $templateLibraryApiService;
- $this->siteId = 360;
- }
- /**
- * 模版库管理
- * @return Factory|View
- */
- public function templateLibrary()
- {
- $variable = [];
- $trees = TemplateLibrary::query()->with(['templateLibraryRelation'])
- ->orderBy('sort', 'asc')
- ->whereNull('deleted_at')->get()->toArray();
- foreach ($trees as $key => $tree) {
- $trees[$key]['name'] = "{$tree['name']}#{$tree['id']}";
- $trees[$key]['title'] = $tree['name'];
- $trees[$key]['images'] = json_decode($tree['images'], true);
- }
- $trees = list_to_tree($trees, 'id', 'pid', 'children');
- return view('admin.stencil.template_library', [
- 'trees' => $trees,
- 'variable' => $variable,
- 'siteList' => Site::query()->where('is_stencil', 1)->get(),
- 'templateLibraryVar' => TemplateLibraryVar::query()->whereNull('deleted_at')->get(),
- ]);
- }
- /**
- * 添加模版库列表节点
- * @param Request $request
- * @return JsonResponse
- */
- public function templateLibraryAddNode(Request $request)
- {
- $result = $request->all();
- $update = [
- 'name' => $result['node'] ?? '',
- 'pid' => $result['pid'] ?? 0,
- ];
- TemplateLibrary::query()->insert($update);
- return response()->json(['message' => '添加成功']);
- }
- /**
- * 修改模版库列表节点
- * @param Request $request
- * @return JsonResponse
- */
- public function templateLibraryUpdateNode(Request $request)
- {
- $result = $request->all();
- $update = [
- 'name' => $result['node'] ?? '',
- 'pid' => $result['pid'] ?? 0,
- ];
- TemplateLibrary::query()->where('id', $result['id'])->update($update);
- return response()->json(['message' => '添加成功']);
- }
- /**
- * 删除模版库列表节点
- * @param Request $request
- * @return JsonResponse
- */
- public function templateLibraryDelNode(Request $request)
- {
- $result = $request->all();
- $update = [
- 'deleted_at' => date('Y-m-d H:i:s'),
- ];
- TemplateLibrary::query()->where('id', $result['id'])->update($update);
- return response()->json(['message' => '添加成功']);
- }
- /**
- * 复制模版库列表节点
- * @param Request $request
- * @return JsonResponse
- */
- public function templateLibraryCopyNode(Request $request)
- {
- $result = $request->all();
- $array = TemplateLibrary::query()
- ->whereNull('deleted_at')->get()->toArray();
- $parentList = TemplateLibrary::query()->where('id', $result['id'])
- ->whereNull('deleted_at')->first()->toArray();
- $childList = $this->getTemplateLibraryList($array, $result['id']);
- $childList[] = $parentList;
- $list = $this->arraySort($childList, 'id');
- $list = array_merge($list);
- $ids = [];
- foreach ($list as $key => $item) {
- if ($item['pid'] == 0 && $key == 0) {
- $update = [
- 'name' => $item['name'],
- 'alias' => $item['alias'],
- 'memo' => $item['memo'],
- 'sort' => $item['sort'],
- 'pid' => 0,
- 'css' => $item['css'],
- 'images' => $item['images'],
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- } elseif ($item['pid'] != 0 && $key == 0) {
- $ids[$item['pid']] = $item['pid'];
- $update = [
- 'name' => $item['name'],
- 'alias' => $item['alias'],
- 'memo' => $item['memo'],
- 'sort' => $item['sort'],
- 'pid' => $ids[$item['pid']] ?? 0,
- 'css' => $item['css'],
- 'images' => $item['images'],
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- } else {
- $update = [
- 'name' => $item['name'],
- 'alias' => $item['alias'],
- 'memo' => $item['memo'],
- 'sort' => $item['sort'],
- 'pid' => $ids[$item['pid']],
- 'css' => $item['css'],
- 'images' => $item['images'],
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- }
- $id = TemplateLibrary::query()->insertGetId($update);
- $ids[$item['id']] = $id;
- }
- return response()->json(['message' => '添加成功']);
- }
- /**
- * 递归父级目录
- * @param $array
- * @param int $pid
- * @return array
- */
- public function getTemplateLibraryList($array, $pid = 0)
- {
- static $data = [];
- foreach ($array as $key => $item) {
- if ($item['pid'] == $pid) {
- $data[] = [
- 'id' => $item['id'],
- 'name' => "{$item['name']}",
- 'alias' => $item['alias'] ?? '',
- 'memo' => $item['memo'] ?? '',
- 'sort' => $item['sort'] ?? 0,
- 'pid' => $item['pid'] ?? 0,
- 'css' => $item['css'] ?? '',
- 'images' => $item['images'] ?? json_encode([]),
- ];
- unset($array[$key]);
- $this->getTemplateLibraryList($array, $item['id']);
- }
- }
- return $data;
- }
- /**
- * 添加顶级菜单
- * @param Request $request
- * @return JsonResponse
- */
- public function addParentMenu(Request $request)
- {
- $result = $request->all();
- $update = [
- 'name' => $result['name'],
- 'pid' => 0,
- ];
- TemplateLibrary::query()->insert($update);
- return response()->json(['message' => '添加成功']);
- }
- /**
- * 保存模版信息
- * @param Request $request
- * @return JsonResponse
- */
- public function saveTemplateLibrary(Request $request)
- {
- $result = $request->all();
- $update = [
- 'name' => $result['name'] ?? '',
- 'alias' => $result['alias'] ?? '',
- 'memo' => $result['memo'] ?? '',
- 'sort' => $result['sort'] ?? 0,
- 'pid' => $result['pid'] ?? '',
- 'css' => $result['css'] ?? '',
- 'images' => json_encode($result['images'] ?? []),
- ];
- try {
- //事务
- DB::transaction(function () use ($result, $update) {
- TemplateLibrary::query()->where('id', $result['template_id'])->update($update);
- if (!empty($result['variableIds'])) {
- $variableList = [];
- foreach ($result['variableIds'] as $item) {
- $variableList[] = [
- 'template_library_id' => $result['template_id'],
- 'variable_id' => $item,
- ];
- }
- TemplateLibraryRelation::query()->where('template_library_id', $result['template_id'])->delete();
- TemplateLibraryRelation::query()->insert($variableList);
- }
- });
- } catch (\Throwable $exception) {
- return response()->json(['message' => $exception->getMessage()], 400);
- }
- if (!empty($result['siteId'] && !empty($result['nodeIds']))) {
- $ids = [];
- $level = [];
- foreach ($result['nodeIds'] as $key => $id) {
- $res = explode(':', $id);
- $ids[] = $res[0];
- $level[$res[0]] = $res[1];
- }
- $templateLibraryList = TemplateLibrary::query()->whereIn('id', $ids)->get()->toArray();
- if (!empty($templateLibraryList)) {
- $templateLibraryResult = [];
- foreach ($templateLibraryList as $item) {
- $templateLibraryResult[] = [
- 'id' => $item['id'] ?? 0,
- 'name' => $item['name'] ?? '',
- 'alias' => $item['alias'] ?? '',
- 'description' => $item['memo'] ?? '',
- 'rank' => $item['rank'] ?? 0,
- 'html' => '',
- 'allow_write_file' => 0,
- 'allow_clear_cache' => 0,
- 'create_time' => time(),
- 'parent_id' => $item['pid'],
- 'level' => $level[$item['id']],
- 'images' => $item['images'] ?? json_encode([]),
- ];
- }
- //清空表
- DB::connection($this->templateLibraryApiService->connection($this->siteId))
- ->table('content_template')->truncate();
- //插入到项目数据库形成新的父子关系
- $this->loop($templateLibraryResult);
- $variableList = TemplateLibraryVar::query()->get()->toArray();
- if (!empty($variableList)) {
- $variableListResult = [];
- foreach ($variableList as $item) {
- $variableListResult[] = [
- 'name' => $item['variable'] ?? '',
- 'caption' => $item['tag'] ?? '',
- 'description' => $item['memo'] ?? '',
- 'input_type' => $item['input_type'] ?? '',
- 'input_value' => $item['input_value'] ?? '',
- 'input_opts' => $item['input_opts'] ?? '',
- 'input_length' => $item['input_length'] ?? 0,
- 'allow_blank' => $item['allow_blank'] ?? 0,
- 'regex_match' => $item['regex_match'] ?? '',
- 'regex_error' => $item['regex_error'] ?? '',
- 'rank' => $item['rank'] ?? 0,
- 'create_time' => $item['create_time'] ?? time(),
- 'width' => $item['width'] ?? 0,
- 'height' => $item['height'] ?? 0,
- 'size' => $item['size'] ?? 0,
- ];
- }
- DB::connection($this->templateLibraryApiService->connection($request['siteId']))->table('content_template_var')->truncate();
- DB::connection($this->templateLibraryApiService->connection($request['siteId']))
- ->table('content_template_var')->insert($variableListResult);
- }
- }
- }
- return response()->json(['message' => '保存成功']);
- }
- /**
- * 插入数据库形成新的父子关系
- * @param $array
- */
- public function loop($array)
- {
- $ids = [];
- foreach ($array as $key => $item) {
- if ($item['parent_id'] == 0 && $key == 0) {
- $update = [
- 'id' => $item['id'],
- 'name' => $item['name'],
- 'alias' => $item['alias'] ?? '',
- 'description' => $item['description'] ?? '',
- 'rank' => $item['rank'] ?? 0,
- 'html' => $item['html'] ?? 0,
- 'allow_write_file' => $item['allow_write_file'] ?? '',
- 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
- 'create_time' => $item['create_time'],
- 'parent_id' => 0,
- 'level' => $item['level'],
- 'images' => $item['images'],
- ];
- } elseif ($item['parent_id'] != 0 && $key == 0) {
- $ids[$item['parent_id']] = $item['parent_id'];
- $update = [
- 'id' => $item['id'],
- 'name' => $item['name'],
- 'alias' => $item['alias'] ?? '',
- 'description' => $item['description'] ?? '',
- 'rank' => $item['rank'] ?? 0,
- 'html' => $item['html'] ?? 0,
- 'allow_write_file' => $item['allow_write_file'] ?? '',
- 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
- 'create_time' => $item['create_time'],
- 'parent_id' => $ids[$item['parent_id']] ?? 0,
- 'level' => $item['level'],
- 'images' => $item['images'],
- ];
- } else {
- $update = [
- 'id' => $item['id'],
- 'name' => $item['name'],
- 'alias' => $item['alias'] ?? '',
- 'description' => $item['description'] ?? '',
- 'rank' => $item['rank'] ?? 0,
- 'html' => $item['html'] ?? 0,
- 'allow_write_file' => $item['allow_write_file'] ?? '',
- 'allow_clear_cache' => $item['allow_clear_cache'] ?? '',
- 'create_time' => $item['create_time'],
- 'parent_id' => $ids[$item['parent_id']],
- 'level' => $item['level'],
- 'images' => $item['images'],
- ];
- }
- $id = DB::connection($this->templateLibraryApiService->connection($this->siteId))
- ->table('content_template')->insertGetId($update);
- $ids[$item['id']] = $id;
- }
- }
- /**
- * 添加模版变量
- * @param Request $request
- * @param $id
- * @return Factory|JsonResponse|View
- */
- public function addVariable(Request $request, $id)
- {
- if (!$request->ajax()) {
- $info = TemplateLibraryVar::query()->where('id', $id)->first();
- return view('admin.stencil.add_variable', [
- 'id' => $id,
- 'info' => $info,
- ]);
- }
- $result = $request->all();
- $update = [
- 'variable' => $result['variable'] ?? '',
- 'tag' => $result['tag'] ?? '',
- 'sort' => $result['sort'] ?? 0,
- 'memo' => $result['memo'] ?? '',
- 'input_type' => $result['input_type'] ?? '',
- 'input_opts' => $result['input_opts'] ?? '',
- 'input_length' => $result['input_length'] ?? '',
- 'width' => $result['width'] ?? '',
- 'height' => $result['height'] ?? '',
- 'size' => $result['size'] ?? '',
- 'input_value' => $result['input_value'] ?? '',
- 'regex_match' => $result['regex_match'] ?? '',
- 'regex_error' => $result['regex_error'] ?? '',
- ];
- if (!empty($id)) {
- TemplateLibraryVar::query()->where('id', $id)->update($update);
- } else {
- TemplateLibraryVar::query()->insert($update);
- }
- return response()->json(['message' => '保存成功']);
- }
- /**
- * 删除模版变量
- * @param $id
- * @return JsonResponse
- */
- public function delDelVariable($id)
- {
- $update = [
- 'deleted_at' => date('Y-m-d H:i:s')
- ];
- TemplateLibraryVar::query()->where('id', $id)->update($update);
- return response()->json(['message' => '保存成功']);
- }
- }
|