Skip to content

Commit

Permalink
Event settings: Authored changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
manuxi committed Mar 29, 2021
1 parent 23d9d62 commit 865c568
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Controller/Admin/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sulu\Bundle\MediaBundle\Entity\MediaRepositoryInterface;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Bundle\SecurityBundle\Entity\UserRepository;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -42,6 +43,7 @@ class EventController extends AbstractRestController implements ClassResourceInt
private $entityManager;
private $routeManager;
private $routeRepository;
private $userRepository;

public function __construct(
EventRepository $eventRepository,
Expand All @@ -53,6 +55,7 @@ public function __construct(
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
EntityManagerInterface $entityManager,
ViewHandlerInterface $viewHandler,
UserRepository $userRepository,
?TokenStorageInterface $tokenStorage = null
) {
parent::__construct($viewHandler, $tokenStorage);
Expand All @@ -64,6 +67,7 @@ public function __construct(
$this->locationRepository = $locationRepository;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->userRepository = $userRepository;
}

public function cgetAction(Request $request): Response
Expand Down Expand Up @@ -208,6 +212,18 @@ protected function mapDataToEntity(array $data, Event $entity): void
$this->locationRepository->findById((int) $locationId)
);
}

//settings (author, authored) changeable
$authorId = $data['author'] ?? null;
$author = $this->userRepository->findUserById($authorId);
if ($author) {
$entity->setAuthor($author);
}

$authored = $data['authored'] ?? null;
if ($authored) {
$entity->setAuthored(new \DateTime($authored));
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Interfaces/AuthorTranslationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Manuxi\SuluEventBundle\Entity\Interfaces;

use Sulu\Component\Security\Authentication\UserInterface;

interface AuthorTranslationInterface
{
public function getAuthor(): ?int;
public function setAuthor(UserInterface $author);
}
1 change: 1 addition & 0 deletions src/Entity/Interfaces/AuthoredTranslationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
interface AuthoredTranslationInterface
{
public function getAuthored(): ?\DateTime;
public function setAuthored(\DateTime $authored);
}
12 changes: 12 additions & 0 deletions src/Entity/Traits/AuthorTranslationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Manuxi\SuluEventBundle\Entity\Traits;

use JMS\Serializer\Annotation as Serializer;
use Sulu\Component\Security\Authentication\UserInterface;

trait AuthorTranslationTrait
{
Expand All @@ -21,4 +22,15 @@ public function getAuthor(): ?int

return $translation->getAuthor() ? $translation->getAuthor()->getId() : null;
}

public function setAuthor(UserInterface $author): self
{
$translation = $this->getTranslation($this->locale);
if (!$translation) {
$translation = $this->createTranslation($this->locale);
}

$translation->setAuthor($author);
return $this;
}
}
11 changes: 11 additions & 0 deletions src/Entity/Traits/AuthoredTranslationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function getAuthored(): ?\DateTime

return $translation->getAuthored();
}

public function setAuthored(\DateTime $authored): self
{
$translation = $this->getTranslation($this->locale);
if (!$translation) {
$translation = $this->createTranslation($this->locale);
}

$translation->setAuthored($authored);
return $this;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<argument type="service" id="sulu_event.doctrine_list_representation_factory"/>
<argument type="service" id="doctrine.orm.default_entity_manager"/>
<argument type="service" id="fos_rest.view_handler.default"/>
<argument type="service" id="sulu_security.user_repository"/>
<argument type="service" id="security.token_storage"/>

<tag name="sulu.context" context="admin"/>
Expand Down

0 comments on commit 865c568

Please sign in to comment.