WIP add reversed elements fields

parent dc4a65cb
web_profiler:
toolbar: true
intercept_redirects: false
intercept_redirects: true
framework:
profiler: { only_exceptions: false }
......@@ -194,6 +194,12 @@ services:
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Page d'Accueil }
admin.categories:
class: App\Admin\CategoryAdmin
arguments: [~, App\Document\Category, 'App\Controller\Admin\CategoryAdminController']
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Catégories }
admin.map_config:
class: App\Admin\ConfigurationMapAdmin
arguments: [~, App\Document\Configuration, 'App\Controller\Admin\ConfigurationAdminController']
......@@ -218,18 +224,18 @@ services:
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Marqueur / Popup }
admin.contributions_config:
class: App\Admin\ConfigurationContributionsAdmin
arguments: [~, App\Document\Configuration, 'App\Controller\Admin\ConfigurationAdminController']
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: sonata_user, label: Qui peut Contribuer? }
admin.form_config:
class: App\Admin\ConfigurationFormAdmin
arguments: [~, App\Document\Configuration, 'App\Controller\Admin\ConfigurationAdminController']
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Formulaire }
admin.contributions_config:
class: App\Admin\ConfigurationContributionsAdmin
arguments: [~, App\Document\Configuration, 'App\Controller\Admin\ConfigurationAdminController']
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Qui peut Contribuer? }
admin.email_config:
class: App\Admin\ConfigurationMailAdmin
arguments: [~, App\Document\Configuration, 'App\Controller\Admin\ConfigurationAdminController']
......@@ -317,12 +323,6 @@ services:
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Éléments }
admin.categories:
class: App\Admin\CategoryAdmin
arguments: [~, App\Document\Category, 'App\Controller\Admin\CategoryAdminController']
tags:
- { name: sonata.admin, manager_type: doctrine_mongodb, group: custom, label: Catégories }
admin.imports:
class: App\Admin\ImportAdmin
arguments: [~, App\Document\Import, 'App\Controller\Admin\ImportAdminController']
......
......@@ -12,7 +12,7 @@ services:
- /var/www/var/sessions
- /var/www/web/uploads
ports:
- "80:80"
- "3008:80"
links:
- mongo:mongo
tty: true
......
......@@ -138,19 +138,26 @@ class DatabaseIntegrityWatcher
if (array_key_exists("App\Document\Element", $documentManaged)) {
foreach ($documentManaged["App\Document\Element"] as $key => $element) {
// If name have changed, fixs elements referencing this element
// Fixs elements referencing this element
$elementsFields = [];
$bidirdectionalElementsFields = [];
$config = $this->getConfig($dm);
foreach ($config->getElementFormFields() as $field) {
if ($field->type == 'elements') $elementsFields[] = $field->name;
if ($field->type == 'elements') {
$elementsFields[] = $field->name;
if (isset($field->reversedBy)) $bidirdectionalElementsFields[] = $field;
}
}
if (count($elementsFields)) {
$uow = $dm->getUnitOfWork();
$uow->computeChangeSets();
$changeset = $uow->getDocumentChangeSet($element);
$elementToUpdates = [];
$privateProps = $config->getApi()->getPublicApiPrivateProperties();
// If name have changed, update element which reference this element
if (array_key_exists('name', $changeset)) {
$newName = $changeset['name'][1];
$privateProps = $config->getApi()->getPublicApiPrivateProperties();
foreach ($elementsFields as $fieldName) {
$fieldPath = in_array($fieldName, $privateProps) ? 'privateData' : 'data';
$fieldPath .= '.' . $fieldName . '.' . $element->getId();
......@@ -159,16 +166,58 @@ class DatabaseIntegrityWatcher
->field($fieldPath)->set($newName)
->field($fieldPath)->exists(true)
->getQuery()->execute();
$elementIds = array_keys($dm->getRepository('App\Document\Element')->createQueryBuilder()
$elementToUpdates = $elementToUpdates + $dm->getRepository('App\Document\Element')->createQueryBuilder()
->field($fieldPath)->exists(true)
->select('id')->hydrate(false)->getQuery()->execute()->toArray());
if (count($elementIds)) {
$elementIdsString = '"'.implode(',', $elementIds).'"';
$this->asyncService->callCommand('app:elements:updateJson', ['ids' => $elementIdsString]);
}
->select('id')->hydrate(false)->getQuery()->execute()->toArray();
}
dump("name changed, update following linked elements", $elementToUpdates);
}
// If bidirectional element field have changed, update reverse relation
// exple A { parent: B }, we should auto update B { children: A }
foreach ($bidirdectionalElementsFields as $field) {
$path = in_array($field->name, $privateProps) ? 'privateData' : 'data';
if (array_key_exists($path, $changeset)) {
$changes = $changeset[$path];
dump("changes $path : $field->name ", $changes, (array) $changes[0][$field->name], (array) $changes[1][$field->name]);
$oldValue = $changes[0] && $changes[0][$field->name] ? array_keys((array) $changes[0][$field->name]) : [];
$newValue = $changes[1] && $changes[1][$field->name]? array_keys((array) $changes[1][$field->name]) : [];
$removedElements = array_diff($oldValue, $newValue);
$addedElements = array_diff($newValue, $oldValue);
$reverseName = $field->reversedBy;
$fieldPath = in_array($reverseName, $privateProps) ? 'privateData' : 'data';
$fieldPath .= '.' . $reverseName . '.' . $element->getId();
dump("element $field->name changed, update $reverseName. New element", $addedElements, "removedElements", $removedElements);
// Updates elements throught reverse relation
$dm->getRepository('App\Document\Element')->createQueryBuilder()
->updateMany()
->field('id')->in($addedElements)
->field($fieldPath)->set($element->getName())
->getQuery()->execute();
$elementToUpdates = $elementToUpdates + $dm->getRepository('App\Document\Element')->createQueryBuilder()
->field('id')->in($addedElements)
->select('id')->hydrate(false)->getQuery()->execute()->toArray();
$dm->getRepository('App\Document\Element')->createQueryBuilder()
->updateMany()
->field('id')->in($removedElements)
->field($fieldPath)->unsetField()->exists(true)
->getQuery()->execute();
$elementToUpdates = $elementToUpdates + $dm->getRepository('App\Document\Element')->createQueryBuilder()
->field('id')->in($removedElements)
->select('id')->hydrate(false)->getQuery()->execute()->toArray();
}
}
dump("elementToUpdates", $elementToUpdates);
if (count($elementToUpdates)) {
$ids = array_unique(array_keys($elementToUpdates));
$elementIdsString = '"'.implode(',', $ids).'"';
$this->asyncService->callCommand('app:elements:updateJson', ['ids' => $elementIdsString]);
}
// $toto->salut;
}
}
}
......
......@@ -115,6 +115,7 @@
},
elements: {
icon: iconAttr,
reversedBy: { label: 'Inversée par', placeholder: "Le nom d'un autre champ de type \"lien vers un autre élément\"" },
multiple: { label: 'Plusieurs choix', type: 'checkbox' },
separator: { label: '' }, // separate important attrs from others
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment