Data.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Push\Lib;
  3. use Dever;
  4. class Data
  5. {
  6. public function get($key, $name, $total = 10, $page = false, $limit = false)
  7. {
  8. $info = $this->getInfo($key, $name, $total);
  9. $data = array();
  10. if ($info) {
  11. if (!$info['num']) {
  12. $info['num'] = 20;
  13. }
  14. if ($limit > 0) {
  15. $info['num'] = $limit;
  16. }
  17. $where = array();
  18. $where['info_id'] = $info['id'];
  19. if ($page) {
  20. $where['page'] = array($info['num'], 'list');
  21. $data = Dever::db('push/data')->getAllPage($where);
  22. } else {
  23. $where['limit'] = '0,' . $info['num'];
  24. $data = Dever::db('push/data')->getAll($where);
  25. }
  26. if ($data) {
  27. foreach ($data as $k => $v) {
  28. $data[$k] = $this->getData($v);
  29. }
  30. }
  31. }
  32. return $data;
  33. }
  34. public function getOne($key, $name, $total = 10, $data_name)
  35. {
  36. $info = $this->getInfo($key, $name, $total);
  37. $data = array();
  38. if ($info) {
  39. $where = array();
  40. $where['info_id'] = $info['id'];
  41. $where['name'] = $data_name;
  42. $data = Dever::db('push/data')->one($where);
  43. if ($data) {
  44. $data = $this->getData($data);
  45. } else {
  46. $where['type'] = 10;
  47. Dever::db('push/data')->insert($where);
  48. }
  49. }
  50. return $data;
  51. }
  52. private function getInfo($key, $name, $total)
  53. {
  54. $info = Dever::db('push/info')->one(array('key' => $key));
  55. if (!$info) {
  56. $insert['key'] = $key;
  57. $insert['name'] = $name;
  58. $insert['num'] = $total;
  59. $insert['col'] = '1,3';
  60. $info['id'] = Dever::db('push/info')->insert($insert);
  61. $info['num'] = $insert['num'];
  62. }
  63. return $info;
  64. }
  65. private function getData($info)
  66. {
  67. $info['id'] = 0;
  68. if ($info['type'] < 10 || $info['type'] == 13) {
  69. $table = Dever::config('base')->type_table[$info['type']];
  70. $method = 'one';
  71. if ($info['type'] == 1) {
  72. $id = $info['article_id'];
  73. }
  74. unset($info['article_id']);
  75. if ($id > 0) {
  76. $info['id'] = $id;
  77. $data = Dever::db($table)->$method($id);
  78. if (isset($data) && $data) {
  79. if (isset($data['name']) && $data['name'] && !$info['name']) {
  80. $info['name'] = $data['name'];
  81. }
  82. if (isset($data['pic_cover']) && $data['pic_cover'] && !$info['pic']) {
  83. $info['pic'] = $data['pic_cover'];
  84. }
  85. if (isset($data['pdate']) && $data['pdate']) {
  86. $info['cdate'] = $data['pdate'];
  87. }
  88. if (isset($data['cate_id']) && $data['cate_id']) {
  89. $cate = Dever::db('content/cate')->one($data['cate_id']);
  90. $info['cate_name'] = $cate['name'];
  91. }
  92. if (isset($data['author_id']) && $data['author_id']) {
  93. $author = Dever::db('content/author')->one($data['author_id']);
  94. $info['author_name'] = $author['name'];
  95. }
  96. $info['link'] = Dever::load('main/content')->link($data);
  97. if (isset($data['template']) && $data['template'] == 2) {
  98. $info['name'] = '<i class="ico-video"></i>' . $info['name'];
  99. }
  100. }
  101. }
  102. } else {
  103. unset($info['article_id']);
  104. if (isset($info['olink']) && $info['olink']) {
  105. $info['link'] = $info['olink'];
  106. }
  107. }
  108. if ($info['link']) {
  109. if (!strstr($info['link'], 'http')) {
  110. $info['link'] = 'http://' . $info['link'];
  111. }
  112. }
  113. $info['cdate'] = date('Y-m-d', $info['cdate']);
  114. if (isset($info['name']) && $info['name']) {
  115. $info['name'] = Dever::cut($info['name'], 20, '...');
  116. }
  117. return $info;
  118. }
  119. }