| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | L
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 友情链接管理
- // +----------------------------------------------------------------------
- namespace app\admin\controller\links;
- use app\admin\model\general\Lang as LangModel;
- use app\admin\model\links\Links as LinksModel;
- use app\common\controller\Backend;
- use think\facade\Db;
- /**
- * 友情链接管理
- */
- class Admin extends Backend
- {
- protected $modelValidate = true;
- protected function syncLangData($id, array $row, $lang)
- {
- $defaultLang = LangModel::getDefaultMark();
- if ($id > 0) {
- $data = [
- 'did' => $id,
- 'lang' => $lang,
- 'name' => $row['name'] ?? '',
- 'description' => $row['description'] ?? '',
- 'update_time' => time(),
- ];
- $exists = Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->find();
- if ($exists) {
- Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->update($data);
- } else {
- $data['create_time'] = time();
- Db::name('links_data')->insert($data);
- }
- }
- if ($lang !== $defaultLang) {
- unset($row['name'], $row['description']);
- }
- return $row;
- }
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new LinksModel;
- $this->assign("statusList", $this->modelClass->getStatusList());
- }
- /**
- * 新增友情链接
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $row = $this->request->post('row/a', [], 'trim');
- if ($row) {
- $lang = $row['lang'] ?? LangModel::getDefaultMark();
- unset($row['lang']);
- $result = false;
- Db::startTrans();
- try {
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->validate($row, $validate);
- }
- $saveRow = $this->syncLangData(0, $row, $lang);
- $result = $this->modelClass->save($saveRow);
- if ($result === false) {
- throw new \Exception('未插入任何行');
- }
- $this->syncLangData($this->modelClass->id, $row, $lang);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success('新增成功');
- }
- $this->error('未插入任何行');
- }
- $this->error('参数不能为空');
- }
- $Terms = Db::name('Terms')->where("module", "links")->select();
- $this->assign("Terms", $Terms);
- $this->assign('langs', LangModel::getEnabledList());
- $this->assign('currentLang', LangModel::getDefaultMark());
- $this->assign('defaultLang', LangModel::getDefaultMark());
- return $this->fetch();
- }
- /**
- * 编辑友情链接
- */
- public function edit()
- {
- $id = $this->request->param('id/d', 0);
- $lang = $this->request->param('lang/s', LangModel::getDefaultMark());
- $defaultLang = LangModel::getDefaultMark();
- if ($this->request->isPost()) {
- $row = $this->request->post('row/a', [], 'trim');
- if ($row) {
- $lang = $row['lang'] ?? $defaultLang;
- unset($row['lang']);
- $result = false;
- Db::startTrans();
- try {
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $this->validate($row, $validate);
- }
- $saveRow = $this->syncLangData($id, $row, $lang);
- $result = $this->modelClass->update($saveRow);
- if ($result === false) {
- throw new \Exception('未更新任何行');
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success('修改成功');
- }
- $this->error('未更新任何行');
- }
- $this->error('参数不能为空');
- }
- $data = $this->modelClass->find($id);
- if (!$data) {
- $this->error('记录不存在');
- }
- $data = $data->toArray();
- if ($lang !== $defaultLang) {
- $translate = Db::name('links_data')->where(['did' => $id, 'lang' => $lang])->find();
- if (!$translate) {
- $translate = Db::name('links_data')->where(['did' => $id, 'lang' => $defaultLang])->find();
- }
- if ($translate) {
- $data['name'] = $translate['name'] ?: ($data['name'] ?? '');
- $data['description'] = $translate['description'] ?: ($data['description'] ?? '');
- }
- }
- $Terms = Db::name('Terms')->where("module", "links")->select();
- $langStatus = [];
- foreach (LangModel::getEnabledList() as $item) {
- $langStatus[$item->mark] = Db::name('links_data')->where(['did' => $id, 'lang' => $item->mark])->count() > 0 ? 1 : 0;
- }
- $this->assign("Terms", $Terms);
- $this->assign('data', $data);
- $this->assign('langs', LangModel::getEnabledList());
- $this->assign('currentLang', $lang);
- $this->assign('defaultLang', $defaultLang);
- $this->assign('langStatus', $langStatus);
- return $this->fetch();
- }
- public function del()
- {
- $ids = $this->request->param('ids', '', 'trim');
- if (!$ids) {
- $this->error('参数不能为空');
- }
- $idsArr = array_filter(explode(',', $ids));
- if (empty($idsArr)) {
- $this->error('参数不能为空');
- }
- Db::startTrans();
- try {
- $list = $this->modelClass->where('id', 'in', $idsArr)->select();
- foreach ($list as $row) {
- Db::name('links_data')->where('did', $row->id)->delete();
- $row->delete();
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('删除成功');
- }
- public function index()
- {
- if ($this->request->isAjax()) {
- list($page, $limit, $where) = $this->buildTableParames();
- $list = $this->modelClass->where($where)->order('listorder desc,id desc')->page($page, $limit)->select();
- $total = $this->modelClass->where($where)->count();
- $currentLang = $this->request->param('lang/s', LangModel::getDefaultMark());
- $_list = [];
- foreach ($list as $item) {
- $row = $item->toArray();
- $statusMap = [];
- foreach (LangModel::getEnabledList() as $langItem) {
- $statusMap[$langItem->mark] = Db::name('links_data')->where(['did' => $row['id'], 'lang' => $langItem->mark])->count() > 0 ? 1 : 0;
- }
- $row['lang_status'] = $statusMap;
- if ($currentLang !== LangModel::getDefaultMark()) {
- $translate = Db::name('links_data')->where(['did' => $row['id'], 'lang' => $currentLang])->find();
- if (!$translate) {
- $translate = Db::name('links_data')->where(['did' => $row['id'], 'lang' => LangModel::getDefaultMark()])->find();
- }
- if ($translate) {
- $row['name'] = $translate['name'] ?: $row['name'];
- $row['description'] = $translate['description'] ?: $row['description'];
- }
- }
- $_list[] = $row;
- }
- return json(['code' => 0, 'count' => $total, 'data' => $_list]);
- }
- $langOptions = [];
- foreach (LangModel::getEnabledList() as $item) {
- $langOptions[] = [
- 'mark' => $item->mark,
- 'name' => $item->name,
- 'code' => $item->code,
- ];
- }
- $this->assignconfig('langs', $langOptions);
- return $this->fetch();
- }
- }
|