-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
320 lines (281 loc) · 9.6 KB
/
plugin.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
319
320
<?php
/**
* CkEditor
*
* PHP version 7
*
* @category Comment
* @package Xpressengine\Plugins\Comment
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
namespace Xpressengine\Plugins\Comment;
use Illuminate\Console\Application as Artisan;
use Xpressengine\Permission\Grant;
use Xpressengine\Plugin\AbstractPlugin;
use Xpressengine\Plugins\Comment\Migrations\Migration;
use Xpressengine\Plugins\Comment\Models\Comment;
use Route;
use View;
use Gate;
use XeDB;
use XeConfig;
use XeEditor;
use XeLang;
use XeRegister;
use XeSkin;
use XeStorage;
use XeTag;
use XeToggleMenu;
use XeTrash;
use XeUI;
use Schema;
use Xpressengine\User\Rating;
/**
* Plugin
*
* @category Comment
* @package Xpressengine\Plugins\Comment
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
class Plugin extends AbstractPlugin
{
/**
* activate
*
* @param null $installedVersion installed version
* @return void
*/
public function activate($installedVersion = null)
{
if (XeConfig::get('comment_map') === null) {
XeConfig::set('comment_map', []);
}
}
/**
* @return void
*/
public function install()
{
// put translation source
XeLang::putFromLangDataSource('comment', base_path('plugins/comment/langs/lang.php'));
XeDB::transaction(function () {
/** @var Handler $handler */
$handler = $this->getHandler();
$grant = new Grant();
$grant->set('create', [
Grant::RATING_TYPE => Rating::USER,
Grant::GROUP_TYPE => [],
Grant::USER_TYPE => [],
Grant::EXCEPT_TYPE => [],
Grant::VGROUP_TYPE => []
]);
$grant->set('manage', [
Grant::RATING_TYPE => Rating::MANAGER,
Grant::GROUP_TYPE => [],
Grant::USER_TYPE => [],
Grant::EXCEPT_TYPE => [],
Grant::VGROUP_TYPE => []
]);
app('xe.permission')->register($handler->getKeyForPerm(), $grant);
// 기본 설정
XeConfig::set('comment', $handler->getDefaultConfig());
XeToggleMenu::setActivates('comment', null, []);
// pivot table
// schema 처리는 transaction 에 의해 롤백 되지 않으므로 가장 마지막에 수행 함
(new Migration())->up();
});
}
public function checkInstalled()
{
return (new Migration())->tableExists();
}
public function uninstall()
{
XeDB::transaction(function () {
$map = XeConfig::getOrNew('comment_map');
foreach ($map as $instanceId) {
// document instance 및 instance config 제거
$this->getHandler()->drop($instanceId);
// instance permission 제거
app('xe.permission')->destroy($this->getHandler()->getKeyForPerm($instanceId));
// toggle menu 설정 제거
XeConfig::removeByName(XeToggleMenu::getConfigKey('comment', $instanceId));
}
// 최상위 permissin 제거
app('xe.permission')->destroy($this->getHandler()->getKeyForPerm());
// 최상위 config 제거
XeConfig::removeByName('comment');
// map data 제거
XeConfig::removeByName('comment_map');
// 최상위 설정 제거
XeConfig::removeByName(XeToggleMenu::getConfigKey('comment', null));
// drop pivot table
// schema 처리는 transaction 에 의해 롤백 되지 않으므로 가장 마지막에 수행 함
(new Migration())->down();
});
}
public function boot()
{
$this->routes();
$this->intercept();
$this->registerSettingsMenu();
Gate::policy(Comment::class, CommentPolicy::class);
CommentPolicy::setCertifiedResolver(function (Comment $comment) {
return $this->getHandler()->isCertified($comment);
});
XeUI::setAlias('comment', 'uiobject/comment@comment');
XeSkin::setDefaultSkin('comment', 'comment/skin/comment@default');
XeSkin::setDefaultSettingsSkin('comment', 'comment/settingsSkin/comment@default');
XeTrash::register(RecycleBin::class);
$commands = [
Console\Commands\MakeCommentSkin::class
];
Artisan::starting(static function ($artisan) use ($commands) {
$artisan->resolveCommands($commands);
});
}
public function register()
{
app()->singleton('xe.plugin.comment', function () {
return $this;
});
app()->singleton(Handler::class, function ($app) {
$proxyClass = $app['xe.interception']->proxy(Handler::class, 'XeComment');
$counter = $app['xe.counter']->make($app['request'], Handler::COUNTER_VOTE, ['assent', 'dissent']);
return new $proxyClass(
$app['xe.document'],
$app['session.store'],
$counter,
$app['auth']->guard(),
$app['xe.permission'],
$app['xe.config'],
$app['xe.keygen']
);
});
app()->alias(Handler::class, 'xe.comment');
}
private function intercept()
{
intercept(
'XeComment@createInstance',
'comment::setEditor',
function ($func, $targetInstanceId, $division = false) {
$func($targetInstanceId, $division);
$instanceId = $this->getHandler()->getInstanceId($targetInstanceId);
XeEditor::setInstance($instanceId, 'editor/ckeditor@ckEditor');
}
);
intercept('XeComment@remove', 'comment::relateRemove', function ($func, $comment) {
XeStorage::unBindAll($comment->getKey(), true);
XeTag::set($comment->getKey(), []);
return $func($comment);
});
}
private function routes()
{
Route::group(['namespace' => 'Xpressengine\\Plugins\\Comment\\Controllers'], function () {
require plugins_path('comment/routes.php');
});
}
private function registerSettingsMenu()
{
$menus = [
'contents.comment' => [
'title' => 'comment::comment',
'display' => true,
'description' => '',
'ordering' => 3000
],
'contents.comment.content' => [
'title' => 'comment::commentMange',
'display' => true,
'description' => '',
'ordering' => 3010
],
'contents.comment.trash' => [
'title' => 'comment::trashManage',
'display' => true,
'description' => '',
'ordering' => 3020
],
];
foreach ($menus as $id => $menu) {
XeRegister::push('settings/menu', $id, $menu);
}
}
public function getSettingsURI()
{
return route('comment::setting.global');
}
public function getInstanceSettingURI($instanceId)
{
return route('comment::setting', $instanceId);
}
public function getHandler()
{
return app(Handler::class);
}
/**
* @param null $installedVersion install version
* @return void
*/
public function update($installedVersion = null)
{
// ver 0.9.1
if (XeConfig::get(XeToggleMenu::getConfigKey('comment', null)) == null) {
XeToggleMenu::setActivates('comment', null, []);
}
// ver 0.9.13
$handler = $this->getHandler();
$permission = app('xe.permission')->getOrNew($handler->getKeyForPerm());
if (!$permission['manage']) {
$grant = new Grant();
$create = $permission['create'];
foreach ($create as $type => $value) {
$grant->add('create', $type, $value);
}
$grant->set('manage', [
Grant::RATING_TYPE => Rating::MANAGER,
Grant::GROUP_TYPE => [],
Grant::USER_TYPE => [],
Grant::EXCEPT_TYPE => [],
Grant::VGROUP_TYPE => []
]);
app('xe.permission')->register($handler->getKeyForPerm(), $grant);
}
// after v0.9.18
if (!Schema::hasColumn('comment_target', 'target_type')) {
Schema::table('comment_target', function ($table) {
$table->string('target_type')->nullable();
});
}
XeLang::putFromLangDataSource('comment', base_path('plugins/comment/langs/lang.php'));
}
/**
* @return boolean
*/
public function checkUpdated($installedVersion = null)
{
// ver 0.9.1
if (XeConfig::get(XeToggleMenu::getConfigKey('comment', null)) == null) {
return false;
}
// ver 0.9.13
$handler = $this->getHandler();
$permission = app('xe.permission')->getOrNew($handler->getKeyForPerm());
if (!$permission['manage']) {
return false;
}
// after v0.9.18
if (!Schema::hasColumn('comment_target', 'target_type')) {
return false;
}
return true;
}
}