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(); } }