<?php
/**
* +----------------------------------------------------------------------
* | TickyPHP [ This is a freeware ]
* +----------------------------------------------------------------------
* | Copyright (c) 2015 All rights reserved.
* +----------------------------------------------------------------------
* | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
* +----------------------------------------------------------------------
* | SVN: $Id: Urlrule.php 76892 2019-01-25 15:28:06 luomingui $
* +----------------------------------------------------------------------
* | 文件功能:对应的表名:tky_urlrule
* +----------------------------------------------------------------------
*/
namespace application\admin\controller;
use ticky\request;
use ticky\response;
class urlrule extends auth {
public function index() {
$search = $this->search_frm();
$ret = $this->db->page('urlrule', $search['sql'], '', $this->p);
$this->assign('page', $ret['page']);
$this->assign('items', $ret['items']);
$this->assign('search', $search['arr']);
$this->display('urlrule/index');
}
public function add() {
if (request::isPost()) {
$data = $this->post_frm();
if (!preg_match('/^([a-zA-Z0-9]|[\/\(\)\\\+\-~!@_]){0,30}$/', $data['urlrule'])) {
$this->showmsg(['code' => 200, 'msg' => 'URL规则不符合规范', 'url' => '/admin/urlrule']);
}
$r = db('urlrule')->field('urlrule')->where(array('urlrule' => $data['urlrule']))->find();
if ($r) {
$this->showmsg(['code' => 200, 'msg' => 'URL规则已存在!', 'url' => '/admin/urlrule']);
}
db('urlrule')->add($data);
$this->showmsg(['code' => 200, 'msg' => '添加成功', 'url' => '/admin/urlrule']);
} else {
$this->assign('postUrl', '/admin/urlrule/add');
$this->assign('action', '添加');
$this->display('urlrule/manage');
}
}
public function update() {
if (request::isPost()) {
$data = $this->post_frm();
if (!preg_match('/^([a-zA-Z0-9]|[\/\(\)\\\+\-~!@_]){0,30}$/', $data['urlrule'])) {
$this->showmsg(['code' => 200, 'msg' => 'URL规则不符合规范', 'url' => '/admin/urlrule']);
}
$id = request::post('urlruleid', 0);
$this->db->table('urlrule')->where(['urlruleid' => $id])->update($data);
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('修改成功', '/admin/urlrule');
}
} else {
$urlruleid = request::get('id', 0);
$item = $this->db->table('urlrule')->where(['urlruleid' => $urlruleid])->find();
$this->assign('postUrl', '/admin/urlrule/update');
$this->assign('action', '修改');
$this->assign('item', $item);
$this->display('urlrule/manage');
}
}
public function delete() {
$id = request::get('id', 0);
$result = $this->db->table('urlrule')->where(['urlruleid' => $id])->delete();
if (request::isAjax()) {
if ($result) {
response::ajax(['code' => 200, 'msg' => '删除成功!']);
} else {
response::ajax(['code' => 403, 'msg' => '删除失败! id=' . $id]);
}
} else {
showmsg('删除成功', '/admin/urlrule');
}
}
public function batchremove() {
$optype = request::post('optype', 'del');
$ids = request::post('ids', []);
if ($optype == "del") {
for ($i = 0; $i < count($ids); $i++) {
$id = $ids[$i];
$this->db->table('urlrule')->where(['urlruleid' => $id])->delete();
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('删除成功', '/admin/urlrule');
}
}
}
private function search_frm() {
$search = request::get('search', []);
$where = '1=1 ';
if ($urlruleid = trim($search['urlruleid'])) {
$where .= "and urlruleid = '{$urlruleid}' ";
}
if ($name = trim($search['name'])) {
$where .= "and name = '{$name}' ";
}
if ($urlrule = trim($search['urlrule'])) {
$where .= "and urlrule = '{$urlrule}' ";
}
if ($route = trim($search['route'])) {
$where .= "and route = '{$route}' ";
}
return [
'arr' => $search,
'sql' => $where
];
}
private function post_frm() {
$arr = array();
$arr['name'] = request::post('name', '');
$arr['urlrule'] = request::post('urlrule', '');
$arr['route'] = request::post('route', '');
return $arr;
}
}