/**
  * 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'));
 }
 /**
  * Returns an array containing all completeness info and missing attributes for a product
  *
  * @param ProductInterface                           $product
  * @param \Pim\Bundle\CatalogBundle\Entity\Channel[] $channels
  * @param \Pim\Bundle\CatalogBundle\Entity\Locale[]  $locales
  * @param string                                     $localeCode
  *
  * @return array
  */
 public function getProductCompleteness(ProductInterface $product, array $channels, array $locales, $localeCode)
 {
     $family = $product->getFamily();
     $getCodes = function ($entities) {
         return array_map(function ($entity) {
             return $entity->getCode();
         }, $entities);
     };
     $channelTemplate = array_fill_keys($getCodes($channels), array('completeness' => null, 'missing' => array()));
     $localeCodes = $getCodes($locales);
     $completenesses = array_fill_keys($localeCodes, $channelTemplate);
     if (!$family) {
         return $completenesses;
     }
     $allCompletenesses = $product->getCompletenesses();
     foreach ($allCompletenesses as $completeness) {
         $locale = $completeness->getLocale();
         $channel = $completeness->getChannel();
         $completenesses[$locale->getCode()][$channel->getCode()]['completeness'] = $completeness;
     }
     $requirements = $this->familyRepository->getFullRequirementsQB($family, $localeCode)->getQuery()->getResult();
     $productValues = $product->getValues();
     foreach ($requirements as $requirement) {
         if ($requirement->isRequired()) {
             $this->addRequirementToCompleteness($completenesses, $requirement, $productValues, $localeCodes);
         }
     }
     return $completenesses;
 }
 /**
  * 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);
             }
         }
         $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 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;
 }
 /**
  * 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));
 }
 /**
  * {@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;
 }
 /**
  * {@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()];
 }
 /**
  * Get choices
  *
  * @return array
  */
 public function getChoices()
 {
     return $this->repository->getChoices(['localeCode' => $this->userContext->getCurrentLocaleCode()]);
 }