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.

180 lines
5.0 KiB

8 years ago
  1. <?php
  2. namespace app\fladmin\controller;
  3. class Product extends Base
  4. {
  5. public function _initialize()
  6. {
  7. parent::_initialize();
  8. }
  9. public function index()
  10. {
  11. $where = array();
  12. if(isset($_REQUEST["keyword"]))
  13. {
  14. $where['title'] = array('like','%'.$_REQUEST['keyword'].'%');
  15. }
  16. if(isset($_REQUEST["typeid"]) && $_REQUEST["typeid"]!=0)
  17. {
  18. $where['typeid'] = $_REQUEST["typeid"];
  19. }
  20. if(isset($_REQUEST["id"]))
  21. {
  22. $where['typeid'] = $_REQUEST["id"];
  23. }
  24. $prolist = parent::pageList('product',$where);
  25. $posts = array();
  26. foreach($prolist as $key=>$value)
  27. {
  28. $info = db('product_type')->field('content',true)->where("id=".$value['typeid'])->find();
  29. $value['typename'] = $info['typename'];
  30. $posts[] = $value;
  31. }
  32. $this->assign('page',$prolist->render());
  33. $this->assign('posts',$posts);
  34. return $this->fetch();
  35. }
  36. public function add()
  37. {
  38. if(!empty($_GET["catid"])){$this->assign('catid',$_GET["catid"]);}else{$this->assign('catid',0);}
  39. return $this->fetch();
  40. }
  41. public function doadd()
  42. {
  43. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  44. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  45. $_POST['addtime'] = $_POST['pubdate'] = time(); //添加&更新时间
  46. $_POST['user_id'] = session('admin_user_info')['id']; // 发布者id
  47. //关键词
  48. if(!empty($_POST["keywords"]))
  49. {
  50. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  51. }
  52. else
  53. {
  54. if(!empty($_POST["title"]))
  55. {
  56. $title=$_POST["title"];
  57. $title=str_replace("","",$title);
  58. $title=str_replace(",","",$title);
  59. $_POST['keywords']=get_keywords($title);//标题分词
  60. }
  61. }
  62. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  63. if(db('product')->insert($_POST))
  64. {
  65. $this->success('添加成功!', FLADMIN.'/Product' , 1);
  66. }
  67. else
  68. {
  69. $this->error('添加失败!请修改后重新添加', FLADMIN.'/Product/add' , 3);
  70. }
  71. }
  72. public function edit()
  73. {
  74. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  75. $this->assign('id',$id);
  76. $this->assign('post',db('product')->where("id=$id")->find());
  77. return $this->fetch();
  78. }
  79. public function doedit()
  80. {
  81. if(!empty($_POST["id"])){$id = $_POST["id"];}else {$id="";exit;}
  82. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  83. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}}//description
  84. $_POST['pubdate'] = time();//更新时间
  85. $_POST['user_id'] = session('admin_user_info')['id']; // 修改者id
  86. //关键词
  87. if(!empty($_POST["keywords"]))
  88. {
  89. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  90. }
  91. else
  92. {
  93. if(!empty($_POST["title"]))
  94. {
  95. $title=$_POST["title"];
  96. $title=str_replace("","",$title);
  97. $title=str_replace(",","",$title);
  98. $_POST['keywords']=get_keywords($title);//标题分词
  99. }
  100. }
  101. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  102. if(db('product')->where("id=$id")->update($_POST))
  103. {
  104. $this->success('修改成功!', FLADMIN.'/Product' , 1);
  105. }
  106. else
  107. {
  108. $this->error('修改失败!', FLADMIN.'/Product/edit?id='.$_POST["id"] , 3);
  109. }
  110. }
  111. public function del()
  112. {
  113. if(!empty($_GET["id"])){$id = $_GET["id"];}else{$this->error('删除失败!请重新提交',FLADMIN.'/Product' , 3);}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  114. if(db('product')->where("id in ($id)")->delete())
  115. {
  116. $this->success("$id ,删除成功", FLADMIN.'/Product' , 1);
  117. }
  118. else
  119. {
  120. $this->error("$id ,删除失败!请重新提交", FLADMIN.'/Product', 3);
  121. }
  122. }
  123. //商品推荐
  124. public function recommendarc()
  125. {
  126. if(!empty($_GET["id"])){$id = $_GET["id"];}else{$this->error('删除失败!请重新提交',FLADMIN.'/Product' , 3);} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  127. $data['tuijian'] = 1;
  128. if(db('product')->where("id in ($id)")->update($data))
  129. {
  130. $this->success("$id ,推荐成功", FLADMIN.'/Product', 1);
  131. }
  132. else
  133. {
  134. $this->error("$id ,推荐失败!请重新提交", FLADMIN.'/Product', 3);
  135. }
  136. }
  137. //商品是否存在
  138. public function productexists()
  139. {
  140. if(!empty($_GET["title"]))
  141. {
  142. $map['title'] = $_GET["title"];
  143. }
  144. else
  145. {
  146. $map['title']="";
  147. }
  148. if(!empty($_GET["id"]))
  149. {
  150. $map['id'] = array('NEQ',$_GET["id"]);
  151. }
  152. return db('product')->where($map)->count();
  153. }
  154. }