/** * Get a single family * * @param int $identifier * * @return JsonResponse */ public function getAction($identifier) { $family = $this->familyRepository->findOneByIdentifier($identifier); if (null === $family) { throw new NotFoundHttpException(sprintf('Family with code "%s" not found', $identifier)); } return new JsonResponse($this->normalizer->normalize($family, 'json')); }
/** * Get families with given $filters. * In this particular case, we'll only have 1 filter based on ids * (We don't have raw filters yet for family grid) * * @param array $filters * * @return \Doctrine\Common\Collections\ArrayCollection */ protected function getFamilies(array $filters) { $resolver = new OptionsResolver(); $resolver->setRequired(['field', 'operator', 'value']); $filter = current($filters); $filter = $resolver->resolve($filter); $familiesIds = $filter['value']; return new ArrayCollection($this->familyRepository->findByIds($familiesIds)); }
/** * Normalizes families for the select2 * * @param FamilyRepositoryInterface $familyRepository * @param string $familyCodes * @return array */ protected function normalizeFamily(FamilyRepositoryInterface $familyRepository, $familyCodes) { $familyCodes = explode(',', $familyCodes); $result = []; $families = $familyRepository->findBy(['code' => $familyCodes]); foreach ($families as $family) { $familyLabel = $family->getLabel(); $result[] = ['id' => $family->getCode(), 'text' => $familyLabel]; } return $result; }
function it_builds_the_view(FormView $formView, FormInterface $form, FamilyRepositoryInterface $familyRepository, FamilyInterface $familyMugs, FamilyInterface $familyWebcams) { $options['repository'] = $familyRepository; $options['multiple'] = true; $form->getData()->willReturn('mugs,webcams'); $familyRepository->findBy(["code" => ["mugs", "webcams"]])->willReturn([$familyMugs, $familyWebcams]); $familyMugs->getLabel()->willReturn('Mugs'); $familyWebcams->getLabel()->willReturn('Webcams'); $familyMugs->getCode()->willReturn('mugs'); $familyWebcams->getCode()->willReturn('webcams'); $this->buildView($formView, $form, $options); }
/** * Returns completenesses filled * * @param array $completenesses * @param ProductInterface $product * @param array $locales * @param string $localeCode * * @return array */ protected function fillCompletenessesTemplate(array $completenesses, ProductInterface $product, array $locales, $localeCode) { $allCompletenesses = $product->getCompletenesses(); foreach ($allCompletenesses as $completeness) { $locale = $completeness->getLocale(); $channel = $completeness->getChannel(); $compLocaleCode = $locale->getCode(); if (isset($completenesses[$compLocaleCode])) { $completenesses[$compLocaleCode]['channels'][$channel->getCode()]['completeness'] = $completeness; $completenesses[$compLocaleCode]['locale'] = $compLocaleCode; $completenesses[$compLocaleCode]['stats']['total']++; if (0 === $completeness->getMissingCount()) { $completenesses[$compLocaleCode]['stats']['complete']++; } } } $requirements = $this->familyRepository->getFullRequirementsQB($product->getFamily(), $localeCode)->getQuery()->getResult(); $productValues = $product->getValues(); foreach ($requirements as $requirement) { if ($requirement->isRequired()) { $this->addRequirementToCompleteness($completenesses, $requirement, $productValues, $locales); } } return $completenesses; }
/** * {@inheritdoc} */ public function hasAttributeInVariantGroup($productId, $attributeCode) { $product = $this->getProductAsArray($productId); if (!isset($product['groupIds'])) { return false; } return $this->groupRepository->hasAttribute($product['groupIds'], $attributeCode); }
/** * {@inheritdoc} */ public function createProduct($identifier = null, $familyCode = null) { $product = new $this->productClass(); $identifierAttribute = $this->attributeRepository->getIdentifier(); $productValue = $this->createProductValue($identifierAttribute); $product->addValue($productValue); if (null !== $identifier) { $productValue->setData($identifier); } if (null !== $familyCode) { $family = $this->familyRepository->findOneByIdentifier($familyCode); $product->setFamily($family); } $event = new GenericEvent($product); $this->eventDispatcher->dispatch(ProductEvents::CREATE, $event); return $product; }
/** * {@inheritdoc} */ public function findCommonAttributeIds(array $productIds) { $results = $this->findValuesCommonAttributeIds($productIds); $familyIds = $this->findFamiliesFromProductIds($productIds); if (!empty($familyIds)) { $families = $this->familyRepository->findAttributeIdsFromFamilies($familyIds); } $attIds = null; foreach ($results as $result) { $familyAttr = isset($result['_id']['family']) ? $families[$result['_id']['family']] : []; $prodAttIds = array_unique(array_merge($result['attribute'], $familyAttr)); if (null === $attIds) { $attIds = $prodAttIds; } else { $attIds = array_intersect($attIds, $prodAttIds); } } return $attIds; }
/** * Generate family requirements information to be used to * calculate completenesses. * * @param ProductInterface $product * @param ChannelInterface $channel * * @return array */ protected function getFamilyRequirements(ProductInterface $product = null, ChannelInterface $channel = null) { $selectFamily = null; if (null !== $product) { $selectFamily = $product->getFamily(); } $families = $this->familyRepository->getFullFamilies($selectFamily, $channel); $familyRequirements = []; foreach ($families as $family) { $reqsByChannels = []; $channels = []; foreach ($family->getAttributeRequirements() as $attributeReq) { $channel = $attributeReq->getChannel(); $channels[$channel->getCode()] = $channel; if (!isset($reqsByChannels[$channel->getCode()])) { $reqsByChannels[$channel->getCode()] = []; } $reqsByChannels[$channel->getCode()][] = $attributeReq; } $familyRequirements[$family->getId()] = $this->getFieldsNames($channels, $reqsByChannels); } return $familyRequirements; }
/** * Remove an attribute * * @param int $familyId * @param int $attributeId * * @AclAncestor("pim_enrich_family_edit_attributes") * * @throws DeleteException * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function removeAttributeAction($familyId, $attributeId) { $family = $this->familyRepository->find($familyId); if (null === $family) { throw new NotFoundHttpException(sprintf('%s entity not found', $this->familyClass)); } $attribute = $this->attributeRepo->find($attributeId); if (null === $attribute) { throw new NotFoundHttpException(sprintf('%s entity not found', $this->attributeClass)); } if (false === $family->hasAttribute($attribute)) { throw new DeleteException($this->translator->trans('flash.family.attribute not found')); } elseif (AttributeTypes::IDENTIFIER === $attribute->getAttributeType()) { throw new DeleteException($this->translator->trans('flash.family.identifier not removable')); } elseif ($attribute === $family->getAttributeAsLabel()) { throw new DeleteException($this->translator->trans('flash.family.label attribute not removable')); } else { $family->removeAttribute($attribute); foreach ($family->getAttributeRequirements() as $requirement) { if ($requirement->getAttribute() === $attribute) { $family->removeAttributeRequirement($requirement); $this->doctrine->getManagerForClass(ClassUtils::getClass($requirement))->remove($requirement); } } $errors = $this->validator->validate($family); if (count($errors) > 0) { throw new DeleteException($errors[0]->getMessage()); } $this->familySaver->save($family); } if ($this->request->isXmlHttpRequest()) { return new Response('', 204); } else { return new RedirectResponse($this->router->generate('pim_enrich_family_edit', ['id' => $family->getId()])); } }
/** * {@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()]; }