szurubooru/src/Dao/PostDao.php

132 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Dao;
2014-09-15 11:38:24 +02:00
class PostDao extends AbstractDao implements ICrudDao
{
2014-09-21 09:35:43 +02:00
private $tagDao;
private $fileService;
private $thumbnailService;
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
2014-09-21 09:35:43 +02:00
\Szurubooru\Dao\TagDao $tagDao,
\Szurubooru\Services\FileService $fileService,
\Szurubooru\Services\ThumbnailService $thumbnailService)
{
parent::__construct(
$databaseConnection,
'posts',
new \Szurubooru\Dao\EntityConverters\PostEntityConverter());
2014-09-21 09:35:43 +02:00
$this->tagDao = $tagDao;
$this->fileService = $fileService;
$this->thumbnailService = $thumbnailService;
}
2014-09-15 11:38:24 +02:00
public function findByName($name)
{
return $this->findOneBy('name', $name);
}
public function findByContentChecksum($checksum)
{
return $this->findOneBy('contentChecksum', $checksum);
}
protected function afterLoad(\Szurubooru\Entities\Entity $post)
2014-09-15 11:38:24 +02:00
{
$post->setLazyLoader(
\Szurubooru\Entities\Post::LAZY_LOADER_CONTENT,
function(\Szurubooru\Entities\Post $post)
{
return $this->fileService->load($post->getContentPath());
});
$post->setLazyLoader(
\Szurubooru\Entities\Post::LAZY_LOADER_THUMBNAIL_SOURCE_CONTENT,
function(\Szurubooru\Entities\Post $post)
{
return $this->fileService->load($post->getThumbnailSourceContentPath());
});
$post->setLazyLoader(
\Szurubooru\Entities\Post::LAZY_LOADER_TAGS,
function(\Szurubooru\Entities\Post $post)
2014-09-15 11:38:24 +02:00
{
return $this->getTags($post);
});
}
protected function afterSave(\Szurubooru\Entities\Entity $post)
2014-09-15 11:38:24 +02:00
{
$this->syncContent($post);
$this->syncThumbnailSourceContent($post);
2014-09-21 09:35:43 +02:00
$this->syncTags($post);
2014-09-15 11:38:24 +02:00
}
private function getTags(\Szurubooru\Entities\Post $post)
{
2014-09-21 09:35:43 +02:00
return $this->tagDao->findByPostId($post->getId());
2014-09-15 11:38:24 +02:00
}
private function syncContent(\Szurubooru\Entities\Post $post)
{
$targetPath = $post->getContentPath();
$content = $post->getContent();
if ($content)
$this->fileService->save($targetPath, $content);
else
$this->fileService->delete($targetPath, $content);
$this->thumbnailService->deleteUsedThumbnails($targetPath);
}
private function syncThumbnailSourceContent(\Szurubooru\Entities\Post $post)
{
$targetPath = $post->getThumbnailSourceContentPath();
$content = $post->getThumbnailSourceContent();
if ($content)
$this->fileService->save($targetPath, $content);
else
$this->fileService->delete($targetPath);
$this->thumbnailService->deleteUsedThumbnails($targetPath);
}
2014-09-21 09:35:43 +02:00
private function syncTags(\Szurubooru\Entities\Post $post)
2014-09-15 11:38:24 +02:00
{
2014-09-21 09:35:43 +02:00
$tagNames = array_filter(array_unique(array_map(
function ($tag)
{
2014-09-21 09:35:43 +02:00
return $tag->getName();
},
2014-09-21 09:35:43 +02:00
$post->getTags())));
2014-09-15 11:38:24 +02:00
2014-09-21 09:35:43 +02:00
$this->tagDao->createMissingTags($tagNames);
$tagIds = array_map(
function($tag)
{
return $tag->getId();
},
$this->tagDao->findByNames($tagNames));
2014-09-15 11:38:24 +02:00
2014-09-21 09:35:43 +02:00
$existingTagRelationIds = array_map(
2014-09-15 11:38:24 +02:00
function($arrayEntity)
{
2014-09-21 09:35:43 +02:00
return $arrayEntity['tagId'];
2014-09-15 11:38:24 +02:00
},
2014-09-21 09:35:43 +02:00
iterator_to_array($this->fpdo->from('postTags')->where('postId', $post->getId())));
2014-09-15 11:38:24 +02:00
2014-09-21 09:35:43 +02:00
$tagRelationsToInsert = array_diff($tagIds, $existingTagRelationIds);
$tagRelationsToDelete = array_diff($existingTagRelationIds, $tagIds);
2014-09-15 11:38:24 +02:00
2014-09-21 09:35:43 +02:00
foreach ($tagRelationsToInsert as $tagId)
{
$this->fpdo->insertInto('postTags')->values(['postId' => $post->getId(), 'tagId' => $tagId])->execute();
}
foreach ($tagRelationsToDelete as $tagId)
2014-09-15 11:38:24 +02:00
{
2014-09-21 09:35:43 +02:00
$this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->and('tagId', $tagId)->execute();
2014-09-15 11:38:24 +02:00
}
}
}