/**
  * 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 ProductInterface $product
  *
  * @return array
  */
 protected function getLabels(ProductInterface $product)
 {
     $labels = [];
     foreach ($this->localeRepository->getActivatedLocaleCodes() as $localeCode) {
         $labels[$localeCode] = $product->getLabel($localeCode);
     }
     return ['label' => $labels];
 }
 function it_provides_default_values($decoratedProvider, ChannelRepositoryInterface $channelRepository, LocaleRepositoryInterface $localeRepository, LocaleInterface $locale, ChannelInterface $channel)
 {
     $channel->getCode()->willReturn('channel_code');
     $channelRepository->getFullChannels()->willReturn([$channel]);
     $locale->getCode()->willReturn('locale_code');
     $localeRepository->getActivatedLocaleCodes()->willReturn([$locale]);
     $decoratedProvider->getDefaultValues()->willReturn(['decoratedParam' => true]);
     $this->getDefaultValues()->shouldReturnWellFormedDefaultValues();
 }
 /**
  * {@inheritdoc}
  */
 public function getDefaultValues()
 {
     $parameters = $this->simpleProvider->getDefaultValues();
     $parameters['decimalSeparator'] = LocalizerInterface::DEFAULT_DECIMAL_SEPARATOR;
     $parameters['dateFormat'] = LocalizerInterface::DEFAULT_DATE_FORMAT;
     $parameters['with_media'] = true;
     $parameters['filePath'] = sys_get_temp_dir() . 'csv_products_export.csv';
     $defaultChannel = $this->channelRepository->getFullChannels()[0];
     $defaultLocaleCode = $this->localeRepository->getActivatedLocaleCodes()[0];
     $parameters['filters'] = ['data' => [['field' => 'enabled', 'operator' => OPERATORS::EQUALS, 'value' => true], ['field' => 'completeness', 'operator' => OPERATORS::GREATER_OR_EQUAL_THAN, 'value' => 100], ['field' => 'categories.code', 'operator' => OPERATORS::IN_CHILDREN_LIST, 'value' => []]], 'structure' => ['scope' => $defaultChannel->getCode(), 'locales' => [$defaultLocaleCode]]];
     return $parameters;
 }
 /**
  * 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;
 }
 /**
  * Get cached active locales
  *
  * @return array
  */
 protected function getActiveLocales()
 {
     if (!$this->activeLocales) {
         $this->activeLocales = $this->localeRepository->getActivatedLocales();
     }
     return $this->activeLocales;
 }
 /**
  * @param string $code
  *
  * @return LocaleInterface
  */
 protected function getLocale($code)
 {
     if (!array_key_exists($code, $this->locales)) {
         $this->locales[$code] = $this->localeRepository->findOneByIdentifier($code);
     }
     return $this->locales[$code];
 }
 /**
  * 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;
 }
 /**
  * Edit attribute form
  *
  * @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)) {
         $request->getSession()->getFlashBag()->add('success', new Message('flash.attribute.updated'));
     }
     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)];
 }
 /**
  * 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']));
         }
     }
 }
 /**
  * @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);
         }
     }
 }
 /**
  * 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;
 }
 /**
  * @param AttributeInterface $attribute
  * @param string             $field
  * @param array              $availableLocaleCodes
  */
 protected function setAvailableLocales(AttributeInterface $attribute, $field, array $availableLocaleCodes)
 {
     $locales = [];
     foreach ($availableLocaleCodes as $localeCode) {
         $locale = $this->localeRepository->findOneByIdentifier($localeCode);
         if (null !== $locale) {
             $locales[] = $locale;
         }
     }
     $this->accessor->setValue($attribute, $field, $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'];
     $this->validator->validateFields($item, $requiredFields, false);
     $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)));
         }
     }
 }
 /**
  * @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;
         $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);
     }
 }
 /**
  * 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');
 }
 /**
  * @return array
  */
 protected function getActivatedLocaleCodes()
 {
     return $this->localeRepository->getActivatedLocaleCodes();
 }
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $locales = $this->localeRepository->getActivatedLocaleCodes();
     $resolver->setDefaults(['choices' => array_combine($locales, $locales), 'required' => true, 'select2' => true, 'multiple' => true, 'label' => 'pim_connector.export.locales.label', 'help' => 'pim_connector.export.locales.help', 'attr' => ['data-tab' => 'content']]);
 }
 /**
  * Returns the default application locale
  *
  * @return LocaleInterface|null
  */
 protected function getDefaultLocale()
 {
     return $this->localeRepository->findOneByIdentifier($this->defaultLocale);
 }
 /**
  * {@inheritdoc}
  */
 public function collect()
 {
     return ['nb_channels' => $this->channelRepository->countAll(), 'nb_locales' => $this->localeRepository->countAllActivated(), 'nb_products' => $this->productRepository->countAll(), 'nb_attributes' => $this->attributeRepository->countAll(), 'nb_families' => $this->familyRepository->countAll(), 'nb_users' => $this->userRepository->countAll()];
 }