ItemController.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $item_id = I("item_id");
  53. $keyword = I("keyword");
  54. $login_user = session("login_user");
  55. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  56. $this->checkItemVisit($uid , $item_id);
  57. $item = D("Item")->where("item_id = '$item_id' ")->find();
  58. //是否有搜索词
  59. if ($keyword) {
  60. $pages = D("Page")->where("item_id = '$item_id' and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `order` asc ")->select();
  61. }else{
  62. //获取所有父目录id为0的页面
  63. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `order` asc ")->select();
  64. //获取所有目录
  65. $catalogs = D("Catalog")->where("item_id = '$item_id' ")->order(" `order` asc ")->select();
  66. if ($catalogs) {
  67. foreach ($catalogs as $key => &$catalog) {
  68. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `order` asc ")->select();
  69. $catalog['pages'] = $temp ? $temp: array();
  70. }
  71. }
  72. }
  73. $share_url = get_domain().__APP__.'/'.$item_id;
  74. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  75. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  76. $this->assign("keyword" , $keyword);
  77. $this->assign("ItemPermn" , $ItemPermn);
  78. $this->assign("ItemCreator" , $ItemCreator);
  79. $this->assign("share_url" , $share_url);
  80. $this->assign("catalogs" , $catalogs);
  81. $this->assign("pages" , $pages);
  82. $this->assign("item" , $item);
  83. $this->assign("login_user" , $login_user);
  84. $this->display();
  85. }
  86. //删除项目
  87. public function delete(){
  88. $item_id = I("item_id");
  89. $login_user = $this->checkLogin();
  90. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  91. $this->message("你无权限");
  92. return;
  93. }
  94. $this->assign("item_id" , $item_id);
  95. $this->display();
  96. }
  97. //删除项目
  98. public function ajaxDelete(){
  99. $login_user = $this->checkLogin();
  100. $item_id = I("item_id");
  101. $password = I("password");
  102. $item = D("Item")->where("item_id = '$item_id' ")->find();
  103. if(! D("User")-> checkLogin($item['username'],$password)){
  104. $return['error_code'] = 10102 ;
  105. $return['error_message'] = '密码错误' ;
  106. $this->sendResult($return);
  107. return ;
  108. }
  109. D("Page")->where("item_id = '$item_id' ")->limit(1000)->delete();
  110. D("Catalog")->where("item_id = '$item_id' ")->limit(100)->delete();
  111. D("PageHistory")->where("item_id = '$item_id' ")->limit(1000)->delete();
  112. $return = D("Item")->where("item_id = '$item_id' ")->limit(1)->delete();
  113. if (!$return) {
  114. $return['error_code'] = 10103 ;
  115. $return['error_message'] = 'request fail' ;
  116. }
  117. $this->sendResult($return);
  118. }
  119. //输入访问密码
  120. public function pwd(){
  121. $item_id = I("item_id");
  122. if (!IS_POST) {
  123. $this->assign("item_id" , $item_id);
  124. $this->display ();
  125. }else{
  126. $password = I("password");
  127. $v_code = I("v_code");
  128. if ($v_code && $v_code == session('v_code')) {
  129. $item = D("Item")->where("item_id = '$item_id' ")->find();
  130. if ($item['password'] == $password) {
  131. session("visit_item_".$item_id , 1 );
  132. header("location:".U("Home/item/show").'?item_id='.$item_id);
  133. }else{
  134. $this->message("访问密码不正确");
  135. }
  136. }else{
  137. $this->message("验证码不正确");
  138. }
  139. }
  140. }
  141. //导出word
  142. public function word(){
  143. import("Vendor.Parsedown.Parsedown");
  144. $Parsedown = new \Parsedown();
  145. $item_id = I("item_id");
  146. $login_user = $this->checkLogin();
  147. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  148. $this->message("你无权限");
  149. return;
  150. }
  151. $item = D("Item")->where("item_id = '$item_id' ")->find();
  152. //获取所有父目录id为0的页面
  153. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `order` asc ")->select();
  154. //获取所有目录
  155. $catalogs = D("Catalog")->where("item_id = '$item_id' ")->order(" `order` asc ")->select();
  156. if ($catalogs) {
  157. foreach ($catalogs as $key => &$catalog) {
  158. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `order` asc ")->select();
  159. $catalog['pages'] = $temp ? $temp: array();
  160. }
  161. }
  162. $data = '';
  163. $parent = 1;
  164. if ($pages) {
  165. foreach ($pages as $key => $value) {
  166. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  167. $data .= '<div style="margin-left:20px;">';
  168. $data .= $Parsedown->text($value['page_content']);
  169. $data .= '</div>';
  170. $parent ++;
  171. }
  172. }
  173. //var_export($catalogs);
  174. if ($catalogs) {
  175. foreach ($catalogs as $key => $value) {
  176. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  177. $data .= '<div style="margin-left:20px;">';
  178. $child = 1 ;
  179. if ($value['pages']) {
  180. foreach ($value['pages'] as $page) {
  181. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  182. $data .= '<div style="margin-left:20px;">';
  183. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  184. $data .= '</div>';
  185. $child ++;
  186. }
  187. }
  188. $data .= '</div>';
  189. $parent ++;
  190. }
  191. }
  192. output_word($data,$item['item_name']);
  193. }
  194. }