ItemController.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class ItemController extends BaseController {
  5. //项目列表页
  6. public function index(){
  7. $login_user = $this->checkLogin();
  8. $items = D("Item")->where("uid = '$login_user[uid]' or item_id in ( select item_id from item_member where uid = '$login_user[uid]' ) ")->select();
  9. $this->assign("items" , $items);
  10. $this->assign("login_user" , $login_user);
  11. $this->display();
  12. }
  13. //新建项目
  14. public function add(){
  15. $login_user = $this->checkLogin();
  16. $item_id = I("item_id");
  17. if (!IS_POST) {
  18. $item = D("Item")->where("item_id = '$item_id' ")->find();
  19. $this->assign("item" , $item);
  20. $this->display ();
  21. }else{
  22. $item_name = I("item_name");
  23. $password = I("password");
  24. $item_description = I("item_description");
  25. if ($item_id > 0 ) {
  26. $data = array(
  27. "item_name" => $item_name ,
  28. "password" => $password ,
  29. "item_description" => $item_description ,
  30. );
  31. $ret = D("Item")->where("item_id = '$item_id' ")->save($data);
  32. }else{
  33. $insert = array(
  34. "uid" => $login_user['uid'] ,
  35. "username" => $login_user['username'] ,
  36. "item_name" => $item_name ,
  37. "password" => $password ,
  38. "item_description" => $item_description ,
  39. "addtime" =>time()
  40. );
  41. $ret = D("Item")->add($insert);
  42. }
  43. if ($ret) {
  44. $this->message("操作成功!",U('Home/Item/index'));
  45. }else{
  46. $this->message("操作失败!",U('Home/Item/index'));
  47. }
  48. }
  49. }
  50. //展示单个项目
  51. public function show(){
  52. $this->checkLogin(false);
  53. $item_id = I("item_id");
  54. $keyword = I("keyword");
  55. $login_user = session("login_user");
  56. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  57. $this->checkItemVisit($uid , $item_id);
  58. $item = D("Item")->where("item_id = '$item_id' ")->find();
  59. //是否有搜索词
  60. if ($keyword) {
  61. $pages = D("Page")->where("item_id = '$item_id' and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `order` asc ")->select();
  62. }else{
  63. //获取所有父目录id为0的页面
  64. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `order` asc ")->select();
  65. //获取所有目录
  66. $catalogs = D("Catalog")->where("item_id = '$item_id' ")->order(" `order` asc ")->select();
  67. if ($catalogs) {
  68. foreach ($catalogs as $key => &$catalog) {
  69. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `order` asc ")->select();
  70. $catalog['pages'] = $temp ? $temp: array();
  71. }
  72. }
  73. }
  74. $share_url = get_domain().__APP__.'/'.$item_id;
  75. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  76. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  77. $this->assign("keyword" , $keyword);
  78. $this->assign("ItemPermn" , $ItemPermn);
  79. $this->assign("ItemCreator" , $ItemCreator);
  80. $this->assign("share_url" , $share_url);
  81. $this->assign("catalogs" , $catalogs);
  82. $this->assign("pages" , $pages);
  83. $this->assign("item" , $item);
  84. $this->assign("login_user" , $login_user);
  85. $this->display();
  86. }
  87. //删除项目
  88. public function delete(){
  89. $item_id = I("item_id");
  90. $login_user = $this->checkLogin();
  91. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  92. $this->message("你无权限");
  93. return;
  94. }
  95. $this->assign("item_id" , $item_id);
  96. $this->display();
  97. }
  98. //删除项目
  99. public function ajaxDelete(){
  100. $login_user = $this->checkLogin();
  101. $item_id = I("item_id");
  102. $password = I("password");
  103. $item = D("Item")->where("item_id = '$item_id' ")->find();
  104. if(! D("User")-> checkLogin($item['username'],$password)){
  105. $return['error_code'] = 10102 ;
  106. $return['error_message'] = '密码错误' ;
  107. $this->sendResult($return);
  108. return ;
  109. }
  110. D("Page")->where("item_id = '$item_id' ")->limit(1000)->delete();
  111. D("Catalog")->where("item_id = '$item_id' ")->limit(100)->delete();
  112. D("PageHistory")->where("item_id = '$item_id' ")->limit(1000)->delete();
  113. $return = D("Item")->where("item_id = '$item_id' ")->limit(1)->delete();
  114. if (!$return) {
  115. $return['error_code'] = 10103 ;
  116. $return['error_message'] = 'request fail' ;
  117. }
  118. $this->sendResult($return);
  119. }
  120. //输入访问密码
  121. public function pwd(){
  122. $item_id = I("item_id");
  123. if (!IS_POST) {
  124. $this->assign("item_id" , $item_id);
  125. $this->display ();
  126. }else{
  127. $password = I("password");
  128. $v_code = I("v_code");
  129. if ($v_code && $v_code == session('v_code')) {
  130. $item = D("Item")->where("item_id = '$item_id' ")->find();
  131. if ($item['password'] == $password) {
  132. session("visit_item_".$item_id , 1 );
  133. header("location:".U("Home/item/show").'?item_id='.$item_id);
  134. }else{
  135. $this->message("访问密码不正确");
  136. }
  137. }else{
  138. $this->message("验证码不正确");
  139. }
  140. }
  141. }
  142. //导出word
  143. public function word(){
  144. import("Vendor.Parsedown.Parsedown");
  145. $Parsedown = new \Parsedown();
  146. $item_id = I("item_id");
  147. $login_user = $this->checkLogin();
  148. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  149. $this->message("你无权限");
  150. return;
  151. }
  152. $item = D("Item")->where("item_id = '$item_id' ")->find();
  153. //获取所有父目录id为0的页面
  154. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `order` asc ")->select();
  155. //获取所有目录
  156. $catalogs = D("Catalog")->where("item_id = '$item_id' ")->order(" `order` asc ")->select();
  157. if ($catalogs) {
  158. foreach ($catalogs as $key => &$catalog) {
  159. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `order` asc ")->select();
  160. $catalog['pages'] = $temp ? $temp: array();
  161. }
  162. }
  163. $data = '';
  164. $parent = 1;
  165. if ($pages) {
  166. foreach ($pages as $key => $value) {
  167. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  168. $data .= '<div style="margin-left:20px;">';
  169. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  170. $data .= '</div>';
  171. $parent ++;
  172. }
  173. }
  174. //var_export($catalogs);
  175. if ($catalogs) {
  176. foreach ($catalogs as $key => $value) {
  177. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  178. $data .= '<div style="margin-left:20px;">';
  179. $child = 1 ;
  180. if ($value['pages']) {
  181. foreach ($value['pages'] as $page) {
  182. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  183. $data .= '<div style="margin-left:20px;">';
  184. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  185. $data .= '</div>';
  186. $child ++;
  187. }
  188. }
  189. $data .= '</div>';
  190. $parent ++;
  191. }
  192. }
  193. output_word($data,$item['item_name']);
  194. }
  195. }