* image upload handling
* old static image migrator implementation as cli command * auth id fix, renew works now * templates refactored to work with uploaded images
This commit is contained in:
@@ -10,6 +10,8 @@ use UtilityModule\Hydrator\DoctrineObject;
|
||||
|
||||
class AwardeeManager
|
||||
{
|
||||
const IMAGE_DIRECTORY = 'data/user-data/images/awardee';
|
||||
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
@@ -24,6 +26,9 @@ class AwardeeManager
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Awardee[]|null
|
||||
*/
|
||||
public function getAwardees(): ?array
|
||||
{
|
||||
return $this->entityManager->getRepository(Awardee::class)->findBy([], [
|
||||
@@ -42,6 +47,10 @@ class AwardeeManager
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Awardee|null
|
||||
*/
|
||||
public function getAwardee(int $id): ?Awardee
|
||||
{
|
||||
/** @var Awardee $awardee */
|
||||
@@ -63,12 +72,14 @@ class AwardeeManager
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return Awardee
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function create($data): Awardee
|
||||
{
|
||||
unset($data['slug']);
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, new Awardee());
|
||||
$this->entityManager->persist($awardee);
|
||||
@@ -85,6 +96,7 @@ class AwardeeManager
|
||||
*/
|
||||
public function update(int $id, $data): Awardee
|
||||
{
|
||||
unset($data['slug']);
|
||||
$awardee = $this->entityManager->getRepository(Awardee::class)->find($id);
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, $awardee);
|
||||
@@ -99,7 +111,8 @@ class AwardeeManager
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function delete(int $id): bool {
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
if (null !== ($entity = $this->getAwardee($id))) {
|
||||
$this->entityManager->remove($entity);
|
||||
$this->entityManager->flush();
|
||||
@@ -107,4 +120,69 @@ class AwardeeManager
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public function getProfileImage(string $slug, string $type): string
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
if (file_exists($fileName)) {
|
||||
return file_get_contents($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException("$fileName not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $imageData
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function setProfileImage(string $slug, string $imageData, string $type): bool
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$this->ensureImageDirectoryExists();
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
return false !== file_put_contents($fileName, $imageData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteProfileImage(string $slug, string $type): bool
|
||||
{
|
||||
/** @var Awardee $entity */
|
||||
$entity = $this->entityManager->getRepository(Awardee::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId(), $type);
|
||||
if (file_exists($fileName)) {
|
||||
return unlink($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function ensureImageDirectoryExists()
|
||||
{
|
||||
if (!is_dir(self::IMAGE_DIRECTORY)) {
|
||||
mkdir(self::IMAGE_DIRECTORY, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
private function getImageFileName(int $id, string $type): string
|
||||
{
|
||||
return sprintf('%s/%s-%s.jpg', self::IMAGE_DIRECTORY, $id, $type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ use UtilityModule\Hydrator\DoctrineObject;
|
||||
|
||||
class JudgeManager
|
||||
{
|
||||
const IMAGE_DIRECTORY = 'data/user-data/images/judge';
|
||||
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
@@ -24,7 +26,10 @@ class JudgeManager
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getJudges()
|
||||
/**
|
||||
* @return Judge[]|null
|
||||
*/
|
||||
public function getJudges(): ?array
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j, t')
|
||||
@@ -32,14 +37,14 @@ class JudgeManager
|
||||
->leftJoin('j.titles', 't')
|
||||
->orderBy('j.name', 'ASC')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return array
|
||||
* @return Judge[]|null
|
||||
*/
|
||||
public function getJudgesByYear(int $year)
|
||||
public function getJudgesByYear(int $year): ?array
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j,t')
|
||||
@@ -49,7 +54,7 @@ class JudgeManager
|
||||
->orderBy('j.name', 'ASC')
|
||||
->setParameter('year', $year)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,6 +76,7 @@ class JudgeManager
|
||||
*/
|
||||
public function create($data): Judge
|
||||
{
|
||||
unset($data['slug']);
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, new Judge());
|
||||
$this->entityManager->persist($judge);
|
||||
@@ -87,6 +93,7 @@ class JudgeManager
|
||||
*/
|
||||
public function update(int $id, $data): Judge
|
||||
{
|
||||
unset($data['slug']);
|
||||
$judge = $this->entityManager->getRepository(Judge::class)->find($id);
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, $judge);
|
||||
@@ -110,4 +117,66 @@ class JudgeManager
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return string
|
||||
*/
|
||||
public function getProfileImage(string $slug): string
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
if (file_exists($fileName)) {
|
||||
return file_get_contents($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param string $imageData
|
||||
* @return bool
|
||||
*/
|
||||
public function setProfileImage(string $slug, string $imageData): bool
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$this->ensureImageDirectoryExists();
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
return false !== file_put_contents($fileName, $imageData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteProfileImage(string $slug): bool
|
||||
{
|
||||
/** @var Judge $entity */
|
||||
$entity = $this->entityManager->getRepository(Judge::class)->findOneBy([
|
||||
'slug' => $slug,
|
||||
]);
|
||||
$fileName = $this->getImageFileName($entity->getId());
|
||||
if (file_exists($fileName)) {
|
||||
return unlink($fileName);
|
||||
}
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function ensureImageDirectoryExists()
|
||||
{
|
||||
if (!is_dir(self::IMAGE_DIRECTORY)) {
|
||||
mkdir(self::IMAGE_DIRECTORY, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
private function getImageFileName(int $id): string
|
||||
{
|
||||
return sprintf('%s/%s.jpg', self::IMAGE_DIRECTORY, $id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user