<?php
/**
* +----------------------------------------------------------------------
* | TickyPHP [ This is a freeware ]
* +----------------------------------------------------------------------
* | Copyright (c) 2019 All rights reserved.
* +----------------------------------------------------------------------
* | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
* +----------------------------------------------------------------------
* | SVN: $Id: Tag.php 78891 2019-01-28 14:48:08 luomingui $
* +----------------------------------------------------------------------
* | 文件功能:对应的表名:tky_tag
* +----------------------------------------------------------------------
*/
namespace application\admin\controller;
use ticky\request;
use ticky\response;
class tag extends auth {
public function index() {
$search = $this->search_frm();
$ret = $this->db->page('tag', $search['sql'], 'id', $this->p);
$this->assign('page', $ret['page']);
$this->assign('items', $ret['items']);
$this->assign('search', $search['arr']);
$this->display('tag/index');
}
public function add() {
if (request::isPost()) {
$_POST['tags'] = str_replace(',', ',', strip_tags($_POST['tags']));
$arr = array_unique(explode(',', $_POST['tags']));
foreach ($arr as $val) {
$tagid = db('tag')->where(array('tag' => $val))->find();
if (!$tagid) {
db('tag')->add(array('tag' => $val, 'total' => 0, 'inputtime' => NOW_TIME));
}
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '添加成功']);
} else {
showmsg('添加成功', '/admin/tag');
}
} else {
$this->assign('postUrl', '/admin/tag/add');
$this->assign('action', '添加');
$this->display('tag/manage');
}
}
public function update() {
if (request::isPost()) {
$data = $this->post_frm();
$id = request::post('id', 0);
db('tag')->where(['id' => $id])->update($data);
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('修改成功', '/admin/tag');
}
} else {
$id = request::get('id', 0);
$item = db('tag')->where(['id' => $id])->find();
$this->assign('postUrl', '/admin/tag/update');
$this->assign('action', '修改');
$this->assign('item', $item);
$this->display('tag/manage');
}
}
public function delete() {
$id = request::post('id', 0);
$result = $this->db->table('tag')->where(['id' => $id])->delete();
if (request::isAjax()) {
if ($result) {
response::ajax(['code' => 200, 'msg' => '删除成功!']);
} else {
response::ajax(['code' => 403, 'msg' => '删除失败! id=' . $id]);
}
} else {
showmsg('删除成功', '/admin/tag');
}
}
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('tag')->where(['id' => $id])->delete();
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('删除成功', '/admin/tag');
}
}
}
private function search_frm() {
$search = request::get('search', []);
$where = '1=1 ';
if ($id = trim($search['id'])) {
$where .= "and id = '{$id}' ";
}
if ($tag = trim($search['tag'])) {
$where .= "and tag = '{$tag}' ";
}
if ($total = trim($search['total'])) {
$where .= "and total = '{$total}' ";
}
if ($inputtime = trim($search['inputtime'])) {
$where .= "and inputtime = '{$inputtime}' ";
}
return [
'arr' => $search,
'sql' => $where
];
}
private function post_frm() {
$arr = array();
$arr['tag'] = request::post('tag', '');
$arr['total'] = request::post('total', '0');
$arr['inputtime'] = NOW_TIME;
return $arr;
}
}