Commit 2201e247 by Van

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

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