Commit 2201e247 by Van

fix(all): some change on translations. Bug slug already here

parent b858aaea
...@@ -2,6 +2,8 @@ tourinsoft: ...@@ -2,6 +2,8 @@ tourinsoft:
url: 'http://wcf.tourinsoft.com/Syndication/3.0/' url: 'http://wcf.tourinsoft.com/Syndication/3.0/'
ot_key: '' ot_key: ''
cdn_url: '' cdn_url: ''
default_locale: 'FR'
locales : ['FR', 'EN', 'ES']
field_mapping: field_mapping:
SyndicObjectID: SyndicObjectID:
label: Identifiant de l'offre label: Identifiant de l'offre
......
...@@ -68,13 +68,16 @@ class FluxController extends BackendBase ...@@ -68,13 +68,16 @@ class FluxController extends BackendBase
* @throws \Exception * @throws \Exception
*/ */
public function add(Request $request) { public function add(Request $request) {
$form = $this->createFormBuilder(FluxType::class, new Flux()) $form = $this->createFormBuilder(FluxType::class, new Flux(), [
'config' => $this->config['tourinsoft']
])
->getForm() ->getForm()
->handleRequest($request); ->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) if ($form->isSubmitted() && $form->isValid())
{ {
$flux = $form->getData(); $flux = $form->getData();
$flux->setCreatedAt(new \DateTimeImmutable()); $flux->setCreatedAt(new \DateTimeImmutable());
$flux->setEnabled(true); $flux->setEnabled(true);
$this->getRepository(Flux::class)->save($flux); $this->getRepository(Flux::class)->save($flux);
...@@ -96,7 +99,10 @@ class FluxController extends BackendBase ...@@ -96,7 +99,10 @@ class FluxController extends BackendBase
{ {
$flux = $this->assertFlux($request->get('id')); $flux = $this->assertFlux($request->get('id'));
$form = $this->createFormBuilder(FluxType::class, $flux, ['edit_mode' => true]) $form = $this->createFormBuilder(FluxType::class, $flux, [
'edit_mode' => true,
'config' => $this->config['tourinsoft']
])
->getForm() ->getForm()
->handleRequest($request); ->handleRequest($request);
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Form; namespace Bolt\Extension\Appolo\Tourinsoft\Form;
use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux;
use Bolt\Translation\Translator; use Bolt\Translation\Translator;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
...@@ -53,22 +55,12 @@ class FluxType extends AbstractType ...@@ -53,22 +55,12 @@ class FluxType extends AbstractType
] ]
]) ])
->add( ->add(
'key', 'keys',
TextType::class, KeyType::class,
[ [
'label' => Translator::__('appolo.tourinsoft.key'), 'label' => false,
'required' => true, 'config' => $options['config'],
'attr' => [ 'edit_mode' => $options['edit_mode']
'class' => 'form-control'
],
'label_attr' => [
'class' => 'main control-label col-xs-12'
],
'constraints' => array(
new NotBlank([
'message' => Translator::__('appolo.tourinsoft.key_is_required')
])
),
]) ])
->add( ->add(
'contentType', 'contentType',
...@@ -103,7 +95,9 @@ class FluxType extends AbstractType ...@@ -103,7 +95,9 @@ class FluxType extends AbstractType
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'edit_mode' => false 'data_class' => Flux::class,
'edit_mode' => false,
'config' => []
]); ]);
} }
......
<?php
namespace Bolt\Extension\Appolo\Tourinsoft\Form;
use Bolt\Translation\Translator;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
class KeyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if(!empty($options['config']['locales']))
{
foreach ($options['config']['locales'] as $locale)
{
$constraints = [];
$required = $locale == $options['config']['default_locale'];
if($locale == $options['config']['default_locale'])
{
$constraints = [
new NotBlank([
'message' => Translator::__('appolo.tourinsoft.key_is_required')
])
];
}
$builder
->add(
$locale,
TextType::class,
[
'label' => Translator::__('appolo.tourinsoft.key').sprintf(Translator::__(' for %s syndication'), $locale),
'required' => $required,
'attr' => [
'class' => 'form-control'
],
'label_attr' => [
'class' => 'main control-label col-xs-12'
],
'constraints' => $constraints,
])
;
}
}
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'edit_mode' => false,
'config' => []
]);
}
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Storage\Entity; namespace Bolt\Extension\Appolo\Tourinsoft\Storage\Entity;
use Bolt\Storage\Entity\Entity; use Bolt\Storage\Entity\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/** /**
* Class Flux * Class Flux
...@@ -16,8 +17,8 @@ class Flux extends Entity ...@@ -16,8 +17,8 @@ class Flux extends Entity
protected $title; protected $title;
/** @var string */ /** @var string */
protected $code; protected $code;
/** @var string */ /** @var array */
protected $key; protected $keys;
/** @var string */ /** @var string */
protected $contentType; protected $contentType;
...@@ -35,6 +36,12 @@ class Flux extends Entity ...@@ -35,6 +36,12 @@ class Flux extends Entity
/** @var \Datetime */ /** @var \Datetime */
protected $synchronizedAt; protected $synchronizedAt;
public function __construct(array $data = [])
{
parent::__construct($data);
//$this->keys = new ArrayCollection();
}
/** /**
* @return int * @return int
*/ */
...@@ -80,20 +87,20 @@ class Flux extends Entity ...@@ -80,20 +87,20 @@ class Flux extends Entity
} }
/** /**
* @return string * @return array
*/ */
public function getKey() public function getKeys()
{ {
return $this->key; return $this->keys;
} }
/** /**
* @param string $key * @param array $keys
* @return Flux * @return Flux
*/ */
public function setKey($key) public function setKeys($keys)
{ {
$this->key = $key; $this->keys = $keys;
return $this; return $this;
} }
......
...@@ -15,7 +15,7 @@ class Flux extends BaseTable ...@@ -15,7 +15,7 @@ class Flux extends BaseTable
$this->table->addColumn('id', 'integer', ['autoincrement' => true]); $this->table->addColumn('id', 'integer', ['autoincrement' => true]);
$this->table->addColumn('title', 'string', ['notnull' => true]); $this->table->addColumn('title', 'string', ['notnull' => true]);
$this->table->addColumn('code', 'string', ['notnull' => true]); $this->table->addColumn('code', 'string', ['notnull' => true]);
$this->table->addColumn('key', 'string', ['notnull' => true]); $this->table->addColumn('keys', 'json_array', ['notnull' => true]);
$this->table->addColumn('contentType', 'string', ['notnull' => true]); $this->table->addColumn('contentType', 'string', ['notnull' => true]);
$this->table->addColumn('viewless', 'boolean', ['default' => false]); $this->table->addColumn('viewless', 'boolean', ['default' => false]);
$this->table->addColumn('searchable', 'boolean', ['default' => true]); $this->table->addColumn('searchable', 'boolean', ['default' => true]);
...@@ -30,7 +30,7 @@ class Flux extends BaseTable ...@@ -30,7 +30,7 @@ class Flux extends BaseTable
*/ */
protected function addIndexes() protected function addIndexes()
{ {
$this->table->addIndex(['key']); $this->table->addIndex(['keys']);
$this->table->addIndex(['id']); $this->table->addIndex(['id']);
} }
......
...@@ -50,15 +50,15 @@ class Synchronisation ...@@ -50,15 +50,15 @@ class Synchronisation
*/ */
public function sync() { public function sync() {
$options = $this->config['tourinsoft']; $options = $this->config['tourinsoft'];
$options['key'] = $this->flux->getKey(); $options['keys'] = $this->flux->getKeys();
// Parse tourinsoft flux // Parse tourinsoft flux
$parser = new Parser($options); $parser = new Parser($options);
$keys = $parser->getKeys(); $fields = $parser->getFields();
$arrayStream = $parser->getArrayStream(); $arrayStream = $parser->getArrayStream();
// Write in contenttypes.yml file // Write in contenttypes.yml file
$file = new ContentTypeFile($this->application, $this->flux, $keys, $options); $file = new ContentTypeFile($this->application, $this->flux, $fields, $options);
$file->updateInFile(); $file->updateInFile();
$this->application['config']->initialize(); $this->application['config']->initialize();
...@@ -70,8 +70,9 @@ class Synchronisation ...@@ -70,8 +70,9 @@ class Synchronisation
$schema->update(); $schema->update();
// Add data // Add data
$data = new Data($this->application, $this->flux->getCode(), $arrayStream); $data = new Data($this->application, $this->flux->getCode(), $arrayStream, $options);
$data->insert(); $data->insert();
} }
/** /**
......
...@@ -53,9 +53,11 @@ class ContentTypeFile ...@@ -53,9 +53,11 @@ class ContentTypeFile
$this->flux->getCode() => [ $this->flux->getCode() => [
'name' => ucfirst($this->flux->getTitle()), 'name' => ucfirst($this->flux->getTitle()),
'singular_name' => ucfirst($this->flux->getContentType()), 'singular_name' => ucfirst($this->flux->getContentType()),
'slug' => strtolower($this->flux->getTitle()),
'singular_slug' => strtolower($this->flux->getContentType()),
'fields' => $this->getFields(), 'fields' => $this->getFields(),
'record_template' => strtolower('tourinsoft/record/'.$this->flux->getContentType() . '.twig'), 'record_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/' .$this->flux->getContentType() . '.twig'),
'listing_template' => strtolower('tourinsoft/listing/listing_'.$this->flux->getContentType() . '.twig'), 'listing_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/listing_'.$this->flux->getContentType() . '.twig'),
'viewless' => $this->flux->isViewless(), 'viewless' => $this->flux->isViewless(),
'searchable' => $this->flux->isSearchable() 'searchable' => $this->flux->isSearchable()
] ]
...@@ -77,19 +79,38 @@ class ContentTypeFile ...@@ -77,19 +79,38 @@ class ContentTypeFile
private function getFields() private function getFields()
{ {
$fields = []; $fields = [];
foreach ($this->keys as $key) { foreach ($this->keys as $key) {
$fields[strtolower($key)] = [ $fields[strtolower($key)] = [
'type' => 'text', 'type' => 'text',
'label' => $this->getFieldLabel($key), 'label' => $this->getFieldLabel($key),
'readonly' => true 'readonly' => true,
'translatable' => true
]; ];
} }
$fields['locale'] = [
'type' => 'locale',
'group' => 'content'
];
$fields['slug'] = [ $fields['slug'] = [
'type' => 'slug', 'type' => 'slug',
'uses' => ['syndicobjectid'] 'uses' => 'nomoffre'
];
if(!empty($this->options['locales'])) {
foreach ($this->options['locales'] as $locale) {
$dataKey = strtolower($locale).'data';
$slugKey = strtolower($locale).'slug';
$fields[$dataKey] = [
'type' => 'hidden'
];
$fields[$slugKey] = [
'type' => 'locale_data',
'index' => true
]; ];
}
}
return $fields; return $fields;
} }
......
...@@ -6,6 +6,7 @@ use Bolt\Application; ...@@ -6,6 +6,7 @@ use Bolt\Application;
use Bolt\Exception\InvalidRepositoryException; use Bolt\Exception\InvalidRepositoryException;
use Bolt\Storage\Entity\Builder; use Bolt\Storage\Entity\Builder;
use Bolt\Storage\EntityManager; use Bolt\Storage\EntityManager;
use Cocur\Slugify\Slugify;
class Data class Data
{ {
...@@ -21,6 +22,10 @@ class Data ...@@ -21,6 +22,10 @@ class Data
* @var string * @var string
*/ */
private $contentType; private $contentType;
/**
* @var array
*/
private $options;
/** /**
* Data constructor. * Data constructor.
...@@ -28,11 +33,12 @@ class Data ...@@ -28,11 +33,12 @@ class Data
* @param string $contentType * @param string $contentType
* @param array $arrayStream * @param array $arrayStream
*/ */
public function __construct(Application $application, $contentType = '', array $arrayStream = []) { public function __construct(Application $application, $contentType = '', array $arrayStream = [], array $options = []) {
$this->application = $application; $this->application = $application;
$this->arrayStream = $arrayStream; $this->arrayStream = $arrayStream;
$this->contentType = $contentType; $this->contentType = $contentType;
$this->options = $options;
} }
/** /**
...@@ -69,11 +75,13 @@ class Data ...@@ -69,11 +75,13 @@ class Data
/** /**
* @return array * @return array
* @throws \Exception
*/ */
private function getFormattedItems() { private function getFormattedItems() {
$items = []; $items = [];
foreach ($this->arrayStream['value'] as $item) { if(!empty($defaultStream = $this->arrayStream[$this->options['default_locale']])) {
foreach ($defaultStream['value'] as $item) {
$data = []; $data = [];
foreach ($item as $k => $v) { foreach ($item as $k => $v) {
$key = strtolower($k); $key = strtolower($k);
...@@ -81,10 +89,59 @@ class Data ...@@ -81,10 +89,59 @@ class Data
} }
$data['title'] = $data['syndicobjectname']; $data['title'] = $data['syndicobjectname'];
$data['slug'] = Slugify::create()->slugify($data['nomoffre']);
$data['status'] = 'published'; $data['status'] = 'published';
$data['datepublish'] = new \DateTimeImmutable(); $data['datepublish'] = new \DateTimeImmutable();
$this->getTranslatedItem($data);
array_push($items, $data); array_push($items, $data);
} }
}
return $items; return $items;
} }
private function getTranslatedItem(array &$data = []) {
if(!empty($this->options['locales'])) {
foreach ($this->options['locales'] as $locale) {
$dataKey = strtolower($locale).'data';
$slugKey = strtolower($locale).'slug';
if($locale == $this->options['default_locale']) {
$data[$dataKey] = json_encode([]);
$data[$slugKey] = '';
} else {
$dataLocale = $this->getDataForLocale($locale, $data['syndicobjectid']);
$data[$dataKey] = json_encode($dataLocale);
$data[$slugKey] = $dataLocale['slug'];
}
}
}
}
/**
* @param $locale
* @param $id
* @return array
*/
private function getDataForLocale($locale, $id) {
if(!empty($defaultStream = $this->arrayStream[$locale])) {
foreach ($defaultStream['value'] as $item) {
if($item['SyndicObjectID'] === $id) {
$data = [];
foreach ($item as $k => $v) {
$key = strtolower($k);
$data[$key] = $v;
}
$data['title'] = $data['syndicobjectname'];
$data['slug'] = Slugify::create()->slugify($data['nomoffre']);
return $data;
}
}
}
}
} }
\ No newline at end of file
...@@ -14,10 +14,6 @@ class Parser ...@@ -14,10 +14,6 @@ class Parser
* @var array * @var array
*/ */
private $arrayStream = []; private $arrayStream = [];
/**
* @var string
*/
private $url;
/** /**
* Parser constructor. * Parser constructor.
...@@ -26,27 +22,31 @@ class Parser ...@@ -26,27 +22,31 @@ class Parser
*/ */
public function __construct(array $options = []) public function __construct(array $options = [])
{ {
if(empty($options['url']) || empty($options['ot_key']) || empty($options['key'])) { if(empty($options['url']) || empty($options['ot_key']) || empty($options['keys'])) {
throw new \Exception('La configuration n\'est pas valide.'); throw new \Exception('La configuration n\'est pas valide.');
} }
$this->options = $options; $this->options = $options;
$this->url = $options['url'].$options['ot_key'].'/'.$options['key'].'/Objects?$format=JSON';
$this->getStreamAsArray(); foreach ($options['keys'] as $locale => $key) {
$url = $options['url'].$options['ot_key'].'/'.$key.'/Objects?$format=JSON';
$this->getStreamAsArray($url, $locale);
}
} }
/** /**
* @return array * @return array
*/ */
public function getKeys() { public function getFields() {
$keys = []; $keys = [];
foreach ($this->arrayStream['value'] as $item) { if(!empty($defaultStream = $this->arrayStream[$this->options['default_locale']])) {
foreach ($defaultStream['value'] as $item) {
foreach (array_keys($item) as $key) { foreach (array_keys($item) as $key) {
if(!in_array($key, $keys)) { if(!in_array($key, $keys)) {
array_push($keys, $key); array_push($keys, $key);
} }
} }
} }
}
return $keys; return $keys;
} }
...@@ -59,18 +59,20 @@ class Parser ...@@ -59,18 +59,20 @@ class Parser
} }
/** /**
* @throws \Exception * @param string $url
* @param string $locale
* @throws \GuzzleHttp\Exception\GuzzleException
*/ */
protected function getStreamAsArray() { protected function getStreamAsArray($url, $locale) {
$client = new Client(); $client = new Client();
$res = $client->request('GET', $this->url); $res = $client->request('GET', $url);
if($res->getStatusCode() !== 200) { if($res->getStatusCode() !== 200) {
throw new \Exception('Le flux n\'a pu être récupéré.'); throw new \Exception('Le flux n\'a pu être récupéré.');
} }
try { try {
$this->arrayStream = json_decode($res->getBody(), true); $this->arrayStream[$locale] = json_decode($res->getBody(), true);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception('Le flux n\'a pu être décodé.'); throw new \Exception('Le flux n\'a pu être décodé.');
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
{% endblock page_title %} {% endblock page_title %}
{% block page_main %} {% block page_main %}
{{ form_start(form) }} {{ form_start(form) }}
<div class="col-md-8"> <div class="col-md-8">
<div data-bolt-fieldset="text"> <div data-bolt-fieldset="text">
...@@ -30,15 +31,21 @@ ...@@ -30,15 +31,21 @@
</div> </div>
</fieldset> </fieldset>
</div> </div>
{% for key in form.keys %}
<div data-bolt-fieldset="text"> <div data-bolt-fieldset="text">
<fieldset class="form-group bolt-field-text"> <fieldset class="form-group bolt-field-text">
{{ form_label(form.key) }} {{ form_label(attribute(form.keys, key.vars.name)) }}
<div class="col-xs-12"> <div class="col-xs-12">
{{ form_widget(form.key) }} {{ form_widget(attribute(form.keys, key.vars.name)) }}
{{ form_errors(form.key) }} {{ form_errors(attribute(form.keys, key.vars.name)) }}
</div> </div>
</fieldset> </fieldset>
</div> </div>
{% endfor %}
<div data-bolt-fieldset="text"> <div data-bolt-fieldset="text">
<fieldset class="form-group bolt-field-text"> <fieldset class="form-group bolt-field-text">
{{ form_label(form.contentType) }} {{ form_label(form.contentType) }}
...@@ -82,3 +89,4 @@ ...@@ -82,3 +89,4 @@
<!-- / sidebar --> <!-- / sidebar -->
{{ form_end(form) }} {{ form_end(form) }}
{% endblock %} {% endblock %}
...@@ -32,15 +32,17 @@ ...@@ -32,15 +32,17 @@
</fieldset> </fieldset>
</div> </div>
{% for key in form.keys %}
<div data-bolt-fieldset="text"> <div data-bolt-fieldset="text">
<fieldset class="form-group bolt-field-text"> <fieldset class="form-group bolt-field-text">
{{ form_label(form.key) }} {{ form_label(attribute(form.keys, key.vars.name)) }}
<div class="col-xs-12"> <div class="col-xs-12">
{{ form_widget(form.key) }} {{ form_widget(attribute(form.keys, key.vars.name)) }}
{{ form_errors(form.key) }} {{ form_errors(attribute(form.keys, key.vars.name)) }}
</div> </div>
</fieldset> </fieldset>
</div> </div>
{% endfor %}
<div data-bolt-fieldset="text"> <div data-bolt-fieldset="text">
<fieldset class="form-group bolt-field-text"> <fieldset class="form-group bolt-field-text">
......
...@@ -35,7 +35,12 @@ ...@@ -35,7 +35,12 @@
<td class="id hidden-xs">{{ item.id }}</td> <td class="id hidden-xs">{{ item.id }}</td>
<td><span><strong>{{ item.title }}</strong></span></td> <td><span><strong>{{ item.title }}</strong></span></td>
<td>{{ item.code }}</td> <td>{{ item.code }}</td>
<td>{{ item.key }}</td> <td>
{% for index, value in item.keys %}
<strong>{{ index }}</strong> : {{ value }}<br />
{% endfor %}
{#{{ item.key }}#}
</td>
<td>{{ item.contentType }}</td> <td>{{ item.contentType }}</td>
<td class="actions"> <td class="actions">
<div class="btn-group"> <div class="btn-group">
......
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