Commit 997c4138 authored by Sebastian Castro's avatar Sebastian Castro

Element display categories name in API

parent 2bd5dc16
......@@ -12,12 +12,12 @@ class DataUpdateActionsController extends BulkActionsAbstractController
$qb = $em->createQueryBuilder('BiopenCoreBundle:User');
$qb->field('email')->notEqual(null);
$query = $qb->getQuery();
$users = $query->execute();
$users = $query->execute();
$gamificationService = $this->get('biopen_user.gamification');
$gamificationService = $this->get('biopen_user.gamification');
$i = 0;
foreach ($users as $key => $user)
foreach ($users as $key => $user)
{
$gamificationService->updateGamification($user);
......@@ -28,20 +28,9 @@ class DataUpdateActionsController extends BulkActionsAbstractController
}
$em->flush();
$em->clear();
$em->clear();
$request->getSession()->getFlashBag()->add('success', count($users) . " utilisateurs ont été mis à jour");
return $this->redirect($this->generateUrl('admin_biopen_core_user_list'));
}
public function updateElementOptionsStringAction(Request $request) { return $this->elementsBulkAction('updateElementOptionsString', $request); }
public function updateElementOptionsString($element)
{
$optionsArray = array_map( function($ov) { return $this->optionList[$ov->getOptionId()]['name']; }, $element->getOptionValues()->toArray());
$optionsString = join(',', $optionsArray);
$element->setOptionsString($optionsString);
}
}
\ No newline at end of file
......@@ -23,23 +23,25 @@ class OptionValue
*/
public $optionId;
/**
/**
* @Expose
* @MongoDB\Field(type="string")
* @MongoDB\Field(type="string")
*/
public $description;
/**
/**
* @Expose
* @MongoDB\Field(type="int")
* @MongoDB\Field(type="int")
*/
public $index = 0;
public function toJson()
public function toJson($optionName)
{
$result = "{";
$result .= '"categoryId":"' . $this->optionId . '"';
$result .= '"id":' . $this->optionId ;
if ($optionName) $result .= ', "name":' . $optionName;
$result .=', "description":' . json_encode($this->description);
$result .=', "index":' . $this->index;
$result .= "}";
return $result;
}
......
......@@ -19,11 +19,11 @@ class DatabaseIntegrityWatcher
{
protected $asyncService;
public function __construct($asyncService)
public function __construct($asyncService)
{
$this->asyncService = $asyncService;
}
// use post remove instead?
public function preRemove(\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs $args)
{
......@@ -37,7 +37,7 @@ class DatabaseIntegrityWatcher
if ($users->count() > 0)
{
$i = 0;
foreach ($users as $user) {
foreach ($users as $user) {
$user->removeGroup($group);
}
}
......@@ -52,27 +52,47 @@ class DatabaseIntegrityWatcher
{
$webhook = $document;
$contributions = $dm->createQueryBuilder('BiopenGeoDirectoryBundle:UserInteractionContribution')
->field('webhookPosts.webhook.$id')->equals($webhook->getId())
->field('webhookPosts.webhook.$id')->equals($webhook->getId())
->getQuery()->execute();
foreach($contributions as $contrib)
{
$contrib->getElement()->setPreventJsonUpdate(true);
foreach($contrib->getWebhookPosts() as $post)
{
if ($post->getWebhook()->getId() == $webhook->getId()) $contrib->removeWebhookPost($post);
}
$contrib->getElement()->setPreventJsonUpdate(true);
foreach($contrib->getWebhookPosts() as $post)
{
if ($post->getWebhook()->getId() == $webhook->getId()) $contrib->removeWebhookPost($post);
}
}
}
}
public function preFlush(\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs $eventArgs)
public function preUpdate(\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs $args)
{
$document = $args->getDocument();
$dm = $args->getDocumentManager();
// When changing the name of one Option, we need to update json representation of every element
// using this option
if ($document instanceof Option)
{
$uow = $dm->getUnitOfWork();
$uow->computeChangeSets();
$changeset = $uow->getDocumentChangeSet($document);
if (array_key_exists("name", $changeset)) {
$query = $dm->createQueryBuilder('BiopenGeoDirectoryBundle:Element')->field('optionValues.optionId')->in([$document->getId()]);
$elementIds = array_keys($query->select('id')->hydrate(false)->getQuery()->execute()->toArray());
$elementIdsString = '"' . implode(',',$elementIds) . '"';
$this->asyncService->callCommand('app:elements:updateJson', ['ids' => $elementIdsString]);
}
}
}
public function preFlush(\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs $eventArgs)
{
$dm = $eventArgs->getDocumentManager();
$optionsDeleted = array_filter($dm->getUnitOfWork()->getScheduledDocumentDeletions(), function($doc) { return $doc instanceof Option; });
$optionsDeleted = array_filter($dm->getUnitOfWork()->getScheduledDocumentDeletions(), function($doc) { return $doc instanceof Option; });
if (count($optionsDeleted) == 0) return;
$optionsIdDeleted = array_map(function($option) { return $option->getId(); }, $optionsDeleted);
$this->asyncService->callCommand('app:elements:removeOptions', ['ids' => implode($optionsIdDeleted, ',')]);
}
......
......@@ -10,6 +10,7 @@ class ElementJsonGenerator
{
protected $currElementChangeset;
protected $config = null;
protected $options = null;
public function getConfig($dm)
{
......@@ -17,6 +18,14 @@ class ElementJsonGenerator
return $this->config;
}
public function getOptions($dm)
{
// load all options so we don't need to do a query on each element being modified
if (!$this->options) $this->options = $dm->getRepository('BiopenGeoDirectoryBundle:Option')->createQueryBuilder()
->select('name')->hydrate(false)->getQuery()->execute()->toArray();
return $this->options;
}
public function preFlush(\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs $eventArgs)
{
$dm = $eventArgs->getDocumentManager();
......@@ -46,6 +55,7 @@ class ElementJsonGenerator
{
if (!$element->getGeo()) { return; }
$config = $this->getConfig($dm);
$options = $this->getOptions($dm);
// -------------------- FULL JSON ----------------
......@@ -65,7 +75,7 @@ class ElementJsonGenerator
$sortedOptionsValues = $element->getSortedOptionsValues();
$optValuesLength = count($sortedOptionsValues);
// Options values ids
$baseJson .= ', "categories": [';
$baseJson .= ', "categoriesIds": [';
if ($sortedOptionsValues)
{
for ($i=0; $i < $optValuesLength; $i++) {
......@@ -74,15 +84,23 @@ class ElementJsonGenerator
}
$baseJson = rtrim($baseJson, ',');
$baseJson .= '],';
// Options values with descriptionO
$optionDescriptionsJson = [];
// option values names
$optionsString = '';
$optionsFullJson = [];
if ($sortedOptionsValues)
{
for ($i=0; $i < $optValuesLength; $i++) {
if ($sortedOptionsValues[$i]->getDescription()) $optionDescriptionsJson[] = $sortedOptionsValues[$i]->toJson();
}
for ($i=0; $i < $optValuesLength; $i++) {
$optionValue = $sortedOptionsValues[$i];
$optionName = json_encode($options[$optionValue->getOptionId()]['name']);
$optionsString .= $optionName . ',';
$optionsFullJson[] = $sortedOptionsValues[$i]->toJson($optionName);
}
}
if (count($optionDescriptionsJson)) $baseJson .= '"categoriesDescriptions": [' . implode(",", $optionDescriptionsJson) . '],';
$optionsString = rtrim($optionsString, ',');
$baseJson .= '"categories": [' . $optionsString . '],';
$element->setOptionsString($optionsString); // we also update optionsString attribute which is used in exporting from element admin list
// Options values with description
if (count($optionsFullJson)) $baseJson .= '"categoriesFull": [' . implode(",", $optionsFullJson) . '],';
// CUSTOM DATA
if ($element->getData())
......@@ -135,7 +153,7 @@ class ElementJsonGenerator
// -------------------- COMPACT JSON ----------------
// [id, customData, latitude, longitude, status, moderationState]
$compactFields = $this->getConfig($dm)->getMarker()->getFieldsUsedByTemplate();
$compactFields = $config->getMarker()->getFieldsUsedByTemplate();
$compactData = [];
foreach ($compactFields as $field) $compactData[] = $element->getProperty($field);
......
......@@ -12,10 +12,6 @@ biopen_bulk_actions_delete_element_reported:
# -----------
# DATA UPDATE
# -----------
biopen_bulk_actions_update_options_string:
path: /updateOptionsString
defaults: { _controller: Biopen\GeoDirectoryBundle\Controller\Admin\BulkActions\DataUpdateActionsController::updateElementOptionsStringAction }
biopen_bulk_actions_update_gamification:
path: /updateGamification
defaults: { _controller: Biopen\GeoDirectoryBundle\Controller\Admin\BulkActions\DataUpdateActionsController::updateGamificationAction }
......
......@@ -57,6 +57,7 @@ services:
tags:
- { name: doctrine_mongodb.odm.event_listener, event: preRemove }
- { name: doctrine_mongodb.odm.event_listener, event: preFlush }
- { name: doctrine_mongodb.odm.event_listener, event: preUpdate }
biopen.element_import:
class: Biopen\GeoDirectoryBundle\Services\ElementImportService
......
......@@ -4,15 +4,11 @@
{% block page_content %}
<iframe height="190" sandbox="allow-same-origin allow-scripts" src="https://video.colibris-outilslibres.org/videos/embed/b3367cfb-445d-441c-b368-15038890897a" frameborder="0" allowfullscreen></iframe>
<div>
<div>
<a class="btn btn-primary" href={{ path('biopen_bulk_actions_detect_duplicates') }}>Détecter les doublons</a>
<a class="btn btn-primary" href={{ path('biopen_duplicates_index') }}>Gérer les doublons détectés</a>
</div>
<div><a class="btn btn-danger" href={{ path('biopen_bulk_actions_delete_element_reported') }}>Supprimer les éléments reportés comme n'existant plus</a></div>
</br>
<div><a class="btn btn-default" href={{ path('biopen_bulk_actions_update_options_string') }}>
<i class="fa fa-refresh" aria-hidden="true"></i>
Mettre à jour le noms des catégories des éléments (à faire avant un export) </a></div>
<style>
.btn { margin-bottom: 10px; }
......
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