2014-09-15 11:38:24 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Controllers\ViewProxies;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Helpers\EnumHelper;
|
|
|
|
use Szurubooru\Helpers\MimeHelper;
|
|
|
|
use Szurubooru\Privilege;
|
|
|
|
use Szurubooru\Services\AuthService;
|
|
|
|
use Szurubooru\Services\FavoritesService;
|
|
|
|
use Szurubooru\Services\HistoryService;
|
|
|
|
use Szurubooru\Services\PrivilegeService;
|
|
|
|
use Szurubooru\Services\ScoreService;
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
class PostViewProxy extends AbstractViewProxy
|
|
|
|
{
|
2014-09-25 23:53:47 +02:00
|
|
|
const FETCH_USER = 'fetchUser';
|
|
|
|
const FETCH_TAGS = 'fetchTags';
|
|
|
|
const FETCH_RELATIONS = 'fetchRelations';
|
2014-09-28 16:56:15 +02:00
|
|
|
const FETCH_HISTORY = 'fetchHistory';
|
|
|
|
const FETCH_OWN_SCORE = 'fetchOwnScore';
|
|
|
|
const FETCH_FAVORITES = 'fetchFavorites';
|
2014-09-25 23:53:47 +02:00
|
|
|
|
2014-09-28 16:56:15 +02:00
|
|
|
private $privilegeService;
|
|
|
|
private $authService;
|
|
|
|
private $historyService;
|
|
|
|
private $favoritesService;
|
2014-10-05 14:54:21 +02:00
|
|
|
private $scoreService;
|
2014-09-21 09:35:43 +02:00
|
|
|
private $tagViewProxy;
|
2014-09-21 18:21:54 +02:00
|
|
|
private $userViewProxy;
|
2014-09-28 16:56:15 +02:00
|
|
|
private $snapshotViewProxy;
|
2014-09-21 09:35:43 +02:00
|
|
|
|
2014-09-21 18:21:54 +02:00
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
PrivilegeService $privilegeService,
|
|
|
|
AuthService $authService,
|
|
|
|
HistoryService $historyService,
|
|
|
|
FavoritesService $favoritesService,
|
|
|
|
ScoreService $scoreService,
|
2014-09-21 18:21:54 +02:00
|
|
|
TagViewProxy $tagViewProxy,
|
2014-09-28 16:56:15 +02:00
|
|
|
UserViewProxy $userViewProxy,
|
|
|
|
SnapshotViewProxy $snapshotViewProxy)
|
2014-09-21 09:35:43 +02:00
|
|
|
{
|
2014-09-28 16:56:15 +02:00
|
|
|
$this->privilegeService = $privilegeService;
|
|
|
|
$this->authService = $authService;
|
|
|
|
$this->historyService = $historyService;
|
|
|
|
$this->favoritesService = $favoritesService;
|
2014-10-05 14:54:21 +02:00
|
|
|
$this->scoreService = $scoreService;
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->tagViewProxy = $tagViewProxy;
|
2014-09-21 18:21:54 +02:00
|
|
|
$this->userViewProxy = $userViewProxy;
|
2014-09-28 16:56:15 +02:00
|
|
|
$this->snapshotViewProxy = $snapshotViewProxy;
|
2014-09-21 09:35:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 23:53:47 +02:00
|
|
|
public function fromEntity($post, $config = [])
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
|
|
|
$result = new \StdClass;
|
2014-09-28 16:56:15 +02:00
|
|
|
if (!$post)
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
$result->id = $post->getId();
|
|
|
|
$result->idMarkdown = $post->getIdMarkdown();
|
|
|
|
$result->name = $post->getName();
|
|
|
|
$result->uploadTime = $post->getUploadTime();
|
|
|
|
$result->lastEditTime = $post->getLastEditTime();
|
2014-10-08 14:47:47 +02:00
|
|
|
$result->safety = EnumHelper::postSafetyToString($post->getSafety());
|
|
|
|
$result->contentType = EnumHelper::postTypeToString($post->getContentType());
|
2014-09-28 16:56:15 +02:00
|
|
|
$result->contentChecksum = $post->getContentChecksum();
|
|
|
|
$result->contentMimeType = $post->getContentMimeType();
|
2014-10-08 14:47:47 +02:00
|
|
|
$result->contentExtension = MimeHelper::getExtension($post->getContentMimeType());
|
2014-09-28 16:56:15 +02:00
|
|
|
$result->source = $post->getSource();
|
|
|
|
$result->imageWidth = $post->getImageWidth();
|
|
|
|
$result->imageHeight = $post->getImageHeight();
|
|
|
|
$result->featureCount = $post->getFeatureCount();
|
|
|
|
$result->lastFeatureTime = $post->getLastFeatureTime();
|
|
|
|
$result->originalFileSize = $post->getOriginalFileSize();
|
|
|
|
$result->favoriteCount = $post->getFavoriteCount();
|
|
|
|
$result->score = $post->getScore();
|
2014-10-04 18:25:27 +02:00
|
|
|
$result->commentCount = $post->getCommentCount();
|
2014-09-28 16:56:15 +02:00
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_TAGS]))
|
|
|
|
$result->tags = $this->tagViewProxy->fromArray($post->getTags());
|
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_USER]))
|
|
|
|
$result->user = $this->userViewProxy->fromEntity($post->getUser());
|
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_RELATIONS]))
|
|
|
|
$result->relations = $this->fromArray($post->getRelatedPosts());
|
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_HISTORY]))
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
if ($this->privilegeService->hasPrivilege(Privilege::VIEW_HISTORY))
|
2014-09-28 16:56:15 +02:00
|
|
|
$result->history = $this->snapshotViewProxy->fromArray($this->historyService->getPostHistory($post));
|
|
|
|
else
|
|
|
|
$result->history = [];
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
2014-09-28 16:56:15 +02:00
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn())
|
2014-10-05 14:54:21 +02:00
|
|
|
$result->ownScore = $this->scoreService->getScoreValue($this->authService->getLoggedInUser(), $post);
|
2014-09-28 16:56:15 +02:00
|
|
|
|
|
|
|
if (!empty($config[self::FETCH_FAVORITES]))
|
|
|
|
$result->favorites = $this->userViewProxy->fromArray($this->favoritesService->getFavoriteUsers($post));
|
|
|
|
|
|
|
|
|
2014-09-15 11:38:24 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|