Skip to content

Commit

Permalink
New properties in location; Lists with images
Browse files Browse the repository at this point in the history
  • Loading branch information
manuxi committed Apr 2, 2021
1 parent 037fb06 commit f369b98
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 66 deletions.
29 changes: 27 additions & 2 deletions src/Common/DoctrineListRepresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Manuxi\SuluEventBundle\Common;

use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactory;
use Sulu\Component\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineFieldDescriptor;
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
Expand All @@ -15,15 +16,18 @@ class DoctrineListRepresentationFactory
private $restHelper;
private $listBuilderFactory;
private $fieldDescriptorFactory;
private $mediaManager;

public function __construct(
RestHelperInterface $restHelper,
DoctrineListBuilderFactory $listBuilderFactory,
FieldDescriptorFactoryInterface $fieldDescriptorFactory
FieldDescriptorFactoryInterface $fieldDescriptorFactory,
MediaManagerInterface $mediaManager
) {
$this->restHelper = $restHelper;
$this->listBuilderFactory = $listBuilderFactory;
$this->fieldDescriptorFactory = $fieldDescriptorFactory;
$this->mediaManager = $mediaManager;
}

/**
Expand All @@ -34,7 +38,8 @@ public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = []
): PaginatedRepresentation {
): PaginatedRepresentation
{
/** @var DoctrineFieldDescriptor[] $fieldDescriptors */
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors($resourceKey);

Expand All @@ -50,6 +55,7 @@ public function createDoctrineListRepresentation(
}

$list = $listBuilder->execute();
$list = $this->addImagesToListElements($list, $parameters['locale'] ?? null);

return new PaginatedRepresentation(
$list,
Expand All @@ -59,4 +65,23 @@ public function createDoctrineListRepresentation(
(int) $listBuilder->count()
);
}

/**
* @param mixed[]
*/
private function addImagesToListElements(array $listeElements, ?string $locale): array
{
$ids = array_filter(array_column($listeElements, 'image'));
$images = $this->mediaManager->getFormatUrls($ids, $locale);
foreach ($listeElements as $key => $element) {
if (\array_key_exists('image', $element)
&& $element['image']
&& \array_key_exists($element['image'], $images)
) {
$listeElements[$key]['image'] = $images[$element['image']];
}
}

return $listeElements;
}
}
82 changes: 68 additions & 14 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
namespace Manuxi\SuluEventBundle\DependencyInjection;

use Manuxi\SuluEventBundle\Entity\Event;
use Manuxi\SuluEventBundle\Entity\EventExcerpt;
use Manuxi\SuluEventBundle\Entity\EventExcerptTranslation;
use Manuxi\SuluEventBundle\Entity\EventSeo;
use Manuxi\SuluEventBundle\Entity\EventSeoTranslation;
use Manuxi\SuluEventBundle\Entity\EventTranslation;
use Manuxi\SuluEventBundle\Entity\Location;
use Manuxi\SuluEventBundle\Repository\EventExcerptRepository;
use Manuxi\SuluEventBundle\Repository\EventExcerptTranslationRepository;
use Manuxi\SuluEventBundle\Repository\EventRepository;
use Manuxi\SuluEventBundle\Repository\EventSeoRepository;
use Manuxi\SuluEventBundle\Repository\EventSeoTranslationRepository;
use Manuxi\SuluEventBundle\Repository\EventTranslationRepository;
use Manuxi\SuluEventBundle\Repository\LocationRepository;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -17,20 +29,62 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('sulu_event');
$root = $treeBuilder->getRootNode();

// $root->children()
// ->arrayNode('objects')
// ->addDefaultsIfNotSet()
// ->children()
// ->arrayNode('news')
// ->addDefaultsIfNotSet()
// ->children()
// ->scalarNode('model')->defaultValue(Event::class)->end()
// ->scalarNode('repository')->defaultValue(EventRepository::class)->end()
// ->end()
// ->end()
// ->end()
// ->end()
// ->end();
$root
->children()
->arrayNode('objects')
->addDefaultsIfNotSet()
->children()
->arrayNode('event')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Event::class)->end()
->scalarNode('repository')->defaultValue(EventRepository::class)->end()
->end()
->end()
->arrayNode('event_translation')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(EventTranslation::class)->end()
->scalarNode('repository')->defaultValue(EventTranslationRepository::class)->end()
->end()
->end()
->arrayNode('event_seo')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(EventSeo::class)->end()
->scalarNode('repository')->defaultValue(EventSeoRepository::class)->end()
->end()
->end()
->arrayNode('event_seo_translation')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(EventSeoTranslation::class)->end()
->scalarNode('repository')->defaultValue(EventSeoTranslationRepository::class)->end()
->end()
->end()
->arrayNode('event_excerpt')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(EventExcerpt::class)->end()
->scalarNode('repository')->defaultValue(EventExcerptRepository::class)->end()
->end()
->end()
->arrayNode('event_excerpt_translation')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(EventExcerptTranslation::class)->end()
->scalarNode('repository')->defaultValue(EventExcerptTranslationRepository::class)->end()
->end()
->end()
->arrayNode('location')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Location::class)->end()
->scalarNode('repository')->defaultValue(LocationRepository::class)->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
Expand Down
8 changes: 6 additions & 2 deletions src/DependencyInjection/SuluEventExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Manuxi\SuluEventBundle\Admin\EventAdmin;
use Manuxi\SuluEventBundle\Entity\Event;
use Manuxi\SuluEventBundle\Entity\Location;
use Sulu\Bundle\PersistenceBundle\DependencyInjection\PersistenceExtensionTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
Expand All @@ -15,7 +16,11 @@

class SuluEventExtension extends Extension implements PrependExtensionInterface
{
use PersistenceExtensionTrait;

/**
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
{

Expand All @@ -26,8 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('controller.xml');


// $this->configurePersistence($config['objects'], $container);
$this->configurePersistence($config['objects'], $container);
}

public function prepend(ContainerBuilder $container)
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Event implements AuditableTranslationInterface
*/
private $translations;

protected $locale = 'en';
private $locale = 'en';

private $ext = [];

Expand Down
46 changes: 46 additions & 0 deletions src/Entity/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Manuxi\SuluEventBundle\Repository\LocationRepository;
use Doctrine\ORM\Mapping as ORM;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;

/**
* @ORM\Entity
Expand Down Expand Up @@ -50,6 +51,11 @@ class Location
*/
private $city;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $state;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
Expand All @@ -60,6 +66,11 @@ class Location
*/
private $notes;

/**
* @ORM\ManyToOne(targetEntity="Sulu\Bundle\MediaBundle\Entity\MediaInterface")
*/
private $image;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -120,6 +131,17 @@ public function setCity(?string $city): self
return $this;
}

public function getState(): ?string
{
return $this->state;
}

public function setState(?string $state): self
{
$this->state = $state;
return $this;
}

public function getCountryCode(): ?string
{
return $this->countryCode;
Expand All @@ -142,4 +164,28 @@ public function setNotes($notes): self
return $this;
}

public function getImage(): ?MediaInterface
{
return $this->image;
}

/**
* @return array<string, mixed>|null
*/
public function getImageData(): ?array
{
if (!$this->image) {
return null;
}

return [
'id' => $this->image->getId(),
];
}

public function setImage(?MediaInterface $image): self
{
$this->image = $image;
return $this;
}
}
24 changes: 22 additions & 2 deletions src/Entity/Models/LocationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Manuxi\SuluEventBundle\Entity\Location;
use Manuxi\SuluEventBundle\Entity\Traits\ArrayPropertyTrait;
use Manuxi\SuluEventBundle\Repository\LocationRepository;
use Sulu\Bundle\MediaBundle\Entity\MediaRepositoryInterface;
use Sulu\Component\Rest\Exception\EntityNotFoundException;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -18,14 +19,18 @@ class LocationModel implements LocationModelInterface
use ArrayPropertyTrait;

private $locationRepository;
private $mediaRepository;

public function __construct(
LocationRepository $locationRepository
LocationRepository $locationRepository,
MediaRepositoryInterface $mediaRepository
) {
$this->locationRepository = $locationRepository;
$this->mediaRepository = $mediaRepository;
}

/**
* @throws EntityNotFoundException
* @throws ORMException
* @throws OptimisticLockException
*/
Expand All @@ -43,7 +48,7 @@ public function createLocation(Request $request): Location
*/
public function updateLocation(int $id, Request $request): Location
{
$location = $this->getLocation($id, $request);
$location = $this->getLocation($id);
$location = $this->mapDataToEntity($location, $request->request->all());
return $this->locationRepository->save($location);
}
Expand All @@ -69,6 +74,9 @@ public function deleteLocation(int $id): void
$this->locationRepository->remove($id);
}

/**
* @throws EntityNotFoundException
*/
private function mapDataToEntity(Location $location, array $data): Location
{
$name = $this->getProperty($data, 'name');
Expand All @@ -91,6 +99,10 @@ private function mapDataToEntity(Location $location, array $data): Location
if ($postalCode) {
$location->setPostalCode($postalCode);
}
$state = $this->getProperty($data, 'state');
if ($state) {
$location->setState($state);
}
$countryCode = $this->getProperty($data, 'countryCode');
if ($countryCode) {
$location->setCountryCode($countryCode);
Expand All @@ -99,6 +111,14 @@ private function mapDataToEntity(Location $location, array $data): Location
if ($notes) {
$location->setNotes($notes);
}
$imageId = $this->getPropertyMulti($data, ['image', 'id']);
if ($imageId) {
$image = $this->mediaRepository->findMediaById((int) $imageId);
if (!$image) {
throw new EntityNotFoundException($this->mediaRepository->getClassName(), $imageId);
}
$location->setImage($image);
}
return $location;
}
}
10 changes: 3 additions & 7 deletions src/Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- Controllers -->
<service id="Manuxi\SuluEventBundle\Controller\Admin\LocationController"
public="true">
<service id="Manuxi\SuluEventBundle\Controller\Admin\LocationController" public="true">
<argument type="service" id="sulu_event.model.location"/>
<argument type="service" id="sulu_event.doctrine_list_representation_factory"/>
<argument type="service" id="fos_rest.view_handler.default"/>
Expand All @@ -15,8 +13,7 @@
<tag name="sulu.context" context="admin"/>
</service>

<service id="Manuxi\SuluEventBundle\Controller\Admin\EventController"
public="true">
<service id="Manuxi\SuluEventBundle\Controller\Admin\EventController" public="true">
<argument type="service" id="sulu_event.model.event"/>
<argument type="service" id="sulu_event.model.event_seo"/>
<argument type="service" id="sulu_event.model.event_excerpt"/>
Expand All @@ -29,8 +26,7 @@
<tag name="sulu.context" context="admin"/>
</service>

<service id="Manuxi\SuluEventBundle\Controller\Website\EventController"
public="true">
<service id="Manuxi\SuluEventBundle\Controller\Website\EventController" public="true">
<argument type="service" id="request_stack"/>
<argument type="service" id="sulu_media.media_manager"/>
<argument type="service" id="sulu_event.repository.event"/>
Expand Down
Loading

0 comments on commit f369b98

Please sign in to comment.