ExportController.class.php 8.9 KB

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