-
Notifications
You must be signed in to change notification settings - Fork 8
/
Archive.php
318 lines (277 loc) · 7.07 KB
/
Archive.php
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace TypechoPlugin\Dynamics;
use Typecho\Db;
use Typecho\Request;
use Typecho\Widget;
use Typecho\Widget\Helper\PageNavigator\Box;
use Widget\User;
use Widget\Options;
/**
* Class Archive
* @package TypechoPlugin\Dynamics
*
* @property int total
* @property int page
* @property int pageSize
* @property string type
* @property string|null slug
* @property string|null title
* @property string|null description
* @property string|null keywords
*/
class Archive extends Widget
{
/**
* @var Db
*/
protected $db;
/**
* @var User
*/
public $user;
/**
* @var Option
*/
public $option;
/**
* @var Options
*/
public $options;
/**
* @var Dynamic
*/
public $dynamic, $dynamics;
/**
* Widget init
*
* @throws Db\Exception
*/
protected function init()
{
$this->db = Db::get();
$this->user = User::alloc();
$this->option = Option::alloc();
$this->options = Options::alloc();
$this->dynamics = Dynamic::alloc();
$this->dynamics->archive = &$this;
// compatible with older versions
$this->dynamic = &$this->dynamics;
$this->page = $this->request->get('paging', 1);
$this->pageSize = $this->option->pageSize;
}
/**
* 近期动态
*
* @throws Db\Exception
*/
private function select()
{
$this->db->fetchAll(
$this->dynamics->select()
->where('table.dynamics.status != ?', 'hidden')
->order('table.dynamics.created', Db::SORT_DESC)
->page($this->page, $this->pageSize),
[$this->dynamics, 'push']
);
$this->total = $this->dynamics->size(
$this->db->select()->where('table.dynamics.status != ?', 'hidden')
);
}
/**
* 动态首页
*
* @throws Db\Exception
*/
public function index()
{
$this->type = 'index';
$this->import('functions.php');
$this->select();
$this->import('index.php');
}
/**
* 单个动态
*
* @throws Db\Exception
*/
public function post()
{
$this->type = 'post';
$this->slug = $this->request->slug;
$this->import('functions.php');
$did = $this->option
->parseUrl($this->slug);
if (empty($did)) {
$this->error404();
}
$this->db->fetchAll(
$this->dynamics->select()
->where('table.dynamics.did = ?', $did),
[$this->dynamics, 'push']
);
if ($this->error404) {
$this->error404();
}
$this->title = date(
'm月d日, Y年', $this->dynamics->created
);
$this->description = mb_substr(
strip_tags($this->dynamics->content),
0, 200, 'utf8'
);
$this->import('post.php');
}
/**
* 展示分页
* @param bool $import
* @throws Db\Exception
*/
public function page($import = false)
{
$this->type = 'page';
if ($import) {
$this->import('functions.php');
}
$this->select();
if ($import) {
$this->import('page.php');
}
}
/**
* 404
*/
public function error404()
{
$this->response->setStatus(404);
$this->type = '404';
$this->import('functions.php');
$this->import('404.php');
exit;
}
/**
* 当前位置,类似博客主题的 $this->is();
* 首页 index
* 动态 post
* 404 404
* @param $type
* @return bool
*/
public function is($type): bool
{
return $this->type == $type;
}
/**
* 引入博客主题的文件
*
* @param $fileName
*/
public function need($fileName)
{
if (file_exists($path = $this->option->_themeFile($fileName))) {
require_once $path;
}
}
/**
* 引入动态主题的文件
*
* @param $fileName
*/
public function import($fileName)
{
if (file_exists($path = $this->option->themeFile($fileName))) {
require_once $path;
}
}
/**
* 输出归档标题
*
* @param mixed $defines
* @param string $before
* @param string $end
* @access public
* @return void
*/
public function titleArchive($defines = NULL, $before = ' » ', $end = '')
{
if ($this->title) {
$define = '%s';
if (is_array($defines) && !empty($defines[$this->type])) {
$define = $defines[$this->type];
}
echo $before . sprintf($define, $this->title) . $end;
}
}
/**
* @param string $prev
* @param string $next
* @param int $splitPage
* @param string $splitWord
* @param string $template
* @throws Widget\Exception
*/
public function pageNav($prev = '«', $next = '»', $splitPage = 3, $splitWord = '...', $template = '')
{
if ($this->dynamics->have()) {
$default = array(
'wrapTag' => 'ol',
'wrapClass' => 'dynamics-page-navigator'
);
if (is_string($template)) {
parse_str($template, $config);
} else {
$config = $template;
}
$template = array_merge($default, $config);
if ($this->total > $this->pageSize) {
$query = Request::getInstance()->makeUriByRequest('paging={page}');
/** 使用盒状分页 */
$nav = new Box(
$this->total, $this->page, $this->pageSize, $query
);
echo '<' . $template['wrapTag'] . (empty($template['wrapClass'])
? '' : ' class="' . $template['wrapClass'] . '"') . '>';
$nav->render($prev, $next, $splitPage, $splitWord, $template);
echo '</' . $template['wrapTag'] . '>';
}
}
}
/**
* header
*/
public function header()
{
$this->pluginHandle()->header($this);
}
/**
* single
*/
public function single()
{
$this->pluginHandle()->single($this);
}
/**
* footer
*/
public function footer()
{
$this->pluginHandle()->footer($this);
}
/**
* 关键词
*
* @return mixed
*/
public function ___keywords(): string
{
return $this->keywords ?: $this->options->keywords;
}
/**
* 描述
*
* @return mixed
*/
public function ___description(): string
{
return $this->description ?: $this->options->description;
}
}