add list pagination

This commit is contained in:
Dennis Eichhorn 2020-04-05 17:06:17 +02:00
parent 609cae2ffe
commit a224d30676
6 changed files with 124 additions and 59 deletions

View File

@ -63,5 +63,31 @@
"foreignKey": "account_id"
}
}
},
"news_tag": {
"name": "news_tag",
"fields": {
"news_tag_id": {
"name": "news_tag_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"news_tag_dst": {
"name": "news_tag_dst",
"type": "INT",
"null": false,
"foreignTable": "news",
"foreignKey": "news_id"
},
"news_tag_src": {
"name": "news_tag_src",
"type": "INT",
"null": false,
"foreignTable": "tag",
"foreignKey": "tag_id"
}
}
}
}

View File

@ -27,6 +27,8 @@ use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Model\Message\FormValidation;
use phpOMS\Utils\Parser\Markdown\Markdown;
use phpOMS\Message\Http\HttpResponse;
use Modules\Tag\Models\NullTag;
/**
* News controller class.
@ -164,6 +166,22 @@ final class ApiController extends Controller
$newsArticle->setStatus((int) ($request->getData('status') ?? NewsStatus::VISIBLE));
$newsArticle->setFeatured((bool) ($request->getData('featured') ?? true));
if (!empty($tags = $request->getDataJson('tags'))) {
foreach ($tags as $tag) {
if (!isset($tag['id'])) {
$request->setData('title', $tag['title'], true);
$request->setData('color', $tag['color'], true);
$request->setData('language', $tag['language'], true);
$internalResponse = new HttpResponse();
$this->app->moduleManager->get('Tag')->apiTagCreate($request, $internalResponse, null);
$newsArticle->addTag($internalResponse->get($request->getUri()->__toString())['response']);
} else {
$newsArticle->addTag(new NullTag((int) $tag['id']));
}
}
}
return $newsArticle;
}

View File

@ -54,8 +54,17 @@ final class BackendController extends Controller implements DashboardElementInte
$view->setTemplate('/Modules/News/Theme/Backend/news-dashboard');
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1000701001, $request, $response));
$news = NewsArticleMapper::getNewest(50);
$view->addData('news', $news);
if ($request->getData('ptype') === '-') {
$view->setData('news',
NewsArticleMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage())
::getBeforePivot((int) ($request->getData('id') ?? 0), null, 25)
);
} else {
$view->setData('news',
NewsArticleMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage())
::getAfterPivot((int) ($request->getData('id') ?? 0), null, 25)
);
}
return $view;
}

View File

@ -120,12 +120,12 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable
private bool $featured = false;
/**
* Badge.
* Tags.
*
* @var Tag[]
* @since 1.0.0
*/
private array $badges = [];
private array $tags = [];
/**
* Constructor.
@ -139,32 +139,6 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable
$this->publish = new \DateTime('now');
}
/**
* Get badges
*
* @return Tag[]
*
* @since 1.0.0
*/
public function getBadges() : array
{
return $this->badges;
}
/**
* Add badge
*
* @param Tag $badge Badge to add
*
* @return void
*
* @since 1.0.0
*/
public function addBadge(Tag $badge) : void
{
$this->badges[] = $badge;
}
/**
* Get content
*
@ -431,6 +405,32 @@ class NewsArticle implements ArrayableInterface, \JsonSerializable
$this->featured = $featured;
}
/**
* Get tags
*
* @return array
*
* @since 1.0.0
*/
public function getTags() : array
{
return $this->tags;
}
/**
* Add tag
*
* @param Tag $tag Tag
*
* @return void
*
* @since 1.0.0
*/
public function addTag(Tag $tag) : void
{
$this->tags[] = $tag;
}
/**
* {@inheritdoc}
*/

View File

@ -16,6 +16,7 @@ namespace Modules\News\Models;
use Modules\Admin\Models\AccountMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use Modules\Tag\Models\TagMapper;
/**
* News mapper class.
@ -60,6 +61,21 @@ final class NewsArticleMapper extends DataMapperAbstract
],
];
/**
* Has many relation.
*
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
protected static array $hasMany = [
'tags' => [
'mapper' => TagMapper::class,
'table' => 'news_tag',
'self' => 'news_tag_src',
'external' => 'news_tag_dst',
],
];
/**
* Primary table.
*

View File

@ -13,6 +13,7 @@
declare(strict_types=1);
use Modules\News\Models\NewsType;
use \phpOMS\Uri\UriFactory;
/**
* @var \phpOMS\Views\View $this
@ -20,39 +21,34 @@ use Modules\News\Models\NewsType;
*/
$newsList = $this->getData('news');
$previous = empty($newsList) ? '{/prefix}news/dashboard' : '{/prefix}news/dashboard?{?}&id=' . \reset($newsList)->getId() . '&ptype=-';
$next = empty($newsList) ? '{/prefix}news/dashboard' : '{/prefix}news/dashboard?{?}&id=' . \end($newsList)->getId() . '&ptype=+';
echo $this->getData('nav')->render(); ?>
<div class="row">
<div class="col-xs-12">
<div class="box wf-100">
<table id="newsList" class="default">
<caption><?= $this->getHtml('News'); ?><i class="fa fa-download floatRight download btn"></i></caption>
<thead>
<tr>
<td>
<td><?= $this->getHtml('Type') ?><i class="sort-asc fa fa-chevron-up"></i><i class="sort-desc fa fa-chevron-down"></i>
<td class="wf-100"><?= $this->getHtml('Title') ?><i class="sort-asc fa fa-chevron-up"></i><i class="sort-desc fa fa-chevron-down"></i>
<td><?= $this->getHtml('Author') ?><i class="sort-asc fa fa-chevron-up"></i><i class="sort-desc fa fa-chevron-down"></i>
<td><?= $this->getHtml('Date') ?><i class="sort-asc fa fa-chevron-up"></i><i class="sort-desc fa fa-chevron-down"></i>
<tbody>
<?php $count = 0; foreach ($newsList as $key => $news) : ++$count;
$url = \phpOMS\Uri\UriFactory::build('{/prefix}news/article?{?}&id=' . $news->getId());
$color = 'darkred';
if ($news->getType() === NewsType::ARTICLE) { $color = 'green'; }
elseif ($news->getType() === NewsType::HEADLINE) { $color = 'purple'; }
elseif ($news->getType() === NewsType::LINK) { $color = 'yellow'; }
?>
<tr data-href="<?= $url; ?>">
<td><a href="<?= $url; ?>"><?= $news->isFeatured() ? '<i class="fa fa-star favorite"></i>' : ''; ?></a>
<td data-label="<?= $this->getHtml('Type') ?>"><a href="<?= $url; ?>"><span class="tag <?= $this->printHtml($color); ?>"><?= $this->getHtml('TYPE' . $news->getType()) ?></span></a>
<td data-label="<?= $this->getHtml('Title') ?>"><a href="<?= $url; ?>"><?= $this->printHtml($news->getTitle()); ?></a>
<td data-label="<?= $this->getHtml('Author') ?>"><a href="<?= $url; ?>"><?= $this->printHtml($news->getCreatedBy()->getName1()); ?></a>
<td data-label="<?= $this->getHtml('Date') ?>"><a href="<?= $url; ?>"><?= $this->printHtml($news->getPublish()->format('Y-m-d')); ?></a>
<?php endforeach; ?>
<?php if ($count === 0) : ?>
<tr><td colspan="5" class="empty"><?= $this->getHtml('Empty', '0', '0'); ?>
<?php endif; ?>
</table>
<?php foreach ($newsList as $news) : $url = UriFactory::build('{/prefix}news/article?id=' . $news->getId()); ?>
<div class="portlet">
<div class="portlet-head"><a href="<?= $url; ?>"><?= $this->printHtml($news->getTitle()); ?></a></div>
<div class="portlet-body">
<article>
<?= \substr($news->getContent(), 0, 500) . (\strlen($news->getContent()) > 500 ? '...' : ''); ?>
</article>
</div>
<div class="portlet-foot">
<div class="overflowfix">
<?php $tags = $news->getTags(); foreach ($tags as $tag) : ?>
<span class="tag" style="background: <?= $this->printHtml($tag->getColor()); ?>"><?= $this->printHtml($tag->getTitle()); ?></span>
<?php endforeach; ?>
<a href="<?= $url; ?>" class="button floatRight">More</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="plain-portlet">
<a class="button" href="<?= UriFactory::build($previous); ?>"><?= $this->getHtml('Previous', '0', '0'); ?></a>
<a class="button" href="<?= UriFactory::build($next); ?>"><?= $this->getHtml('Next', '0', '0'); ?></a>
</div>
</div>