<?php
namespace application\admin\controller;
use ticky\request;
use ticky\response;
class content extends auth {
public function index() {
$search = $this->search_frm();
$modelid = request::get('modelid', 1);
$model = db('model')->where(['modelid' => $modelid])->find();
$modeltable = $model['tablename'];
$ret = db('content a')
->field('a.*')
->join(db()->prefix . '' . $modeltable . " g on a.contentid=g.contentid")
->where($search['sql'])
->newpage();
foreach ($ret['items'] as $row) {
$item = db('category')->where('catid=' . $row['catid'])->find();
$row['catname'] = $item['catname'];
$row['edit_url'] = "/admin/content/update/?action=edit&tablename=" . $modeltable . "&modelid=" . $modelid . "&contentid=" . $row['contentid'] . '&catid=' . $row['catid'];
$row['del_url'] = '/admin/content/delete/?' . "&contentid=" . $row['contentid'] . "&modelid=" . $modelid;
$data[] = $row;
}
$this->assign('page', $ret['page']);
$this->assign('items', $data);
$this->assign('modelid', $modelid); $this->assign('manage_url', "/admin/content/add/?action=add&tablename=" . $modeltable . "&modelid=" . $modelid);
$this->assign('search', $search['arr']);
$this->display('content/index');
}
public function add() {
if (request::isPut()) {
$tablename = request::get('tablename', 'news');
$data = $this->post_frm();
$contentid = $this->db->table('content')->add($data);
if ($tablename == 'news') {
$news_data = $this->post_news_frm();
$news_data['contentid'] = $contentid;
$this->db->table('news')->add($news_data);
} else if ($tablename == 'download') {
$download_data = $this->post_download_frm();
$download_data['contentid'] = $contentid;
$this->db->table('download')->add($download_data);
} else if ($tablename == 'product') {
$product_data = $this->post_product_frm();
$product_data['contentid'] = $contentid;
$this->db->table('product')->add($product_data);
}
showmsg('添加成功', '/admin/content');
} else {
load_extend('content_form.class.php');
$action = request::get('action', 'add');
$tablename = request::get('tablename', 'news');
$modelid = request::get('modelid', 1);
$sitemodel = getsitemodel();
$content_form = new \content_form($modelid);
$this->assign('postUrl', '/admin/content/add');
$this->assign('action', '添加');
$item['allowcomment'] = 0;
$item['readperm'] = 0;
$item['flag'] = "0,0,0,0,0,0,0";
$item['modelid'] = $modelid;
$this->assign('item', $item);
$string = $content_form->content_add();
$this->assign('from', $string);
$this->assign('modelid', $modelid);
$this->assign('tablename', $tablename);
$this->assign('navtitle', str_replace('模型', '', $sitemodel[$modelid]));
$this->assign('viewfile', 'content/content_' . $tablename);
$this->display('content/manage');
}
}
public function update() {
if (request::isPut()) {
$tablename = request::get('tablename', 'news');
$data = $this->post_frm();
$contentid = $this->db->table('content')->add($data);
if ($tablename == 'news') {
$news_data = $this->post_news_frm();
$news_data['contentid'] = $contentid;
$this->db->table('news')->where(['contentid' => $contentid])->update($news_data);
} else if ($tablename == 'download') {
$download_data = $this->post_download_frm();
$download_data['contentid'] = $contentid;
$this->db->table('download')->where(['contentid' => $contentid])->update($download_data);
} else if ($tablename == 'product') {
$product_data = $this->post_product_frm();
$product_data['contentid'] = $contentid;
$this->db->table('product')->where(['contentid' => $contentid])->update($product_data);
}
showmsg('更新成功', '/admin/content');
} else {
load_extend('content_form.class.php');
$action = request::get('action', 'add');
$tablename = request::get('tablename', 'news');
$modelid = request::get('modelid', 1);
$sitemodel = getsitemodel();
$content_form = new \content_form($modelid);
$contentid = request::get('contentid', 0);
$model = db('model')->where(['modelid' => $modelid])->find();
$content = db('content')->where(['contentid' => $contentid])->find();
$model_content = db($model['tablename'])->where(['contentid' => $contentid])->find();
$item = array_merge($content, $model_content);
$this->assign('postUrl', '/admin/content/update');
$this->assign('action', '修改');
$this->assign('item', $item);
$string = $content_form->content_edit($item);
$this->assign('from', $string);
$this->assign('modelid', $modelid);
$this->assign('tablename', $tablename);
$this->assign('navtitle', str_replace('模型', '', $sitemodel[$modelid]));
$this->assign('viewfile', 'content/content_' . $tablename);
$this->display('content/manage');
}
}
public function delete() {
$id = request::post('id', 0);
$content = db('content')->where(['contentid' => $id])->find();
$model = db('model')->where(['modelid' => $content['modelid']])->find();
if (is_array($model)) {
$modeltable = $model['tablename'];
db($modeltable)->where(['contentid' => $id])->delete();
db('content')->where(['contentid' => $id])->delete();
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '删除成功!']);
} else {
showmsg('删除成功', '/admin/content');
}
}
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];
$content = db('content')->where(['contentid' => $id])->find();
if (is_array($content)) {
$model = db('model')->where(['modelid' => $content['modelid']])->find();
if (is_array($model)) {
$modeltable = $model['tablename'];
db($modeltable)->where(['contentid' => $id])->delete();
db('content')->where(['contentid' => $id])->delete();
}
}
}
if (request::isAjax()) {
response::ajax(['code' => 200, 'msg' => '修改成功']);
} else {
showmsg('删除成功', '/admin/content');
}
}
}
private function search_frm() {
$search = request::get('search', []);
$where = '1=1 ' . 'and siteid=' . $_COOKIE['ticky_tickysite'] . ' ';
if ($contentid = trim($search['contentid'])) {
$where .= " and contentid = '{$contentid}' ";
}
if ($modelid = trim($search['modelid'])) {
$where .= " and modelid = '{$modelid}' ";
}
if ($catid = trim($search['catid'])) {
$where .= " and catid = '{$catid}' ";
}
if ($uid = trim($search['uid'])) {
$where .= " and uid = '{$uid}' ";
}
if ($title = trim($search['title'])) {
$where .= " and title = '{$title}' ";
}
if ($description = trim($search['description'])) {
$where .= " and description = '{$description}' ";
}
if ($url = trim($search['url'])) {
$where .= " and url = '{$url}' ";
}
if ($flag = trim($search['flag'])) {
$where .= " and flag = '{$flag}' ";
}
if ($tag = trim($search['tag'])) {
$where .= " and tag = '{$tag}' ";
}
if ($sort = trim($search['sort'])) {
$where .= " and sort = '{$sort}' ";
}
if ($allowcomment = trim($search['allowcomment'])) {
$where .= " and allowcomment = '{$allowcomment}' ";
}
if ($views = trim($search['views'])) {
$where .= "and views = '{$views}' ";
}
if ($replies = trim($search['replies'])) {
$where .= "and replies = '{$replies}' ";
}
if ($readperm = trim($search['readperm'])) {
$where .= "and readperm = '{$readperm}' ";
}
if ($readpoint = trim($search['readpoint'])) {
$where .= "and readpoint = '{$readpoint}' ";
}
if ($is_push = trim($search['is_push'])) {
$where .= "and is_push = '{$is_push}' ";
}
if ($status = trim($search['status'])) {
$where .= "and status = '{$status}' ";
}
if ($inputtime = trim($search['inputtime'])) {
$where .= "and inputtime = '{$inputtime}' ";
}
if ($updatetime = trim($search['updatetime'])) {
$where .= "and updatetime = '{$updatetime}' ";
}
return [
'arr' => $search,
'sql' => $where
];
}
private function post_frm() {
$flag = request::post('flag', []);
$arr = array();
$arr['modelid'] = request::post('modelid', '');
$arr['catid'] = request::post('catid', '');
$arr['siteid'] = $_COOKIE['ticky_tickysite'];
$arr['uid'] = $this->user_id;
$arr['title'] = request::post('title', '');
$arr['description'] = request::post('description', '');
$arr['url'] = request::post('url', '');
$arr['flag'] = implode(",", $flag);
$arr['tag'] = request::post('tag', '');
$arr['allowcomment'] = request::post('allowcomment', '0');
$arr['replies'] = request::post('replies', '0');
$arr['readperm'] = request::post('readperm', '0');
$arr['readpoint'] = request::post('readpoint', '0');
$arr['is_push'] = request::post('is_push', '0');
$arr['status'] = request::post('status', '0');
$arr['inputtime'] = NOW_TIME;
$arr['updatetime'] = NOW_TIME;
return $arr;
}
private function post_news_frm() {
$arr = array();
$arr['author'] = request::post('authorid', '');
$arr['befrom'] = request::post('befromid', '');
$arr['content'] = request::post('content', '');
return $arr;
}
private function post_download_frm() {
$arr = array();
$arr['down_url'] = request::post('down_url', '');
$arr['copytype'] = request::post('copytype', '');
$arr['systems'] = request::post('systems', '');
$arr['language'] = request::post('language', '');
$arr['version'] = request::post('version', '');
$arr['filesize'] = request::post('filesize', '');
$arr['classtype'] = request::post('classtype', '');
$arr['stars'] = request::post('stars', '');
$arr['content'] = request::post('content', '');
return $arr;
}
private function post_product_frm() {
$arr = array();
$arr['brand'] = request::post('brand', '');
$arr['standard'] = request::post('standard', '');
$arr['yieldly'] = request::post('yieldly', '');
$arr['pictures'] = request::post('pictures', '');
$arr['price'] = request::post('price', '');
$arr['unit'] = request::post('unit', '');
$arr['stock'] = request::post('stock', '');
$arr['content'] = request::post('content', '');
return $arr;
}
}