Data.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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('key');
  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('key');
  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 && $path && $data['module']) {
  39. foreach ($data['module'] as $k => $v) {
  40. $v['config'] = $config;
  41. $result[] = Dever::render($path . '/' . $v['key'], $v);
  42. }
  43. return implode("\r\n", $result);
  44. } else {
  45. return $data;
  46. }
  47. }
  48. # 获取某个页面的数据
  49. public function getPageData($page_id, $cdate = 'Y-m-d H:i')
  50. {
  51. $where = array();
  52. if (is_numeric($page_id)) {
  53. $where['id'] = $page_id;
  54. } else {
  55. $where['key'] = $page_id;
  56. }
  57. $data = Dever::db('page/page')->find($where);
  58. if ($data) {
  59. $data['pdate_string'] = date('Y.m.d', $data['pdate']);
  60. unset($data['state']);
  61. unset($data['reorder']);
  62. unset($data['cdate']);
  63. $data['module'] = Dever::db('page/page_module')->getData(array('page_id' => $data['id']));
  64. if ($data['module']) {
  65. foreach ($data['module'] as $k => $v) {
  66. $where = array();
  67. $where['module_id'] = $v['id'];
  68. $page = Dever::input($v['key'] . '_page');
  69. if ($page) {
  70. $where['page'] = array($v['num'], 'list');
  71. $data['module'][$k]['data'] = Dever::db('page/data')->getAllPage($where);
  72. } else {
  73. $where['limit'] = '0,' . $v['num'];
  74. $data['module'][$k]['data'] = Dever::db('page/data')->getAll($where);
  75. }
  76. if ($data['module'][$k]['data']) {
  77. $result = $data['module'][$k]['data'];
  78. $data['module'][$k]['data'] = array();
  79. foreach ($result as $k1 => $v1) {
  80. $v1 = $this->getOne($v1, $v, $cdate);
  81. if ($v1) {
  82. $data['module'][$k]['data'][] = $v1;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return $data;
  90. }
  91. # 获取某个推送位的所有数据
  92. public function get($key, $name, $total = 10, $col = '1,2,3', $func = '-1', $page = false, $limit = false, $cdate = 'Y-m-d H:i')
  93. {
  94. if (!$page) {
  95. $page = Dever::input($key . '_page');
  96. }
  97. $module = Dever::db('page/module')->one(array('key' => $key));
  98. if (!$module) {
  99. $module['content'] = 1;
  100. $module['key'] = $key;
  101. $module['name'] = $name;
  102. $module['num'] = $total;
  103. $module['col'] = $col;
  104. $module['func'] = $func;
  105. $module['page_id'] = 1;
  106. $module['id'] = Dever::db('page/module')->insert($module);
  107. }
  108. $result = array();
  109. if ($module) {
  110. if (!$module['num']) {
  111. $module['num'] = 20;
  112. }
  113. if ($limit > 0) {
  114. $module['num'] = $limit;
  115. }
  116. if ($module['content'] == 1 && $total > 0) {
  117. Dever::setInput('search_option_module_id', $module['id']);
  118. $where = array();
  119. $where['module_id'] = $module['id'];
  120. if ($page) {
  121. $where['page'] = array($module['num'], 'list');
  122. $data = Dever::db('page/data')->getAllPage($where);
  123. } else {
  124. $where['limit'] = '0,' . $module['num'];
  125. $data = Dever::db('page/data')->getAll($where);
  126. }
  127. if ($data) {
  128. foreach ($data as $k => $v) {
  129. $v = $this->getOne($v, $module, $cdate);
  130. if ($v) {
  131. $result[] = $v;
  132. }
  133. }
  134. }
  135. } else {
  136. $result = $module;
  137. }
  138. }
  139. return $result;
  140. }
  141. # 获取某条推送位的数据
  142. public function getOne($data, $module = false, $cdate = 'Y-m-d H:i')
  143. {
  144. $data = is_array($data) ? $data : Dever::db('page/data')->one($data);
  145. $source = array();
  146. $module = $module ? $module : Dever::db('page/module')->one($data['module_id']);
  147. $module['col'] = explode(',', $module['col']);
  148. $col = array();
  149. if ($data['type'] > 0) {
  150. $func = Dever::db('page/func')->one($data['type']);
  151. if ($func) {
  152. if ($func['type'] == 1) {
  153. $method = $func['api'];
  154. $col = Dever::array_decode($func['col']);
  155. $source = Dever::load($method, $data['type_id']);
  156. if (!$source) {
  157. return false;
  158. }
  159. } else {
  160. $data['type_id'] = $func['api'];
  161. }
  162. }
  163. }
  164. $result = array();
  165. $lang = Dever::getLang();
  166. if ($lang) {
  167. $key = $lang . '_col_';
  168. } else {
  169. $key = 'col_';
  170. }
  171. foreach ($data as $k => $v) {
  172. $s = strstr($k, $key);
  173. if (!$s && $lang && strstr($k, 'col_')) {
  174. $s = true;
  175. $key = 'col_';
  176. } elseif ($k == 'id' || $k == 'type' || $k == 'type_id') {
  177. $result[$k] = $v;
  178. } elseif ($k == 'cdate') {
  179. $result['cdate_string'] = date($cdate, $v);
  180. }
  181. if ($s) {
  182. $id = str_replace($key, '', $k);
  183. if (!in_array($id, $module['col'])) {
  184. continue;
  185. }
  186. $col_module = Dever::db('page/col')->one($id);
  187. if (!$col_module) {
  188. continue;
  189. }
  190. if (!$v && $source && $col) {
  191. foreach ($col as $k1 => $v1) {
  192. if ($v1['col_id'] == $id && isset($source[$v1['name']]) && $source[$v1['name']]) {
  193. $v = $source[$v1['name']];
  194. }
  195. }
  196. }
  197. if (strstr($v, "\n")) {
  198. //$v = str_replace("\n", '<br />', $v);
  199. }
  200. if (strstr($v, "font-size")) {
  201. $v = str_replace("font-size", 'font-sizes', $v);
  202. }
  203. if (strstr($k, 'pics')) {
  204. $v = explode(',', $v);
  205. }
  206. if (strstr($col_module['key'], 'link') && !strstr($v, 'pages/')) {
  207. $v = Dever::url($v, 'main');
  208. }
  209. $result[$col_module['key']] = $v;
  210. }
  211. }
  212. return $result;
  213. }
  214. }