From 5766f4ea5b3034259872040df5ac6c9f2d93c496 Mon Sep 17 00:00:00 2001 From: devlike Date: Thu, 8 Feb 2018 11:08:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=AB=99=E7=82=B9CMS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/posts/Archives.php | 478 ++++++++++++++++++ .../admin/controller/posts/Channel.php | 220 ++++++++ application/admin/controller/posts/Modelx.php | 356 +++++++++++++ application/admin/controller/posts/Page.php | 156 ++++++ application/admin/controller/posts/Sites.php | 353 +++++++++++++ 5 files changed, 1563 insertions(+) create mode 100644 application/admin/controller/posts/Archives.php create mode 100644 application/admin/controller/posts/Channel.php create mode 100644 application/admin/controller/posts/Modelx.php create mode 100644 application/admin/controller/posts/Page.php create mode 100644 application/admin/controller/posts/Sites.php diff --git a/application/admin/controller/posts/Archives.php b/application/admin/controller/posts/Archives.php new file mode 100644 index 00000000..06d4eaf8 --- /dev/null +++ b/application/admin/controller/posts/Archives.php @@ -0,0 +1,478 @@ +model = model('Archives'); + + /* + if(!$this->auth->isSuperAdmin()){ + $this->model->where('site_id', 'in', function ($query) { + $admin = $this->auth->getUserInfo(); + $query->table(config("database.prefix") . 'sites')->where('user_id', $admin['id'])->field('id'); + }); + }*/ + } + + /** + * 读取栏目列表 + */ + private function initForm() + { + //从总后台进入 + if ($this->auth->isSuperAdmin()){ + $sitelist = model('Sites')->all(); + $this->view->assign('siteList', $sitelist); + $first_siteid = $sitelist?$sitelist[0]['id']:null; + } + //从站点后台进入 + else { + $this->view->assign('siteList', null); + $first_siteid = Session::get('user_site_id'); + } + $tree = Tree::instance(); + $tree->init(model('channel')->with('sites')->order('site_id asc,weigh desc,id desc')->select(), 'pid'); + $this->channellist = $tree->getTreeList($tree->getTreeArray(0), 'name'); + $channeldata = []; //0 => ['mode' => 'default','site_id'=>0, 'name' => __('None')]]; + foreach ($this->channellist as $k => $v) + { + if(isset($first_siteid) && !is_null($first_siteid) && $v['site_id']!=$first_siteid) { + //do nothing + }else { + $channeldata[$v['id']] = $v; + $channeldata[$v['id']]['disabled'] = 0; + $channeldata[$v['id']]['sitename'] = $v['sites']['name'] == '' ? __('Main site') : $v['sites']['name']; + if ($v['type'] != 'list') { + $channeldata[$v['id']]['disabled'] = 1; + } + } + } + $this->view->assign("channelList", $channeldata); + $this->view->assign("extra", ''); + $ml = Seven::build_langs('row[lang]',null,['siteid'=>$first_siteid]); + $this->view->assign('multilanguage',$ml); + } + + /** + * 查看 + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); + if ($this->request->isAjax()) + { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) + { + return $this->selectpage(); + } + list($where, $sort, $order, $offset, $limit) = $this->buildparams(false, true); + $total = $this->model + ->where($where) + //->where('site_id', Session::get('admin.siteid')) + ->where('isDelete',0) + ->order($sort, $order) + ->count(); + + $tablename = $this->model->getQuery()->getTable(); + $list = $this->model + ->where($where) + //->where('site_id', Session::get('admin.siteid')) + ->where('isDelete',0) + ->with('channel') + ->field('subtitle,content',true, $tablename) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + $this->initForm(); + return $this->view->fetch(); + } + + /** + * 回收站 + */ + public function recyclebin() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); + if ($this->request->isAjax()) + { + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where('isDelete','>',0) + ->where($where) + ->order($sort, $order) + ->count(); + + $tablename = $this->model->getQuery()->getTable(); + $list = $this->model + ->where('isDelete','>',0) + ->where($where) + ->with('channel') + ->field('id,channel_id,title,cover,type,isDelete',false, $tablename) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + + /** + * 添加 + */ + public function add() + { + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + + if ($this->dataLimit) + { + $params[$this->dataLimitField] = $this->auth->id; + } + if(empty($params['updatetime'])) $params['updatetime'] = time(); + + if (!isset($params['site_id'])) { + $params['site_id'] = Session::get('user_site_id'); + } + $params['authorid']=Session::get("admin.id"); + + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; + $this->model->validate($validate); + } + $result = $this->model->allowField(true)->save($params); + if ($result !== false) + { + Counter::create(['item_id'=>$this->model->id,'item_type'=>'archive','views'=>$params['views']]); + Modelx::saveExtraForm($params['type'],$this->model->id, $params); + $this->success(); + } + else + { + $this->error($this->model->getError()); + } + } + catch (\think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + + $this->initForm(); + return $this->view->fetch(); + } + + /** + * 编辑 + */ + public function edit($ids = NULL) + { + $row = $this->model->get($ids); + if (!$row) + $this->error(__('No Results were found')); + + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + //没有自定义“修改时间”,将以实际修改时间为准 + if($params['updatetime_old']==$params['updatetime']){ + $params['updatetime']=time(); + }else{ + $params['updatetime']=strtotime($params['updatetime']); + } + unset($params['updatetime_old']); + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; + $row->validate($validate); + } + $result = $row->allowField(true)->save($params); + + if ($result !== false) + { + Counter::where(['item_id'=>$ids, 'item_type'=>'archive'])->update(['views'=>$params['views']]); + Modelx::saveExtraForm($row['type'], $ids, $params); + + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + catch (think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->initForm(); + $channeldata = []; //0 => ['mode' => 'default','site_id'=>0, 'name' => __('None')]]; + foreach ($this->channellist as $k => $v) + { + if($v['site_id']==$row['site_id']) { + $channeldata[$v['id']] = $v; + $channeldata[$v['id']]['disabled'] = 0; + if ($v['type'] != 'list') { + $channeldata[$v['id']]['disabled'] = 1; + } + if ($row['type'] != $v['model']) { + $channeldata[$v['id']]['disabled'] = 1; + } + } + } + $this->view->assign("channelList", $channeldata); + $this->view->assign("extra", Modelx::getExtraForm($row->type, $ids)); + $this->view->assign('multilanguage', Seven::build_langs('row[lang]',$row['lang'],['siteid'=>$row->site_id])); + + $this->view->assign("row", $row); + return $this->view->fetch(); + } + + /** + * 删除 + */ + public function del($ids = "") + { + if ($ids) + { + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField,'in', $adminIds); + } + $result=$this->model->where($pk, 'in', $ids)->update(['isDelete'=>time()]); + + if ($result!=false) + { + $this->success(); + } + else + { + $this->error(__('No rows were deleted')); + } + } + $this->error(__('Parameter %s can not be empty', 'ids')); + } + /* + * Destroy + * 销毁,无参数时清空回收站 + * 删除后一并将访问统计、模型表相对应数据清除 + */ + public function destroy($ids="") + { + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + + if($ids=='') + { + $this->model->where(['isDelete'=>['>',0]]); + } + else + { + $this->model->where($pk, 'in', $ids); + } + $list = $this->model->select(); + + if(!$list){ + $this->error(__('No rows were deleted')); + } + $count = 0; + foreach ($list as $k => $v) + { + $result = $v->delete(); + //删除相关附件,扩展&模型表 + if($result!=false) { + Hook::listen('archive_del', $v); + Counter::where(['item_type'=>'archive', 'item_id' => $v[$pk]])->delete(); + $count++; + } + } + if ($count) + { + $this->success(); + }else { + $this->error($this->model->getError()); + } + $this->error(__('Operation failed')); + } + + /** + * 还原 + */ + public function restore($ids = "") + { + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + if ($ids=='') + { + $this->model->where('isDelete','>', 0); + } else { + $this->model->where($pk, 'in', $ids); + } + $count = $this->model->restore('1=1'); + if ($count) + { + $this->success(); + } + $this->error(__('No rows were updated')); + } + + /** + * 批量移动到其他分类 + */ + public function move($ids="") + { + $ids = $ids ? $ids : $this->request->param("ids"); + $channel_id = $this->request->param('channel_id'); + $moveable = []; + + $dstCate = \app\admin\model\Channel::get($channel_id); + + if(!$dstCate){ + $this->success(__('You have no permission')); + } + if ($ids) + { + + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $lists = $this->model->where($pk, 'in', $ids)->select(); + $count=0; + foreach($lists as $k => $v) + { + //忽略不同模型 + if ($v['type']==$dstCate['model'] && $v['channel_id']!=$dstCate['id']){ + $moveable[] = $v[$pk]; + } + } + \app\admin\model\AdminLog::setTitle(__('Move')); + foreach($moveable as $k=>$v) { + if(sesion('admin.id')!=1) { + $count += $this->model->where($pk, $v)->update(['channel_id' => $channel_id, 'site_id' => $dstCate['site_id']]); + }else { //非超级管理员,不能跨站移动 + $count += $this->model->where($pk, $v)->update(['channel_id' => $channel_id]); + } + } + if($count){ + $this->success(); + }else { + $this->success(__('No rows were updated')); + } + } + $this->success(__('Parameter %s can not be empty', 'ids')); + } + + + /** + * 输出模型表单 + */ + public function get_model_fields() + { + $result = ['name'=>'', 'html'=>'']; + if($this->request->isAjax()) + { + $modelname=$this->request->post('model'); + $form = Modelx::getExtraForm($modelname); + if($form != ''){ + $result['name'] = $modelname; + $result['html'] = $form; + } + $this->success('', '', $result); + } + } + + /** + * 校验字段的值的唯一性 + */ + public function check_element_available() + { + $field = $this->request->post('name'); + $value = $this->request->post('value'); + if($field && $value) { + //$params = ['diyname' => $value]; + $params = [preg_replace('/row\[(\w+)\]/i', '$1', $field) => $value]; + $result = $this->model->get($params); + if ($result != false) { + $this->error(__('Name already exist')); + } else { + $this->success(); + } + }else{ + $this->error(__('Parameter %s can not be empty', '')); + } + } +} diff --git a/application/admin/controller/posts/Channel.php b/application/admin/controller/posts/Channel.php new file mode 100644 index 00000000..e5fc2b16 --- /dev/null +++ b/application/admin/controller/posts/Channel.php @@ -0,0 +1,220 @@ +request->filter(['strip_tags']); + $this->model = model('Channel'); + + $tree = Tree::instance(); + $tree->init($this->model->order('weigh desc,id desc')->with('Sites')->with('archives')->select(), 'pid'); + + $this->channellist = $tree->getTreeList($tree->getTreeArray(0), 'name'); + } + + private function initForm(){ + //从总后台进入 + if ($this->auth->isSuperAdmin()){ + $sitelist = Sites::all(); + $this->view->assign('siteList', $sitelist); + $first_siteid = $sitelist?$sitelist[0]['id']:null; + } + //从站点后台进入 + else { + $this->view->assign('siteList', null); + } + + + $channeldata = [0 => ['site_id'=>0, 'name' => __('None')]]; + foreach ($this->channellist as $k => $v) + { + if(isset($first_siteid) && !is_null($first_siteid) && $v['site_id']!=$first_siteid) { + //do nothing + }else { + $channeldata[$v['id']] = $v; + } + } + $this->view->assign("parentList", $channeldata); + + /* + $tpldir=APP_PATH.'index/view/channel'; + $templatelist=scandir($tpldir); + foreach($templatelist as $k=>$file){ + if($file=='.' || $file=='..'){ + unset($templatelist[$k]); + }else { + $templatelist[$k] = str_replace('.html', '', $file); + } + } + $this->view->assign('templatelist', $templatelist); + */ + + $this->view->assign("flagList", $this->model->getFlagList()); + $this->view->assign("typeList", $this->model->getTypeList()); + $this->view->assign("modelList", model('PostsModelx')->all()); + + $ml = Seven::build_langs('row[lang]',null,['siteid'=>Session::get("admin.siteid")]); + $this->view->assign('multilanguage',$ml); + + } + + /** + * 查看 + */ + public function index() + { + if ($this->request->isAjax()) + { + $search = $this->request->request("search"); + //构造父类select列表选项数据 + $list = []; + if ($search) + { + foreach ($this->channellist as $k => $v) + { + if (stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) + { + $list[] = $v; + } + } + } + else + { + $list = $this->channellist; + } + $total = count($list); + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + + /** + * Add + */ + public function add() + { + $this->initForm(); + return parent::add(); + } + + + /** + * 编辑 + */ + public function edit($ids = NULL) + { + $row = $this->model->get($ids); + if (!$row) + $this->error(__('No Results were found')); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + if (!in_array($row[$this->dataLimitField], $adminIds)) + { + $this->error(__('You have no permission')); + } + } + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + /* + * 已经弃用,如果为了兼容老版可取消注释 + foreach ($params as $k => &$v) + { + $v = is_array($v) ? implode(',', $v) : $v; + } + */ + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; + $row->validate($validate); + } + $result = $row->allowField(true)->save($params); + if ($result !== false) + { + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + catch (\think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->initForm(); + $this->view->assign('multilanguage', Seven::build_langs('row[lang]',$row['lang'],['siteid'=>$row->site_id])); + $this->view->assign("row", $row); + return $this->view->fetch(); + } + + + + /** + * 返回栏目列表,供Ajax调用 + */ + public function getlist() + { + if($this->request->isAjax()){ + foreach ($this->channellist as $k => $v){ + $list[$v['id']]=$v['name']; + } + return json($list); + } + } + + /** + * 校验字段的值的唯一性 + */ + public function check_element_available() + { + $field = $this->request->post('name'); + $value = $this->request->post('value'); + if($field && $value) { + $params = [preg_replace('/row\[(\w+)\]/i', '$1', $field) => $value]; + $result = $this->model->get($params); + if ($result != false) { + $this->error(__('Name already exist')); + } else { + $this->success(); + } + }else{ + $this->error(__('Parameter %s can not be empty', '')); + } + } +} diff --git a/application/admin/controller/posts/Modelx.php b/application/admin/controller/posts/Modelx.php new file mode 100644 index 00000000..3162674a --- /dev/null +++ b/application/admin/controller/posts/Modelx.php @@ -0,0 +1,356 @@ +model = model('PostsModelx'); + + } + + /** + * Index + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); + if ($this->request->isAjax()) + { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) + { + return $this->selectpage(); + } + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->order($sort, $order) + ->count(); + + $list = $this->model + ->where($where) + ->field('extra', true) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + $this->view->assign('typeList', $this->model->getTypeList()); + return $this->view->fetch(); + } + + /** + * Edit + */ + public function edit($ids = NULL) + { + $row = $this->model->get($ids); + if (!$row) + $this->error(__('No Results were found')); + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; + $row->validate($validate); + } + $result = $row->allowField(true)->save($params); + if ($result !== false) + { + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + catch (think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $params = json_decode($row['extra'], true); + unset($row['extra']); + $row['extra']=$params; + $this->view->assign("row", $row); + $this->view->assign('typeList', $this->model->getTypeList()); + return $this->view->fetch(); + } + + + /** + * 字段设置 + */ + public function fieldset() + { + if($this->request->isAjax()) { + $ids = $this->request->post('ids/a'); + $fieldset = $this->request->post('fieldset'); + if(!empty($fieldset)){ + $row = $this->model->get($ids); + if(!$row) + $this->error(__('No Results were found')); + + $modelx = $row['name']; + $modelname = $row['title']; + $tablename = config('database.prefix') . 'extra' . $row->name; + + //do check && make sql + $json = json_decode($fieldset,true); + $sql = $cols = []; + foreach ($json as $k=>$v){ + switch (strtolower($v['type'])){ + case "string": + $v['length'] = isset($v['length']) ? $v['length']>0 && $v['length']<256 ? $v['length'] : 255: 255; + $cols[$v['field']] = 'varchar(' . $v['length'] . ')'; + $sql[$v['field']] = "`". $v['field'] . "` VARCHAR(" . $v['length'] . ") NOT NULL DEFAULT '' COMMENT '". $v['fieldname'] . "' "; + break; + case "number": + $v['float'] = isset($v['float']) ? $v['float']<=0? 0 : $v['float']<3 ? $v['float']: 2: 0; + if($v['float']==0) { + $sql[$v['field']] = "`" . $v['field'] . "` INT(10) NOT NULL DEFAULT '0' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'int(10)'; + }else{ + $cols[$v['field']] = 'float(10,'.$v['float'].')'; + $sql[$v['field']] = "`" . $v['field'] . "` FLOAT(10,". $v['float'] . ") NOT NULL DEFAULT '0.0' COMMENT '". $v['fieldname'] . "' "; + } + break; + case "datetime": + $sql[$v['field']] = "`". $v['field'] . "` INT(10) NOT NULL DEFAULT '0' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'int(10)'; + break; + case "textarea": case"richtext": case"editor": + $sql[$v['field']] = "`". $v['field'] . "` TEXT NOT NULL DEFAULT '' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'text'; + break; + case "images": case"files": + $sql[$v['field']] = "`". $v['field'] . "` TEXT NOT NULL DEFAULT '' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'text'; + break; + case "image": case "file": + $sql[$v['field']] = "`". $v['field'] . "` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'varchar(255)'; + break; + case "select": case "selects": case "checkbox": case"radio": + $sql[$v['field']] = "`". $v['field'] . "` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '". $v['fieldname'] . "' "; + $cols[$v['field']] = 'varchar(255)'; + break; + default: + break; + } + } + //save + $result = $row->save(['extra'=>$fieldset]); + if ($result === false){ + $this->error($row->getError()); + } + unset($row); + + if(Db::query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='". config('database.database') . "' AND TABLE_NAME='". $tablename . "';")) + { + $columns=[]; + foreach (Db::query("SHOW COLUMNS FROM `" . $tablename . "`;") as $k=>$v){ + if($v['Field']!='id' && $v['Field']!='item_id'){ + $columns[$v['Field']] = $v['Type']; + } + } + + $arr = array_diff_key($cols, $columns); + $alterSql = []; + foreach ($arr as $k=>$v){ + $alterSql[$k] = " ADD COLUMN " . $sql[$k]; + unset($cols[$k]); + } + $arr=array_intersect_assoc($columns, $cols); + foreach ($arr as $k => $v) { + unset($cols[$k]); + unset($columns[$k]); + } + foreach($columns as $k=>$v){ + if(isset($cols[$k])) { + $alterSql[$k] = " MODIFY COLUMN " . $sql[$k] ; + } else { + $alterSql[$k] = " DROP COLUMN `" . $k . "` "; + } + } + $execSql = "ALTER TABLE `".$tablename."` ". join(',',$alterSql) . ";"; + }else { + $fieldset = join(", ", $sql); + $execSql = <<error($e->getMessage()); + } + + } catch (\think\exception\PDOException $e) { + $this->error($e->getMessage()); + } + $this->success(); + } + } + + //$this->view->assign("params", $params); + $this->view->assign('typeList', $this->model->getTypeList()); + return $this->view->fetch(); + } + + /** + * 修改字段状态 + */ + public function chgstatus($ids=null) + { + if ($this->request->isAjax()) + { + $row = $this->model->get($ids); + if (!$row) + $this->error(); + + if(empty($row['extra'])) + $this->error(); + + $field = $this->request->post('field'); + $params = json_decode($row['extra'],true); + + foreach($params as $k => &$v){ + if($v['field']==$field){ + $v['status'] = $v['status']=='hidden' ? 'normal' : 'hidden'; + } + } + $params = json_encode($params, JSON_UNESCAPED_UNICODE); + + $result = $row->save(['extra'=>$params]); + if ($result !== false) + { + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + } + + public function rulelist() + { + $search = $this->request->post('searchValue'); + $rules = $this->model->getRules(); + if($search){ + $result[] = ['id'=> $search, 'name'=> $rules[$search]]; + }else { + foreach ($rules as $k => $v) { + $result[] = ['id' => $k, 'name' => $v]; + } + } + return ['list'=>$result]; + + } + + /** + * 检查模型名称是否可用 + */ + public function check() + { + $params = $this->request->post("row/a"); + if ($params) + { + + $result = $this->model->get($params); + if ($result!=false) + { + $this->error( __('Name already exist')); + } + else + { + $this->success(); + } + } + else + { + $this->error( __('Invalid parameters')); + } + } + + public function check_element_available() + { + if($this->request->isAjax()) { + $modelid = $this->request->post('id'); + $val = $this->request->post('value'); + + $m = $this->model->get($modelid); + $tablename = config('database.prefix') . 'extra' . $m->name; + + if (Db::query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" . config('database.database') . "' AND TABLE_NAME='" . $tablename . "';")) { + + foreach (Db::query("SHOW COLUMNS FROM `" . $tablename . "`;") as $k => $v) { + if ($v['Field'] == $val) { + //return json(['error' => __('Field already exist')]); + $this->error(__('Field already exist')); + } + } + } + //return json(['ok' => '']); + $this->success(); + } + } +} diff --git a/application/admin/controller/posts/Page.php b/application/admin/controller/posts/Page.php new file mode 100644 index 00000000..40a65c01 --- /dev/null +++ b/application/admin/controller/posts/Page.php @@ -0,0 +1,156 @@ +model = model('Page'); + } + + /** + * 查看 + */ + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags']); + if ($this->request->isAjax()) + { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) + { + return $this->selectpage(); + } + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->order($sort, $order) + ->count(); + + $list = $this->model + ->where($where) + ->with('sites') + ->field('keywords,description,content', true) + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + + + /** + * 添加/修改 通用参数 + */ + private function initForm() + { + //从总后台进入 + if ($this->auth->isSuperAdmin()){ + $sitelist = Sites::all(); + $this->view->assign('siteList', $sitelist); + $first_siteid = $sitelist?$sitelist[0]['id']:null; + } + //从站点后台进入 + else { + $this->view->assign('siteList', null); + $first_siteid = \think\Session::get('user_site_id'); + } + $this->view->assign("flagList", ['hot' => __('Hot'), 'recommend' => __('Recommend')] ); + + $ml = Seven::build_langs('row[lang]',null,['siteid'=>$first_siteid]); + + $this->view->assign('multilanguage',$ml); + } + + /** + * Add + */ + public function add() + { + $this->initForm(); + return parent::add(); + } + + /** + * 编辑 + */ + public function edit($ids = NULL) + { + $row = $this->model->get($ids); + if (!$row) + $this->error(__('No Results were found')); + + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) + { + if (!in_array($row[$this->dataLimitField], $adminIds)) + { + $this->error(__('You have no permission')); + } + } + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + foreach ($params as $k => &$v) + { + $v = is_array($v) ? implode(',', $v) : $v; + } + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; + $row->validate($validate); + } + $result = $row->allowField(true)->save($params); + if ($result !== false) + { + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + catch (think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->view->assign("row", $row); + $this->initForm(); + $this->view->assign('multilanguage', Seven::build_langs('row[lang]',$row['lang'])); + return $this->view->fetch(); + } + +} diff --git a/application/admin/controller/posts/Sites.php b/application/admin/controller/posts/Sites.php new file mode 100644 index 00000000..d1906b8d --- /dev/null +++ b/application/admin/controller/posts/Sites.php @@ -0,0 +1,353 @@ +model = model('Sites'); + + $modulelist = []; + $this->view->assign('moduleList', $modulelist); + $this->view->assign('hostname', config('url_domain_root')); + } + + /** + * Index + */ + public function index() + { + //从站点管理登录,进入自己的站点配置页 + if(Session::has("user_site_id")) { + $this->redirect('posts/sites/config?addtabs=1'); + } + //超级管理员从总后台登录 + else { + //设置过滤方法 + $this->request->filter(['strip_tags']); + if ($this->request->isAjax()) { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('pkey_name')) { + return $this->selectpage(); + } + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + $total = $this->model + ->where($where) + ->order($sort, $order) + ->count(); + + $list = $this->model + ->alias('s') + ->where($where) + ->join('admin a','a.id=s.user_id') + ->field('a.username, s.*') + ->order($sort, $order) + ->limit($offset, $limit) + ->select(); + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + + } + } + + /** + * Config + */ + public function config() + { + $row = $this->model->get(Session::get("user_site_id")); + + $settings = ['seo' => [ + 'zh-cn' => [ + 'title'=>'', + 'keywords'=>'', + 'description'=>'' + ] + ], 'custom' => '']; + + if($this->request->isPost()){ + + $params = $this->request->post("row/a"); + if ($params) { + foreach ($params as $k => $v) { + if( in_array($k, ['name','domain','lang'])){ + if($k=='lang') + $params[$k] = implode(',', $v); + }else{ + unset($params[$k]); + } + } + $seo = $this->request->post("seo/a"); + $custom = $this->request->post("custom/a"); + if ($seo) { + $settings['seo'] = json_encode($seo, JSON_UNESCAPED_UNICODE); + } + if ($custom) { + $arr = []; + foreach ($custom as $k => $v) { + $arr[$v['field']] = $v['value']; + } + $settings['custom'] = json_encode($arr, JSON_UNESCAPED_UNICODE); + } + if($row) $params['id'] = $row['id']; + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; + $this->model->validate($validate); + } + $result = $this->model->allowField(true)->save($params); + + if ($result !== false) + { + $this->model->config()->save($settings); + $this->model->refreshRulesCache(); + $this->success(); + } + else + { + $this->error($this->model->getError()); + } + } + catch (\think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + + $this->view->assign('row', $row); + + if($row) { + $settings['seo'] = !empty($row->config->seo) ? json_decode($row->config->seo, true) : null; + $settings['custom'] = !empty($row->config->custom) ? json_decode($row->config->custom, true) : null; + } + $this->view->assign('settings', $settings); + $this->view->assign('multilanguage', Seven::build_langs('row[lang]', $row?$row['lang']:'', ['type'=>'checkbox'])); + return $this->view->fetch(); + + } + + /** + * Add + */ + public function add() + { + if(!$this->auth->isSuperAdmin()) { + $this->error(__('You have no permission')); + } + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + foreach ($params as $k => &$v) + { + $v = is_array($v) ? implode(',', $v) : $v; + } + + $seo = $this->request->post("seo/a"); + $custom = $this->request->post("custom/a"); + $settings = ['seo'=>'', 'custom'=>'']; + if($seo){ + $settings['seo'] = json_encode($seo, JSON_UNESCAPED_UNICODE); + } + if($custom){ + $arr = []; + foreach ($custom as $k=>$v){ + $arr[ $v['field'] ] = $v['value']; + } + $settings['custom'] = json_encode($arr, JSON_UNESCAPED_UNICODE); + } + + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate; + $this->model->validate($validate); + } + $result = $this->model->allowField(true)->save($params); + + if ($result !== false) + { + $this->model->config()->save($settings); + $this->model->refreshRulesCache(); + $this->success(); + } + else + { + $this->error($this->model->getError()); + } + } + catch (\think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->view->assign('multilanguage', Seven::build_langs('row[lang]', '', ['type'=>'checkbox'])); + return $this->view->fetch(); + } + + /** + * 编辑 + */ + public function edit($ids = NULL) + { + if(!$this->auth->isSuperAdmin()) { + $this->error(__('You have no permission')); + } + $row = $this->model->get($ids); + if (!$row) + $this->error(__('No Results were found')); + + + if ($this->request->isPost()) + { + $params = $this->request->post("row/a"); + if ($params) + { + //foreach ($params as $k => &$v) + //{ + // $v = is_array($v) ? implode(',', $v) : $v; + //} + $seo = $this->request->post("seo/a"); + $custom = $this->request->post("custom/a"); + $settings = ['seo'=>'', 'custom'=>'']; + if($seo){ + $settings['seo'] = json_encode($seo, JSON_UNESCAPED_UNICODE); + } + if($custom){ + $arr = []; + foreach ($custom as $k=>$v){ + $arr[ $v['field'] ] = $v['value']; + } + $settings['custom'] = json_encode($arr, JSON_UNESCAPED_UNICODE); + } + + try + { + //是否采用模型验证 + if ($this->modelValidate) + { + $name = basename(str_replace('\\', '/', get_class($this->model))); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate; + $row->validate($validate); + } + $result = $row->allowField(true)->save($params); + if ($result !== false) + { + $row->config()->save($settings); + $this->model->refreshRulesCache(); + $this->success(); + } + else + { + $this->error($row->getError()); + } + } + catch (think\exception\PDOException $e) + { + $this->error($e->getMessage()); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $arr['seo'] = !empty($row->config->seo) ? json_decode($row->config->seo,true) : null; + $arr['custom'] = !empty($row->config->custom) ? json_decode($row->config->custom,true) : null; + $this->view->assign("row", $row); + $this->view->assign('settings', $arr); + $this->view->assign('multilanguage', Seven::build_langs('row[lang]', $row['lang'], ['type'=>'checkbox'])); + return $this->view->fetch(); + } + + public function get_site_langs() + { + $value = $this->request->post('lang'); + if($this->request->isAjax()){ + $result['html'] = Seven::build_langs('row[lang]',null,['siteid'=>$value]); + $this->success('','', $result); + } + } + + /** + * 返回站点的栏目列表/可选语言版本 + * 供栏目和文章的 添加&编辑 ajax调用 + */ + public function get_site_info() + { + if($this->request->isAjax()) { + $site_id = $this->request->post('site_id'); + $result['lang'] = Seven::build_langs('row[lang]', null, ['siteid' => $site_id]); + $result['list'] = []; + $list = model('channel')->where('site_id', $site_id)->field('id,pid,type,name')->select(); + if ($list) { + $tree = \fast\Tree::instance(); + $tree->init($list, 'pid'); + $result['list'] = $tree->getTreeList($tree->getTreeArray(0), 'name'); + foreach ($result['list'] as $k => &$v) { + $v['disabled'] = 0; + if ($v['type'] != 'list') { + $v['disabled'] = 1; + } + } + } + $this->success('', null, $result); + } + } + + public function check_element_available() + { + $params = $this->request->post("domain"); + //保留域名 + $invail = ['www','admin']; + if ($params) + { + if (in_array($params, $invail)) { + $this->error( __('Domain already exist')); + } + $result = $this->model->get(['domain'=>$params]); + if ($result!=false) + { + $this->error( __('Domain already exist')); + } + else + { + $this->success(); + } + } + else + { + $this->error( __('Invalid parameters')); + } + } + +}