You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. class CategoryController extends CommonController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function index()
  12. {
  13. $catlist = category_tree(get_category('arctype',0));
  14. if($catlist)
  15. {
  16. foreach($catlist as $k=>$v)
  17. {
  18. $arctype = DB::table("arctype")->where('id', $v['id'])->first();
  19. $catlist[$k]['typedir'] = $arctype->typedir;
  20. $catlist[$k]['addtime'] = $arctype->addtime;
  21. }
  22. }
  23. $data['catlist'] = $catlist;
  24. return view('admin.category.index', $data);
  25. }
  26. public function add()
  27. {
  28. if(!empty($_GET["reid"]))
  29. {
  30. $id = $_GET["reid"];
  31. if(preg_match('/[0-9]*/',$id)){}else{exit;}
  32. if($id!=0)
  33. {
  34. $data['postone'] = object_to_array(DB::table("arctype")->where('id', $id)->first(), 1);
  35. }
  36. $data['id'] = $id;
  37. }
  38. else
  39. {
  40. $data['id'] = 0;
  41. }
  42. return view('admin.category.add', $data);
  43. }
  44. public function doadd()
  45. {
  46. if(!empty($_POST["prid"])){if($_POST["prid"]=="top"){$_POST['pid']=0;}else{$_POST['pid'] = $_POST["prid"];}}//父级栏目id
  47. $_POST['addtime'] = time();//添加时间
  48. unset($_POST["prid"]);
  49. unset($_POST["_token"]);
  50. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  51. if(DB::table('arctype')->insert($_POST))
  52. {
  53. success_jump('添加成功!');
  54. }
  55. else
  56. {
  57. error_jump('添加失败!请修改后重新添加');
  58. }
  59. }
  60. public function edit()
  61. {
  62. $id = $_GET["id"];if(preg_match('/[0-9]*/',$id)){}else{exit;}
  63. $data['id'] = $id;
  64. $post = object_to_array(DB::table('arctype')->where('id', $id)->first(), 1);
  65. $reid = $post['pid'];
  66. if($reid!=0){$data['postone'] = object_to_array(DB::table('arctype')->where('id', $reid)->first());}
  67. $data['post'] = $post;
  68. return view('admin.category.edit', $data);
  69. }
  70. public function doedit()
  71. {
  72. if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else {$id="";exit;}
  73. $_POST['addtime'] = time(); //添加时间
  74. unset($_POST["_token"]);
  75. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  76. if(DB::table('arctype')->where('id', $id)->update($_POST))
  77. {
  78. success_jump('修改成功!', route('admin_category'));
  79. }
  80. else
  81. {
  82. error_jump('修改失败!请修改后重新添加');
  83. }
  84. }
  85. public function del()
  86. {
  87. if(!empty($_REQUEST["id"])){$id = $_REQUEST["id"];}else{error_jump('删除失败!请重新提交');} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  88. if(DB::table('arctype')->where('pid', $id)->first())
  89. {
  90. error_jump('删除失败!请先删除子栏目');
  91. }
  92. else
  93. {
  94. if(DB::table('arctype')->where('id', $id)->delete())
  95. {
  96. if(DB::table("article")->where('typeid', $id)->count()>0) //判断该分类下是否有文章,如果有把该分类下的文章也一起删除
  97. {
  98. if(DB::table("article")->where('typeid', $id)->delete())
  99. {
  100. success_jump('删除成功');
  101. }
  102. else
  103. {
  104. error_jump('栏目下的文章删除失败!');
  105. }
  106. }
  107. else
  108. {
  109. success_jump('删除成功');
  110. }
  111. }
  112. else
  113. {
  114. error_jump('删除失败!请重新提交');
  115. }
  116. }
  117. }
  118. }