diff --git a/src/Document/Element.php b/src/Document/Element.php index 1694cc2fb5a288cf27cb7035bd0ff6fcc839a706..c095409b5678bd63bf80a3aaa5e409d56b5f7b40 100644 --- a/src/Document/Element.php +++ b/src/Document/Element.php @@ -68,7 +68,7 @@ class Element /** * Main properties available on all elements */ - const CORE_FIELDS = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'files', 'owner', 'source', 'openHours', 'email', 'customFormatedAddress']; + const CORE_FIELDS = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'files', 'owner', 'source', 'sourceKey', 'openHours', 'email', 'customFormatedAddress']; /** * @var int diff --git a/src/Document/Import.php b/src/Document/Import.php index 0a29dd906304cfe8dcffcacbe23900f075cd68f7..f4d47c30431a60d1c9099fd1cfbe3038bc597c7c 100644 --- a/src/Document/Import.php +++ b/src/Document/Import.php @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Vich\UploaderBundle\Mapping\Annotation as Vich; +use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique; abstract class ImportState { @@ -28,6 +29,7 @@ abstract class ImportState * @MongoDB\InheritanceType("SINGLE_COLLECTION") * @MongoDB\DiscriminatorField("type") * @MongoDB\DiscriminatorMap({"normal"="Import", "dynamic"="ImportDynamic"}) + * @Unique(fields="sourceName") */ class Import extends AbstractFile { diff --git a/src/EventListener/DatabaseIntegrityWatcher.php b/src/EventListener/DatabaseIntegrityWatcher.php index 9f4d3f4a2adf8c945e7f1c874e57566a9a6c28f8..a48b5d627f2341ecf196d1d3974d6da6fdb36a87 100755 --- a/src/EventListener/DatabaseIntegrityWatcher.php +++ b/src/EventListener/DatabaseIntegrityWatcher.php @@ -126,6 +126,22 @@ class DatabaseIntegrityWatcher } } } + if ($document instanceof ImportDynamic) { + $changeset = $dm->getChangeSet($document); + if (array_key_exists('sourceName', $changeset)) { + $dm->query('Element')->updateMany() + ->field('sourceKey')->set($changeset['sourceName'][1]) + ->field('source')->references($document) + ->execute(); + $elementIds = $dm->query('Element') + ->field('source')->references($document) + ->getIds(); + if (count($elementIds)) { + $elementIdsString = '"'.implode(',', $elementIds).'"'; + $this->asyncService->callCommand('app:elements:updateJson', ['ids' => $elementIdsString]); + } + } + } } public function preFlush(\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs $eventArgs) diff --git a/src/Services/ElementImportOneService.php b/src/Services/ElementImportOneService.php index db5d0a5528d02c8018c17861b69fcbe7bbc6f7f9..1de97329a5e9927485b8401b74d492cd9ed55678 100755 --- a/src/Services/ElementImportOneService.php +++ b/src/Services/ElementImportOneService.php @@ -132,8 +132,9 @@ class ElementImportOneService $address = new PostalAddress($row['streetAddress'], $row['addressLocality'], $row['postalCode'], $row['addressCountry'], $row['customFormatedAddress']); $element->setAddress($address); - $defaultSourceName = $import ? $import->getSourceName() : 'Inconnu'; - $element->setSourceKey((strlen($row['source']) > 0 && 'Inconnu' != $row['source']) ? $row['source'] : $defaultSourceName); + $sourceKey = $import ? $import->getSourceName() : 'Inconnu'; + if (!$import->isDynamicImport() && (strlen($row['source']) > 0 && 'Inconnu' != $row['source'])) $sourceKey = $row['source']; + $element->setSourceKey($sourceKey); $element->setSource($import); if (array_key_exists('owner', $row)) { @@ -183,10 +184,10 @@ class ElementImportOneService } else { if ($updateExisting) { // create edit contribution - $contribution = $this->interactionService->createContribution($element, null, 1, $element->getStatus()); + $this->interactionService->createContribution($element, null, 1, $element->getStatus()); } else { // create import contribution if first time imported - $contribution = $this->interactionService->createContribution($element, null, 0, $element->getStatus()); + $this->interactionService->createContribution($element, null, 0, $element->getStatus()); } }