/** * @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; }
/** * 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; }
/** * 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 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)]; }
/** * 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 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 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)); } } } }
/** * @return array */ protected function getActivatedLocaleCodes() { return $this->localeRepository->getActivatedLocaleCodes(); }