ItemController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 ".C('DB_PREFIX')."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 item_id from ".C('DB_PREFIX')."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. $item_domain = I("item_domain");
  39. if(!ctype_alnum($item_domain)){
  40. //echo '个性域名只能是字母或数字的组合';exit;
  41. $this->message(L('item_domain_illegal'));
  42. return false;
  43. }
  44. $password = I("password");
  45. $item_description = I("item_description");
  46. if ($item_id > 0 ) {
  47. $data = array(
  48. "item_name" => $item_name ,
  49. "item_domain" => $item_domain ,
  50. "password" => $password ,
  51. "item_description" => $item_description ,
  52. );
  53. $ret = D("Item")->where("item_id = '$item_id' ")->save($data);
  54. }else{
  55. $insert = array(
  56. "uid" => $login_user['uid'] ,
  57. "username" => $login_user['username'] ,
  58. "item_name" => $item_name ,
  59. "password" => $password ,
  60. "item_description" => $item_description ,
  61. "item_domain" => $item_domain ,
  62. "addtime" =>time()
  63. );
  64. $ret = D("Item")->add($insert);
  65. }
  66. if ($ret) {
  67. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  68. }else{
  69. $this->message(L('operation_failed'),U('Home/Item/index'));
  70. }
  71. }
  72. }
  73. //展示单个项目
  74. public function show(){
  75. $this->checkLogin(false);
  76. $item_id = I("item_id/d");
  77. $item_domain = I("item_domain/s");
  78. $current_page_id = I("page_id/d");
  79. //判断个性域名
  80. if ($item_domain) {
  81. $item_domain = mysql_escape_string($item_domain);
  82. $item = D("Item")->where("item_domain = '$item_domain' ")->find();
  83. if ($item['item_id']) {
  84. $item_id = $item['item_id'] ;
  85. }
  86. }
  87. $keyword = I("keyword");
  88. $login_user = session("login_user");
  89. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  90. $this->checkItemVisit($uid , $item_id);
  91. $item = D("Item")->where("item_id = '$item_id' ")->find();
  92. //是否有搜索词
  93. if ($keyword) {
  94. $keyword = mysql_escape_string($keyword);
  95. $pages = D("Page")->where("item_id = '$item_id' and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `s_number` asc ")->select();
  96. }else{
  97. //获取所有父目录id为0的页面
  98. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  99. //获取所有二级目录
  100. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  101. if ($catalogs) {
  102. foreach ($catalogs as $key => &$catalog) {
  103. //该二级目录下的所有子页面
  104. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  105. $catalog['pages'] = $temp ? $temp: array();
  106. //该二级目录下的所有子目录
  107. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  108. $catalog['catalogs'] = $temp ? $temp: array();
  109. if($catalog['catalogs']){
  110. //获取所有三级目录的子页面
  111. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  112. //该二级目录下的所有子页面
  113. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  114. $catalog3['pages'] = $temp ? $temp: array();
  115. }
  116. }
  117. }
  118. }
  119. }
  120. $share_url = get_domain().__APP__.'/'.$item_id;
  121. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  122. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  123. $this->assign("current_page_id" , $current_page_id);
  124. $this->assign("keyword" , $keyword);
  125. $this->assign("ItemPermn" , $ItemPermn);
  126. $this->assign("ItemCreator" , $ItemCreator);
  127. $this->assign("share_url" , $share_url);
  128. $this->assign("catalogs" , $catalogs);
  129. $this->assign("pages" , $pages);
  130. $this->assign("item" , $item);
  131. $this->assign("login_user" , $login_user);
  132. $this->display();
  133. }
  134. //删除项目
  135. public function delete(){
  136. $item_id = I("item_id");
  137. $login_user = $this->checkLogin();
  138. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  139. $this->message(L('no_permissions'));
  140. return;
  141. }
  142. $this->assign("item_id" , $item_id);
  143. $this->display();
  144. }
  145. //删除项目
  146. public function ajaxDelete(){
  147. $login_user = $this->checkLogin();
  148. $item_id = I("item_id/d");
  149. $password = I("password");
  150. $item = D("Item")->where("item_id = '$item_id' ")->find();
  151. if(! D("User")-> checkLogin($item['username'],$password)){
  152. $return['error_code'] = 10102 ;
  153. $return['error_message'] = L('incorrect_password') ;
  154. $this->sendResult($return);
  155. return ;
  156. }
  157. D("Page")->where("item_id = '$item_id' ")->delete();
  158. D("Catalog")->where("item_id = '$item_id' ")->delete();
  159. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  160. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  161. if (!$return) {
  162. $return['error_code'] = 10103 ;
  163. $return['error_message'] = 'request fail' ;
  164. }
  165. $this->sendResult($return);
  166. }
  167. //输入访问密码
  168. public function pwd(){
  169. $item_id = I("item_id/d");
  170. if (!IS_POST) {
  171. $this->assign("item_id" , $item_id);
  172. $this->display ();
  173. }else{
  174. $password = I("password");
  175. $v_code = I("v_code");
  176. if ($v_code && $v_code == session('v_code')) {
  177. $item = D("Item")->where("item_id = '$item_id' ")->find();
  178. if ($item['password'] == $password) {
  179. session("visit_item_".$item_id , 1 );
  180. header("location:".U("Home/Item/show").'&item_id='.$item_id);
  181. }else{
  182. $this->message(L('access_password_are_incorrect'));
  183. }
  184. }else{
  185. $this->message(L('verification_code_are_incorrect'));
  186. }
  187. }
  188. }
  189. //导出word
  190. public function word(){
  191. import("Vendor.Parsedown.Parsedown");
  192. $Parsedown = new \Parsedown();
  193. $item_id = I("item_id/d");
  194. $login_user = $this->checkLogin();
  195. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  196. $this->message(L('no_permissions'));
  197. return;
  198. }
  199. $item = D("Item")->where("item_id = '$item_id' ")->find();
  200. //获取所有父目录id为0的页面
  201. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  202. //获取所有二级目录
  203. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  204. if ($catalogs) {
  205. foreach ($catalogs as $key => &$catalog) {
  206. //该二级目录下的所有子页面
  207. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  208. $catalog['pages'] = $temp ? $temp: array();
  209. //该二级目录下的所有子目录
  210. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  211. $catalog['catalogs'] = $temp ? $temp: array();
  212. if($catalog['catalogs']){
  213. //获取所有三级目录的子页面
  214. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  215. //该二级目录下的所有子页面
  216. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  217. $catalog3['pages'] = $temp ? $temp: array();
  218. }
  219. }
  220. }
  221. }
  222. $data = '';
  223. $parent = 1;
  224. if ($pages) {
  225. foreach ($pages as $key => $value) {
  226. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  227. $data .= '<div style="margin-left:20px;">';
  228. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  229. $data .= '</div>';
  230. $parent ++;
  231. }
  232. }
  233. //var_export($catalogs);
  234. if ($catalogs) {
  235. foreach ($catalogs as $key => $value) {
  236. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  237. $data .= '<div style="margin-left:20px;">';
  238. $child = 1 ;
  239. if ($value['pages']) {
  240. foreach ($value['pages'] as $page) {
  241. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  242. $data .= '<div style="margin-left:20px;">';
  243. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  244. $data .= '</div>';
  245. $child ++;
  246. }
  247. }
  248. if ($value['catalogs']) {
  249. $parent2 = 1 ;
  250. foreach ($value['catalogs'] as $key3 => $value3) {
  251. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  252. $data .= '<div style="margin-left:20px;">';
  253. $child2 = 1 ;
  254. if ($value3['pages']) {
  255. foreach ($value3['pages'] as $page3) {
  256. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  257. $data .= '<div style="margin-left:30px;">';
  258. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  259. $data .= '</div>';
  260. $child2 ++;
  261. }
  262. }
  263. $data .= '</div>';
  264. $parent2 ++;
  265. }
  266. }
  267. $data .= '</div>';
  268. $parent ++;
  269. }
  270. }
  271. output_word($data,$item['item_name']);
  272. }
  273. }