ItemController.class.php 14 KB

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