/** * Transforms AdvertisementPrortyValues value entity/collection IDs to objects * */ public function transform($advertisement) { $advertisementPropertyValues = $advertisement->getAdvertisementPropertyValues(); $advertisementProperties = $advertisement->getAdvertisementType()->getAdvertisementTypeConfigurations(); if (!$this->em) { throw new \Exception('EntityManager is required in ' . get_class($this) . ' when editing Advertisement!'); } $currentProperties = $collectionPropertyValues = $collectionProperties = array(); foreach ($advertisementPropertyValues as $each) { $property = $each->getAdvertisementPropertyName(); $currentProperties[] = $property->getId(); if (!$each->getValue()) { continue; } if ($property->getName() == 'highlights') { $newValue = json_decode($each->getValue(), true); foreach ($newValue as $i => $highlight) { if ($mediaId = $highlight['icon']) { $media = $this->em->getRepository('MediaBundle:Media')->find($mediaId); $newValue[$i]['icon'] = $media; } } $each->setValue($newValue); } if ($property->getDataType()->getFormField() == 'entity') { $newValue = $this->em->getRepository($property->getDataClass())->find($each->getValue()); if ($property->getDataType()->getColumnType() != 'collection') { $each->setValue($property->getName() == 'media_id' ? $newValue : $newValue); continue; } if (!isset($collectionPropertyValues[$property->getId()])) { $collectionProperties[] = $each; $collectionPropertyValues[$property->getId()] = new ArrayCollection(); } $collectionPropertyValues[$property->getId()]->add($newValue); } else { if ($property->getDataType()->getColumnType() == 'collection') { $newValue = $each->getValue(); $defaultValue = new ArrayCollection(); if ($property->getDataType()->getFormField() == 'file') { $newValue = $this->em->getRepository($property->getDataClass())->find($each->getValue()); } elseif ($property->getDataType()->getFormField() == 'choice') { $defaultValue = array(); } if (!isset($collectionPropertyValues[$property->getId()])) { $collectionProperties[] = $each; $collectionPropertyValues[$property->getId()] = $defaultValue; } if (is_array($defaultValue)) { $collectionPropertyValues[$property->getId()][] = $newValue; } else { $collectionPropertyValues[$property->getId()]->set($each->getId(), $newValue); } } } } foreach ($collectionProperties as $each) { $propertyId = $each->getAdvertisementPropertyName()->getId(); $each->setValue($collectionPropertyValues[$propertyId]); } foreach ($advertisementProperties as $each) { if (!in_array($each->getId(), $currentProperties)) { $newObj = new AdvertisementPropertyValue(); $newObj->setAdvertisementPropertyName($each); $newObj->setAdvertisement($advertisement); if ($each->getName() == 'media_id') { $newObj->setValue(new Media()); } elseif ($each->getName() == 'highlights') { $highlights = new ArrayCollection(); $highlights->add(array()); $newObj->setValue($highlights); } if ($each->getDataType()->getColumnType() == 'collection') { if ($each->getDataType()->getFormField() == 'entity') { $newObj->setValue(new ArrayCollection()); } else { $newObj->setValue(array()); } } $advertisement->addAdvertisementPropertyValue($newObj); } } return $advertisement; }
/** * {@inheritdoc} */ public function refreshUser(UserInterface $user) { $userRepository = $this->em->getRepository('AppBundle:Core\\User'); return $userRepository->find($user->getId()); }