form表单公用
<?php
/**
* 后台总控制器
*/
namespace app\common\controller;
use think\Controller;
use app\common\service\DataService;
class Admin extends Controller
{
/**
* 默认操作数据表
* @var string
*/
protected $table;
/**
* 检测登录权限
*/
public $chkLogin = true;
/**
* 检查节点访问权限
*/
public $chkAuth = true;
/**
* 页面标题
*/
public $title = '后台管理';
/**
* 结构分隔符
*/
public static $dept = '<span style="color:gray;">├─ </span>';
/**
* 表单默认操作
* @param Query $db 数据库查询对象
* @param string $tpl 显示模板名字
* @param string $pk 更新主键规则
* @param array $where 查询规则
* @param array $data 扩展数据
* @return array|string
*/
protected function _form($db = null, $tpl = null, $pk = null, $where = [], $data = [])
{
$db = is_null($db) ? db($this->table) : (is_string($db) ? db($db) : $db);
$pk = empty($pk) ? ($db->getPk() ? $db->getPk() : 'id') : $pk;
$pk_value = input($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : null));
// POST请求, 数据自动存库
if ($this->request->isPost()) {
$data = array_merge(input('post.'), $data);
if ($this->_callback('_form_filter', $data) !== false) {
$result = DataService::save($db, $data, $pk, $where);
$url = input('open_type') == 'modal' ? '' : cookie('_listpage_');
return $result !== false ? $this->success('恭喜, 数据保存成功!', $url) : $this->error('数据保存失败, 请稍候再试!');
}
}
// 非POST请求, 获取数据并显示表单页面
$row = $db->where($pk, $pk_value)->where($where)->find();
$vo = ($pk_value !== null) ? array_merge($row, $data) : $data;
if (false !== $this->_callback('_form_filter', $vo)) {
$this->view->title = !empty($this->title) ? $this->title : null;
$this->view->vo = !empty($vo) ? $vo : null;
return $this->fetch($tpl);
}
}
/**
* 列表集成处理方法
* @param Query $db 数据库查询对象
* @param bool $is_page 是启用分页
* @param bool $is_display 是否直接输出显示
* @param bool $total 总记录数
* @return array|string
*/
protected function _list($db = null, $is_page = true, $is_display = true, $total = false)
{
$db = is_null($db) ? db($this->table) : (is_string($db) ? db($db) : $db);
# 列表排序默认处理
if ($this->request->isPost() && input('post.action') === 'resort') {
$data = input('post.');
unset($data['action']);
foreach ($data as $key => &$value) {
if (false === $db->where('id', intval(ltrim($key, '_')))->update(['sort' => $value])) {
$this->error('列表排序失败,请稍候再试!');
}
}
$this->success('列表排序成功,正在刷新列表!', '');
}
# 默认排序
if ($db->getOptions('order') === null) {
$fields = $db->getTableFields();
in_array('sort', $fields) && $db->order('sort asc');
}
# 列表显示
$result = [];
if ($is_page) {
// $row_page = intval($this->request->get('rows', cookie('rows')));
// cookie('rows', $row_page >= 10 ? $row_page : 15);
// $row_page = 1;
// 每页条数
$rows = input('limit', config('paginate.list_rows'), 'intval');
$page = $db->paginate($rows, $total, ['query' => input('get.', '', 'urlencode')]);
// 获取总数
$result['total'] = $page->total();
$result['list'] = $page->all();
$result['page'] = preg_replace(['|href="(.*?)"|', '|pagination|'], ['data-load="$1"', 'pagination pull-right'], $page->render());
} else {
$result['list'] = $db->select();
}
if ($this->_callback('_data_filter', $result['list']) === false) {
return $result;
}
if ($is_display) {
!empty($this->title) && $this->view->title = $this->title;
cookie('_listpage_', url('admin/index/index', '', '') . '#' .$this->request->url());
exit($this->fetch('', $result));
}
return $result;
}
/**
* 当前对象回调成员方法
* @param string $method
* @param array $data
* @return bool
*/
protected function _callback($method, &$data)
{
foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
if (method_exists($this, $_method) && false === $this->$_method($data)) {
return false;
}
}
return true;
}
/**
* 更新操作
* @param string $name 类型名称
*/
protected function _save ($name = '')
{
$arr = [
'success' => [
'delete' => ($name ? : '删除') . '成功!',
'forbid' => ($name ? : '禁用') . '成功!',
'resume' => ($name ? : '启用') . '成功!',
],
'error' => [
'delete' => ($name ? : '删除') . '失败,请稍候再试!',
'forbid' => ($name ? : '禁用') . '失败,请稍候再试!',
'resume' => ($name ? : '启用') . '失败,请稍候再试!',
],
];
$success = $arr['success'][$this->request->action()];
$error = $arr['error'][$this->request->action()];
return DataService::update($this->table) ? $this->success($success, '') : $this->error($error);
}
/**
* 生成静态页面
*/
protected function makeHtml ($mode, $id = '')
{
$root = $this->request->root();
// 获取内容
$content = action('article/Index/' . $mode, ['id' => $id]);
if (!empty($id)) {
$file = 'topic/' . $id . '.html';
// 替换内容
$content = str_replace($root . '/'. $mode. '/', $root . '/topic/', $content);
} else {
$file = 'index.html';
// 替换内容
$content = str_replace($root . '/category/', $root . '/topic/', $content);
$content = str_replace($root . '/detail/', $root . '/topic/', $content);
}
// 替换内容需要正则拿出所有需要替换的链接,循环判定静态文件是否存在,存在则使用静态替换,不存在则使用动态路径
// ...............
file_put_contents(ROOT_PATH . $file, $content);
}
/**
* 全站生成静态
*/
protected function makeHtmls ()
{
}
}
子类 /** * 标签列表 */ public function index () { $this->view->name = $this->title; // 设置页面标题 $this->title = $this->title . '管理'; // 实例Query对象 $db = db($this->table)->where('delete_time', null)->where('type', $this->type); if ($area = input('countryid', 0, 'intval')) { $db->where('area', $area); } // 实例化并显示 return parent::_list($db, 'expert/index'); } /** * 列表数据处理 * @param array $data */ protected function _index_data_filter(&$data) { foreach ($data as &$vo) { $vo['create'] = date('Y-m-d H:i:s', $vo['create_time']); // 显示地区 $vo['country'] = db('Country')->where('id', $vo['area'])->value('namecn') ? : '未设置'; // 显示行业标签 $vo['industry'] = db('Industry')->where('id', $vo['industry'])->value('namecn') ? : '未设置'; } } /** * 表单数据默认处理 * @param array $data */ protected function _form_filter(&$data) { if ($this->request->isPost()) { if (!isset($data['id'])) { $data['create_time'] = time(); $data['type'] = $this->type; } } } /** * 标签编辑 */ public function add() { $this->view->name = $this->title; $this->title = '新增' . $this->title; return $this->_form($this->table, 'expert/form'); } /** * 标签编辑 */ public function edit() { $this->view->name = $this->title; $this->title = '编辑' . $this->title; return $this->_form($this->table, 'expert/form'); }
form表单公用的更多相关文章
- [js开源组件开发]query组件,获取url参数和form表单json格式
query组件,获取url参数和form表单json格式 距离上次的组件[js开源组件开发]ajax分页组件一转眼过去了近二十天,或许我一周一组件的承诺有了质疑声,但其实我一直在做,只是没人看到……, ...
- 项目回顾1-图片上传-form表单还是base64-前端图片压缩
第一个项目终于上线了,是一个叫亲青筹的公益众筹平台,微信端,电脑端还有后台界面大部分都是我完成的,几个月过来,感觉收获了很多,觉得要总结一下. 首先想到的是图片上传的问题.在通常表单数据都是ajax上 ...
- Django学习笔记(6)——Form表单
知识储备:HTML表单form学习 表单,在前端页面中属于最常见的一个东西了.基本上网站信息的提交都用到了表单,所以下面来学习Django中优雅的表单系统:Form 表单的主要作用是在网页上提供一个图 ...
- JS form 表单收集 数据 formSerialize
做后台系统的时候通常会用到form表单来做数据采集:每次一个字段一个字段的去收集就会很麻烦,网站也有form.js插件可以进行表单收集,并封装成一个对象,通过ajax方法传到后台:现在介绍一种直觉采集 ...
- C#之Form表单认证
原文地址: https://blog.csdn.net/chadcao/article/details/7859394 ASP.NET的安全认证,共有“Windows”.“Form”.“Passpor ...
- form表单验证-Javascript
Form表单验证: js基础考试内容,form表单验证,正则表达式,blur事件,自动获取数组,以及css布局样式,动态清除等.完整代码如下: <!DOCTYPE html PUBLIC &qu ...
- Form 表单提交参数
今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后 ...
- form表单 ----在路上(15)
form 表单就是将用户的信息提交到服务器,服务器会将信息存储活着根据信息查询数据进行增删改查,再将其返回给用户. 基本格式: <form action="" method ...
- form表单的字符串进行utf-8编码
<form>表单有assept-charset属性.该属性规定字符的编码方式,默认是"unknown",与文档的字符集相同. 该属性除了Internet explore ...
随机推荐
- PS 使用笔记 - PS 让工作台适应 当前图层
1. PS 让工作台适应 当前图层 选中图层 打开 [图像]=>[裁切] 勾选以下,确定即可
- dp题2
1.seq 给出数组 A,则 l 到 r 的一段序列可以选择以下两种得分方式之一进行得分:1.得到
- 20155324 2016-2017-2 《Java程序设计》第5周学习总结
20155324 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 try.catch 1.求平均数程序示例 import java.util.Scanner; ...
- 隐马尔可夫模型HMM(二)概率计算问题
摘自 1.李航的<统计学习方法> 2.http://www.cnblogs.com/pinard/p/6955871.html 一.概率计算问题 上一篇介绍了概率计算问题是给定了λ(A,B ...
- sqlserver 备份脚本
BACKUP DATABASE 数据库名称 TO DISK='d:\3333.bak' ---根据时间生成文件名 --将SQL脚本赋值给变量declare @SqlBackupDataBase as ...
- luogu P4774 [NOI2018]屠龙勇士
传送门 这题真的是送温暖啊qwq,而且最重要的是yyb巨佬在Day2前几天正好学了crt,还写了博客 然而我都没仔细看,结果我就同步赛打铁了QAQ 我们可以先根据题意,使用set维护,求出每次的攻击力 ...
- js 关键字 in 的使用方法
参考地址:http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930643.html in 操作符用于确定某个元素是否在数组中,判断某个属 ...
- Shiro入门 - 通过自定义Realm连数数据库进行认证
添加shiro-realm.ini文件 [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Sprin ...
- 磁盘是随机存储设备,但不是随机存储器(RAM)。为什么?
磁盘是随机存储设备,但不是随机存储器(RAM).为什么?
- npm 的 --save 和 --save-dev 的区别
--save-dev 是作为开发依赖保存到 packsge.json 中的 devDependencies 中,即在开发环境中用到的依赖,如 webpack.babel 等用于开发打包的依赖,只是在执 ...