Article.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Article extends BaseMall
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/article.lang.php');
  21. }
  22. /**
  23. * 文章列表显示页面
  24. */
  25. public function index()
  26. {
  27. $ac_id = intval(input('param.ac_id'));
  28. if (empty($ac_id)) {
  29. $this->error(lang('param_error')); //'缺少参数:文章类别编号'
  30. }
  31. /**
  32. * 得到导航ID
  33. */
  34. $nav_id = intval(input('param.nav_id'));
  35. View::assign('index_sign', $nav_id);
  36. /**
  37. * 根据类别编号获取文章类别信息
  38. */
  39. $articleclass_model = model('articleclass');
  40. $article_class = $articleclass_model->getOneArticleclass($ac_id);
  41. View::assign('class_name', $article_class['ac_name']);
  42. if (empty($article_class) || !is_array($article_class)) {
  43. $this->error(lang('article_article_class_not_exists')); //'该文章分类并不存在'
  44. }
  45. $default_count = 5; //定义最新文章列表显示文章的数量
  46. /**
  47. * 分类导航
  48. */
  49. $nav_link = array(
  50. array(
  51. 'title' => lang('homepage'), 'link' => HOME_SITE_URL
  52. ), array(
  53. 'title' => $article_class['ac_name']
  54. )
  55. );
  56. View::assign('nav_link_list', $nav_link);
  57. /**
  58. * 左侧分类导航
  59. */
  60. $articleclass_list = $articleclass_model->getArticleclassList(array());
  61. $tree = new \mall\Tree();
  62. $tree->setTree($articleclass_list, 'ac_id', 'ac_parent_id', 'ac_name');
  63. View::assign('sub_class_list', $tree->getArrayList());
  64. /**
  65. * 文章列表
  66. */
  67. $child_class_list = $articleclass_model->getChildClass($ac_id);
  68. $ac_ids = array();
  69. if (!empty($child_class_list) && is_array($child_class_list)) {
  70. foreach ($child_class_list as $v) {
  71. $ac_ids[] = $v['ac_id'];
  72. }
  73. }
  74. $ac_ids = implode(',', $ac_ids);
  75. $article_model = model('article');
  76. $condition = array();
  77. $condition[] = array('ac_id', 'in', $ac_ids);
  78. $condition[] = array('article_show', '=', '1');
  79. $article_list = $article_model->getArticleList($condition, 12);
  80. View::assign('article', $article_list);
  81. View::assign('show_page', $article_model->page_info->render());
  82. /**
  83. * 最新文章列表
  84. */
  85. $count = count($article_list);
  86. $new_article_list = array();
  87. if (!empty($article_list) && is_array($article_list)) {
  88. for ($i = 0; $i < ($count > $default_count ? $default_count : $count); $i++) {
  89. $new_article_list[] = $article_list[$i];
  90. }
  91. }
  92. View::assign('new_article_list', $new_article_list);
  93. //SEO 设置
  94. $seo = model('seo')->type('article')->param(array('article_class' => $article_class['ac_name']))->show();
  95. $this->_assign_seo($seo);
  96. return View::fetch($this->template_dir . 'article_list');
  97. }
  98. /**
  99. * 单篇文章显示页面
  100. */
  101. public function show()
  102. {
  103. $article_id = intval(input('param.article_id'));
  104. if (empty($article_id)) {
  105. $this->error(lang('param_error')); //'缺少参数:文章编号'
  106. }
  107. /**
  108. * 根据文章编号获取文章信息
  109. */
  110. $article_model = model('article');
  111. $condition = array();
  112. $condition[] = array('article_id', '=', $article_id);
  113. $article = $article_model->getOneArticle($condition);
  114. if (empty($article) || !is_array($article) || $article['article_show'] == '0') {
  115. $this->error(lang('article_show_not_exists')); //'该文章并不存在'
  116. }
  117. View::assign('article', $article);
  118. /**
  119. * 根据类别编号获取文章类别信息
  120. */
  121. $articleclass_model = model('articleclass');
  122. $article_class = $articleclass_model->getOneArticleclass($article['ac_id']);
  123. if (empty($article_class) || !is_array($article_class)) {
  124. $this->error(lang('article_show_delete')); //'该文章已随所属类别被删除'
  125. }
  126. $default_count = 5; //定义最新文章列表显示文章的数量
  127. /**
  128. * 分类导航
  129. */
  130. $nav_link = array(
  131. array(
  132. 'title' => lang('homepage'), 'link' => HOME_SITE_URL
  133. ), array(
  134. 'title' => $article_class['ac_name'],
  135. 'link' => (string)url('Article/index', array('ac_id' => $article_class['ac_id']))
  136. ), array(
  137. 'title' => lang('article_show_article_content')
  138. )
  139. );
  140. View::assign('nav_link_list', $nav_link);
  141. /**
  142. * 左侧分类导航
  143. */
  144. $articleclass_list = $articleclass_model->getArticleclassList(array());
  145. $tree = new \mall\Tree();
  146. $tree->setTree($articleclass_list, 'ac_id', 'ac_parent_id', 'ac_name');
  147. View::assign('sub_class_list', $tree->getArrayList());
  148. /**
  149. * 文章列表
  150. */
  151. $child_class_list = $articleclass_model->getChildClass($article_class['ac_id']);
  152. $ac_ids = array();
  153. if (!empty($child_class_list) && is_array($child_class_list)) {
  154. foreach ($child_class_list as $v) {
  155. $ac_ids[] = $v['ac_id'];
  156. }
  157. }
  158. $ac_ids = implode(',', $ac_ids);
  159. $article_model = model('article');
  160. $condition = array();
  161. $condition[] = array('ac_id', 'in', $ac_ids);
  162. $condition[] = array('article_show', '=', '1');
  163. $article_list = $article_model->getArticleList($condition);
  164. /**
  165. * 寻找上一篇与下一篇
  166. */
  167. $pre_article = $next_article = array();
  168. if (!empty($article_list) && is_array($article_list)) {
  169. $pos = 0;
  170. foreach ($article_list as $k => $v) {
  171. if ($v['article_id'] == $article['article_id']) {
  172. $pos = $k;
  173. break;
  174. }
  175. }
  176. if ($pos > 0 && is_array($article_list[$pos - 1])) {
  177. $pre_article = $article_list[$pos - 1];
  178. }
  179. if ($pos < count($article_list) - 1 and is_array($article_list[$pos + 1])) {
  180. $next_article = $article_list[$pos + 1];
  181. }
  182. }
  183. View::assign('pre_article', $pre_article);
  184. View::assign('next_article', $next_article);
  185. /**
  186. * 最新文章列表
  187. */
  188. $count = count($article_list);
  189. $new_article_list = array();
  190. if (!empty($article_list) && is_array($article_list)) {
  191. for ($i = 0; $i < ($count > $default_count ? $default_count : $count); $i++) {
  192. $new_article_list[] = $article_list[$i];
  193. }
  194. }
  195. View::assign('new_article_list', $new_article_list);
  196. $seo_param = array();
  197. $seo_param['name'] = $article['article_title'];
  198. $seo_param['article_class'] = $article_class['ac_name'];
  199. $seo_param['description'] = ds_substing(htmlspecialchars_decode($article['article_content']));
  200. //SEO 设置
  201. $seo = model('seo')->type('article_content')->param($seo_param)->show();
  202. $this->_assign_seo($seo);
  203. return View::fetch($this->template_dir . 'article_show');
  204. }
  205. }