ItemController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. //读取需要置顶的项目
  10. $top_items = D("ItemTop")->where("uid = '$login_user[uid]'")->select();
  11. if ($top_items) {
  12. $top_item_ids = array() ;
  13. foreach ($top_items as $key => $value) {
  14. $top_item_ids[] = $value['item_id'];
  15. }
  16. foreach ($items as $key => $value) {
  17. $items[$key]['top'] = 0 ;
  18. if (in_array($value['item_id'], $top_item_ids) ) {
  19. $items[$key]['top'] = 1 ;
  20. $tmp = $items[$key] ;
  21. unset($items[$key]);
  22. array_unshift($items,$tmp) ;
  23. }
  24. }
  25. $items = array_values($items);
  26. }
  27. $share_url = get_domain().__APP__.'/uid/'.$login_user['uid'];
  28. $this->assign("items" , $items);
  29. $this->assign("login_user" , $login_user);
  30. $this->assign("share_url" , $share_url);
  31. $this->display();
  32. }
  33. //我公开的项目列表
  34. public function showByUid(){
  35. $login_user = $this->checkLogin(false); //如果用户有登录,则赋值给$login_user
  36. $uid = I("uid/d");
  37. $show_user = D("User")->where(" uid = '$uid' ")->find();
  38. if ($show_user) {
  39. $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();
  40. $this->assign("items" , $items);
  41. $this->assign("show_user" , $show_user);
  42. $this->assign("login_user" , $login_user);
  43. }
  44. if (LANG_SET == 'en-us') {
  45. $help_url = "http://api.pkbigdata.com/help-en";
  46. }
  47. else{
  48. $help_url = "http://api.pkbigdata.com/help";
  49. }
  50. $this->assign("help_url" , $help_url);
  51. $this->display();
  52. }
  53. //新建项目
  54. public function add(){
  55. $login_user = $this->checkLogin();
  56. $item_id = I("item_id/d");
  57. if (!IS_POST) {
  58. $item = D("Item")->where("item_id = '$item_id' ")->find();
  59. $this->assign("item" , $item);
  60. $this->display ();
  61. }else{
  62. $item_name = I("item_name");
  63. $item_domain = I("item_domain") ? I("item_domain") : '';
  64. $copy_item_id = I("copy_item_id");
  65. $password = I("password");
  66. $item_description = I("item_description");
  67. $item_type = I("item_type");
  68. if ($item_domain) {
  69. $item = D("Item")->where("item_domain = '%s' and item_id !='%s' ",array($item_domain,$item_id))->find();
  70. if ($item) {
  71. //个性域名已经存在
  72. $this->message(L('domain_already_exists'));
  73. return false;
  74. }
  75. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  76. //echo '个性域名只能是字母或数字的组合';exit;
  77. $this->message(L('item_domain_illegal'));
  78. return false;
  79. }
  80. }
  81. //如果是复制项目
  82. if ($copy_item_id > 0) {
  83. if (!$this->checkItemPermn($login_user['uid'] , $copy_item_id)) {
  84. $this->message(L('no_permissions'));
  85. return;
  86. }
  87. $ret = D("Item")->copy($copy_item_id,$login_user['uid'],$item_name,$item_description,$password,$item_domain);
  88. if ($ret) {
  89. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  90. }else{
  91. $this->message(L('operation_failed'),U('Home/Item/index'));
  92. }
  93. return ;
  94. }
  95. if ($item_id > 0 ) {
  96. $data = array(
  97. "item_name" => $item_name ,
  98. "item_domain" => $item_domain ,
  99. "password" => $password ,
  100. "item_description" => $item_description ,
  101. );
  102. $ret = D("Item")->where("item_id = '$item_id' ")->save($data);
  103. }else{
  104. $insert = array(
  105. "uid" => $login_user['uid'] ,
  106. "username" => $login_user['username'] ,
  107. "item_name" => $item_name ,
  108. "password" => $password ,
  109. "item_description" => $item_description ,
  110. "item_domain" => $item_domain ,
  111. "item_type" => $item_type ,
  112. "addtime" =>time()
  113. );
  114. $item_id = D("Item")->add($insert);
  115. }
  116. if ($item_id) {
  117. //如果是单页应用,则新建一个默认页
  118. if ($item_type == 2 ) {
  119. $insert = array(
  120. 'author_uid' => $login_user['uid'] ,
  121. 'author_username' => $login_user['username'],
  122. "page_title" => $item_name ,
  123. "item_id" => $item_id ,
  124. "cat_id" => 0 ,
  125. "page_content" => '欢迎使用showdoc。点击右上方的编辑按钮进行编辑吧!' ,
  126. "addtime" =>time()
  127. );
  128. D("Page")->add($insert);
  129. }
  130. $this->message(L('operation_succeeded'),U('Home/Item/index'));
  131. }else{
  132. $this->message(L('operation_failed'),U('Home/Item/index'));
  133. }
  134. }
  135. }
  136. //根据项目类型展示项目
  137. public function show(){
  138. $this->checkLogin(false);
  139. $item_id = I("item_id/d");
  140. $item_domain = I("item_domain/s");
  141. $current_page_id = I("page_id/d");
  142. //判断个性域名
  143. if ($item_domain) {
  144. $item = D("Item")->where("item_domain = '%s'",array($item_domain))->find();
  145. if ($item['item_id']) {
  146. $item_id = $item['item_id'] ;
  147. }
  148. }
  149. $login_user = session("login_user");
  150. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  151. $this->checkItemVisit($uid , $item_id);
  152. $item = D("Item")->where("item_id = '$item_id' ")->find();
  153. if ($item['item_type'] == 1 ) {
  154. $this->_show_regular_item($item);
  155. }
  156. elseif ($item['item_type'] == 2 ) {
  157. $this->_show_single_page_item($item);
  158. }else{
  159. $this->_show_regular_item($item);
  160. }
  161. }
  162. //展示常规项目
  163. private function _show_regular_item($item){
  164. $item_id = $item['item_id'];
  165. $current_page_id = I("page_id/d");
  166. $keyword = I("keyword");
  167. $login_user = session("login_user");
  168. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  169. //是否有搜索词
  170. if ($keyword) {
  171. $keyword = \SQLite3::escapeString($keyword) ;
  172. $pages = D("Page")->where("item_id = '$item_id' and ( page_title like '%{$keyword}%' or page_content like '%{$keyword}%' ) ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  173. }else{
  174. //获取所有父目录id为0的页面
  175. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  176. //获取所有二级目录
  177. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  178. if ($catalogs) {
  179. foreach ($catalogs as $key => &$catalog) {
  180. //该二级目录下的所有子页面
  181. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  182. $catalog['pages'] = $temp ? $temp: array();
  183. //该二级目录下的所有子目录
  184. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  185. $catalog['catalogs'] = $temp ? $temp: array();
  186. if($catalog['catalogs']){
  187. //获取所有三级目录的子页面
  188. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  189. //该二级目录下的所有子页面
  190. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->field("page_id,author_uid,cat_id,page_title,addtime")->select();
  191. $catalog3['pages'] = $temp ? $temp: array();
  192. }
  193. }
  194. }
  195. }
  196. }
  197. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  198. $share_url = get_domain().__APP__.'/'.$domain;
  199. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  200. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  201. if (LANG_SET == 'en-us') {
  202. $help_url = "http://api.pkbigdata.com/help-en";
  203. }
  204. else{
  205. $help_url = "http://api.pkbigdata.com/help";
  206. }
  207. $this->assign("help_url" , $help_url);
  208. $this->assign("current_page_id" , $current_page_id);
  209. $this->assign("keyword" , $keyword);
  210. $this->assign("ItemPermn" , $ItemPermn);
  211. $this->assign("ItemCreator" , $ItemCreator);
  212. $this->assign("share_url" , $share_url);
  213. $this->assign("catalogs" , $catalogs);
  214. $this->assign("pages" , $pages);
  215. $this->assign("item" , $item);
  216. $this->assign("login_user" , $login_user);
  217. $this->display("show_regular");
  218. }
  219. //展示单页项目
  220. private function _show_single_page_item($item){
  221. $item_id = $item['item_id'];
  222. $current_page_id = I("page_id/d");
  223. $login_user = session("login_user");
  224. $uid = $login_user['uid'] ? $login_user['uid'] : 0 ;
  225. //获取页面
  226. $page = D("Page")->where(" item_id = '$item_id' ")->find();
  227. $domain = $item['item_domain'] ? $item['item_domain'] : $item['item_id'];
  228. $share_url = get_domain().__APP__.'/'.$domain;
  229. $ItemPermn = $this->checkItemPermn($uid , $item_id) ;
  230. $ItemCreator = $this->checkItemCreator($uid , $item_id);
  231. $this->assign("current_page_id" , $current_page_id);
  232. $this->assign("ItemPermn" , $ItemPermn);
  233. $this->assign("ItemCreator" , $ItemCreator);
  234. $this->assign("share_url" , $share_url);
  235. $this->assign("catalogs" , $catalogs);
  236. $this->assign("page" , $page);
  237. $this->assign("item" , $item);
  238. $this->assign("login_user" , $login_user);
  239. $this->display("show_single_page");
  240. }
  241. //删除项目
  242. public function delete(){
  243. $item_id = I("item_id");
  244. $login_user = $this->checkLogin();
  245. if (!$this->checkItemCreator($login_user['uid'] , $item_id)) {
  246. $this->message(L('no_permissions'));
  247. return;
  248. }
  249. $this->assign("item_id" , $item_id);
  250. $this->display();
  251. }
  252. //删除项目
  253. public function ajaxDelete(){
  254. $login_user = $this->checkLogin();
  255. $item_id = I("item_id/d");
  256. $password = I("password");
  257. $item = D("Item")->where("item_id = '$item_id' ")->find();
  258. if(! D("User")-> checkLogin($item['username'],$password)){
  259. $return['error_code'] = 10102 ;
  260. $return['error_message'] = L('incorrect_password') ;
  261. $this->sendResult($return);
  262. return ;
  263. }
  264. D("Page")->where("item_id = '$item_id' ")->delete();
  265. D("Catalog")->where("item_id = '$item_id' ")->delete();
  266. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  267. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  268. $return = D("Item")->where("item_id = '$item_id' ")->delete();
  269. if (!$return) {
  270. $return['error_code'] = 10103 ;
  271. $return['error_message'] = 'request fail' ;
  272. }
  273. $this->sendResult($return);
  274. }
  275. //输入访问密码
  276. public function pwd(){
  277. $item_id = I("item_id/d");
  278. $CloseVerify = C('CloseVerify');
  279. $refer_url = I('refer_url');
  280. //var_dump(urldecode($refer_url));
  281. $this->assign('CloseVerify',$CloseVerify);
  282. $this->assign('refer_url',$refer_url);
  283. if (!IS_POST) {
  284. $this->assign("item_id" , $item_id);
  285. $this->display ();
  286. }else{
  287. $password = I("password");
  288. $v_code = I("v_code");
  289. if ( $CloseVerify || ( $v_code && $v_code == session('v_code') )) {
  290. $item = D("Item")->where("item_id = '$item_id' ")->find();
  291. if ($item['password'] == $password) {
  292. session("visit_item_".$item_id , 1 );
  293. if ($refer_url) {
  294. header("location:".base64_decode($refer_url));
  295. }else{
  296. header("location:".U("Home/Item/show").'&item_id='.$item_id);
  297. }
  298. }else{
  299. $this->message(L('access_password_are_incorrect'));
  300. }
  301. }else{
  302. $this->message(L('verification_code_are_incorrect'));
  303. }
  304. }
  305. }
  306. //导出word
  307. public function export(){
  308. $login_user = $this->checkLogin();
  309. $item_id = I("item_id/d");
  310. $uid = $login_user['uid'] ;
  311. $this->checkItemPermn($uid , $item_id) ;
  312. $item = D("Item")->where("item_id = '$item_id' ")->find();
  313. //对于单页项目,直接导出。对于普通项目,则让其选择目录
  314. if ($item['item_type'] == 2 ) {
  315. $url = 'server/index.php?s=/api/export/word&item_id='.$item_id ;
  316. header("location:{$url}");
  317. }else{
  318. $this->assign("item_id",$item_id);
  319. $this->display();
  320. }
  321. }
  322. public function itemList(){
  323. $login_user = $this->checkLogin();
  324. $items = D("Item")->where("uid = '$login_user[uid]' ")->select();
  325. $items = $items ? $items : array();
  326. $this->sendResult($items);
  327. }
  328. public function setting(){
  329. $login_user = $this->checkLogin();
  330. $item_id = I("item_id/d");
  331. $uid = $login_user['uid'] ;
  332. $this->checkItemPermn($uid , $item_id) ;
  333. $this->assign("item_id",$item_id);
  334. $this->display();
  335. }
  336. }