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