/**
  * Remove associated products from a list of product ids
  *
  * @param GenericEvent $event
  */
 public function removeAssociatedProducts(GenericEvent $event)
 {
     $productIds = $event->getSubject();
     $assocTypeCount = $this->assocTypeRepository->countAll();
     foreach ($productIds as $productId) {
         $this->productRepository->removeAssociatedProduct($productId, $assocTypeCount);
     }
 }
 /**
  * @return AssociationTypeInterface
  */
 protected function getAssociationType()
 {
     $params = $this->extractor->getDatagridParameter(RequestParameters::ADDITIONAL_PARAMETERS, []);
     $associationTypeId = isset($params['associationType']) ? $params['associationType'] : null;
     if (!$associationTypeId) {
         $associationTypeId = $this->extractor->getDatagridParameter('associationType');
     }
     if (!$associationTypeId) {
         throw new \LogicException('The current association type must be configured');
     }
     $associationType = $this->assocTypeRepository->findOneBy(['id' => $associationTypeId]);
     return $associationType;
 }
 /**
  * Get the association field names
  *
  * @return array
  */
 public function resolveAssociationColumns()
 {
     if (null === $this->assocFieldsCache) {
         $fieldNames = [];
         $assocTypes = $this->assocTypeRepository->findAll();
         foreach ($assocTypes as $assocType) {
             $fieldNames[] = $assocType->getCode() . self::GROUP_ASSOCIATION_SUFFIX;
             $fieldNames[] = $assocType->getCode() . self::PRODUCT_ASSOCIATION_SUFFIX;
         }
         $this->assocFieldsCache = $fieldNames;
     }
     return $this->assocFieldsCache;
 }
 /**
  * {@inheritdoc}
  */
 public function sort(array $columns, array $context = [])
 {
     $identifier = $this->productRepository->getIdentifierProperties()[0];
     if (isset($context['filters']['structure']['attributes']) && !empty($context['filters']['structure']['attributes'])) {
         $rawColumns = array_merge([$identifier], $this->firstDefaultColumns, array_map(function ($associationType) {
             return $associationType->getCode();
         }, $this->associationTypeRepository->findAll()), $context['filters']['structure']['attributes']);
         $sortedColumns = [];
         foreach ($rawColumns as $columnCode) {
             $sortedColumns = array_merge($sortedColumns, array_filter($columns, function ($columnCandidate) use($columnCode) {
                 return 0 !== preg_match(sprintf('/^%s(-.*)*/', $columnCode), $columnCandidate);
             }));
         }
         return $sortedColumns;
     }
     array_unshift($this->firstDefaultColumns, $identifier);
     return parent::sort($columns);
 }
 /**
  * {@inheritdoc}
  */
 public function addMissingAssociations(ProductInterface $product)
 {
     $missingAssocTypes = $this->assocTypeRepository->findMissingAssociationTypes($product);
     if (!empty($missingAssocTypes)) {
         foreach ($missingAssocTypes as $associationType) {
             $association = new $this->associationClass();
             $association->setAssociationType($associationType);
             $product->addAssociation($association);
         }
     }
     return $this;
 }
 /**
  * Edit an association type
  *
  * @param int     $id
  *
  * @Template
  * @AclAncestor("pim_enrich_associationtype_edit")
  *
  * @return array
  */
 public function editAction($id)
 {
     $associationType = $this->assocTypeRepo->find($id);
     if (!$associationType) {
         throw new NotFoundHttpException(sprintf('%s entity not found', 'PimCatalogBundle:AssociationType'));
     }
     if ($this->assocTypeHandler->process($associationType)) {
         $this->request->getSession()->getFlashBag()->add('success', new Message('flash.association type.updated'));
         return new RedirectResponse($this->router->generate('pim_enrich_associationtype_edit', ['id' => $id]));
     }
     $usageCount = $this->assocRepository->countForAssociationType($associationType);
     return ['form' => $this->assocTypeForm->createView(), 'usageCount' => $usageCount];
 }
 /**
  * Prepare fields list for CSV headers
  *
  * @param array $attributesList
  *
  * @return array
  */
 protected function prepareFieldsList(array $attributesList = [])
 {
     $fieldsList = $this->prepareAttributesList($attributesList);
     $fieldsList[] = ProductNormalizer::FIELD_FAMILY;
     $fieldsList[] = ProductNormalizer::FIELD_CATEGORY;
     $fieldsList[] = ProductNormalizer::FIELD_GROUPS;
     $associationTypes = $this->assocTypeRepo->findAll();
     foreach ($associationTypes as $associationType) {
         $fieldsList[] = sprintf('%s-groups', $associationType->getCode());
         $fieldsList[] = sprintf('%s-products', $associationType->getCode());
     }
     return $fieldsList;
 }
 /**
  * Display association grids
  *
  * @param Request $request the request
  * @param int     $id      the product id (owner)
  *
  * @AclAncestor("pim_enrich_associations_view")
  *
  * @return Response
  */
 public function associationsAction(Request $request, $id)
 {
     $product = $this->findProductOr404($id);
     $associationTypes = $this->assocTypeRepository->findAll();
     return $this->templating->renderResponse('PimEnrichBundle:Association:_associations.html.twig', ['product' => $product, 'associationTypes' => $associationTypes, 'dataLocale' => $request->get('dataLocale', null)]);
 }
 /**
  * @return JsonResponse
  */
 public function indexAction()
 {
     $associationTypes = $this->associationTypeRepo->findAll();
     $data = $this->normalizer->normalize($associationTypes, 'internal_api');
     return new JsonResponse($data);
 }