Merge branch 'master' of ssh://git.thisfro.ch:222/thisfro/pflaenz.li

This commit is contained in:
Jannis Portmann 2021-07-23 11:32:48 +02:00
commit 34088972f8
12 changed files with 419 additions and 393 deletions

View file

@ -17,6 +17,7 @@
"symfony/asset": "5.2.*",
"symfony/console": "5.2.*",
"symfony/dotenv": "5.2.*",
"symfony/filesystem": "5.2.*",
"symfony/flex": "^1.3.1",
"symfony/form": "5.2.*",
"symfony/framework-bundle": "5.2.*",

592
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210715135559 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE offering ADD coordinates TEXT DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN offering.coordinates IS \'(DC2Type:array)\'');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE offering DROP coordinates');
}
}

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210715142350 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE offering ADD coordinate JSON DEFAULT NULL');
$this->addSql('ALTER TABLE offering DROP coordinates');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE offering ADD coordinates TEXT DEFAULT NULL');
$this->addSql('ALTER TABLE offering DROP coordinate');
$this->addSql('COMMENT ON COLUMN offering.coordinates IS \'(DC2Type:array)\'');
}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210715142732 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE offering ALTER coordinate TYPE TEXT');
$this->addSql('ALTER TABLE offering ALTER coordinate DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN offering.coordinate IS \'(DC2Type:object)\'');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE offering ALTER coordinate TYPE JSON');
$this->addSql('ALTER TABLE offering ALTER coordinate DROP DEFAULT');
$this->addSql('COMMENT ON COLUMN offering.coordinate IS NULL');
}
}

View file

@ -10,8 +10,11 @@ use App\Repository\WishRepository;
use App\Service\PlzToCoordinate;
use App\Service\DistanceCalculator;
use App\Service\OfferPhotoHelper;
use Doctrine\ORM\EntityManagerInterface;
use Location\Coordinate;
use Location\Formatter\Coordinate\GeoJSON;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -37,7 +40,7 @@ class OfferController extends AbstractController
}
#[Route('/new', name: 'new_offer')]
public function newOffer(Request $request, string $photoDir): Response
public function newOffer(Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper, PlzToCoordinate $plzconverter): Response
{
$offer = new Offering();
$form = $this->createForm(OfferingFormType::class, $offer);
@ -49,16 +52,13 @@ class OfferController extends AbstractController
$offer->setByUser($user);
$offer->setCreatedAt(new \DateTime());
if ($offerPlz = $form['zipCode']->getData()) {
$coordinate = $plzconverter->convertPlzToCoordinate($offerPlz);
$offer->setCoordinate($coordinate->format(new GeoJSON()));
}
if ($photo = $form['photo']->getData()) {
$filename = uniqid().'.'.$photo->guessExtension();
try {
$photo->move($photoDir, $filename);
} catch (FileException $e) {
// unable to upload the photo, give up
$this->addFlash("error", "There was an error uploading the photo: ".$e);
return $this->redirectToRoute('new_offer');
}
$offer->setPhotoFilename($filename);
$offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer);
}
$this->entityManager->persist($offer);
@ -75,9 +75,9 @@ class OfferController extends AbstractController
}
#[Route('/offer/{id}', name: 'show_offer')]
public function show_offer(Offering $offer, WishRepository $wishRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
public function showOffer(Offering $offer, WishRepository $wishRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
{
$distance = 0;
$distance = null;
$user = $this->getUser();
$offerPlz = $offer->getZipCode();
@ -88,7 +88,7 @@ class OfferController extends AbstractController
if (isset($userPlz))
{
$distance = $distanceCalculator->calculateDistance($plzconverter->getCoordinates($offerPlz), $plzconverter->getCoordinates($userPlz));
$distance = $distanceCalculator->calculateDistance($plzconverter->convertPlzToCoordinate($offerPlz), $plzconverter->convertPlzToCoordinate($userPlz));
}
return $this->render('app/offer.html.twig', [
@ -100,7 +100,7 @@ class OfferController extends AbstractController
}
#[Route('/offer/edit/{id}', name: 'edit_offer')]
public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir): Response
public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response
{
$form = $this->createForm(OfferingFormType::class, $offer);
$user = $this->getUser();
@ -113,15 +113,9 @@ class OfferController extends AbstractController
$offer->setCreatedAt(new \DateTime());
if ($photo = $form['photo']->getData()) {
$filename = uniqid(random_bytes(6)).'.'.$photo->guessExtension();
try {
$photo->move($photoDir, $filename);
} catch (FileException $e) {
// unable to upload the photo, give up
$this->addFlash("error", "There was an error uploading the photo: ".$e);
return $this->redirectToRoute('new_offer');
}
$offer->setPhotoFilename($filename);
$oldFilename = $offer->getPhotoFilename();
$offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer);
$offerPhotoHelper->deleteOfferPhoto($photoDir, $oldFilename);
}
$this->entityManager->persist($offer);
@ -139,16 +133,13 @@ class OfferController extends AbstractController
}
#[Route('/offer/delete/{id}', name: 'delete_offer')]
public function deleteOffer(Offering $offer, string $photoDir): Response
public function deleteOffer(Offering $offer, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response
{
$user = $this->getUser();
if ($offer->getByUser() === $user)
{
if ($offer->getPhotoFilename())
{
unlink($photoDir . '/' . $offer->getPhotoFilename());
}
$offerPhotoHelper->deleteOfferPhoto($photoDir, $offer->getPhotoFilename());
$this->entityManager->remove($offer);
$this->entityManager->flush();
$this->addFlash(

View file

@ -49,7 +49,7 @@ class RegistrationController extends AbstractController
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('no-reply@thisfro.ch', 'plantex no-reply'))
->from(new Address('no-reply@pflaenz.li', 'Pflänzli no-reply'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')

View file

@ -152,7 +152,7 @@ class ResetPasswordController extends AbstractController
}
$email = (new TemplatedEmail())
->from(new Address('no-reply@thisfro.ch', 'plantex no-reply'))
->from(new Address('no-reply@pflaenz.li', 'Pflänzli no-reply'))
->to($user->getEmail())
->subject('Your password reset request')
->htmlTemplate('reset_password/email.html.twig')

View file

@ -12,6 +12,7 @@ use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
@ -26,7 +27,7 @@ class TradeController extends AbstractController
if($user != $offer->getByUser())
{
$email = (new TemplatedEmail())
->from('no-reply@thisfro.ch')
->from(new Address('no-reply@pflaenz.li', 'Pflänzli no-reply'))
->to($offer->getByUser()->getEmail())
->replyTo($user->getEmail())
->subject($user->getUsername() . ' wants to trade with you!')

View file

@ -51,6 +51,11 @@ class Offering
*/
private $description;
/**
* @ORM\Column(type="object", nullable=true)
*/
private $coordinate;
public function getId(): ?int
{
return $this->id;
@ -132,4 +137,16 @@ class Offering
{
return (string) $this-getTitle();
}
public function getCoordinate()
{
return $this->coordinate;
}
public function setCoordinate($coordinate): self
{
$this->coordinate = $coordinate;
return $this;
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace App\Service;
use App\Entity\Offering;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class OfferPhotoHelper
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->filesystem = new Filesystem();
}
public function uploadOfferPhoto(string $photoDir, UploadedFile $photo, Offering $offer)
{
$filename = uniqid().'.'.$photo->guessExtension();
try {
$photo->move($photoDir, $filename);
} catch (FileException $e) {
// unable to upload the photo, give up
$this->addFlash("error", "There was an error uploading the photo: ".$e);
return $this->redirectToRoute('new_offer');
}
$offer->setPhotoFilename($filename);
}
public function deleteOfferPhoto(string $photoDir, string $filename)
{
$file = $photoDir . '/' . $filename;
if($this->filesystem->exists($file)) {
$this->filesystem->remove($file);
}
}
}

View file

@ -6,7 +6,7 @@ use Location\Coordinate;
class PlzToCoordinate
{
public function getCoordinates(int $plz)
public function convertPlzToCoordinate(int $plz)
{
$content = file_get_contents("https://swisspost.opendatasoft.com/api/records/1.0/search/?dataset=plz_verzeichnis_v2&q=postleitzahl%3D" . $plz);
$result = json_decode($content);