ItemController.class.php 8.8 KB

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