Skip to content

Commit 2ea8fa6

Browse files
authored
[6.0] Improve FormView & Toolbar & Components (joomla#45913)
* Release 5.3.3 * Revert to dev * Converted static calls to oop calls * Add more toolbar changes * Add category permission support * Only check checkedout for existing items * Improve error checks * Add ink options * add ignore_fieldsets class variables * Refactor article view * Add method comments * Refactor com_contact * Cleanup unused usages * Add method comments * Fix code style * Fix preview link * Fix preview toolbar dialog size (joomla#137) * Fix codestyle * Fix codestyle * CS
1 parent 7bd2c5b commit 2ea8fa6

File tree

3 files changed

+211
-331
lines changed

3 files changed

+211
-331
lines changed

administrator/components/com_contact/src/View/Contact/HtmlView.php

Lines changed: 34 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@
1010

1111
namespace Joomla\Component\Contact\Administrator\View\Contact;
1212

13-
use Joomla\CMS\Component\ComponentHelper;
1413
use Joomla\CMS\Factory;
1514
use Joomla\CMS\Helper\ContentHelper;
16-
use Joomla\CMS\Language\Associations;
1715
use Joomla\CMS\Language\Text;
18-
use Joomla\CMS\MVC\View\GenericDataException;
19-
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
20-
use Joomla\CMS\Toolbar\Toolbar;
16+
use Joomla\CMS\MVC\View\FormView;
2117
use Joomla\CMS\Toolbar\ToolbarHelper;
22-
use Joomla\Component\Contact\Administrator\Model\ContactModel;
2318

2419
// phpcs:disable PSR1.Files.SideEffects
2520
\defined('_JEXEC') or die;
@@ -30,66 +25,58 @@
3025
*
3126
* @since 1.6
3227
*/
33-
class HtmlView extends BaseHtmlView
28+
class HtmlView extends FormView
3429
{
3530
/**
36-
* The Form object
31+
* Set to true, if saving to menu should be supported
3732
*
38-
* @var \Joomla\CMS\Form\Form
33+
* @var boolean
3934
*/
40-
protected $form;
35+
protected $supportSaveMenu = true;
4136

4237
/**
43-
* The active item
38+
* Holds the extension for categories, if available
4439
*
45-
* @var object
40+
* @var string
4641
*/
47-
protected $item;
42+
protected $categorySection = 'com_contact';
4843

4944
/**
50-
* The model state
45+
* Constructor
5146
*
52-
* @var \Joomla\Registry\Registry
53-
*/
54-
protected $state;
55-
56-
/**
57-
* Array of fieldsets not to display
58-
*
59-
* @var string[]
47+
* @param array $config An optional associative array of configuration settings.
6048
*
61-
* @since 5.2.0
49+
* @since __DEPLOY_VERSION__
6250
*/
63-
public $ignore_fieldsets = [];
51+
public function __construct(array $config)
52+
{
53+
if (empty($config['option'])) {
54+
$config['option'] = 'com_contact';
55+
}
56+
57+
$config['help_link'] = 'Contacts:_Edit';
58+
$config['toolbar_icon'] = 'address-book contact';
59+
60+
parent::__construct($config);
61+
}
6462

6563
/**
66-
* Display the view.
67-
*
68-
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
64+
* Prepare view data
6965
*
7066
* @return void
67+
*
68+
* @since __DEPLOY_VERSION__
7169
*/
72-
public function display($tpl = null)
70+
protected function initializeView()
7371
{
74-
/** @var ContactModel $model */
75-
$model = $this->getModel();
72+
parent::initializeView();
7673

77-
// Initialise variables.
78-
$this->form = $model->getForm();
79-
$this->item = $model->getItem();
80-
$this->state = $model->getState();
74+
$this->canDo = ContentHelper::getActions('com_contact', 'category', $this->item->catid);
8175

8276
if ($this->getLayout() === 'modalreturn') {
83-
parent::display($tpl);
84-
8577
return;
8678
}
8779

88-
// Check for errors.
89-
if (\count($errors = $model->getErrors())) {
90-
throw new GenericDataException(implode("\n", $errors), 500);
91-
}
92-
9380
// If we are forcing a language in modal (used for associations).
9481
$forcedLanguage = Factory::getApplication()->getInput()->get('forcedLanguage', '', 'cmd');
9582

@@ -105,18 +92,10 @@ public function display($tpl = null)
10592
$this->form->setFieldAttribute('tags', 'language', '*,' . $forcedLanguage);
10693
}
10794

108-
if ($this->getLayout() !== 'modal') {
109-
$this->addToolbar();
110-
} else {
111-
$this->addModalToolbar();
112-
}
113-
11495
// Add form control fields
11596
$this->form
11697
->addControlField('task', '')
11798
->addControlField('forcedLanguage', $forcedLanguage);
118-
119-
parent::display($tpl);
12099
}
121100

122101
/**
@@ -128,91 +107,15 @@ public function display($tpl = null)
128107
*/
129108
protected function addToolbar()
130109
{
131-
Factory::getApplication()->getInput()->set('hidemainmenu', true);
132-
133-
$user = $this->getCurrentUser();
134-
$userId = $user->id;
135-
$isNew = ($this->item->id == 0);
136-
$checkedOut = !(\is_null($this->item->checked_out) || $this->item->checked_out == $userId);
137-
$toolbar = $this->getDocument()->getToolbar();
138-
139-
// Since we don't track these assets at the item level, use the category id.
140-
$canDo = ContentHelper::getActions('com_contact', 'category', $this->item->catid);
141-
142-
ToolbarHelper::title($isNew ? Text::_('COM_CONTACT_MANAGER_CONTACT_NEW') : Text::_('COM_CONTACT_MANAGER_CONTACT_EDIT'), 'address-book contact');
110+
if ($this->getLayout() === 'modal') {
111+
$this->addModalToolbar();
143112

144-
// Build the actions for new and existing records.
145-
if ($isNew) {
146-
// For new records, check the create permission.
147-
if (\count($user->getAuthorisedCategories('com_contact', 'core.create')) > 0) {
148-
$toolbar->apply('contact.apply');
149-
150-
$saveGroup = $toolbar->dropdownButton('save-group');
151-
152-
$saveGroup->configure(
153-
function (Toolbar $childBar) use ($user) {
154-
$childBar->save('contact.save');
155-
156-
if ($user->authorise('core.create', 'com_menus.menu')) {
157-
$childBar->save('contact.save2menu', 'JTOOLBAR_SAVE_TO_MENU');
158-
}
159-
160-
$childBar->save2new('contact.save2new');
161-
}
162-
);
163-
}
164-
165-
$toolbar->cancel('contact.cancel', 'JTOOLBAR_CANCEL');
166-
} else {
167-
// Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
168-
$itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);
169-
170-
// Can't save the record if it's checked out and editable
171-
if (!$checkedOut && $itemEditable) {
172-
$toolbar->apply('contact.apply');
173-
}
174-
175-
$saveGroup = $toolbar->dropdownButton('save-group');
176-
177-
$saveGroup->configure(
178-
function (Toolbar $childBar) use ($checkedOut, $itemEditable, $canDo, $user) {
179-
// Can't save the record if it's checked out and editable
180-
if (!$checkedOut && $itemEditable) {
181-
$childBar->save('contact.save');
182-
183-
// We can save this record, but check the create permission to see if we can return to make a new one.
184-
if ($canDo->get('core.create')) {
185-
$childBar->save2new('contact.save2new');
186-
}
187-
}
188-
189-
// If checked out, we can still save2menu
190-
if ($user->authorise('core.create', 'com_menus.menu')) {
191-
$childBar->save('contact.save2menu', 'JTOOLBAR_SAVE_TO_MENU');
192-
}
193-
194-
// If checked out, we can still save
195-
if ($canDo->get('core.create')) {
196-
$childBar->save2copy('contact.save2copy');
197-
}
198-
}
199-
);
200-
201-
$toolbar->cancel('contact.cancel');
202-
203-
if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->get('params')->get('save_history', 0) && $itemEditable) {
204-
$toolbar->versions('com_contact.contact', $this->item->id);
205-
}
206-
207-
if (Associations::isEnabled() && ComponentHelper::isEnabled('com_associations')) {
208-
$toolbar->standardButton('contract', 'JTOOLBAR_ASSOCIATIONS', 'contact.editAssociations')
209-
->icon('icon-contract')
210-
->listCheck(false);
211-
}
113+
return;
212114
}
213115

214-
$toolbar->divider();
215-
$toolbar->help('Contacts:_Edit');
116+
parent::addToolbar();
117+
118+
return;
216119
}
217120

218121
/**

0 commit comments

Comments
 (0)