Data.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Page\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. # 直接获取某个页面的基本信息
  7. public function getPageInfo($key = false)
  8. {
  9. if (!$key) {
  10. $page = Dever::input('page');
  11. if ($page) {
  12. $key = $page;
  13. } else {
  14. $key = Dever::uri();
  15. }
  16. }
  17. if (!$key) {
  18. $key = 'home';
  19. }
  20. $where['key'] = $key;
  21. $data = Dever::db('page/page')->find($where);
  22. return $data;
  23. }
  24. # 直接获取某个页面的内容
  25. public function getPage($path = 'module', $cdate = 'Y-m-d H:i', $config = array())
  26. {
  27. $page = Dever::input('page');
  28. if ($page) {
  29. $uri = $page;
  30. } else {
  31. $uri = Dever::uri();
  32. }
  33. if (!$uri) {
  34. $uri = 'home';
  35. }
  36. $data = $this->getPageData($uri, $cdate);
  37. $result = array();
  38. if ($data && $data['module']) {
  39. foreach ($data['module'] as $k => $v) {
  40. $v['config'] = $config;
  41. $result[] = Dever::render($path . '/' . $v['key'], $v);
  42. }
  43. }
  44. return implode("\r\n", $result);
  45. }
  46. # 获取某个页面的数据
  47. public function getPageData($page_id, $cdate = 'Y-m-d H:i')
  48. {
  49. $where = array();
  50. if (is_numeric($page_id)) {
  51. $where['id'] = $page_id;
  52. } else {
  53. $where['key'] = $page_id;
  54. }
  55. $data = Dever::db('page/page')->find($where);
  56. if ($data) {
  57. $data['module'] = Dever::db('page/page_module')->getData(array('page_id' => $data['id']));
  58. if ($data['module']) {
  59. foreach ($data['module'] as $k => $v) {
  60. $where = array();
  61. $where['module_id'] = $v['id'];
  62. $where['limit'] = '0,' . $v['num'];
  63. $data['module'][$k]['data'] = Dever::db('page/data')->getAll($where);
  64. if ($data['module'][$k]['data']) {
  65. foreach ($data['module'][$k]['data'] as $k1 => $v1) {
  66. $data['module'][$k]['data'][$k1] = $this->getOne($v1, 2, $cdate);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return $data;
  73. }
  74. # 获取某个推送位的所有数据
  75. public function get($key, $name, $total = 10, $col = '1,2,3', $func = '-1', $page = false, $limit = false, $cdate = 'Y-m-d H:i')
  76. {
  77. $module = Dever::db('page/module')->one(array('key' => $key));
  78. if (!$module) {
  79. $insert['key'] = $key;
  80. $insert['name'] = $name;
  81. $insert['num'] = $total;
  82. $insert['col'] = $col;
  83. $insert['func'] = $func;
  84. $module['id'] = Dever::db('page/module')->insert($insert);
  85. $module['num'] = $insert['num'];
  86. }
  87. $data = array();
  88. if ($module) {
  89. if (!$module['num']) {
  90. $module['num'] = 20;
  91. }
  92. if ($limit > 0) {
  93. $module['num'] = $limit;
  94. }
  95. if ($module['content'] == 1 && $total > 0) {
  96. Dever::setInput('search_option_module_id', $module['id']);
  97. $where = array();
  98. $where['module_id'] = $module['id'];
  99. if ($page) {
  100. $where['page'] = array($module['num'], 'list');
  101. $data = Dever::db('page/data')->getAllPage($where);
  102. } else {
  103. $where['limit'] = '0,' . $module['num'];
  104. $data = Dever::db('page/data')->getAll($where);
  105. }
  106. if ($data) {
  107. foreach ($data as $k => $v) {
  108. $data[$k] = $this->getOne($v, 2, $cdate);
  109. }
  110. }
  111. } else {
  112. $data = $module;
  113. }
  114. }
  115. return $data;
  116. }
  117. # 获取某条推送位的数据
  118. public function getOne($module, $state = 2, $cdate = 'Y-m-d H:i')
  119. {
  120. $data = is_array($module) ? $module : Dever::db('page/data')->one($module);
  121. $source = array();
  122. $module = array();
  123. $col = array();
  124. if ($data['type'] > 0) {
  125. $func = Dever::db('page/func')->one($data['type']);
  126. if ($func) {
  127. if ($func['type'] == 1) {
  128. $method = $func['api'];
  129. $col = Dever::array_decode($func['col']);
  130. $source = Dever::load($method, $data['type_id']);
  131. } else {
  132. $data['type_id'] = $func['api'];
  133. }
  134. }
  135. }
  136. if ($state == 1) {
  137. $result = array();
  138. } else {
  139. $result = $data;
  140. $result['cdate_string'] = date($cdate, $result['cdate']);
  141. }
  142. $lang = Dever::getLang();
  143. if ($lang) {
  144. $key = $lang . '_col_';
  145. } else {
  146. $key = 'col_';
  147. }
  148. foreach ($data as $k => $v) {
  149. $s = strstr($k, $key);
  150. if ($lang && strstr($k, 'col_') && !$s) {
  151. $s = true;
  152. $key = 'col_';
  153. }
  154. if ($s) {
  155. $id = str_replace($key, '', $k);
  156. $col_module = Dever::db('page/col')->one($id);
  157. if (!$col_module) {
  158. continue;
  159. }
  160. if (!$v && $source && $col) {
  161. foreach ($col as $k1 => $v1) {
  162. if ($v1['col_id'] == $id && isset($source[$v1['name']]) && $source[$v1['name']]) {
  163. $v = $source[$v1['name']];
  164. }
  165. }
  166. }
  167. if (strstr($v, "\n")) {
  168. $v = str_replace("\n", '<br />', $v);
  169. }
  170. if (strstr($v, 'pics')) {
  171. $v = explode(',', $v);
  172. }
  173. if ($state == 1) {
  174. if (strstr($v, 'jpg') || strstr($v, 'png') || strstr($v, 'gif')) {
  175. $v = '<img src="'.$v.'" width="150"/>';
  176. }
  177. if (!isset($result[$k])) {
  178. $result[$k] = array();
  179. }
  180. $result[$k][$col_module['name']] = $v;
  181. } else {
  182. if (strstr($col_module['key'], 'link')) {
  183. $v = Dever::url($v, 'main');
  184. }
  185. $result[$col_module['key']] = $v;
  186. }
  187. }
  188. }
  189. return $result;
  190. }
  191. }