Skip to content

Commit 61539c5

Browse files
authored
[6.0] Improve ListView toolbar & components (joomla#45521)
* Prevent exception if $error is no array * Update ListView to reflect current status of the toolbar * Convert contacts list view to ListView class * Convert modules list view to ListView class * Convert Actionlogs list view to LiewView class * Convert banners list view to ListView class * Use normal models * Apply suggestions from code review Co-authored-by: Richard Fath <[email protected]> * Fix namespace usage * Fix code style * Codestyle * Remove unused usages * Remove phpstan occurences
1 parent 2ea8fa6 commit 61539c5

File tree

6 files changed

+244
-588
lines changed

6 files changed

+244
-588
lines changed

administrator/components/com_actionlogs/src/View/Actionlogs/HtmlView.php

Lines changed: 26 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@
1111
namespace Joomla\Component\Actionlogs\Administrator\View\Actionlogs;
1212

1313
use Joomla\CMS\Component\ComponentHelper;
14-
use Joomla\CMS\Form\Form;
1514
use Joomla\CMS\Language\Text;
16-
use Joomla\CMS\MVC\View\GenericDataException;
17-
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
18-
use Joomla\CMS\Pagination\Pagination;
15+
use Joomla\CMS\MVC\View\ListView;
1916
use Joomla\CMS\Toolbar\ToolbarHelper;
2017
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
21-
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogsModel;
2218

2319
// phpcs:disable PSR1.Files.SideEffects
2420
\defined('_JEXEC') or die;
@@ -29,48 +25,8 @@
2925
*
3026
* @since 3.9.0
3127
*/
32-
class HtmlView extends BaseHtmlView
28+
class HtmlView extends ListView
3329
{
34-
/**
35-
* An array of items.
36-
*
37-
* @var array
38-
* @since 3.9.0
39-
*/
40-
protected $items;
41-
42-
/**
43-
* The model state
44-
*
45-
* @var array
46-
* @since 3.9.0
47-
*/
48-
protected $state;
49-
50-
/**
51-
* The pagination object
52-
*
53-
* @var Pagination
54-
* @since 3.9.0
55-
*/
56-
protected $pagination;
57-
58-
/**
59-
* Form object for search filters
60-
*
61-
* @var Form
62-
* @since 3.9.0
63-
*/
64-
public $filterForm;
65-
66-
/**
67-
* The active search filters
68-
*
69-
* @var array
70-
* @since 3.9.0
71-
*/
72-
public $activeFilters;
73-
7430
/**
7531
* Setting if the IP column should be shown
7632
*
@@ -88,44 +44,46 @@ class HtmlView extends BaseHtmlView
8844
protected $dateRelative = false;
8945

9046
/**
91-
* Method to display the view.
47+
* Constructor
9248
*
93-
* @param string $tpl A template file to load. [optional]
49+
* @param array $config An optional associative array of configuration settings.
9450
*
95-
* @return void
51+
* @since __DEPLOY_VERSION__
52+
*/
53+
public function __construct(array $config)
54+
{
55+
if (empty($config['option'])) {
56+
$config['option'] = 'com_actionlogs';
57+
}
58+
59+
$config['toolbar_title'] = 'COM_ACTIONLOGS_MANAGER_USERLOGS';
60+
$config['toolbar_icon'] = 'list-2 actionlog';
61+
62+
parent::__construct($config);
63+
}
64+
65+
/**
66+
* Prepare view data
9667
*
97-
* @since 3.9.0
68+
* @return void
9869
*
99-
* @throws \Exception
70+
* @since __DEPLOY_VERSION__
10071
*/
101-
public function display($tpl = null)
72+
protected function initializeView()
10273
{
103-
/** @var ActionlogsModel $model */
104-
$model = $this->getModel();
105-
$this->items = $model->getItems();
106-
$this->state = $model->getState();
107-
$this->pagination = $model->getPagination();
108-
$this->filterForm = $model->getFilterForm();
109-
$this->activeFilters = $model->getActiveFilters();
74+
parent::initializeView();
75+
11076
$params = ComponentHelper::getParams('com_actionlogs');
11177
$this->showIpColumn = (bool) $params->get('ip_logging', 0);
11278
$this->dateRelative = (bool) $params->get('date_relative', 1);
11379

114-
if (\count($errors = $model->getErrors())) {
115-
throw new GenericDataException(implode("\n", $errors), 500);
116-
}
117-
118-
$this->addToolbar();
119-
12080
// Load all actionlog plugins language files
12181
ActionlogsHelper::loadActionLogPluginsLanguage();
12282

12383
// Add form control fields
12484
$this->filterForm
12585
->addControlField('task', '')
12686
->addControlField('boxchecked', '0');
127-
128-
parent::display($tpl);
12987
}
13088

13189
/**
@@ -138,6 +96,7 @@ public function display($tpl = null)
13896
protected function addToolbar()
13997
{
14098
ToolbarHelper::title(Text::_('COM_ACTIONLOGS_MANAGER_USERLOGS'), 'icon-list-2');
99+
141100
$toolbar = $this->getDocument()->getToolbar();
142101

143102
$toolbar->standardButton('download', 'COM_ACTIONLOGS_EXPORT_CSV', 'actionlogs.exportSelectedLogs')

administrator/components/com_banners/src/View/Banners/HtmlView.php

Lines changed: 26 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@
1010

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

13-
use Joomla\CMS\Form\Form;
1413
use Joomla\CMS\Helper\ContentHelper;
1514
use Joomla\CMS\Language\Multilanguage;
16-
use Joomla\CMS\Language\Text;
17-
use Joomla\CMS\MVC\View\GenericDataException;
18-
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
19-
use Joomla\CMS\Pagination\Pagination;
20-
use Joomla\CMS\Toolbar\Button\DropdownButton;
21-
use Joomla\CMS\Toolbar\ToolbarHelper;
15+
use Joomla\CMS\MVC\View\ListView;
2216
use Joomla\Component\Banners\Administrator\Model\BannersModel;
23-
use Joomla\Registry\Registry;
2417

2518
// phpcs:disable PSR1.Files.SideEffects
2619
\defined('_JEXEC') or die;
@@ -31,24 +24,8 @@
3124
*
3225
* @since 1.6
3326
*/
34-
class HtmlView extends BaseHtmlView
27+
class HtmlView extends ListView
3528
{
36-
/**
37-
* The search tools form
38-
*
39-
* @var Form
40-
* @since 1.6
41-
*/
42-
public $filterForm;
43-
44-
/**
45-
* The active search filters
46-
*
47-
* @var array
48-
* @since 1.6
49-
*/
50-
public $activeFilters = [];
51-
5229
/**
5330
* Category data
5431
*
@@ -58,68 +35,48 @@ class HtmlView extends BaseHtmlView
5835
protected $categories = [];
5936

6037
/**
61-
* An array of items
38+
* The help link for the view
6239
*
63-
* @var array
64-
* @since 1.6
40+
* @var string
6541
*/
66-
protected $items = [];
42+
protected $helpLink = 'Banners';
6743

6844
/**
69-
* The pagination object
45+
* Constructor
7046
*
71-
* @var Pagination
72-
* @since 1.6
73-
*/
74-
protected $pagination;
75-
76-
/**
77-
* The model state
47+
* @param array $config An optional associative array of configuration settings.
7848
*
79-
* @var Registry
80-
* @since 1.6
49+
* @since __DEPLOY_VERSION__
8150
*/
82-
protected $state;
51+
public function __construct(array $config)
52+
{
53+
if (empty($config['option'])) {
54+
$config['option'] = 'com_banners';
55+
}
8356

84-
/**
85-
* Is this view an Empty State
86-
*
87-
* @var boolean
88-
* @since 4.0.0
89-
*/
90-
private $isEmptyState = false;
57+
$config['toolbar_icon'] = 'bookmark banners';
58+
$config['supports_batch'] = true;
59+
$config['category'] = 'com_banners';
60+
61+
parent::__construct($config);
62+
}
9163

9264
/**
93-
* Method to display the view.
94-
*
95-
* @param string $tpl A template file to load. [optional]
65+
* Prepare view data
9666
*
9767
* @return void
9868
*
99-
* @since 1.6
100-
* @throws \Exception
69+
* @since __DEPLOY_VERSION__
10170
*/
102-
public function display($tpl = null): void
71+
protected function initializeView()
10372
{
73+
parent::initializeView();
74+
10475
/** @var BannersModel $model */
10576
$model = $this->getModel();
106-
$this->categories = $model->getCategoryOrders();
107-
$this->items = $model->getItems();
108-
$this->pagination = $model->getPagination();
109-
$this->state = $model->getState();
110-
$this->filterForm = $model->getFilterForm();
111-
$this->activeFilters = $model->getActiveFilters();
112-
113-
if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
114-
$this->setLayout('emptystate');
115-
}
116-
117-
// Check for errors.
118-
if (\count($errors = $model->getErrors())) {
119-
throw new GenericDataException(implode("\n", $errors), 500);
120-
}
12177

122-
$this->addToolbar();
78+
$this->categories = $model->getCategoryOrders();
79+
$this->canDo = ContentHelper::getActions('com_banners', 'category', $this->state->get('filter.category_id'));
12380

12481
// We do not need to filter by language when multilingual is disabled
12582
if (!Multilanguage::isEnabled()) {
@@ -131,87 +88,5 @@ public function display($tpl = null): void
13188
$this->filterForm
13289
->addControlField('task', '')
13390
->addControlField('boxchecked', '0');
134-
135-
parent::display($tpl);
136-
}
137-
138-
/**
139-
* Add the page title and toolbar.
140-
*
141-
* @return void
142-
*
143-
* @since 1.6
144-
*/
145-
protected function addToolbar(): void
146-
{
147-
$canDo = ContentHelper::getActions('com_banners', 'category', $this->state->get('filter.category_id'));
148-
$user = $this->getCurrentUser();
149-
$toolbar = $this->getDocument()->getToolbar();
150-
151-
ToolbarHelper::title(Text::_('COM_BANNERS_MANAGER_BANNERS'), 'bookmark banners');
152-
153-
if ($canDo->get('core.create') || \count($user->getAuthorisedCategories('com_banners', 'core.create')) > 0) {
154-
$toolbar->addNew('banner.add');
155-
}
156-
157-
if (!$this->isEmptyState && ($canDo->get('core.edit.state') || ($this->state->get('filter.published') == -2 && $canDo->get('core.delete')))) {
158-
/** @var DropdownButton $dropdown */
159-
$dropdown = $toolbar->dropdownButton('status-group', 'JTOOLBAR_CHANGE_STATUS')
160-
->toggleSplit(false)
161-
->icon('icon-ellipsis-h')
162-
->buttonClass('btn btn-action')
163-
->listCheck(true);
164-
165-
$childBar = $dropdown->getChildToolbar();
166-
167-
if ($canDo->get('core.edit.state')) {
168-
if ($this->state->get('filter.published') != 2) {
169-
$childBar->publish('banners.publish')->listCheck(true);
170-
171-
$childBar->unpublish('banners.unpublish')->listCheck(true);
172-
}
173-
174-
if ($this->state->get('filter.published') != -1) {
175-
if ($this->state->get('filter.published') != 2) {
176-
$childBar->archive('banners.archive')->listCheck(true);
177-
} elseif ($this->state->get('filter.published') == 2) {
178-
$childBar->publish('publish')->task('banners.publish')->listCheck(true);
179-
}
180-
}
181-
182-
$childBar->checkin('banners.checkin');
183-
184-
if ($this->state->get('filter.published') != -2) {
185-
$childBar->trash('banners.trash')->listCheck(true);
186-
}
187-
}
188-
189-
if ($this->state->get('filter.published') == -2 && $canDo->get('core.delete')) {
190-
$toolbar->delete('banners.delete', 'JTOOLBAR_DELETE_FROM_TRASH')
191-
->message('JGLOBAL_CONFIRM_DELETE')
192-
->listCheck(true);
193-
}
194-
195-
// Add a batch button
196-
if (
197-
$user->authorise('core.create', 'com_banners')
198-
&& $user->authorise('core.edit', 'com_banners')
199-
&& $user->authorise('core.edit.state', 'com_banners')
200-
) {
201-
$childBar->popupButton('batch', 'JTOOLBAR_BATCH')
202-
->popupType('inline')
203-
->textHeader(Text::_('COM_BANNERS_BATCH_OPTIONS'))
204-
->url('#joomla-dialog-batch')
205-
->modalWidth('800px')
206-
->modalHeight('fit-content')
207-
->listCheck(true);
208-
}
209-
}
210-
211-
if ($user->authorise('core.admin', 'com_banners') || $user->authorise('core.options', 'com_banners')) {
212-
$toolbar->preferences('com_banners');
213-
}
214-
215-
$toolbar->help('Banners');
21691
}
21792
}

0 commit comments

Comments
 (0)