-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathHtmlView.php
298 lines (246 loc) · 9.52 KB
/
HtmlView.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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright (C) 2008 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Content\Administrator\View\Article;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\Component\Content\Administrator\Model\ArticleModel;
use Joomla\Component\Content\Site\Helper\RouteHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* View to edit an article.
*
* @since 1.6
*/
class HtmlView extends BaseHtmlView
{
/**
* The \JForm object
*
* @var \Joomla\CMS\Form\Form
*/
protected $form;
/**
* The active item
*
* @var object
*/
protected $item;
/**
* The model state
*
* @var object
*/
protected $state;
/**
* The actions the user is authorised to perform
*
* @var \Joomla\Registry\Registry
*/
protected $canDo;
/**
* Pagebreak TOC alias
*
* @var string
*/
protected $eName;
/**
* Array of fieldsets not to display
*
* @var string[]
*
* @since 5.2.0
*/
public $ignore_fieldsets = [];
/**
* Execute and display a template script.
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* @return void
*
* @since 1.6
*
* @throws \Exception
*/
public function display($tpl = null)
{
if ($this->getLayout() == 'pagebreak') {
parent::display($tpl);
return;
}
/** @var ArticleModel $model */
$model = $this->getModel();
$this->form = $model->getForm();
$this->item = $model->getItem();
$this->state = $model->getState();
$this->canDo = ContentHelper::getActions('com_content', 'article', $this->item->id);
if ($this->getLayout() === 'modalreturn') {
parent::display($tpl);
return;
}
// Check for errors.
if (\count($errors = $model->getErrors())) {
throw new GenericDataException(implode("\n", $errors), 500);
}
$input = Factory::getApplication()->getInput();
$forcedLanguage = $input->get('forcedLanguage', '', 'cmd');
// If we are forcing a language in modal (used for associations).
if ($this->getLayout() === 'modal' && $forcedLanguage) {
// Set the language field to the forcedLanguage and disable changing it.
$this->form->setValue('language', null, $forcedLanguage);
$this->form->setFieldAttribute('language', 'readonly', 'true');
// Only allow to select categories with All language or with the forced language.
$this->form->setFieldAttribute('catid', 'language', '*,' . $forcedLanguage);
// Only allow to select tags with All language or with the forced language.
$this->form->setFieldAttribute('tags', 'language', '*,' . $forcedLanguage);
}
// Add form control fields
$this->form
->addControlField('task', '')
->addControlField('return', $input->getBase64('return', ''))
->addControlField('forcedLanguage', $forcedLanguage);
if ($this->getLayout() !== 'modal') {
$this->addToolbar();
} else {
$this->addModalToolbar();
}
parent::display($tpl);
}
/**
* Add the page title and toolbar.
*
* @return void
*
* @since 1.6
*
* @throws \Exception
*/
protected function addToolbar()
{
Factory::getApplication()->getInput()->set('hidemainmenu', true);
$user = $this->getCurrentUser();
$userId = $user->id;
$isNew = ($this->item->id == 0);
$checkedOut = !(\is_null($this->item->checked_out) || $this->item->checked_out == $userId);
$toolbar = $this->getDocument()->getToolbar();
// Built the actions for new and existing records.
$canDo = $this->canDo;
ToolbarHelper::title(
Text::_('COM_CONTENT_PAGE_' . ($checkedOut ? 'VIEW_ARTICLE' : ($isNew ? 'ADD_ARTICLE' : 'EDIT_ARTICLE'))),
'pencil-alt article-add'
);
// For new records, check the create permission.
if ($isNew && (\count($user->getAuthorisedCategories('com_content', 'core.create')) > 0)) {
$toolbar->apply('article.apply');
$saveGroup = $toolbar->dropdownButton('save-group');
$saveGroup->configure(
function (Toolbar $childBar) use ($user) {
$childBar->save('article.save');
if ($user->authorise('core.create', 'com_menus.menu')) {
$childBar->save('article.save2menu', 'JTOOLBAR_SAVE_TO_MENU');
}
$childBar->save2new('article.save2new');
}
);
$toolbar->cancel('article.cancel', 'JTOOLBAR_CANCEL');
} else {
// Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
$itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);
if (!$checkedOut && $itemEditable) {
$toolbar->apply('article.apply');
}
$saveGroup = $toolbar->dropdownButton('save-group');
$saveGroup->configure(
function (Toolbar $childBar) use ($checkedOut, $itemEditable, $canDo, $user) {
// Can't save the record if it's checked out and editable
if (!$checkedOut && $itemEditable) {
$childBar->save('article.save');
// We can save this record, but check the create permission to see if we can return to make a new one.
if ($canDo->get('core.create')) {
$childBar->save2new('article.save2new');
}
}
// If checked out, we can still save2menu
if ($user->authorise('core.create', 'com_menus.menu')) {
$childBar->save('article.save2menu', 'JTOOLBAR_SAVE_TO_MENU');
}
// If checked out, we can still save
if ($canDo->get('core.create')) {
$childBar->save2copy('article.save2copy');
}
}
);
$toolbar->cancel('article.cancel', 'JTOOLBAR_CLOSE');
if (!$isNew) {
if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->params->get('save_history', 0) && $itemEditable) {
$toolbar->versions('com_content.article', $this->item->id);
}
$url = RouteHelper::getArticleRoute($this->item->id . ':' . $this->item->alias, $this->item->catid, $this->item->language);
$toolbar->preview(Route::link('site', $url, true), 'JGLOBAL_PREVIEW')
->bodyHeight(80)
->modalWidth(90);
if (PluginHelper::isEnabled('system', 'jooa11y')) {
$toolbar->jooa11y(Route::link('site', $url . '&jooa11y=1', true), 'JGLOBAL_JOOA11Y')
->bodyHeight(80)
->modalWidth(90);
}
if (Associations::isEnabled() && ComponentHelper::isEnabled('com_associations')) {
$toolbar->standardButton('associations', 'JTOOLBAR_ASSOCIATIONS', 'article.editAssociations')
->icon('icon-contract')
->listCheck(false);
}
}
}
$toolbar->divider();
$toolbar->inlinehelp();
$toolbar->help('Articles:_Edit');
}
/**
* Add the modal toolbar.
*
* @return void
*
* @since 5.0.0
*
* @throws \Exception
*/
protected function addModalToolbar()
{
$user = $this->getCurrentUser();
$userId = $user->id;
$isNew = ($this->item->id == 0);
$checkedOut = !(\is_null($this->item->checked_out) || $this->item->checked_out == $userId);
$toolbar = $this->getDocument()->getToolbar();
// Build the actions for new and existing records.
$canDo = $this->canDo;
ToolbarHelper::title(
Text::_('COM_CONTENT_PAGE_' . ($checkedOut ? 'VIEW_ARTICLE' : ($isNew ? 'ADD_ARTICLE' : 'EDIT_ARTICLE'))),
'pencil-alt article-add'
);
$canCreate = $isNew && (\count($user->getAuthorisedCategories('com_content', 'core.create')) > 0);
$canEdit = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);
// For new records, check the create permission.
if ($canCreate || $canEdit) {
$toolbar->apply('article.apply');
$toolbar->save('article.save');
}
$toolbar->cancel('article.cancel');
}
}