/** * @param array $convertedItem * * @return mixed */ protected function findOrCreateObject(array $convertedItem) { $entity = $this->findObject($this->repository, $convertedItem); if (null === $entity) { return $this->factory->create(); } return $entity; }
/** * Create and configure a group instance * * @param string $groupTypeCode * * @return GroupInterface */ public function createGroup($groupTypeCode = null) { $group = $this->create(); if (null !== $groupTypeCode) { $groupType = $this->groupTypeRepository->findOneByIdentifier($groupTypeCode); if (null === $groupType) { throw new \InvalidArgumentException(sprintf('Group type with code "%s" was not found', $groupTypeCode)); } $group->setType($groupType); if ($group->getType()->isVariant()) { $group->setProductTemplate($this->productTemplateFactory->create()); } } return $group; }
/** * Create a tree or category * * @param Request $request * @param int $parent * * @throws AccessDeniedException * * @return Response|RedirectResponse */ public function createAction(Request $request, $parent = null) { if (false === $this->securityFacade->isGranted($this->buildAclName('category_create'))) { throw new AccessDeniedException(); } $category = $this->categoryFactory->create(); if ($parent) { $parent = $this->findCategory($parent); $category->setParent($parent); } $category->setCode($request->get('label')); $this->eventDispatcher->dispatch(CategoryEvents::PRE_CREATE, new GenericEvent($category)); $form = $this->createForm($this->rawConfiguration['form_type'], $category, $this->getFormOptions($category)); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $this->categorySaver->save($category); $message = new Message(sprintf('flash.%s.created', $category->getParent() ? 'category' : 'tree')); $this->addFlash('success', $message); $this->eventDispatcher->dispatch(CategoryEvents::POST_CREATE, new GenericEvent($category)); return $this->redirectToRoute($this->buildRouteName('categorytree_edit'), ['id' => $category->getId()]); } } return $this->render(sprintf('PimEnrichBundle:CategoryTree:%s.html.twig', $request->get('content', 'edit')), ['form' => $form->createView(), 'related_entity' => $this->rawConfiguration['related_entity'], 'acl' => $this->rawConfiguration['acl'], 'route' => $this->rawConfiguration['route']]); }
/** * Ensure that there is an option for all activated locales * * @param Collection $optionValues * * @return Collection */ protected function ensureEmptyOptionValues(Collection $optionValues) { $activeLocales = $this->getActiveLocales(); $usedLocales = []; foreach ($optionValues as $optionValue) { $usedLocales[] = $optionValue->getLocale(); } foreach ($activeLocales as $activatedLocale) { if (!in_array($activatedLocale->getCode(), $usedLocales)) { $attributeOptionValue = $this->optionValueFactory->create(); $attributeOptionValue->setLocale($activatedLocale->getCode()); $attributeOptionValue->setValue(''); $optionValues->add($attributeOptionValue); } } return $optionValues; }
/** * Create an option of an attribute * * @param Request $request * @param int $attributeId * * @return JsonResponse * * @AclAncestor("pim_enrich_attribute_edit") */ public function createAction(Request $request, $attributeId) { $attribute = $this->findAttributeOr404($attributeId); $attributeOption = $this->optionFactory->create(); $attributeOption->setAttribute($attribute); //Should be replaced by a paramConverter $data = json_decode($request->getContent(), true); return $this->manageFormSubmission($attributeOption, $data); }
/** * Create a new option for a simple/multi-select attribute * * @param Request $request * @param int $id * @param string $dataLocale * * @Template("PimEnrichBundle:Attribute:form_options.html.twig") * @AclAncestor("pim_enrich_attribute_edit") * * @return Response */ public function createOptionAction(Request $request, $id, $dataLocale) { $attribute = $this->findAttributeOr404($id); if (!$request->isXmlHttpRequest() || !in_array($attribute->getAttributeType(), $this->choiceAttributeTypes)) { return new RedirectResponse($this->router->generate('pim_enrich_attribute_edit', ['id' => $attribute->getId()])); } $option = $this->optionFactory->create(); $optionValue = $this->optionValueFactory->create(); $optionValue->setLocale($dataLocale); $optionValue->setValue(''); $option->addOptionValue($optionValue); $attribute->addOption($option); $form = $this->formFactory->create('pim_attribute_option_create', $option); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $this->optionSaver->save($option); $response = ['status' => 1, 'option' => ['id' => $option->getId(), 'label' => $option->setLocale($dataLocale)->__toString()]]; return new Response(json_encode($response)); } } return ['attribute' => $attribute, 'form' => $form->createView()]; }