Commit 5d4e0605 authored by Sebastian Castro's avatar Sebastian Castro

Adds ability to configure public fields (which one to export)

parent 8c14ba26
......@@ -31,6 +31,10 @@ class ConfigurationAdmin extends AbstractAdmin
$mailStyle = array('class' => 'col-md-12 col-lg-6');
$featureFormOption = ['delete' => false, 'required'=> false, 'label_attr'=> ['style'=> 'display:none']];
$featureFormTypeOption = ['edit' => 'inline'];
$dm = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb');
$apiProperties = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->findAllCustomProperties();
$formMapper
->tab('Principal')
->with('Le site', array('class' => 'col-md-6'))
......@@ -67,6 +71,8 @@ class ConfigurationAdmin extends AbstractAdmin
->with("Configurer les API (Utilisateurs avancés)")
->add('api.protectPublicApiWithToken', 'checkbox', array('label' => "Protéger l'api publique pour récupérer les élément avec des jetons utilisateurs (i.e. besoin de créer un compte pour utiliser l'api publique)", 'required' => false))
->add('api.internalApiAuthorizedDomains', 'text', array('label' => "Liste des domaines externe qui utiliseront l'API interne. Mettez * si vous voulez que n'importe quel domaine puisse y avoir accès. Cette option est nécessaire si vous voulez afficher vos données avec GoGoCartoJs mais sur un autre serveur.", 'required' => false))
->add('api.publicApiPrivateProperties', 'choice', array("choices" => $apiProperties, 'label' => "Liste des champs que vous ne voulez pas partager dans l'api publique", 'required' => false, 'multiple' => true))
->end()
->with("Liste des apis disponibles")
->add('apilist', 'text', array('mapped' => false, 'label' => false, 'required' => false, 'attr' => ['class' => 'gogo-api-list']))
......
......@@ -13,6 +13,12 @@ class ConfigurationApi
/** @MongoDB\Field(type="bool") */
public $protectPublicApiWithToken = true;
/**
* List of the custom properties we don't want to share in the public API
* @MongoDB\Field(type="collection")
*/
public $publicApiPrivateProperties = [];
/**
* Set internalApiAuthorizedDomains
*
......@@ -56,4 +62,26 @@ class ConfigurationApi
{
return $this->protectPublicApiWithToken;
}
/**
* Set publicApiPrivateProperties
*
* @param collection $publicApiPrivateProperties
* @return $this
*/
public function setPublicApiPrivateProperties($publicApiPrivateProperties)
{
$this->publicApiPrivateProperties = $publicApiPrivateProperties;
return $this;
}
/**
* Get publicApiPrivateProperties
*
* @return collection $publicApiPrivateProperties
*/
public function getPublicApiPrivateProperties()
{
return $this->publicApiPrivateProperties;
}
}
<?php
namespace Biopen\CoreBundle\EventListener;
use Biopen\CoreBundle\Document\Configuration\ConfigurationApi;
class ConfigurationListener
{
protected $asyncService;
public function __construct($asyncService) {
$this->asyncService = $asyncService;
}
public function preUpdate(\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs $args)
{
$document = $args->getDocument();
$dm = $args->getDocumentManager();
if ($document instanceof ConfigurationApi) {
$uow = $dm->getUnitOfWork();
$uow->computeChangeSets();
$changeset = $uow->getDocumentChangeSet($document);
if (array_key_exists("publicApiPrivateProperties", $changeset)) {
$oldPrivateProperties = array_values($changeset['publicApiPrivateProperties'][0]);
$newPrivateProperties = array_values($changeset['publicApiPrivateProperties'][1]);
$removedProps = array_diff($oldPrivateProperties, $newPrivateProperties);
$addedProps = array_diff($newPrivateProperties, $oldPrivateProperties);
$qb = $dm->createQueryBuilder('BiopenGeoDirectoryBundle:Element');
$qb = $qb->updateMany();
foreach ($removedProps as $key => $prop) {
$qb = $qb->field('privateData.' . $prop)->rename('data.' . $prop);
}
foreach ($addedProps as $key => $prop) {
$qb = $qb->field('data.' . $prop)->rename('privateData.' . $prop);
}
$qb->getQuery()->execute();
$this->asyncService->callCommand('app:elements:updateJson', []);
}
}
}
}
......@@ -29,6 +29,12 @@ services:
tags:
- { name: doctrine_mongodb.odm.event_listener, event: postPersist }
biopen.config_listener:
class: Biopen\CoreBundle\EventListener\ConfigurationListener
tags:
- { name: doctrine_mongodb.odm.event_listener, event: preUpdate }
arguments: [ "@biopen.async" ]
biopen.async.factory:
class: Biopen\CoreBundle\Services\AsyncServiceFactory
arguments:
......
......@@ -19,7 +19,6 @@ class ElementAdmin extends ElementAdminShowEdit
{
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine_mongodb')->getManager();
$config = $em->getRepository('BiopenCoreBundle:Configuration')->findConfiguration();
$basicFields = [
'id' => 'id',
'name' => 'name',
......@@ -31,10 +30,10 @@ class ElementAdmin extends ElementAdminShowEdit
'postalCode' => 'address.postalCode',
'addressCountry' => 'address.addressCountry'
];
$publicProperties = $em->getRepository('BiopenGeoDirectoryBundle:Element')->findAllCustomProperties($onlypublic = true);
$customFields = [];
foreach ($config->getElementFormFields() as $key => $field) {
if (property_exists($field, 'name') && !in_array($field->type, ['separator', 'address', 'title', 'email', 'taxonomy', 'openhours', 'header']))
$customFields[$field->name] = 'data';
foreach ($publicProperties as $key => $prop) {
$customFields[$prop] = 'data';
}
return array_merge($basicFields, $customFields);
}
......
......@@ -91,6 +91,10 @@ class ElementAdminShowEdit extends ElementAdminList
->add('data', null, array('template' => '@BiopenAdmin/partials/show_element_data.html.twig'))
->end()
->with('Champs personnalisés privés', array('class' => 'col-md-6 col-sm-12'))
->add('privateData', null, array('template' => '@BiopenAdmin/partials/show_element_data.html.twig'))
->end()
->with('Autre infos', array('class' => 'col-md-6 col-sm-12'))
->add('id')
->add('images', null, array('template' => '@BiopenAdmin/partials/show_element_images.html.twig'))
......
<?php
namespace Biopen\GeoDirectoryBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\SaasBundle\Command\GoGoAbstractCommand;
class UpdateElementsJsonCommand extends GoGoAbstractCommand
{
protected function gogoConfigure()
{
$this
->setName('app:elements:updateJson')
->setDescription('Calculate again all the element json representation');
}
protected function gogoExecute($em, InputInterface $input, OutputInterface $output)
{
try {
$elements = $em->getRepository('BiopenGeoDirectoryBundle:Element')->findVisibles();
$count = $elements->count();
$this->log('Generating json representation for ' . $count . ' elements...');
$i = 0;
foreach ($elements as $key => $element)
{
$element->updateJsonRepresentation();
if ((++$i % 50) == 0) {
$em->flush();
$em->clear();
}
if (($i % 1000) == 0) {
$this->log($i . ' / ' . $count . ' elements completed...');
}
}
$em->flush();
$em->clear();
$this->log('All elements successfully updated');
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}
\ No newline at end of file
......@@ -23,8 +23,6 @@ class BulkActionsAbstractController extends Controller
protected function elementsBulkAction($functionToExecute, $request)
{
$batchSize = 50;
$elementsLeft = null;
$elementLeftCount = 0;
$isStillElementsToProceed = false;
......@@ -60,7 +58,7 @@ class BulkActionsAbstractController extends Controller
$view = $this->$functionToExecute($element);
if ($view) $renderedViews[] = $view;
if ((++$i % 20) == 0) {
if ((++$i % 50) == 0) {
$em->flush();
$em->clear();
}
......
......@@ -193,6 +193,15 @@ class Element
*/
private $data = [];
/**
* @var string
*
* All the custom attributes belonging to the Element that we want to keep private (not available in public api)
*
* @MongoDB\Field(type="hash")
*/
private $privateData = [];
/**
* @var string
*
......@@ -527,6 +536,10 @@ class Element
$privateJson .= '"status": ' . $this->getStatus() . ',';
if ($this->email) $privateJson .= '"email":' . json_encode($this->email) . ',';
$privateJson .= '"moderationState": ' . $this->getModerationState() . ',';
// CUSTOM PRIVATE DATA
foreach ($this->getPrivateData() as $key => $value) {
$privateJson .= '"'. $key .'": ' . json_encode($value) . ',';
}
$privateJson = rtrim($privateJson, ',');
$privateJson .= '}';
$this->setPrivateJson($privateJson);
......@@ -676,6 +689,7 @@ class Element
$this->resetOptionsValues();
$this->openHours = null;
$this->data = null;
$this->privateData = null;
}
/**
......@@ -1425,6 +1439,28 @@ class Element
return $this->data;
}
/**
* Set data
*
* @param hash $data
* @return $this
*/
public function setPrivateData($data)
{
$this->privateData = $data;
return $this;
}
/**
* Get data
*
* @return hash $data
*/
public function getPrivateData()
{
return $this->privateData;
}
/**
* Set email
*
......
......@@ -251,6 +251,45 @@ class ElementRepository extends DocumentRepository
$qb->field('modifiedElement')->references($element);
return $qb->getQuery()->getSingleResult();
}
public function findDataCustomProperties()
{
return array_merge($this->findPublicCustomProperties(), $this->findPrivateCustomProperties());
}
public function findPublicCustomProperties()
{
return $this->findProperties('this.data');
}
public function findPrivateCustomProperties()
{
return $this->findProperties('this.privateData');
}
private function findProperties($rootPath = 'this')
{
$qb = $this->createQueryBuilder('BiopenGeoDirectoryBundle:Element')
->map('function() { for (var key in ' . $rootPath . ') { emit(key, null); } }')
->reduce('function(k, vals) { return null; }');
return array_map(function($array) { return $array['_id']; }, $qb->getQuery()->execute()->toArray());
}
public function findAllCustomProperties($onlyPublic = false)
{
$dataProperties = $onlyPublic ? $this->findPublicCustomProperties() : $this->findDataCustomProperties();
$allProperties = [];
foreach ($dataProperties as $prop) {
$allProperties[$prop] = $prop;
}
$config = $this->getDocumentManager()->getRepository('BiopenCoreBundle:Configuration')->findConfiguration();
foreach ($config->getElementFormFields() as $key => $field) {
if (property_exists($field, 'name') && !in_array($field->type, ['separator', 'address', 'title', 'email', 'taxonomy', 'openhours', 'header']))
$allProperties[$field->name] = $field->name;
}
return $allProperties;
}
}
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