|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Blogs |
|
4
|
|
|
* @category modules |
|
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
|
7
|
|
|
* @license MIT License, see license.txt |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace cs\modules\Blogs; |
|
10
|
|
|
use |
|
11
|
|
|
cs\Cache, |
|
12
|
|
|
cs\Config, |
|
13
|
|
|
cs\Language, |
|
14
|
|
|
cs\User, |
|
15
|
|
|
cs\CRUD_helpers, |
|
16
|
|
|
cs\Singleton; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @method static $this instance($check = false) |
|
20
|
|
|
*/ |
|
21
|
|
|
class Sections { |
|
22
|
|
|
use |
|
23
|
|
|
CRUD_helpers, |
|
24
|
|
|
Singleton; |
|
25
|
|
|
|
|
26
|
|
|
protected $data_model = [ |
|
27
|
|
|
'id' => 'int:0', |
|
28
|
|
|
'parent' => 'int:0', |
|
29
|
|
|
'title' => 'ml:text', |
|
30
|
|
|
'path' => 'ml:text' |
|
31
|
|
|
]; |
|
32
|
|
|
protected $table = '[prefix]blogs_sections'; |
|
33
|
|
|
protected $data_model_ml_group = 'Blogs/sections'; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Cache\Prefix |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $cache; |
|
38
|
|
|
|
|
39
|
|
|
protected function construct () { |
|
40
|
|
|
$this->cache = Cache::prefix('Blogs'); |
|
41
|
|
|
} |
|
42
|
|
|
/** |
|
43
|
|
|
* Returns database index |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function cdb () { |
|
48
|
|
|
return Config::instance()->module('Blogs')->db('posts'); |
|
49
|
|
|
} |
|
50
|
|
|
/** |
|
51
|
|
|
* Get data of specified section |
|
52
|
|
|
* |
|
53
|
|
|
* @param int|int[] $id |
|
54
|
|
|
* |
|
55
|
|
|
* @return array|false |
|
56
|
|
|
*/ |
|
57
|
|
|
function get ($id) { |
|
58
|
|
|
if (is_array($id)) { |
|
59
|
|
|
foreach ($id as &$i) { |
|
60
|
|
|
$i = $this->get($i); |
|
61
|
|
|
} |
|
62
|
|
|
return $id; |
|
63
|
|
|
} |
|
64
|
|
|
$L = Language::instance(); |
|
65
|
|
|
$id = (int)$id; |
|
66
|
|
|
return $this->cache->get( |
|
67
|
|
|
"sections/$id/$L->clang", |
|
68
|
|
|
function () use ($id) { |
|
69
|
|
|
$data = $this->read($id); |
|
70
|
|
|
if ($data) { |
|
71
|
|
|
$data['posts'] = Posts::instance()->get_for_section_count($id); |
|
72
|
|
|
$data['full_title'] = [$data['title']]; |
|
73
|
|
|
$data['full_path'] = [$data['path']]; |
|
74
|
|
|
$parent = $data['parent']; |
|
75
|
|
|
while ($parent > 0) { |
|
76
|
|
|
$section = $this->get($parent); |
|
77
|
|
|
$data['full_title'][] = $section['path']; |
|
78
|
|
|
$data['full_path'][] = $section['path']; |
|
79
|
|
|
$parent = $section['parent']; |
|
80
|
|
|
} |
|
81
|
|
|
$data['full_title'] = implode(' :: ', array_reverse($data['full_title'])); |
|
82
|
|
|
$data['full_path'] = implode('/', array_reverse($data['full_path'])); |
|
83
|
|
|
} |
|
84
|
|
|
return $data; |
|
85
|
|
|
} |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
/** |
|
89
|
|
|
* @return array[] |
|
90
|
|
|
*/ |
|
91
|
|
|
function get_all () { |
|
92
|
|
|
$L = Language::instance(); |
|
93
|
|
|
return $this->cache->get( |
|
94
|
|
|
"sections/all/$L->clang", |
|
95
|
|
|
function () { |
|
96
|
|
|
$result = $this->get( |
|
97
|
|
|
$this->search([], 1, PHP_INT_MAX, 'id', true) ?: [] |
|
98
|
|
|
); |
|
99
|
|
|
usort( |
|
100
|
|
|
$result, |
|
101
|
|
|
function ($a, $b) { |
|
102
|
|
|
return strcmp($a['full_title'], $b['full_title']); |
|
103
|
|
|
} |
|
104
|
|
|
); |
|
105
|
|
|
return $result; |
|
106
|
|
|
} |
|
107
|
|
|
) ?: []; |
|
108
|
|
|
} |
|
109
|
|
|
/** |
|
110
|
|
|
* Get sections ids for each section in full path |
|
111
|
|
|
* |
|
112
|
|
|
* @param string|string[] $path |
|
113
|
|
|
* |
|
114
|
|
|
* @return false|int[] |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
function get_by_path ($path) { |
|
117
|
|
|
$full_path = implode('/', (array)$path); |
|
118
|
|
|
$sections = $this->get_all(); |
|
119
|
|
|
$found = false; |
|
120
|
|
|
foreach ($sections as $section) { |
|
121
|
|
|
if ($section['full_path'] == $full_path) { |
|
122
|
|
|
$found = true; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
if (!$found) { |
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
|
129
|
|
|
$ids = [$section['id']]; |
|
130
|
|
|
while ($section['parent']) { |
|
131
|
|
|
$section = $this->get($section['parent']); |
|
132
|
|
|
$ids[] = $section['id']; |
|
133
|
|
|
} |
|
134
|
|
|
return array_reverse($ids); |
|
135
|
|
|
} |
|
136
|
|
|
/** |
|
137
|
|
|
* Add new section |
|
138
|
|
|
* |
|
139
|
|
|
* @param int $parent |
|
140
|
|
|
* @param string $title |
|
141
|
|
|
* @param string $path |
|
142
|
|
|
* |
|
143
|
|
|
* @return false|int Id of created section on success of <b>false</> on failure |
|
144
|
|
|
*/ |
|
145
|
|
|
function add ($parent, $title, $path) { |
|
146
|
|
|
$id = $this->create($parent, $title, path($path ?: $title)); |
|
147
|
|
|
if ($id) { |
|
148
|
|
|
$this->db_prime()->q( |
|
149
|
|
|
"UPDATE `[prefix]blogs_posts_sections` |
|
150
|
|
|
SET `section` = $id |
|
151
|
|
|
WHERE `section` = '%d'", |
|
152
|
|
|
$parent |
|
153
|
|
|
); |
|
154
|
|
|
unset( |
|
155
|
|
|
$this->cache->posts, |
|
156
|
|
|
$this->cache->sections |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
return $id; |
|
160
|
|
|
} |
|
161
|
|
|
/** |
|
162
|
|
|
* Set data of specified section |
|
163
|
|
|
* |
|
164
|
|
|
* @param int $id |
|
165
|
|
|
* @param int $parent |
|
166
|
|
|
* @param string $title |
|
167
|
|
|
* @param string $path |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
|
|
function set ($id, $parent, $title, $path) { |
|
172
|
|
|
$result = $this->update($id, $parent, $title, path($path ?: $title)); |
|
173
|
|
|
if ($result) { |
|
174
|
|
|
unset($this->cache->sections); |
|
175
|
|
|
} |
|
176
|
|
|
return $result; |
|
177
|
|
|
} |
|
178
|
|
|
/** |
|
179
|
|
|
* Delete specified section |
|
180
|
|
|
* |
|
181
|
|
|
* @param int $id |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
function del ($id) { |
|
186
|
|
|
$id = (int)$id; |
|
187
|
|
|
$section = $this->read($id); |
|
188
|
|
|
if (!$section || !$this->delete($id)) { |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
$new_posts_section = $this->db_prime()->qfs( |
|
192
|
|
|
"SELECT `id` |
|
193
|
|
|
FROM `$this->table` |
|
194
|
|
|
WHERE `parent` = '%s' |
|
195
|
|
|
LIMIT 1", |
|
196
|
|
|
$section['parent'] |
|
197
|
|
|
) ?: $section['parent']; |
|
198
|
|
|
$update = $this->db_prime()->q( |
|
199
|
|
|
[ |
|
200
|
|
|
"UPDATE `[prefix]blogs_sections` |
|
201
|
|
|
SET `parent` = '%2\$d' |
|
202
|
|
|
WHERE `parent` = '%1\$d'", |
|
203
|
|
|
"UPDATE IGNORE `[prefix]blogs_posts_sections` |
|
204
|
|
|
SET `section` = '%3\$d' |
|
205
|
|
|
WHERE `section` = '%1\$d'" |
|
206
|
|
|
], |
|
207
|
|
|
$id, |
|
208
|
|
|
$section['parent'], |
|
209
|
|
|
$new_posts_section |
|
210
|
|
|
); |
|
211
|
|
|
if ($update) { |
|
212
|
|
|
$this->cache->del('/'); |
|
213
|
|
|
} |
|
214
|
|
|
return $update; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.