Create.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. use Dever\Support\Img as Handle;
  5. class Create
  6. {
  7. # 自动创建
  8. public function up($info_id, $source_id, $name, $desc, $year, $type = 11, $data, $cover = '', $num = 9)
  9. {
  10. Dever::config('base')->hook = true;
  11. $where['info_id'] = $info_id;
  12. $where['clear'] = true;
  13. $info = Dever::db('collection/content')->getNew($where);
  14. $result = array();
  15. $result['cover'] = $cover;
  16. if ($type == 11) {
  17. if (is_string($data)) {
  18. $data = explode(',', $data);
  19. }
  20. if (!$cover) {
  21. $cover = $data[0];
  22. $result['cover'] = $cover;
  23. $cover = Dever::load('upload/view')->get($cover, 't', 1);
  24. $cover = Dever::load('upload/view')->get($cover, 'c', 1);
  25. }
  26. }
  27. $result['cover_center'] = $cover;
  28. # 内容最大值
  29. $content_max = $num;
  30. # 章节最大值
  31. $parent_page_max = 12;
  32. # 子章节最大值
  33. $page_max = 4;
  34. if ($info) {
  35. $result['page_id'] = $info['page_id'];
  36. $page = Dever::db('collection/page')->find($info['page_id']);
  37. if (!$page) {
  38. return false;
  39. }
  40. $result['parent_times_id'] = $page['times_id_parent'];
  41. $result['times_id'] = $page['times_id'];
  42. $result['parent_page_id'] = $page['page_id'];
  43. if ($info['reorder'] >= $content_max) {
  44. $result['reorder'] = $info['reorder'] = 1;
  45. if ($page['reorder'] >= $page_max) {
  46. $parent_page = Dever::db('collection/page')->find($result['parent_page_id']);
  47. if ($parent_page['reorder'] >= $parent_page_max) {
  48. $result['parent_page_reorder'] = 1;
  49. $times = Dever::db('collection/times')->find($page['times_id']);
  50. $result['times_reorder'] = $times['reorder'] + 1;
  51. $result['times_id'] = $this->createTimes($info_id, $source_id, $result['times_reorder'], '', $times['year'] + 1, $result['parent_times_id']);
  52. } else {
  53. $result['parent_page_reorder'] = $parent_page['reorder'] + 1;
  54. }
  55. $result['parent_page_id'] = $this->createPage($info_id, $source_id, $result['parent_times_id'], $result['times_id'], $result['parent_page_reorder']);
  56. $result['page_reorder'] = 1;
  57. } else {
  58. # 直接创建子章节
  59. $result['page_reorder'] = $page['reorder'] + 1;
  60. }
  61. $result['page_id'] = $this->createPage($info_id, $source_id, $result['parent_times_id'], $result['times_id'], $result['page_reorder'], $result['parent_page_id'], $desc, $cover);
  62. } else {
  63. $result['reorder'] = $info['reorder'] + 1;
  64. }
  65. $result['content_id'] = $this->createContent($info_id, $source_id, $result['page_id'], $type, $result['reorder'], $cover);
  66. } else {
  67. $result['reorder'] = $result['times_reorder'] = $result['parent_page_reorder'] = $result['page_reorder'] = 1;
  68. $result['parent_times_id'] = $this->createTimes($info_id, $source_id, $result['times_reorder'], $name);
  69. if ($result['parent_times_id']) {
  70. $result['times_id'] = $this->createTimes($info_id, $source_id, $result['times_reorder'], '', $year, $result['parent_times_id']);
  71. if ($result['times_id']) {
  72. $result['parent_page_id'] = $this->createPage($info_id, $source_id, $result['parent_times_id'], $result['times_id'], $result['parent_page_reorder'], -1);
  73. if ($result['parent_page_id']) {
  74. $result['page_id'] = $this->createPage($info_id, $source_id, $result['parent_times_id'], $result['times_id'], $result['page_reorder'], $result['parent_page_id'], $desc, $cover);
  75. if ($result['page_id']) {
  76. $result['content_id'] = $this->createContent($info_id, $source_id, $result['page_id'], $type, $result['reorder'], $cover);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. if ($result['content_id'] && $data) {
  83. if ($type == 11) {
  84. $this->createPic($info_id, $result['content_id'], $data);
  85. }
  86. }
  87. return $result;
  88. }
  89. # 生成时光
  90. public function createTimes($info_id, $source_id, $reorder, $name, $year = '', $parent = -1)
  91. {
  92. $table = 'collection/times';
  93. $where['info_id'] = $info_id;
  94. $where['source_id'] = $source_id;
  95. $where['parent_id'] = $parent;
  96. if ($name) {
  97. $where['name'] = $name;
  98. } else {
  99. $where['name'] = '第' . $reorder . '卷';
  100. }
  101. $param = array();
  102. if ($year) {
  103. $param['year'] = $year;
  104. }
  105. $param['reorder'] = $reorder;
  106. return Dever::upinto($table, $where, $param);
  107. }
  108. # 生成章节
  109. public function createPage($info_id, $source_id, $times_id_parent, $times_id, $reorder = 1, $parent_page_id = -1, $desc = '', $pic = '')
  110. {
  111. $table = 'collection/page';
  112. $where['info_id'] = $info_id;
  113. $where['source_id'] = $source_id;
  114. $where['page_id'] = $parent_page_id;
  115. if ($parent_page_id > 0) {
  116. $where['name'] = '第' . $reorder . '章';
  117. } else {
  118. $where['name'] = '第' . $reorder . '季';
  119. }
  120. $where['times_id_parent'] = $times_id_parent;
  121. $where['times_id'] = $times_id;
  122. $param = array();
  123. $param['reorder'] = $reorder;
  124. if ($desc) {
  125. $param['desc'] = $desc;
  126. }
  127. if ($pic) {
  128. $param['pic'] = $pic;
  129. }
  130. $id = Dever::upinto($table, $where, $param);
  131. return $id;
  132. }
  133. # 生成内容
  134. public function createContent($info_id, $source_id, $page_id, $type, $reorder, $pic)
  135. {
  136. $table = 'collection/content';
  137. $where['info_id'] = $info_id;
  138. $where['source_id'] = $source_id;
  139. $where['name'] = '第' . $reorder . '篇';
  140. $where['page_id'] = $page_id;
  141. $where['type'] = $type;
  142. $param = array();
  143. if ($pic) {
  144. $param['pic'] = $pic;
  145. }
  146. $param['reorder'] = $reorder;
  147. return Dever::upinto($table, $where, $param);
  148. }
  149. # 生成图片
  150. public function createPic($id, $content_id, $pic)
  151. {
  152. $table = 'content/pic';
  153. $where['info_id'] = $id;
  154. $where['content_id'] = $content_id;
  155. foreach ($pic as $k => $v) {
  156. $where['pic_key'] = md5($v);
  157. $param['pic'] = $v;
  158. $info = parse_url($v);
  159. $v = ltrim($info['path'], '/');
  160. $file = Dever::db('upload/file')->find(array('file' => $v));
  161. if ($file) {
  162. $param['pic_info'] = $file['width'] . ',' . $file['height'] . ',' . $file['size'];
  163. }
  164. Dever::upinto($table, $where, $param);
  165. Dever::load('collection/lib/content')->updateTypeId($id, '', $where);
  166. /*
  167. $v = Dever::curl($v . '?imageInfo');
  168. $v = Dever::json_decode($v);
  169. if (isset($v['width']) && $v['width'] > 0) {
  170. Dever::upinto($table, $where, $param);
  171. Dever::load('collection/lib/content')->updateTypeId($id, '', $where);
  172. }
  173. */
  174. }
  175. }
  176. }