123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?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\cms\model;
-
- 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\member\service\User::instance()->id;
- $data['username'] = \app\member\service\User::instance()->username;
- $data['sysadd'] = 0;
-
- //处理数据
- $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);
- }
- }
-
- /**
- * 列表页
- * @param $modeId [模型ID]
- * @param $where [查询条件]
- * @param $moreifo [是否含附表]
- * @param $field []
- * @param $order []
- * @param $limit [条数]
- * @param $page [是否有分页]
- * @param int|bool $simple 是否简洁模式或者总记录数
- * @param array $config 配置参数
- */
- public function getList($modeId, $where, $moreifo, $field = '*', $order = '', $limit = 10, $page = null, $simple = false, $config = [])
- {
- $url_mode = self::$config['site_url_mode'];
- $config['query'] = isset($config['query']) ? array_merge($config['query'], request()->get()) : request()->get();
- $tableName = $this->getModelTableName($modeId);
- $result = [];
- if (isset($tableName) && !empty($tableName)) {
- if (2 == getModel($modeId, 'type') && $moreifo) {
- $extTable = $tableName . $this->ext_table;
- $cmsModel = Db::view($tableName, $field)->where($where)->view($extTable, '*', $tableName . '.id=' . $extTable . '.did', 'LEFT')->order($order);
- if ($page) {
- $result = $cmsModel->paginate($limit, $simple, $config);
- } else {
- $result = $cmsModel->limit($limit)->select();
- }
- } else {
- $cmsModel = Db::name($tableName)->where($where)->order($order)->field($field);
- if ($page) {
- $result = $cmsModel->paginate($limit, $simple, $config);
- } else {
- $result = $cmsModel->limit($limit)->select();
- }
- }
- }
- //数据格式化处理
- if (!empty($result)) {
- $ModelField = cache('ModelField');
- $Category = cache('Category');
- foreach ($result as $key => $vo) {
- $vo = Service::dealModelShowData($ModelField[$modeId], $vo);
- $cat = $url_mode == 1 ? $vo['catid'] : (isset($Category[$vo['catid']]) ? $Category[$vo['catid']]['catdir'] : getCategory($vo['catid'], 'catdir'));
- $vo['url'] = buildContentUrl($cat, $vo['id'], $vo['url']);
- $result[$key] = $vo;
- }
- }
- return $result;
- }
-
- /**
- * 详情页
- * @param [type] $modeId [模型ID]
- * @param [type] $where [查询条件]
- * @param boolean $moreifo [是否含附表]
- * @param string $field []
- * @param string $order []
- */
- public function getContent($modeId, $where, $moreifo = false, $field = '*', $order = '', $cache = false)
- {
- $url_mode = self::$config['site_url_mode'];
- $tableName = $this->getModelTableName($modeId);
- if (2 == getModel($modeId, 'type') && $moreifo) {
- $extTable = $tableName . $this->ext_table;
- $dataInfo = Db::view($tableName, '*')->where($where)->cache($cache)->view($extTable, '*', $tableName . '.id=' . $extTable . '.did', 'LEFT')->find();
- } else {
- $dataInfo = Db::name($tableName)->field($field)->cache($cache)->where($where)->order($order)->find();
- }
- if (!empty($dataInfo)) {
- $ModelField = cache('ModelField');
- $Category = cache('Category');
- $dataInfo = Service::dealModelShowData($ModelField[$modeId], $dataInfo);
- $cat = $url_mode == 1 ? $dataInfo['catid'] : (isset($Category[$dataInfo['catid']]) ? $Category[$dataInfo['catid']]['catdir'] : getCategory($dataInfo['catid'], 'catdir'));
- $dataInfo['url'] = buildContentUrl($cat, $dataInfo['id'], $dataInfo['url']);
- }
- return $dataInfo;
- }
-
- /**
- * TAG标签处理
- */
- private function tagDispose($tags, $id, $catid, $modelid)
- {
- $tags_mode = model('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');
- }
- }
-
- }
|