123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 御宅男 <530765310@qq.com>
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | CMS模型
- // +----------------------------------------------------------------------
- namespace app\admin\model\cms;
-
- use addons\cms\library\FulltextSearch;
- use addons\cms\library\Service;
- use app\common\model\Modelbase;
- use think\Db;
- use think\Model;
-
- /**
- * 模型
- */
- class Cms extends Modelbase
- {
- protected $autoWriteTimestamp = true;
- protected $insert = ['status' => 1];
- protected $ext_table = '_data';
- protected $name = 'ModelField';
-
- protected static $config = [];
- protected static function init()
- {
- self::$config = get_addon_config('cms');
- }
-
- /**
- * 根据模型ID,返回表名
- * @param type $modelid
- * @param type $modelid
- * @return string
- */
- protected function getModelTableName($modelid, $ifsystem = 1)
- {
- $model_cache = cache("Model");
- //表名获取
- $model_table = isset($model_cache[$modelid]['tablename']) ? ucwords($model_cache[$modelid]['tablename']) : '';
- //完整表名获取 判断主表 还是副表
- $tablename = $ifsystem ? $model_table : $model_table . $this->ext_table;
- return $tablename;
- }
-
- //添加模型内容
- public function addModelData($data, $dataExt = [])
- {
- $catid = (int) $data['catid'];
- if (isset($data['modelid'])) {
- $modelid = $data['modelid'];
- unset($data['modelid']);
- } else {
- $modelid = getCategory($catid, 'modelid');
- }
- //完整表名获取
- $tablename = $this->getModelTableName($modelid);
- if (!$this->table_exists($tablename)) {
- throw new \Exception('数据表不存在!');
- }
- Service::getAfterText($data, $dataExt);
-
- //添加用户名
- $data['uid'] = \app\admin\service\User::instance()->id;
- $data['username'] = \app\admin\service\User::instance()->username;
- $data['sysadd'] = 1;
-
- //处理数据
- $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
- list($data, $dataExt) = $dataAll;
- if (!isset($data['create_time'])) {
- $data['create_time'] = request()->time();
- }
- if (!isset($data['update_time'])) {
- $data['update_time'] = request()->time();
- }
- try {
- //主表
- $id = Db::name($tablename)->insertGetId($data);
- //TAG标签处理
- if (!empty($data['tags'])) {
- $this->tagDispose($data['tags'], $id, $catid, $modelid);
- }
- //附表
- if (!empty($dataExt)) {
- $dataExt['did'] = $id;
- Db::name($tablename . $this->ext_table)->insert($dataExt);
- }
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- //更新栏目统计数据
- $this->updateCategoryItems($catid, 'add', 1);
- //推送到站点聚合插件
- if (self::$config['web_site_weburlpush']) {
- hook("weburlpush", buildContentUrl($catid, $id, $data['url'], true, true));
- }
- //新增讯搜索引
- if (self::$config['web_site_searchtype'] === 'xunsearch') {
- FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
- }
- return $id;
- }
-
- //编辑模型内容
- public function editModelData($data, $dataExt = [])
- {
- $catid = (int) $data['catid'];
- $id = (int) $data['id'];
- $modelid = getCategory($catid, 'modelid');
- //完整表名获取
- $tablename = $this->getModelTableName($modelid);
- if (!$this->table_exists($tablename)) {
- throw new \Exception('数据表不存在!');
- }
- Service::getAfterText($data, $dataExt);
- //TAG标签处理
- if (!empty($data['tags'])) {
- $this->tagDispose($data['tags'], $id, $catid, $modelid);
- } else {
- $this->tagDispose([], $id, $catid, $modelid);
- }
- $dataAll = Service::dealModelPostData($modelid, $data, $dataExt);
- list($data, $dataExt) = $dataAll;
-
- if (!isset($data['update_time'])) {
- $data['update_time'] = request()->time();
- }
- //主表
- Db::name($tablename)->where('id', $id)->update($data);
- //附表
- if (!empty($dataExt)) {
- //查询是否存在ID 不存在则新增
- if (Db::name($tablename . $this->ext_table)->where('did', $id)->find()) {
- Db::name($tablename . $this->ext_table)->where('did', $id)->update($dataExt);
- } else {
- $dataExt['did'] = $id;
- Db::name($tablename . $this->ext_table)->insert($dataExt);
- };
- }
- //标签
- hook('content_edit_end', $data);
- //更新讯搜索引
- if (self::$config['web_site_searchtype'] === 'xunsearch') {
- FulltextSearch::update($modelid, $catid, $id, $data, $dataExt);
- }
- }
-
- //删除模型内容
- public function deleteModelData($modeId, $id, $no_delete = false)
- {
- $modelInfo = cache('Model');
- $modelInfo = $modelInfo[$modeId];
-
- $data = Db::name($modelInfo['tablename'])->where('id', $id)->find();
- if (empty($data)) {
- throw new \Exception("该信息不存在!");
- }
- //处理tags
- if (!empty($data['tags'])) {
- $this->tagDispose([], $data['id'], $data['catid'], $modeId);
- }
-
- if ($no_delete) {
- Db::name($modelInfo['tablename'])->where('id', $id)->setField('status', -1);
- } else {
- Db::name($modelInfo['tablename'])->where('id', $id)->delete();
- if (2 == $modelInfo['type']) {
- Db::name($modelInfo['tablename'] . $this->ext_table)->where('did', $id)->delete();
- }
- //更新栏目统计
- $this->updateCategoryItems($data['catid'], 'delete');
- }
- //标签
- hook('content_delete_end', $data);
- //更新讯搜索引
- if (self::$config['web_site_searchtype'] === 'xunsearch') {
- FulltextSearch::del($data['catid'], $id);
- }
- }
-
- /**
- * TAG标签处理
- */
- private function tagDispose($tags, $id, $catid, $modelid)
- {
- $tags_mode = model('admin/cms/Tags');
- if (!empty($tags)) {
- if (strpos($tags, ',') === false) {
- $keyword = explode(' ', $tags);
- } else {
- $keyword = explode(',', $tags);
- }
- $keyword = array_unique($keyword);
- if ('add' == request()->action()) {
- $tags_mode->addTag($keyword, $id, $catid, $modelid);
- } else {
- $tags_mode->updata($keyword, $id, $catid, $modelid);
- }
-
- } else {
- //直接清除已有的tags
- $tags_mode->deleteAll($id, $catid, $modelid);
- }
- }
-
- private function updateCategoryItems($catid, $action = 'add', $cache = 0)
- {
- if ($action == 'add') {
- Db::name('Category')->where('id', $catid)->setInc('items');
- } else {
- Db::name('Category')->where('id', $catid)->where('items', '>', 0)->setDec('items');
- }
- }
- }
|