/**
  * Get the list of all locales
  *
  * @return JsonResponse all activated locales
  */
 public function indexAction()
 {
     $locales = $this->localeRepository->getActivatedLocales();
     $filteredLocales = $this->collectionFilter->filterCollection($locales, 'pim.internal_api.locale.view');
     $normalizedLocales = $this->normalizer->normalize($filteredLocales, 'internal_api');
     return new JsonResponse($normalizedLocales);
 }
 /**
  * @param GenericEvent $event
  */
 public function updateChannel(GenericEvent $event)
 {
     $channel = $event->getSubject();
     if (!$channel instanceof ChannelInterface) {
         return;
     }
     $oldLocales = $this->repository->getDeletedLocalesForChannel($channel);
     $newLocales = $channel->getLocales();
     $updatedLocales = [];
     foreach ($oldLocales as $locale) {
         $locale->removeChannel($channel);
         $updatedLocales[] = $locale;
         if (null !== $this->completeness) {
             $this->completeness->scheduleForChannelAndLocale($channel, $locale);
         }
     }
     foreach ($newLocales as $locale) {
         if (!$locale->hasChannel($channel)) {
             $locale->addChannel($channel);
             $updatedLocales[] = $locale;
         }
     }
     if (!empty($updatedLocales)) {
         $this->saver->saveAll($updatedLocales);
     }
 }
 /**
  * @param ProductInterface $product
  *
  * @return array
  */
 protected function getLabels(ProductInterface $product)
 {
     $labels = [];
     foreach ($this->localeRepository->getActivatedLocaleCodes() as $localeCode) {
         $labels[$localeCode] = $product->getLabel($localeCode);
     }
     return ['label' => $labels];
 }
 /**
  * Return a choice list of activated locales.
  *
  * @return array
  */
 protected function getActivatedLocaleChoices()
 {
     $activatedLocaleCodes = $this->localeRepository->getActivatedLocaleCodes();
     $choices = [];
     foreach ($activatedLocaleCodes as $codes) {
         $choices[$codes] = $codes;
     }
     return $choices;
 }
Exemplo n.º 5
0
 /**
  * Create locales field
  *
  * @param FormBuilderInterface $builder
  *
  * @return ChannelType
  */
 protected function addLocalesField(FormBuilderInterface $builder)
 {
     $builder->add('locales', 'entity', ['required' => true, 'multiple' => true, 'select2' => true, 'by_reference' => false, 'class' => 'Pim\\Bundle\\CatalogBundle\\Entity\\Locale', 'query_builder' => function (LocaleRepositoryInterface $repository) {
         return $repository->getLocalesQB();
     }, 'preferred_choices' => $this->localeRepository->getActivatedLocaleCodes()]);
     return $this;
 }
 /**
  * Prepare attributes list for CSV headers
  *
  * @param array $attributesList
  *
  * @return array
  */
 protected function prepareAttributesList(array $attributesList)
 {
     $scopeCode = $this->catalogContext->getScopeCode();
     $localeCodes = $this->localeRepository->getActivatedLocaleCodes();
     $fieldsList = [];
     foreach ($attributesList as $attribute) {
         $attCode = $attribute->getCode();
         if ($attribute->isLocalizable() && $attribute->isScopable()) {
             foreach ($localeCodes as $localeCode) {
                 $fieldsList[] = sprintf('%s-%s-%s', $attCode, $localeCode, $scopeCode);
             }
         } elseif ($attribute->isLocalizable()) {
             foreach ($localeCodes as $localeCode) {
                 $fieldsList[] = sprintf('%s-%s', $attCode, $localeCode);
             }
         } elseif ($attribute->isScopable()) {
             $fieldsList[] = sprintf('%s-%s', $attCode, $scopeCode);
         } elseif (AttributeTypes::IDENTIFIER === $attribute->getAttributeType()) {
             array_unshift($fieldsList, $attCode);
         } elseif (AttributeTypes::PRICE_COLLECTION === $attribute->getAttributeType()) {
             foreach ($this->currencyManager->getActiveCodes() as $currencyCode) {
                 $fieldsList[] = sprintf('%s-%s', $attCode, $currencyCode);
             }
         } else {
             $fieldsList[] = $attCode;
         }
     }
     return $fieldsList;
 }
 /**
  * @return LocaleInterface[]
  */
 protected function getLocales()
 {
     if (null === $this->locales) {
         $this->locales = $this->localeRepository->getActivatedLocales();
     }
     return $this->locales;
 }
 /**
  * Edit attribute form
  *
  * @param Request $request
  * @param int     $id
  *
  * @Template("PimEnrichBundle:Attribute:form.html.twig")
  * @AclAncestor("pim_enrich_attribute_edit")
  *
  * @return array
  */
 public function editAction(Request $request, $id)
 {
     $attribute = $this->findAttributeOr404($id);
     if ($this->attributeHandler->process($attribute)) {
         $this->addFlash('success', 'flash.attribute.updated');
         return $this->redirectToRoute('pim_enrich_attribute_edit', ['id' => $attribute->getId()]);
     }
     return ['form' => $this->attributeForm->createView(), 'locales' => $this->localeRepository->getActivatedLocaleCodes(), 'disabledLocales' => $this->localeRepository->findBy(['activated' => false]), 'measures' => $this->measuresConfig, 'created' => $this->versionManager->getOldestLogEntry($attribute), 'updated' => $this->versionManager->getNewestLogEntry($attribute)];
 }
 /**
  * Check the consistency of the field with channel associated
  *
  * @param AttributeInterface $attribute
  * @param string             $fieldName
  * @param array              $attributeInfo
  *
  * @throws \InvalidArgumentException
  */
 protected function checkFieldNameLocaleByChannel(AttributeInterface $attribute, $fieldName, array $attributeInfo)
 {
     if ($attribute->isScopable() && $attribute->isLocalizable() && isset($attributeInfo['scope_code']) && isset($attributeInfo['locale_code'])) {
         $channel = $this->channelRepository->findOneByIdentifier($attributeInfo['scope_code']);
         $locale = $this->localeRepository->findOneByIdentifier($attributeInfo['locale_code']);
         if ($channel !== null && $locale !== null && !$channel->hasLocale($locale)) {
             throw new \InvalidArgumentException(sprintf('The locale "%s" of the field "%s" is not available in scope "%s"', $attributeInfo['locale_code'], $fieldName, $attributeInfo['scope_code']));
         }
     }
 }
Exemplo n.º 10
0
 /**
  * Get activated locales as choices
  *
  * @return string[]
  */
 public function getActivatedLocaleChoices()
 {
     $translateIn = $this->getCurrentLocaleCode();
     $activeCodes = $this->localeRepository->getActivatedLocaleCodes();
     $results = [];
     foreach ($activeCodes as $activeCode) {
         $results[$activeCode] = $this->getLocaleLabel($activeCode, $translateIn);
     }
     return $results;
 }
 /**
  * Schedule recalculation of completenesses for all products
  * of a channel
  *
  * @param ChannelInterface $channel
  */
 public function scheduleForChannel(ChannelInterface $channel)
 {
     if ($channel->getId()) {
         $deletedLocaleIds = $this->channelRepository->getDeletedLocaleIdsForChannel($channel);
         foreach ($deletedLocaleIds as $deletedLocaleId) {
             $deletedLocale = $this->localeRepository->find($deletedLocaleId);
             $this->generator->scheduleForChannelAndLocale($channel, $deletedLocale);
         }
     }
 }
 /**
  * @param AttributeInterface $attribute
  * @param array              $data
  */
 protected function setAvailableLocales(AttributeInterface $attribute, array $data)
 {
     $localeSpecificCodes = $attribute->getLocaleSpecificCodes();
     foreach ($data as $localeCode) {
         if (!in_array($localeCode, $localeSpecificCodes)) {
             $locale = $this->localeRepository->findOneByIdentifier($localeCode);
             $attribute->addAvailableLocale($locale);
         }
     }
 }
Exemplo n.º 13
0
 /**
  * Get active locales
  *
  * @return array
  */
 protected function getLocales()
 {
     if (null === $this->locales) {
         $this->locales = [];
         $locales = $this->localeRepository->findBy(['activated' => 1]);
         foreach ($locales as $locale) {
             $this->locales[$locale->getCode()] = $locale;
         }
     }
     return $this->locales;
 }
 /**
  * @param array $item
  * @param array $authorizedFields
  *
  * @throws ArrayConversionException
  */
 protected function validateAuthorizedFields(array $item, array $authorizedFields)
 {
     $localeCodes = $this->localeRepository->getActivatedLocaleCodes();
     foreach ($localeCodes as $code) {
         $authorizedFields[] = 'label-' . $code;
     }
     foreach ($item as $field => $data) {
         if (!in_array($field, $authorizedFields) && !$this->isAttribute($field)) {
             throw new ArrayConversionException(sprintf('Field "%s" is provided, authorized fields are: "%s"', $field, implode(', ', $authorizedFields)));
         }
     }
 }
 /**
  * @param string $code
  * @param bool   $activeOnly
  *
  * @throws ObjectNotFoundException
  *
  * @return LocaleInterface
  */
 protected function getLocale($code, $activeOnly = true)
 {
     if (!array_key_exists($code, $this->locales)) {
         $locale = $this->localeRepository->findOneByIdentifier($code);
         if (!$locale) {
             throw new ObjectNotFoundException(sprintf('Locale with code "%s" was not found.', $code));
         }
         if ($activeOnly && !$locale->isActivated()) {
             throw new ObjectNotFoundException(sprintf('Active locale with code "%s" was not found.', $code));
         }
         $this->locales[$code] = $locale;
     }
     return $this->locales[$code];
 }
 /**
  * @param array $item
  *
  * @throws ArrayConversionException
  */
 protected function validate(array $item)
 {
     $requiredFields = ['attribute', 'code'];
     foreach ($requiredFields as $requiredField) {
         if (!in_array($requiredField, array_keys($item))) {
             throw new ArrayConversionException(sprintf('Field "%s" is expected, provided fields are "%s"', $requiredField, implode(', ', array_keys($item))));
         }
     }
     $authorizedFields = array_merge($requiredFields, ['sort_order']);
     $localeCodes = $this->localeRepository->getActivatedLocaleCodes();
     foreach ($localeCodes as $code) {
         $authorizedFields[] = 'label-' . $code;
     }
     foreach ($item as $field => $data) {
         if (!in_array($field, $authorizedFields)) {
             throw new ArrayConversionException(sprintf('Field "%s" is provided, authorized fields are: "%s"', $field, implode(', ', $authorizedFields)));
         }
     }
 }
 /**
  * Checks that attributes in the header have existing locale, scope and currency.
  *
  * @throws \LogicException
  */
 protected function checkAttributesInHeader()
 {
     $channels = $this->channelRepository->getChannelCodes();
     $locales = $this->localeRepository->getActivatedLocaleCodes();
     $currencies = $this->currencyRepository->getActivatedCurrencyCodes();
     foreach ($this->fieldNames as $fieldName) {
         if (null !== ($info = $this->fieldExtractor->extractColumnInfo($fieldName))) {
             $locale = $info['locale_code'];
             $channel = $info['scope_code'];
             $currency = isset($info['price_currency']) ? $info['price_currency'] : null;
             if (null !== $locale && !in_array($locale, $locales)) {
                 throw new \LogicException(sprintf('Locale %s does not exist.', $locale));
             }
             if (null !== $channel && !in_array($channel, $channels)) {
                 throw new \LogicException(sprintf('Channel %s does not exist.', $channel));
             }
             if (null !== $currency && !in_array($currency, $currencies)) {
                 throw new \LogicException(sprintf('Currency %s does not exist.', $currency));
             }
         }
     }
 }
 function let(TokenStorageInterface $tokenStorage, LocaleRepositoryInterface $localeRepository, ChannelRepositoryInterface $channelRepository, TokenInterface $token, User $user, LocaleInterface $en, LocaleInterface $fr, LocaleInterface $de, ChannelInterface $ecommerce, ChannelInterface $mobile, CategoryInterface $firstTree, CategoryInterface $secondTree, CategoryRepositoryInterface $productCategoryRepo, RequestStack $requestStack, ChoicesBuilderInterface $choicesBuilder)
 {
     $tokenStorage->getToken()->willReturn($token);
     $token->getUser()->willReturn($user);
     $en->getCode()->willReturn('en_US');
     $fr->getCode()->willReturn('fr_FR');
     $de->getCode()->willReturn('de_DE');
     $en->isActivated()->willReturn(true);
     $fr->isActivated()->willReturn(true);
     $de->isActivated()->willReturn(true);
     $localeRepository->findOneByIdentifier('en_US')->willReturn($en);
     $localeRepository->findOneByIdentifier('fr_FR')->willReturn($fr);
     $localeRepository->findOneByIdentifier('de_DE')->willReturn($de);
     $localeRepository->getActivatedLocales()->willReturn([$en, $fr, $de]);
     $channelRepository->findOneBy([])->willReturn($mobile);
     $productCategoryRepo->getTrees()->willReturn([$firstTree, $secondTree]);
     $this->beConstructedWith($tokenStorage, $localeRepository, $channelRepository, $productCategoryRepo, $requestStack, $choicesBuilder, 'en_US');
 }
Exemplo n.º 19
0
 /**
  * Get locale by code
  *
  * @param string $code
  *
  * @return LocaleInterface
  */
 public function getLocaleByCode($code)
 {
     return $this->repository->findOneByIdentifier($code);
 }
 /**
  * @return array
  */
 protected function getActivatedLocaleCodes()
 {
     return $this->localeRepository->getActivatedLocaleCodes();
 }
 /**
  * {@inheritdoc}
  */
 public function collect()
 {
     return ['nb_channels' => $this->channelRepository->countAll(), 'nb_products' => $this->productRepository->countAll(), 'nb_attributes' => $this->attributeRepository->countAll(), 'nb_locales' => $this->localeRepository->countAllActivated(), 'nb_families' => $this->familyRepository->countAll(), 'nb_users' => $this->userRepository->countAll()];
 }
Exemplo n.º 22
0
 /**
  * Get locale by code
  *
  * @param string $code
  *
  * @return \Pim\Bundle\CatalogBundle\Entity\Locale
  */
 public function getLocaleByCode($code)
 {
     return $this->repository->findOneBy(array('code' => $code));
 }
Exemplo n.º 23
0
 /**
  * Returns the default application locale
  *
  * @return LocaleInterface|null
  */
 protected function getDefaultLocale()
 {
     return $this->localeRepository->findOneByIdentifier($this->defaultLocale);
 }