-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnews.php
More file actions
228 lines (206 loc) · 10 KB
/
Copy pathnews.php
File metadata and controls
228 lines (206 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* ikaiCMS - 新闻文章列表页
*
* 合并后使用 ContentModel + ChannelModel
* PHP 8.0+
*/
declare(strict_types=1);
require_once __DIR__ . '/includes/init.php';
HtmlCache::start(600);
// 获取分类参数
$categorySlug = get('cat', '');
$categoryId = getInt('cat_id', 0);
$category = null;
// 获取 news 顶级栏目
$newsChannel = getChannelBySlug('news');
$newsChannelId = $newsChannel ? (int)$newsChannel['id'] : 0;
// 通过 slug 获取子栏目
if ($categorySlug) {
$category = channelModel()->findWhere(['slug' => $categorySlug, 'status' => 1]);
if ($category) {
$categoryId = (int)$category['id'];
}
} elseif ($categoryId > 0) {
$category = getChannel($categoryId);
}
// 页面信息
$pageTitle = $category ? $category['name'] : __('news_title');
$pageKeywords = ($category['seo_keywords'] ?? '') ?: config('site_keywords');
$pageDescription = ($category['seo_description'] ?? '') ?: config('site_description');
// 当前菜单高亮
$currentSlug = 'news';
// 获取子栏目(用于分类导航)
$categories = [];
if ($newsChannelId > 0) {
$categories = channelModel()->where(['parent_id' => $newsChannelId, 'status' => 1]);
}
// 搜索关键词
$keyword = trim(get('keyword', ''));
// 分页
$page = max(1, getInt('page', 1));
$perPage = 10;
$offset = ($page - 1) * $perPage;
// 确定要查询的栏目 ID
$queryChannelId = $categoryId > 0 ? $categoryId : $newsChannelId;
$filters = ['include_children' => true];
if ($keyword !== '') {
$filters['keyword'] = $keyword;
}
// 获取文章总数和列表
$total = contentModel()->getCount($queryChannelId, $filters);
$articles = contentModel()->getList($queryChannelId, $perPage, $offset, $filters);
// 获取导航
$navChannels = getNavChannels();
// 引入头部
require_once theme_path('layouts/header.php');
?>
<!-- Page header -->
<?php
$breadcrumbItems = [];
if ($category) {
$breadcrumbItems[] = ['name' => __('news_title'), 'url' => '/news.html'];
$breadcrumbItems[] = ['name' => $category['name'], 'url' => ''];
} else {
$breadcrumbItems[] = ['name' => __('news_title'), 'url' => ''];
}
$heroChannel = $category ?: ($newsChannel ?: ['name' => __('news_title'), 'description' => '', 'image' => '']);
// Ensure channel var is set for page-hero partial
$_heroChannelBackup = $channel ?? null;
$channel = $heroChannel;
require theme_path('partials/page-hero.php');
$channel = $_heroChannelBackup;
unset($_heroChannelBackup);
?>
<!-- Category nav + search -->
<div class="bg-white border-b">
<div class="container mx-auto px-4">
<div class="flex flex-wrap items-center justify-between gap-4 py-4">
<?php if (!empty($categories)): ?>
<div class="flex flex-wrap gap-3">
<a href="/news.html"
class="px-4 py-2 rounded-full text-sm <?php echo !$category && $keyword === '' ? 'bg-primary text-white' : 'bg-gray-100 text-gray-600 hover:bg-gray-200'; ?>">
<?php echo __('all'); ?>
</a>
<?php foreach ($categories as $cat): ?>
<a href="/news/<?php echo e($cat['slug']); ?>.html"
class="px-4 py-2 rounded-full text-sm <?php echo $categoryId === (int)$cat['id'] ? 'bg-primary text-white' : 'bg-gray-100 text-gray-600 hover:bg-gray-200'; ?>">
<?php echo e($cat['name']); ?>
</a>
<?php endforeach; ?>
</div>
<?php else: ?>
<div></div>
<?php endif; ?>
<form method="get" action="<?php echo $category ? '/news/' . e($category['slug']) . '.html' : '/news.html'; ?>" class="flex items-center gap-2">
<div class="relative">
<input type="text" name="keyword" value="<?php echo e($keyword); ?>"
placeholder="記事を検索..."
class="w-48 border rounded-full pl-4 pr-9 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent">
<button type="submit" class="absolute right-2.5 top-1/2 -translate-y-1/2 text-gray-400 hover:text-primary">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
</button>
</div>
<?php if ($keyword !== ''): ?>
<a href="<?php echo $category ? '/news/' . e($category['slug']) . '.html' : '/news.html'; ?>" class="text-gray-400 hover:text-red-500" title="検索をクリア">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</a>
<?php endif; ?>
</form>
</div>
<?php if ($keyword !== ''): ?>
<div class="pb-3 text-sm text-gray-500">
検索 "<span class="text-primary"><?php echo e($keyword); ?></span>" 全 <span class="text-primary font-medium"><?php echo $total; ?></span> 件
</div>
<?php endif; ?>
</div>
</div>
<!-- Article list -->
<section class="py-12">
<div class="container mx-auto px-4">
<?php if (!empty($articles)): ?>
<div class="space-y-6">
<?php foreach ($articles as $item): ?>
<a href="/news/article/<?php echo $item['id']; ?>.html" class="flex gap-6 bg-white rounded-lg shadow overflow-hidden hover:shadow-lg transition group">
<div class="flex-shrink-0 w-48 md:w-64 overflow-hidden bg-gray-100">
<?php if ($item['cover']): ?>
<img loading="lazy" src="<?php echo e(thumbnail($item['cover'], 'medium')); ?>" alt="<?php echo e($item['title']); ?>"
class="w-full h-full object-cover group-hover:scale-110 transition duration-500">
<?php else: ?>
<div class="w-full h-full flex items-center justify-center text-gray-300 min-h-[120px]">
<svg class="w-12 h-12" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
</div>
<?php endif; ?>
</div>
<div class="flex-1 py-4 pr-4">
<h3 class="text-lg font-bold text-dark group-hover:text-primary transition line-clamp-2">
<?php if ($item['is_top']): ?>
<span class="text-xs bg-red-500 text-white px-1.5 py-0.5 rounded mr-2"><?php echo __('article_top'); ?></span>
<?php endif; ?>
<?php if ($item['is_recommend']): ?>
<span class="text-xs bg-orange-500 text-white px-1.5 py-0.5 rounded mr-2"><?php echo __('article_recommend'); ?></span>
<?php endif; ?>
<?php echo e($item['title']); ?>
</h3>
<p class="mt-2 text-gray-500 text-sm line-clamp-2">
<?php echo e($item['summary'] ?: cutStr(strip_tags($item['content']), 120)); ?>
</p>
<div class="mt-3 flex items-center gap-4 text-xs text-gray-400">
<?php if ($item['channel_name'] ?? ''): ?>
<span class="text-primary"><?php echo e($item['channel_name']); ?></span>
<?php endif; ?>
<?php if ($item['author'] ?? ''): ?>
<span><?php echo e($item['author']); ?></span>
<?php endif; ?>
<span><?php echo date('Y-m-d', (int)$item['publish_time']); ?></span>
<span><?php echo __('detail_views'); ?> <?php echo number_format((int)$item['views']); ?></span>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
<!-- Pagination -->
<?php if ($total > $perPage): ?>
<?php
$totalPages = (int)ceil($total / $perPage);
$pageUrl = function(int $p) use ($category, $keyword): string {
$base = $category ? '/news/' . $category['slug'] : '/news';
$keywordParam = $keyword !== '' ? '?keyword=' . urlencode($keyword) : '';
if ($p === 1) {
return $base . '.html' . $keywordParam;
} else {
return $base . '/page/' . $p . '.html' . $keywordParam;
}
};
?>
<div class="mt-8 flex items-center justify-center gap-2">
<?php if ($page > 1): ?>
<a href="<?php echo $pageUrl($page - 1); ?>" class="px-4 py-2 border rounded hover:bg-gray-100"><?php echo __('list_prev_page'); ?></a>
<?php endif; ?>
<?php
$start = max(1, $page - 2);
$end = min($totalPages, $page + 2);
for ($i = $start; $i <= $end; $i++):
?>
<a href="<?php echo $pageUrl($i); ?>"
class="px-4 py-2 border rounded <?php echo $i === $page ? 'bg-primary text-white border-primary' : 'hover:bg-gray-100'; ?>">
<?php echo $i; ?>
</a>
<?php endfor; ?>
<?php if ($page < $totalPages): ?>
<a href="<?php echo $pageUrl($page + 1); ?>" class="px-4 py-2 border rounded hover:bg-gray-100"><?php echo __('list_next_page'); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else: ?>
<div class="text-center py-16 text-gray-500 bg-white rounded-lg">
<?php echo __('no_content'); ?>
</div>
<?php endif; ?>
</div>
</section>
<?php require_once theme_path('layouts/footer.php'); ?>