/**
  * @param ResourceControllerEvent $event
  */
 public function onEvent(ResourceControllerEvent $event)
 {
     $document = $event->getSubject();
     $metadata = $this->documentManager->getClassMetadata(get_class($document));
     if ($metadata->idGenerator !== ClassMetadata::GENERATOR_TYPE_PARENT) {
         throw new \RuntimeException(sprintf('Document of class "%s" must be using the GENERATOR_TYPE_PARENT identificatio strategy (value %s), it is current using "%s" (this may be an automatic configuration: be sure to map both the `nodename` and the `parentDocument`).', get_class($document), ClassMetadata::GENERATOR_TYPE_PARENT, $metadata->idGenerator));
     }
     // NOTE: that the PHPCR-ODM requires these two fields to be set when
     //       when the GENERATOR_TYPE_PARENT "ID" strategy is used.
     $nameField = $metadata->nodename;
     $parentField = $metadata->parentMapping;
     $parentDocument = $metadata->getFieldValue($document, $parentField);
     $phpcrNode = $this->documentManager->getNodeForDocument($parentDocument);
     $parentPath = $phpcrNode->getPath();
     $baseCandidateName = $metadata->getFieldValue($document, $nameField);
     $candidateName = $baseCandidateName;
     $index = 1;
     while (true) {
         $candidatePath = sprintf('%s/%s', $parentPath, $candidateName);
         $existing = $this->documentManager->find(null, $candidatePath);
         // if the existing document is the document we are updating, then thats great.
         if ($existing === $document) {
             return;
         }
         if (null === $existing) {
             $metadata->setFieldValue($document, $nameField, $candidateName);
             return;
         }
         $candidateName = sprintf('%s-%d', $baseCandidateName, $index);
         $index++;
     }
 }
 public function onSave(ResourceControllerEvent $event)
 {
     $subject = $event->getSubject();
     if ($subject instanceof Routeable) {
         $this->autoGenerator->generate($subject);
     }
     $this->em->flush();
 }
Example #3
0
 function it_adds_message_from_event(SessionInterface $session, FlashBagInterface $flashBag, RequestConfiguration $requestConfiguration, ResourceControllerEvent $event)
 {
     $event->getMessage()->willReturn('sylius.channel.cannot_be_deleted');
     $event->getMessageType()->willReturn(ResourceControllerEvent::TYPE_WARNING);
     $event->getMessageParameters()->willReturn(['%name%' => 'Germany Sylius Webshop']);
     $session->getBag('flashes')->willReturn($flashBag);
     $flashBag->add(ResourceControllerEvent::TYPE_WARNING, ['message' => 'sylius.channel.cannot_be_deleted', 'parameters' => ['%name%' => 'Germany Sylius Webshop']])->shouldBeCalled();
     $this->addFlashFromEvent($requestConfiguration, $event);
 }
Example #4
0
 public function preSave(ResourceControllerEvent $event)
 {
     $resource = $event->getSubject();
     if ($resource instanceof Publishable) {
         if ($resource->isPublic() && $resource->getPublicationDate() === null) {
             $resource->setPublicationDate(new \DateTime());
         }
     }
 }
Example #5
0
 function it_adds_flash_from_event(SessionInterface $session, FlashBagInterface $flashBag, TranslatorInterface $translator, RequestConfiguration $requestConfiguration, ResourceControllerEvent $event)
 {
     $event->getMessage()->willReturn('sylius.channel.cannot_be_deleted');
     $event->getMessageType()->willReturn(ResourceControllerEvent::TYPE_WARNING);
     $event->getMessageParameters()->willReturn(array('%name%' => 'Germany Sylius Webshop'));
     $session->getBag('flashes')->willReturn($flashBag);
     $translator->trans('sylius.channel.cannot_be_deleted', array('%name%' => 'Germany Sylius Webshop'), 'flashes')->willReturn('Channel "Germany Sylius Webshop" cannot be deleted.');
     $flashBag->add(ResourceControllerEvent::TYPE_WARNING, 'Channel "Germany Sylius Webshop" cannot be deleted.')->shouldBeCalled();
     $this->addFlashFromEvent($requestConfiguration, $event);
 }
Example #6
0
 function it_should_use_the_given_replacement_char(ResourceControllerEvent $event, DocumentManagerInterface $documentManager, ClassMetadata $metadata)
 {
     $this->beConstructedWith($documentManager, '_');
     $document = new \stdClass();
     $event->getSubject()->willReturn($document);
     $documentManager->getClassMetadata('stdClass')->willReturn($metadata);
     $metadata->nodename = 'foobar';
     $metadata->getFieldValue($document, 'foobar')->willReturn('Hello//Foo');
     $metadata->setFieldValue($document, 'foobar', 'Hello__Foo')->shouldBeCalled();
     $this->onEvent($event);
 }
 /**
  * Prevent channel deletion if no more channels enabled.
  *
  * @param ResourceControllerEvent $event
  */
 public function onChannelPreDelete(ResourceControllerEvent $event)
 {
     $channel = $event->getSubject();
     if (!$channel instanceof ChannelInterface) {
         throw new UnexpectedTypeException($channel, ChannelInterface::class);
     }
     $results = $this->channelRepository->findBy(array('enabled' => true));
     if (!$results || count($results) === 1 && current($results) === $channel) {
         $event->stop('error.at_least_one');
     }
 }
Example #8
0
 public function pagePreDelete(ResourceControllerEvent $event)
 {
     /* @var $page PageInterface */
     $page = $event->getSubject();
     /*
      * Don't allow deletion of homepage / root
      */
     if ($page->getHomepage() || $page->getRoot()) {
         $event->stop('symedit.page.root_or_home');
     }
 }
 /**
  * Prevent channel deletion if no more channels enabled.
  *
  * @param ResourceControllerEvent $event
  */
 public function onChannelPreDelete(ResourceControllerEvent $event)
 {
     $channel = $event->getSubject();
     if (!$channel instanceof ChannelInterface) {
         throw new UnexpectedTypeException($channel, ChannelInterface::class);
     }
     $results = $this->channelRepository->findBy(['enabled' => true]);
     if (!$results || count($results) === 1 && current($results) === $channel) {
         $event->stop('sylius.ui.the_channel_cannot_be_deleted_at_least_one_enabled_channel_is_required');
     }
 }
 /**
  * If the article is published, and has no publish date,
  * set it the the current datetime.
  *
  * @param ResourceControllerEvent $event
  * @throws UnexpectedTypeException
  */
 public function setPublishedAt(ResourceControllerEvent $event)
 {
     /** @var ArticleInterface $subject */
     $subject = $event->getSubject();
     if (!$subject instanceof ArticleInterface) {
         throw new UnexpectedTypeException($subject, 'Webburza\\Sylius\\ArticleBundle\\Model\\ArticleInterface');
     }
     if ($subject->isPublished() && $subject->getPublishedAt() == null) {
         $subject->setPublishedAt(new DateTime());
     }
 }
Example #11
0
 /**
  * @param ResourceControllerEvent $event
  */
 public function onEvent(ResourceControllerEvent $event)
 {
     $document = $event->getSubject();
     $metadata = $this->documentManager->getClassMetadata(get_class($document));
     if (null === ($nameField = $metadata->nodename)) {
         throw new \RuntimeException(sprintf('In order to use the node name filter on "%s" it is necessary to map a field as the "nodename"', get_class($document)));
     }
     $name = $metadata->getFieldValue($document, $nameField);
     $name = preg_replace('/\\/|:|\\[|\\]|\\||\\*/', $this->replacementCharacter, $name);
     $metadata->setFieldValue($document, $nameField, $name);
 }
Example #12
0
 protected function addShare(ResourceControllerEvent $event)
 {
     $post = $event->getSubject();
     if ($post->isPublished()) {
         try {
             $url = $this->router->generate('blog_slug_view', ['slug' => $post->getSlug()], true);
             $this->session->getFlashBag()->add('share', $url);
         } catch (RouteNotFoundException $e) {
             // The route to view posts doesn't exist, so we can't really share it.
         }
     }
 }
 /**
  * Hook into the Wishlist's pre-remove event,
  * prevent removal if this is the only wishlist for the customer.
  *
  * @param ResourceControllerEvent $event
  */
 public function preRemove(ResourceControllerEvent $event)
 {
     /** @var WishlistInterface $wishlist */
     $wishlist = $event->getSubject();
     /** @var WishlistRepositoryInterface $wishlistRepository */
     $wishlistRepository = $this->container->get('webburza.repository.wishlist');
     // Get the number of wishlists for the customer
     $wishlistCount = $wishlistRepository->getCountForCustomer($wishlist->getCustomer());
     // If this is the only wishlist for the customer, stop the event
     if ($wishlistCount == 1) {
         $event->stop('webburza.sylius.wishlist.cannot_delete', ResourceControllerEvent::TYPE_ERROR, [], 400);
     }
 }
 /**
  * Hook into the customer create event,
  * and create a default wishlist for the customer.
  *
  * @param ResourceControllerEvent $event
  */
 public function postCreate(ResourceControllerEvent $event)
 {
     /** @var CustomerInterface $customer */
     $customer = $event->getSubject();
     /** @var WishlistInterface $wishlist */
     $wishlist = $this->container->get('webburza.factory.wishlist')->createNew();
     $wishlist->setTitle($this->translator->trans('webburza.sylius.wishlist.default_title'));
     $wishlist->setCustomer($customer);
     $wishlist->setPublic($this->container->getParameter('webburza.sylius.wishlist_bundle.default_public'));
     // Dispatch pre-create event
     $this->eventDispatcher->dispatch('webburza.wishlist.pre_create', new ResourceControllerEvent($wishlist));
     /** @var EntityManagerInterface $entityManager */
     $entityManager = $this->container->get('webburza.manager.wishlist');
     $entityManager->persist($wishlist);
     $entityManager->flush();
     // Dispatch post-create event
     $this->eventDispatcher->dispatch('webburza.wishlist.post_create', new ResourceControllerEvent($wishlist));
 }
 function it_should_auto_increment_the_name_if_a_conflict_exists(DocumentManagerInterface $documentManager, ResourceControllerEvent $event, ClassMetadata $metadata, NodeInterface $node)
 {
     $document = new \stdClass();
     $parentDocument = new \stdClass();
     $existingDocument = new \stdClass();
     $event->getSubject()->willReturn($document);
     $documentManager->getClassMetadata('stdClass')->willReturn($metadata);
     $metadata->idGenerator = ClassMetadata::GENERATOR_TYPE_PARENT;
     $metadata->nodename = 'title';
     $metadata->parentMapping = 'parent';
     $metadata->getFieldValue($document, 'parent')->willReturn($parentDocument);
     $documentManager->getNodeForDocument($parentDocument)->willReturn($node);
     $node->getPath()->willReturn('/path/to');
     $metadata->getFieldValue($document, 'title')->willReturn('Hello World');
     $documentManager->find(null, '/path/to/Hello World')->willReturn($existingDocument);
     $documentManager->find(null, '/path/to/Hello World-1')->willReturn($existingDocument);
     $documentManager->find(null, '/path/to/Hello World-2')->willReturn($existingDocument);
     $documentManager->find(null, '/path/to/Hello World-3')->willReturn(null);
     $metadata->setFieldValue($document, 'title', 'Hello World-3')->shouldBeCalled();
     $this->onEvent($event);
 }
Example #16
0
 public function track(ResourceControllerEvent $event)
 {
     $this->tracker->track($event->getSubject());
 }
 function it_should_return_early_if_force_is_false_and_subject_already_has_a_parent(ResourceControllerEvent $event, ClassMetadata $documentMetadata, DocumentManagerInterface $documentManager)
 {
     $subjectDocument = new \stdClass();
     $event->getSubject()->willReturn($subjectDocument);
     $documentManager->getClassMetadata(\stdClass::class)->willReturn($documentMetadata);
     $documentMetadata->parentMapping = 'parent';
     $documentMetadata->getFieldValue($subjectDocument, 'parent')->willReturn(new \stdClass());
     $documentManager->find(null, '/path/to')->shouldNotBeCalled();
     $this->onPreCreate($event);
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function addFlashFromEvent(RequestConfiguration $requestConfiguration, ResourceControllerEvent $event)
 {
     $translatedMessage = $this->translator->trans($event->getMessage(), $event->getMessageParameters(), 'flashes');
     $this->session->getBag('flashes')->add($event->getMessageType(), $translatedMessage);
 }
Example #19
0
 public function update(ResourceControllerEvent $event)
 {
     if ($event->getSubject() instanceof OrderInterface) {
         $this->trackingProcessor->process($event->getSubject());
     }
 }
Example #20
0
 /**
  * {@inheritdoc}
  */
 public function addFlashFromEvent(RequestConfiguration $requestConfiguration, ResourceControllerEvent $event)
 {
     $this->addFlash($event->getMessageType(), $event->getMessage(), $event->getMessageParameters());
 }
 public function updateUser(ResourceControllerEvent $event)
 {
     $user = $event->getSubject();
     $this->userManager->updateUser($user);
 }
Example #22
0
 /**
  * @param ResourceControllerEvent $event
  */
 public function onPreCreate(ResourceControllerEvent $event)
 {
     $document = $event->getSubject();
     $class = get_class($document);
     $this->resolveParent($document, $this->documentManager->getClassMetadata($class));
 }