Merge pull request #3748 from Icinga/feature/sortable-announcements-page-3414

Sortable announcements page
This commit is contained in:
Johannes Meyer 2019-04-18 14:51:16 +02:00 committed by GitHub
commit 0be1053197
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 34 deletions

View File

@ -27,10 +27,26 @@ class AnnouncementsController extends Controller
)
);
$repo = new AnnouncementIniRepository();
$this->view->announcements = $repo
->select(array('id', 'author', 'message', 'start', 'end'))
->order('start', 'DESC');
$announcements = (new AnnouncementIniRepository())
->select([
'id',
'author',
'message',
'start',
'end'
]);
$sortAndFilterColumns = [
'author' => $this->translate('Author'),
'message' => $this->translate('Message'),
'start' => $this->translate('Start'),
'end' => $this->translate('End')
];
$this->setupSortControl($sortAndFilterColumns, $announcements, ['start' => 'desc']);
$this->setupFilterControl($announcements, $sortAndFilterColumns, ['message']);
$this->view->announcements = $announcements->fetchAll();
}
/**

View File

@ -13,6 +13,21 @@ use Icinga\Forms\RepositoryForm;
*/
class AnnouncementForm extends RepositoryForm
{
protected function fetchEntry()
{
$entry = parent::fetchEntry();
if ($entry !== false) {
if ($entry->start !== null) {
$entry->start = (new DateTime())->setTimestamp($entry->start);
}
if ($entry->end !== null) {
$entry->end = (new DateTime())->setTimestamp($entry->end);
}
}
return $entry;
}
/**
* {@inheritDoc}
*/

View File

@ -1,6 +1,12 @@
<?php if (! $compact): ?>
<?php if (! $this->compact): ?>
<div class="controls">
<?= $tabs ?>
<?= $this->tabs ?>
<?= $this->paginator ?>
<div class="sort-controls-container">
<?= $this->limiter ?>
<?= $this->sortBox ?>
</div>
<?= $this->filterEditor ?>
</div>
<?php endif ?>
<div class="content">
@ -17,7 +23,7 @@
)
);
} ?>
<?php if (! $announcements->hasResult()): ?>
<?php if (empty($this->announcements)): ?>
<p><?= $this->translate('No announcements found.') ?></p>
</div>
<?php return; endif ?>
@ -32,7 +38,7 @@
</tr>
</thead>
<tbody>
<?php foreach ($announcements as $announcement): /** @var object $announcement */ ?>
<?php foreach ($this->announcements as $announcement): /** @var object $announcement */ ?>
<?php if ($this->hasPermission('admin')): ?>
<tr href="<?= $this->href('announcements/update', array('id' => $announcement->id)) ?>">
<?php else: ?>
@ -40,8 +46,8 @@
<?php endif ?>
<td><?= $this->escape($announcement->author) ?></td>
<td><?= $this->ellipsis($this->escape($announcement->message), 100) ?></td>
<td><?= $this->formatDateTime($announcement->start->getTimestamp()) ?></td>
<td><?= $this->formatDateTime($announcement->end->getTimestamp()) ?></td>
<td><?= $this->formatDateTime($announcement->start) ?></td>
<td><?= $this->formatDateTime($announcement->end) ?></td>
<?php if ($this->hasPermission('admin')): ?>
<td class="icon-col"><?= $this->qlink(
null,

View File

@ -365,13 +365,11 @@ class SimpleQuery implements QueryInterface, Queryable, Iterator
}
$column = $this->order[$orderIndex][0];
if (array_key_exists($column, $this->flippedColumns)) {
if (array_key_exists($column, $this->flippedColumns) && is_string($this->flippedColumns[$column])) {
$column = $this->flippedColumns[$column];
}
// TODO: throw Exception if column is missing
//$res = strnatcmp(strtolower($a->$column), strtolower($b->$column));
$result = @strcmp(strtolower($a->$column), strtolower($b->$column));
$result = strcmp(strtolower($a->$column), strtolower($b->$column));
if ($result === 0) {
return $this->compare($a, $b, ++$orderIndex);
}

View File

@ -30,25 +30,6 @@ class AnnouncementIniRepository extends IniRepository
'end' => 'timestamp'
));
/**
* Create a DateTime from a timestamp
*
* @param string $timestamp
*
* @return DateTime|null
*/
protected function retrieveTimestamp($timestamp)
{
if ($timestamp !== null) {
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);
return $dateTime;
}
return null;
}
/**
* Get a DateTime's timestamp
*
@ -157,7 +138,7 @@ class AnnouncementIniRepository extends IniRepository
$refresh = null;
foreach ($query as $row) {
$min = min($row->start->getTimestamp(), $row->end->getTimestamp());
$min = min($row->start, $row->end);
if ($refresh === null) {
$refresh = $min;