many fixes and expands and module expansions

This commit is contained in:
Dennis Eichhorn 2021-04-04 17:10:52 +02:00
parent de4648470f
commit b047c9bc33
8 changed files with 220 additions and 3 deletions

View File

@ -95,5 +95,29 @@
"foreignKey": "tag_id"
}
}
},
"news_seen": {
"name": "news_seen",
"fields": {
"news_seen_id": {
"name": "news_seen_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"news_seen_at": {
"name": "news_seen_at",
"type": "DATETIME",
"null": false
},
"news_seen_by": {
"name": "news_seen_by",
"type": "INT",
"null": false,
"foreignTable": "account",
"foreignKey": "account_id"
}
}
}
}

View File

@ -17,7 +17,10 @@ namespace Modules\News\Controller;
use Modules\Dashboard\Models\DashboardElementInterface;
use Modules\News\Models\NewsArticle;
use Modules\News\Models\NewsArticleMapper;
use Modules\News\Models\NewsSeen;
use Modules\News\Models\NewsSeenMapper;
use Modules\News\Models\NewsStatus;
use Modules\News\Models\NullNewsSeen;
use Modules\News\Models\PermissionState;
use phpOMS\Account\PermissionType;
use phpOMS\Contract\RenderableInterface;
@ -82,6 +85,23 @@ final class BackendController extends Controller implements DashboardElementInte
);
}
$seen = NewsSeenMapper::getFor($request->header->account, 'seenBy');
$view->setData('seen', $seen->seenAt);
// @async
if ($seen instanceof NullNewsSeen) {
$seen = new NewsSeen();
$seen->seenBy = (int) $request->header->account;
$seen->seenAt = new \DateTime('now');
NewsSeenMapper::create($seen);
} else {
$newSeen = clone $seen;
$newSeen->seenAt = new \DateTime('now');
NewsSeenMapper::update($newSeen);
}
return $view;
}

60
Models/NewsSeen.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\News\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\News\Models;
/**
* Null model
*
* @package Modules\News\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NewsSeen
{
/**
* Article ID.
*
* @var int
* @since 1.0.0
*/
protected int $id = 0;
public \DateTime $seenAt;
public int $seenBy = 0;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->seenAt = new \DateTime('now');
}
/**
* Get id
*
* @return int
*
* @since 1.0.0
*/
public function getId() : int
{
return $this->id;
}
}

64
Models/NewsSeenMapper.php Normal file
View File

@ -0,0 +1,64 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\News\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\News\Models;
use Modules\Admin\Models\AccountMapper;
use Modules\Comments\Models\CommentListMapper;
use Modules\Tag\Models\TagMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
/**
* News mapper class.
*
* @package Modules\News\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*
* @todo Orange-Management/oms-News#???
* Too complicated select.
* I think the default getAll etc. is too complicated and has too many joins which are not really required.
* Check and fix!
*/
final class NewsSeenMapper extends DataMapperAbstract
{
/**
* Columns.
*
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0
*/
protected static array $columns = [
'news_seen_id' => ['name' => 'news_seen_id', 'type' => 'int', 'internal' => 'id'],
'news_seen_at' => ['name' => 'news_seen_at', 'type' => 'DateTime', 'internal' => 'seenAt'],
'news_seen_by' => ['name' => 'news_seen_by', 'type' => 'int', 'internal' => 'seenBy'],
];
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
protected static string $table = 'news_seen';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'news_seen_id';
}

39
Models/NullNewsSeen.php Normal file
View File

@ -0,0 +1,39 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\News\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\News\Models;
/**
* Null model
*
* @package Modules\News\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class NullNewsSeen extends NewsSeen
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
parent::__construct();
}
}

View File

@ -30,7 +30,7 @@ echo $this->getData('nav')->render(); ?>
<div id="testEditor" class="m-editor">
<section class="portlet">
<div class="portlet-body">
<input id="iTitle" type="text" name="title" form="docForm" value="<?= $news->title; ?>">
<input id="iTitle" type="text" name="title" form="docForm" value="<?= $news->title; ?>" autocomplete="off">
</div>
</section>

View File

@ -18,6 +18,7 @@ use phpOMS\Utils\Parser\Markdown\Markdown;
/** @var \phpOMS\Views\View $this */
/** @var \Modules\News\Models\NewsArticle[] $newsList */
$newsList = $this->getData('news');
$seenAt = $this->getData('seen');
$previous = empty($newsList) ? '{/prefix}news/dashboard' : '{/prefix}news/dashboard?{?}&id=' . \reset($newsList)->getId() . '&ptype=p';
$next = empty($newsList) ? '{/prefix}news/dashboard' : '{/prefix}news/dashboard?{?}&id=' . \end($newsList)->getId() . '&ptype=n';
@ -31,7 +32,15 @@ echo $this->getData('nav')->render(); ?>
$profile = UriFactory::build('{/prefix}profile/single?{?}&id=' . $news->createdBy->getId());
?>
<div class="portlet">
<div class="portlet-head"><a href="<?= $url; ?>"><?= $this->printHtml($news->title); ?></a><span class="floatRight"><a href="<?= $profile; ?>"><?= $this->printHtml($news->createdBy->name3 . ' ' . $news->createdBy->name2 . ' ' . $news->createdBy->name1); ?></a> - <?= $news->publish->format('Y-m-d'); ?></span></div>
<div class="portlet-head">
<?= $seenAt->getTimestamp() < $news->publish->getTimestamp() ? '<strong>' : ''; ?>
<a href="<?= $url; ?>"><?= $this->printHtml($news->title); ?></a>
<span class="floatRight">
<a href="<?= $profile; ?>"><?= $this->printHtml($news->createdBy->name3 . ' ' . $news->createdBy->name2 . ' ' . $news->createdBy->name1); ?>
</a> - <?= $news->publish->format('Y-m-d'); ?>
</span>
<?= $seenAt->getTimestamp() < $news->publish->getTimestamp() ? '</strong>' : ''; ?>
</div>
<div class="portlet-body">
<article>
<?= Markdown::parse(\substr($news->plain, 0, 500)); ?>

View File

@ -14,6 +14,7 @@
declare(strict_types=1);
use phpOMS\Uri\UriFactory;
use Modules\Comments\Models\CommentListStatus;
/** @var \Modules\News\Models\NewsArticle $news */
$news = $this->getData('news');
@ -57,7 +58,7 @@ echo $this->getData('nav')->render(); ?>
<?php
$commentList = $news->comments;
if (!empty($commentList) && $commentList->isActive()) :
if (!empty($commentList) && $commentList->status !== CommentListStatus::INACTIVE) :
/* @todo: check if user has permission to create a comment here, maybe he is only allowed to read comments */
echo $this->getData('commentCreate')->render(1);
echo $this->getData('commentList')->render($commentList);