<?php
/**
* +----------------------------------------------------------------------
* | TickyPHP [ This is a freeware ]
* +----------------------------------------------------------------------
* | Copyright (c) 2015 All rights reserved.
* +----------------------------------------------------------------------
* | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
* +----------------------------------------------------------------------
* | SVN: $Id: Newsclass.php 09594 2018-09-04 09:29:53 luomingui $
* +----------------------------------------------------------------------
* | 文件功能:对应的表名:tky_newsclass
* +----------------------------------------------------------------------
*/
namespace application\admin\controller;
use ticky\request;
use ticky\response;
use ticky\tree;
class category extends auth {
public function index() {
$modelarr = getsitemodel();
$tree = new tree();
$tree->icon = array(' │ ', ' ├─ ', ' └─ ');
$tree->nbsp = ' ';
$data = db('category')->where(['siteid' => $_COOKIE['ticky_tickysite']])->order('sort ASC,catid ASC')->select();
$array = array();
foreach ($data as $v) {
if ($v['type'] == "0") {
$v['catlink'] = '/admin/content/manage/?action=add&modelid=1' . $v['modelid'] . '&catid=' . $v['catid'];
} elseif ($v['type'] == "1") {
$v['catlink'] = '/admin/category/page_content/?catid=' . $v['catid'];
} else {
$v['catlink'] = $v['pclink'] . "' target='_blank";
}
$addurl = '/admin/category/manage/?action=add&modelid=' . $v['modelid'] . '&type=' . $v['type'] . '&catid=' . $v['catid'] . '&parentid=' . $v['parentid'];
$editurl = '/admin/category/manage/?action=edit&type=' . $v['type'] . '&catid=' . $v['catid'] . '&parentid=' . $v['parentid'];
$delurl = '/admin/category/delete/?action=del&type=' . $v['type'] . '&id=' . $v['catid'] . '&parentid=' . $v['parentid'];
$v['id'] = $v['catid'];
$v['parentid'] = $v['parentid'];
$v['name'] = $v['catname'];
$v['cattype'] = $v['type'] == "0" ? '内部栏目' : ($v['type'] == "1" ? '<span style="color:green">单页面</span>' : '<span style="color:red">外部链接</span>');
$v['catmodel'] = $v['modelid'] ? $modelarr[$v['modelid']] : '';
$v['disabled'] = $v['disabled'] == '1' ? '<span style="color:red">是</span>' : '否';
$v['member_publish'] = $v['member_publish'] ? '<span style="color:red">是</span>' : '否';
$v['string'] = '<a title="增加子类" href="' . $addurl . '" class="btn btn-primary btn-xs" style="text-decoration:none">增加子类</a>
<a title="编辑栏目" href="' . $editurl . '" class="btn btn-success btn-xs" style="text-decoration:none">编辑</a>
<a title="删除" href="' . $delurl . '" class="btn btn-warning btn-xs" style="text-decoration:none">删除</a>';
$array[] = $v;
}
$str = "<tr class='text-c'>
<td><input type='text' class='input-text listorder' name='sort[]' value='\$sort' style='width: 50px;'><input type='hidden' name='id[]' value='\$id'></td>
<td>\$id</td>
<td class='text-l'>\$spacer<a href='\$catlink'>\$name</a></td>
<td>\$cattype</td>
<td>\$catmodel</td>
<td>\$disabled</td>
<td>\$member_publish</td>
<td class='td-manage'>\$string</td>
</tr>";
$tree->init($array);
$categorys = $tree->get_tree(0, $str);
$this->assign('items', $categorys);
$this->display('category/index');
}
public function sort() {
if (isset($_POST["dosubmit"])) {
foreach ($_POST['catid'] as $key => $val) {
$this->db->table('category')->where(['catid' => intval($val)])->update(array('sort' => $_POST['sort'][$key]));
}
showmsg('排序栏目成功');
}
}
private function getTree($data, $pid) {
$tree = '';
foreach ($data as $k => $v) {
if ($v['parentid'] == $pid) { $v['children'] = $this->getTree($data, $v['catid']);
$tree[] = $v;
}
}
return $tree;
}
public function add() {
if (request::isPost()) {
$catid = request::get('id', 0);
$type = request::get('type', 0);
if ($type != 2) { $res = $this->db->table('category')->where(array('catdir' => $_POST['catdir']))->find();
if ($res) {
showmsg('栏目目录已存在');
}
}
$data = $this->post_frm();
$catid = $this->db->table('category')->add($data);
if ($type != 2) { if ($type == 1) { $arr = array();
$arr['catid'] = $catid;
$arr['title'] = request::post('catname', '');
$arr['catdir'] = request::post('catdir', '');
$arr['keywords'] = request::post('seo_keywords', '');
$arr['description'] = request::post('seo_description', '');
$arr['updatetime'] = NOW_TIME;
$this->db->table('page')->add($arr);
$this->db->table('category')->where(array('catid' => $catid))->update(array('list_template' => '', 'show_template' => ''));
}
if (get_config('url_rule')) {
$_POST['pclink'] = URL_MODEL == 1 ? SITE_URL . 'index.php?s=/' . $_POST['catdir'] . '/' : SITE_URL . $_POST['catdir'] . '/';
} else {
$_POST['pclink'] = 'home/index/lists/?catid=' . $catid;
}
}
$this->db->table('category')->where(array('catid' => $catid))->update(array('arrchildid' => $catid, 'pclink' => $_POST['pclink'])); if ($_POST['parentid'] != '0') {
$this->repairs($_POST['arrparentid']);
}
showmsg('添加栏目成功');
} else {
$action = request::get('action', 'add');
$item['modelid'] = request::get('modelid', 0);
$item['type'] = request::get('type', 0);
$item['parentid'] = request::get('parentid', 0);
$item['catid'] = request::get('catid', 0);
$item['member_publish'] = 0;
$item['disabled'] = 0;
$item['openpl'] = 0;
$item['closed'] = 0;
$item["parentid"] = request::get('catid', 0);
$this->assign('postUrl', '/admin/category/add');
$this->assign('action', '添加');
$category_temp = select_template('category_temp', 'category_');
$list_temp = select_template('list_temp', 'list_');
$show_temp = select_template('show_temp', 'show_');
$this->assign('category_temp', $category_temp);
$this->assign('list_temp', $list_temp);
$this->assign('show_temp', $show_temp);
$this->assign('item', $item);
if ($item['type'] == 2) {
$title = "外部链接";
} else if ($item['type'] == 1) {
$title = "单页面";
} else {
$title = "内部栏目";
}
$this->assign('title', $title);
$this->display('category/manage');
}
}
public function update() {
if (request::isPost()) {
$data = $this->post_frm();
$id = request::post('catid', 0);
db('category')->where(['catid' => $id])->update($data);
showmsg('修改栏目成功');
} else {
$action = request::get('action', 'add');
$item['modelid'] = request::get('modelid', 0);
$item['type'] = request::get('type', 0);
$item['parentid'] = request::get('parentid', 0);
$item['catid'] = request::get('catid', 0);
$item = db('category')->where(['catid' => $item['catid']])->find();
$item['member_publish'] = $item['member_publish'] ? 0 : $item['member_publish'];
$item['openpl'] = $item['openpl'] ? 0 : $item['openpl'];
$item['closed'] = $item['closed'] ? 0 : $item['closed'];
$this->assign('postUrl', '/admin/category/update');
$this->assign('action', '修改');
$category_temp = select_template('category_temp', 'category_');
$list_temp = select_template('list_temp', 'list_');
$show_temp = select_template('show_temp', 'show_');
$this->assign('category_temp', $category_temp);
$this->assign('list_temp', $list_temp);
$this->assign('show_temp', $show_temp);
$this->assign('item', $item);
if ($item['type'] == 2) {
$title = "外部链接";
} else if ($item['type'] == 1) {
$title = "单页面";
} else {
$title = "内部栏目";
}
$this->assign('title', $title);
$this->display('category/manage');
}
}
public function delete() {
$id = request::post('id', 0);
$type = request::post('type', 0);
$data = $this->db->field('arrparentid,arrchildid')->where(array('catid' => $id))->find();
if (strpos($data['arrchildid'], ',')) {
showmsg('分类删除失败:该分类下有子分类!');
}
if (db('category')->delete(array('catid' => $id))) {
if ($type == 1) {
db('page')->delete(array('catid' => $id)); }
$this->repairs($data['arrparentid']);
delcache('categoryinfo');
delcache('mapping');
showmsg('分类删除成功!');
} else {
showmsg('分类删除失败!');
}
$this->db->table('category')->where(['catid' => $id])->delete();
showmsg('删除成功!');
}
private function post_frm() {
$arr = array();
$arr['siteid'] = $_COOKIE['ticky_tickysite'];
$arr['modelid'] = request::post('modelid', '');
$arr['parentid'] = request::post('parentid', '');
$arr['arrparentid'] = request::post('arrparentid', '');
$arr['arrchildid'] = request::post('arrchildid', '');
$arr['catname'] = request::post('catname', '');
$arr['subtitle'] = request::post('subtitle', '');
$arr['sort'] = request::post('sort', '');
$arr['type'] = request::post('type', '');
$arr['pclink'] = request::post('pclink', '');
$arr['member_publish'] = request::post('member_publish', '');
$arr['disabled'] = request::post('disabled', '');
$arr['catdir'] = request::post('catdir', '');
$arr['catimg'] = request::post('catimg', '');
$arr['category_template'] = request::post('category_template', '');
$arr['list_template'] = request::post('list_template', '');
$arr['show_template'] = request::post('show_template', '');
$arr['seo_title'] = request::post('seo_title', '');
$arr['seo_keywords'] = request::post('seo_keywords', '');
$arr['seo_description'] = request::post('seo_description', '');
if ($_POST['parentid'] == '0') {
$_POST['arrparentid'] = '0';
} else {
$data = db('category')->field('arrparentid,arrchildid')->where(array('catid' => $_POST['parentid']))->find();
$arr['arrparentid'] = $data["arrparentid"] . ',' . $_POST['parentid'];
}
return $arr;
}
private function repairs($arrparentid, $cpath = null) {
$data1 = explode(',', $arrparentid);
$data2 = $cpath ? explode(',', $cpath) : array();
$data = array_merge($data1, $data2);
foreach ($data as $val) {
if ($val) {
$this->repair($val);
}
}
}
private function repair($catid) {
$this->db->update(array('arrchildid' => $this->get_arrchildid($catid)), array('catid' => $catid)); }
private function get_arrchildid($catid) {
$arrchildid = $catid;
$data = $this->db->field('catid')->where("FIND_IN_SET('$catid',arrparentid)")->order('catid ASC')->select();
foreach ($data as $val) {
$arrchildid .= ',' . $val['catid'];
}
return $arrchildid;
}
public function page_content() {
$catid = request::get('catid', 0);
if (request::isPost()) {
$arr = array();
$arr['title'] = request::post('title', '');
$arr['catdir'] = request::post('catdir', '');
$arr['keywords'] = request::post('keywords', '');
$arr['description'] = request::post('description', '');
$arr['content'] = request::post('content', '');
$arr['updatetime'] = NOW_TIME;
db('page')->where(['catid' => $catid])->update($arr);
showmsg('单页内容更新成功!');
} else {
$item = db('page')->where(['catid' => $catid])->find();
$this->assign('title', '修改单页面');
$this->assign('item', $item);
$this->display('category/page_content');
}
}
}