ExportController.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ExportController extends BaseController {
  5. //导出整个项目为word
  6. public function word(){
  7. import("Vendor.Parsedown.Parsedown");
  8. $Parsedown = new \Parsedown();
  9. $item_id = I("item_id/d");
  10. $login_user = $this->checkLogin();
  11. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  12. $this->message(L('no_permissions'));
  13. return;
  14. }
  15. $item = D("Item")->where("item_id = '$item_id' ")->find();
  16. //获取所有父目录id为0的页面
  17. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  18. //获取所有二级目录
  19. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->order(" `s_number` asc ")->select();
  20. if ($catalogs) {
  21. foreach ($catalogs as $key => &$catalog) {
  22. //该二级目录下的所有子页面
  23. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  24. $catalog['pages'] = $temp ? $temp: array();
  25. //该二级目录下的所有子目录
  26. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  27. $catalog['catalogs'] = $temp ? $temp: array();
  28. if($catalog['catalogs']){
  29. //获取所有三级目录的子页面
  30. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  31. //该二级目录下的所有子页面
  32. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  33. $catalog3['pages'] = $temp ? $temp: array();
  34. }
  35. }
  36. }
  37. }
  38. $data = '';
  39. $parent = 1;
  40. if ($pages) {
  41. foreach ($pages as $key => $value) {
  42. $data .= "<h1>{$parent}、{$value['page_title']}</h1>";
  43. $data .= '<div style="margin-left:20px;">';
  44. $data .= htmlspecialchars_decode($Parsedown->text($value['page_content']));
  45. $data .= '</div>';
  46. $parent ++;
  47. }
  48. }
  49. //var_export($catalogs);
  50. if ($catalogs) {
  51. foreach ($catalogs as $key => $value) {
  52. $data .= "<h1>{$parent}、{$value['cat_name']}</h1>";
  53. $data .= '<div style="margin-left:20px;">';
  54. $child = 1 ;
  55. if ($value['pages']) {
  56. foreach ($value['pages'] as $page) {
  57. $data .= "<h2>{$parent}.{$child}、{$page['page_title']}</h2>";
  58. $data .= '<div style="margin-left:20px;">';
  59. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  60. $data .= '</div>';
  61. $child ++;
  62. }
  63. }
  64. if ($value['catalogs']) {
  65. $parent2 = 1 ;
  66. foreach ($value['catalogs'] as $key3 => $value3) {
  67. $data .= "<h2>{$parent}.{$parent2}、{$value3['cat_name']}</h2>";
  68. $data .= '<div style="margin-left:20px;">';
  69. $child2 = 1 ;
  70. if ($value3['pages']) {
  71. foreach ($value3['pages'] as $page3) {
  72. $data .= "<h3>{$parent}.{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  73. $data .= '<div style="margin-left:30px;">';
  74. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  75. $data .= '</div>';
  76. $child2 ++;
  77. }
  78. }
  79. $data .= '</div>';
  80. $parent2 ++;
  81. }
  82. }
  83. $data .= '</div>';
  84. $parent ++;
  85. }
  86. }
  87. output_word($data,$item['item_name']);
  88. }
  89. //把指定目录导出为word
  90. public function word_cat(){
  91. import("Vendor.Parsedown.Parsedown");
  92. $Parsedown = new \Parsedown();
  93. $item_id = I("item_id/d");
  94. $cat_id = I("cat_id/d");
  95. $login_user = $this->checkLogin();
  96. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  97. $this->message(L('no_permissions'));
  98. return;
  99. }
  100. $item = D("Item")->where("item_id = '$item_id' ")->find();
  101. //获取指定目录。先按照目录是二级目录的逻辑来处理
  102. $catalog = D("Catalog")->where("item_id = '$item_id' and cat_id = '$cat_id' and level =2 ")->order(" `s_number` asc ")->find();
  103. if (!empty($catalog)) {
  104. //该二级目录下的所有子页面
  105. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  106. $catalog['pages'] = $temp ? $temp: array();
  107. //该二级目录下的所有子目录
  108. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  109. $catalog['catalogs'] = $temp ? $temp: array();
  110. if($catalog['catalogs']){
  111. //获取所有三级目录的子页面
  112. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  113. //该二级目录下的所有子页面
  114. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->order(" `s_number` asc ")->select();
  115. $catalog3['pages'] = $temp ? $temp: array();
  116. }
  117. }
  118. }else{
  119. //获取指定目录。按照目录是三级目录的逻辑来处理
  120. $catalog = D("Catalog")->where("item_id = '$item_id' and cat_id = '$cat_id' and level =3 ")->order(" `s_number` asc ")->find();
  121. if (!empty($catalog)) {
  122. //该三级目录下的所有子页面
  123. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->order(" `s_number` asc ")->select();
  124. $catalog['pages'] = $temp ? $temp: array();
  125. }
  126. }
  127. $data = '';
  128. $parent = 1;
  129. //var_export($catalog);exit();
  130. $data .= "<h1>{$catalog['cat_name']}</h1>";
  131. $data .= '<div style="margin-left:20px;">';
  132. $child = 1 ;
  133. if ($catalog['pages']) {
  134. foreach ($catalog['pages'] as $page) {
  135. $data .= "<h2>{$child}、{$page['page_title']}</h2>";
  136. $data .= '<div style="margin-left:20px;">';
  137. $data .= htmlspecialchars_decode($Parsedown->text($page['page_content']));
  138. $data .= '</div>';
  139. $child ++;
  140. }
  141. }
  142. if ($catalog['catalogs']) {
  143. $parent2 = 1 ;
  144. foreach ($catalog['catalogs'] as $key3 => $value3) {
  145. $data .= "<h2>{$parent2}、{$value3['cat_name']}</h2>";
  146. $data .= '<div style="margin-left:20px;">';
  147. $child2 = 1 ;
  148. if ($value3['pages']) {
  149. foreach ($value3['pages'] as $page3) {
  150. $data .= "<h3>{$parent2}.{$child2}、{$page3['page_title']}</h3>";
  151. $data .= '<div style="margin-left:30px;">';
  152. $data .= htmlspecialchars_decode($Parsedown->text($page3['page_content']));
  153. $data .= '</div>';
  154. $child2 ++;
  155. }
  156. }
  157. $data .= '</div>';
  158. $parent2 ++;
  159. }
  160. }
  161. $data .= '</div>';
  162. output_word($data,$item['item_name']);
  163. }
  164. //把指定页面导出为word
  165. public function word_page(){
  166. import("Vendor.Parsedown.Parsedown");
  167. $Parsedown = new \Parsedown();
  168. $item_id = I("item_id/d");
  169. $page_id = I("page_id/d");
  170. $login_user = $this->checkLogin();
  171. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  172. $this->message(L('no_permissions'));
  173. return;
  174. }
  175. $temp = D("Page")->where("page_id = '$page_id' ")->order(" `s_number` asc ")->find();
  176. $page= $temp ? $temp: array();
  177. $data = htmlspecialchars_decode($Parsedown->text($page['page_content']));
  178. output_word($data,$page['page_title']);
  179. }
  180. }