-
Notifications
You must be signed in to change notification settings - Fork 145
/
Posts.php
178 lines (145 loc) · 4.91 KB
/
Posts.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
<?php namespace RainLab\Blog\Controllers;
use Lang;
use Flash;
use BackendMenu;
use RainLab\Blog\Models\Post;
use RainLab\Blog\Models\Settings as BlogSettings;
use Backend\Classes\Controller;
use Cms\Classes\Controller as CmsController;
use Cms\Classes\Theme;
/**
* Posts
*/
class Posts extends Controller
{
public $implement = [
\Backend\Behaviors\FormController::class,
\Backend\Behaviors\ListController::class,
\Backend\Behaviors\ImportExportController::class
];
public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $importExportConfig = 'config_import_export.yaml';
/**
* @var array requiredPermissions
*/
public $requiredPermissions = ['rainlab.blog.access_other_posts', 'rainlab.blog.access_posts'];
/**
* @var bool turboVisitControl
*/
public $turboVisitControl = 'disable';
/**
* __construct
*/
public function __construct()
{
parent::__construct();
BackendMenu::setContext('RainLab.Blog', 'blog', 'posts');
}
public function index()
{
$this->vars['postsTotal'] = Post::count();
$this->vars['postsPublished'] = Post::isPublished()->count();
$this->vars['postsDrafts'] = $this->vars['postsTotal'] - $this->vars['postsPublished'];
$this->asExtension('ListController')->index();
}
public function create()
{
BackendMenu::setContextSideMenu('new_post');
$this->bodyClass = 'compact-container';
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
$this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');
return $this->asExtension('FormController')->create();
}
public function update($recordId = null)
{
$this->bodyClass = 'compact-container';
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
$this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');
$result = $this->asExtension('FormController')->update($recordId);
$this->setPreviewPageUrlVars();
return $result;
}
/**
* setPreviewPageUrlVars
*/
protected function setPreviewPageUrlVars()
{
if (
($model = $this->formGetModel()) &&
($cmsPage = BlogSettings::get('preview_cms_page'))
) {
$controller = new CmsController(Theme::getActiveTheme());
$model->setUrl($cmsPage, $controller);
$this->vars['pageUrl'] = $model->url;
}
}
public function export()
{
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-export.css');
return $this->asExtension('ImportExportController')->export();
}
public function listExtendQuery($query)
{
if (!$this->user->hasAnyAccess(['rainlab.blog.access_other_posts'])) {
$query->where('user_id', $this->user->id);
}
}
public function formExtendQuery($query)
{
if (!$this->user->hasAnyAccess(['rainlab.blog.access_other_posts'])) {
$query->where('user_id', $this->user->id);
}
}
public function formExtendFieldsBefore($widget)
{
if (!$model = $widget->model) {
return;
}
// Support for October CMS 3.0 and below
if (!class_exists('Site') && $model instanceof Post && $model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')) {
$widget->secondaryTabs['fields']['content']['type'] = 'RainLab\Blog\FormWidgets\MLBlogMarkdown';
}
if (BlogSettings::get('use_legacy_editor', false)) {
$widget->secondaryTabs['fields']['content']['legacyMode'] = true;
}
// Force richeditor by settings
if ($model instanceof Post && BlogSettings::get('force_richeditor_editor', false)) {
$widget->secondaryTabs['fields']['content']['type'] = 'richeditor';
}
}
public function index_onDelete()
{
if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
foreach ($checkedIds as $postId) {
if ((!$post = Post::find($postId)) || !$post->canEdit($this->user)) {
continue;
}
$post->delete();
}
Flash::success(Lang::get('rainlab.blog::lang.post.delete_success'));
}
return $this->listRefresh();
}
/**
* {@inheritDoc}
*/
public function listInjectRowClass($record, $definition = null)
{
if (!$record->published) {
return 'safe disabled';
}
}
public function formBeforeCreate($model)
{
$model->user_id = $this->user->id;
}
public function onRefreshPreview()
{
$data = post('Post');
$previewHtml = Post::formatHtml($data['content'], true);
return [
'preview' => $previewHtml
];
}
}