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.

122 lines
3.1 KiB

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