Commit 7d3a4216 by Van

fix(all): Sync ok (except for locale, to make manually); Pb slug fixed ;

parent 2201e247
...@@ -77,7 +77,6 @@ class FluxController extends BackendBase ...@@ -77,7 +77,6 @@ class FluxController extends BackendBase
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);
......
...@@ -7,7 +7,6 @@ use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux; ...@@ -7,7 +7,6 @@ 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;
...@@ -60,6 +59,7 @@ class FluxType extends AbstractType ...@@ -60,6 +59,7 @@ class FluxType extends AbstractType
[ [
'label' => false, 'label' => false,
'config' => $options['config'], 'config' => $options['config'],
'read_only' => $options['edit_mode'],
'edit_mode' => $options['edit_mode'] 'edit_mode' => $options['edit_mode']
]) ])
->add( ->add(
......
...@@ -3,16 +3,14 @@ ...@@ -3,16 +3,14 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Synchronisation; namespace Bolt\Extension\Appolo\Tourinsoft\Synchronisation;
use Bolt\Application; use Bolt\Application;
use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux; use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux;
use Bolt\Extension\Appolo\Tourinsoft\Storage\Repository\FluxRepository;
use Bolt\Extension\Appolo\Tourinsoft\Utils\ContentTypeFile; use Bolt\Extension\Appolo\Tourinsoft\Utils\ContentTypeFile;
use Bolt\Extension\Appolo\Tourinsoft\Utils\Data; use Bolt\Extension\Appolo\Tourinsoft\Utils\Data;
use Bolt\Extension\Appolo\Tourinsoft\Utils\Parser; use Bolt\Extension\Appolo\Tourinsoft\Utils\Parser;
use Bolt\Extension\Appolo\Tourinsoft\Utils\Schema; use Bolt\Extension\Appolo\Tourinsoft\Utils\Schema;
use Bolt\Provider\DatabaseSchemaServiceProvider; use Bolt\Provider\DatabaseSchemaServiceProvider;
use Bolt\Provider\ExtensionServiceProvider;
use Bolt\Provider\StorageServiceProvider; use Bolt\Provider\StorageServiceProvider;
use Bolt\Storage\Database\Schema\Table\ContentType;
class Synchronisation class Synchronisation
{ {
...@@ -29,9 +27,11 @@ class Synchronisation ...@@ -29,9 +27,11 @@ class Synchronisation
*/ */
private $application; private $application;
/** /**
* Synchornisation constructor. * Synchornisation constructor.
* @param array $config * @param array $config
* @param Application $application
* @param Flux $flux * @param Flux $flux
* @throws \Exception * @throws \Exception
*/ */
...@@ -46,7 +46,7 @@ class Synchronisation ...@@ -46,7 +46,7 @@ class Synchronisation
} }
/** /**
* * Synchronisation flux
*/ */
public function sync() { public function sync() {
$options = $this->config['tourinsoft']; $options = $this->config['tourinsoft'];
...@@ -61,18 +61,14 @@ class Synchronisation ...@@ -61,18 +61,14 @@ class Synchronisation
$file = new ContentTypeFile($this->application, $this->flux, $fields, $options); $file = new ContentTypeFile($this->application, $this->flux, $fields, $options);
$file->updateInFile(); $file->updateInFile();
$this->application['config']->initialize();
$this->application->register(new StorageServiceProvider());
$this->application->register(new DatabaseSchemaServiceProvider());
// Sync database // Sync database
$schema = new Schema($this->application); $schema = new Schema($this->application);
$schema->update(); $schema->updateStep1();
// Add data // Add data
$data = new Data($this->application, $this->flux->getCode(), $arrayStream, $options); $data = new Data($this->application, $this->flux->getCode(), $arrayStream, $options);
$data->insert(); $data->insert();
} }
/** /**
...@@ -89,6 +85,7 @@ class Synchronisation ...@@ -89,6 +85,7 @@ class Synchronisation
// Sync database // Sync database
$schema = new Schema($this->application); $schema = new Schema($this->application);
$schema->update(); $schema->updateStep1();
} }
} }
\ No newline at end of file
...@@ -175,7 +175,6 @@ class TourinsoftExtension extends SimpleExtension ...@@ -175,7 +175,6 @@ class TourinsoftExtension extends SimpleExtension
$sync->sync(); $sync->sync();
} }
// Return a message to the console // Return a message to the console
$event->output->writeln("<comment>Synchronisation terminée</comment>"); $event->output->writeln("<comment>Synchronisation terminée</comment>");
} }
......
...@@ -52,12 +52,12 @@ class ContentTypeFile ...@@ -52,12 +52,12 @@ class ContentTypeFile
$data = [ $data = [
$this->flux->getCode() => [ $this->flux->getCode() => [
'name' => ucfirst($this->flux->getTitle()), 'name' => ucfirst($this->flux->getTitle()),
'singular_name' => ucfirst($this->flux->getContentType()),
'slug' => strtolower($this->flux->getTitle()), 'slug' => strtolower($this->flux->getTitle()),
'singular_name' => ucfirst($this->flux->getContentType()),
'singular_slug' => strtolower($this->flux->getContentType()), 'singular_slug' => strtolower($this->flux->getContentType()),
'fields' => $this->getFields(), 'fields' => $this->getFields(),
'record_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/' .$this->flux->getContentType() . '.twig'), 'record_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/record.twig'),
'listing_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/listing_'.$this->flux->getContentType() . '.twig'), 'listing_template' => strtolower('tourinsoft/'.$this->flux->getCode().'/listing_record.twig'),
'viewless' => $this->flux->isViewless(), 'viewless' => $this->flux->isViewless(),
'searchable' => $this->flux->isSearchable() 'searchable' => $this->flux->isSearchable()
] ]
...@@ -69,7 +69,8 @@ class ContentTypeFile ...@@ -69,7 +69,8 @@ class ContentTypeFile
/** /**
* Delete content type in file * Delete content type in file
*/ */
public function deleteInFile() { public function deleteInFile()
{
$this->updateOrDelete([], true); $this->updateOrDelete([], true);
} }
...@@ -79,6 +80,12 @@ class ContentTypeFile ...@@ -79,6 +80,12 @@ class ContentTypeFile
private function getFields() private function getFields()
{ {
$fields = []; $fields = [];
$fields['locale'] = [
'type' => 'locale',
'group' => 'content'
];
foreach ($this->keys as $key) { foreach ($this->keys as $key) {
$fields[strtolower($key)] = [ $fields[strtolower($key)] = [
'type' => 'text', 'type' => 'text',
...@@ -88,12 +95,7 @@ class ContentTypeFile ...@@ -88,12 +95,7 @@ class ContentTypeFile
]; ];
} }
$fields['locale'] = [ $fields['slug'] = [
'type' => 'locale',
'group' => 'content'
];
$fields['slug'] = [
'type' => 'slug', 'type' => 'slug',
'uses' => 'nomoffre' 'uses' => 'nomoffre'
]; ];
...@@ -106,12 +108,11 @@ class ContentTypeFile ...@@ -106,12 +108,11 @@ class ContentTypeFile
'type' => 'hidden' 'type' => 'hidden'
]; ];
$fields[$slugKey] = [ $fields[$slugKey] = [
'type' => 'locale_data', 'type' => 'hidden',
'index' => true 'index' => true
]; ];
} }
} }
return $fields; return $fields;
} }
...@@ -119,7 +120,8 @@ class ContentTypeFile ...@@ -119,7 +120,8 @@ class ContentTypeFile
* @param array $data * @param array $data
* @param bool $delete * @param bool $delete
*/ */
private function updateOrDelete(array $data = [], $delete = false) { private function updateOrDelete(array $data = [], $delete = false)
{
/** @var YamlFile $file */ /** @var YamlFile $file */
$file = $this->application['filesystem']->getFile(strtolower('config://contenttypes.yml')); $file = $this->application['filesystem']->getFile(strtolower('config://contenttypes.yml'));
if($file->exists()) { if($file->exists()) {
...@@ -132,7 +134,6 @@ class ContentTypeFile ...@@ -132,7 +134,6 @@ class ContentTypeFile
} }
$yamlData += $data; $yamlData += $data;
} }
$file->dump($yamlData, ['inline' => 4]); $file->dump($yamlData, ['inline' => 4]);
} }
} }
...@@ -141,7 +142,8 @@ class ContentTypeFile ...@@ -141,7 +142,8 @@ class ContentTypeFile
* @param $key * @param $key
* @return mixed * @return mixed
*/ */
private function getFieldLabel($key) { private function getFieldLabel($key)
{
if( if(
empty($this->options) || empty($this->options) ||
empty($this->options['field_mapping']) || empty($this->options['field_mapping']) ||
......
...@@ -32,9 +32,10 @@ class Data ...@@ -32,9 +32,10 @@ class Data
* @param Application $application * @param Application $application
* @param string $contentType * @param string $contentType
* @param array $arrayStream * @param array $arrayStream
* @param array $options
*/ */
public function __construct(Application $application, $contentType = '', array $arrayStream = [], array $options = []) { 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;
...@@ -44,12 +45,12 @@ class Data ...@@ -44,12 +45,12 @@ class Data
/** /**
* Insert data in database * Insert data in database
*/ */
public function insert() { public function insert()
{
$this->deleteData(); $this->deleteData();
$items = $this->getFormattedItems(); $items = $this->getFormattedItems();
$contentType = $this->getContentType(); $contentType = $this->getContentType();
// $contentType['status'] = 'published'; // $contentType['status'] = 'published';
foreach ($items as $k => $item) { foreach ($items as $k => $item) {
$this->application['storage.request.save']->action($item, $contentType, null, null, null, null); $this->application['storage.request.save']->action($item, $contentType, null, null, null, null);
...@@ -59,17 +60,21 @@ class Data ...@@ -59,17 +60,21 @@ class Data
/** /**
* Delete data from contenttype table * Delete data from contenttype table
*/ */
private function deleteData() { private function deleteData()
{
$contentTypeRepository = $this->application['storage']->getRepository($this->contentType); $contentTypeRepository = $this->application['storage']->getRepository($this->contentType);
$deleteFieldQuery = $contentTypeRepository->createQueryBuilder() $deleteFieldQuery = $contentTypeRepository
->delete($contentTypeRepository->getTableName()); ->createQueryBuilder()
->delete($contentTypeRepository
->getTableName());
$deleteFieldQuery->execute(); $deleteFieldQuery->execute();
} }
/** /**
* @return mixed * @return mixed
*/ */
private function getContentType() { private function getContentType()
{
return $this->application['storage']->getContentType($this->contentType); return $this->application['storage']->getContentType($this->contentType);
} }
...@@ -77,46 +82,53 @@ class Data ...@@ -77,46 +82,53 @@ class Data
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
private function getFormattedItems() { private function getFormattedItems()
{
$items = []; $items = [];
if(!empty($defaultStream = $this->arrayStream[$this->options['default_locale']])) { if (!empty($defaultStream = $this->arrayStream[$this->options['default_locale']])) {
foreach ($defaultStream['value'] as $item) { foreach ($defaultStream['value'] as $item) {
$data = []; $data = [];
foreach ($item as $k => $v) { foreach ($item as $k => $v) {
$key = strtolower($k); $key = strtolower($k);
$data[$key] = $v; $data[$key] = $v;
} }
$data['title'] = $data['syndicobjectname']; $data['title'] = $data['syndicobjectname'];
$data['slug'] = Slugify::create()->slugify($data['nomoffre']); $data['slug'] = Slugify::create()->slugify($data['nomoffre']);
$data['status'] = 'published'; $data['status'] = 'published';
$data['datepublish'] = new \DateTimeImmutable(); $data['datepublish'] = new \DateTimeImmutable();
$this->getTranslatedItem($data); $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'])) { * @param array $data
*/
private function getTranslatedItem(array &$data = [])
{
if (!empty($this->options['locales'])) {
foreach ($this->options['locales'] as $locale) { foreach ($this->options['locales'] as $locale) {
$dataKey = strtolower($locale).'data'; $dataKey = strtolower($locale) . 'data';
$slugKey = strtolower($locale).'slug'; $slugKey = strtolower($locale) . 'slug';
if($locale == $this->options['default_locale']) { if ($locale == $this->options['default_locale']) {
$data[$dataKey] = json_encode([]); $data[$dataKey] = json_encode([]);
$data[$slugKey] = ''; $data[$slugKey] = Slugify::create()->slugify($data['nomoffre']);
} else { } else {
$dataLocale = $this->getDataForLocale($locale, $data['syndicobjectid']); $dataLocale = $this->getDataForLocales($locale, $data['syndicobjectid']);
$data[$dataKey] = json_encode($dataLocale); if(!empty($dataLocale)) {
$data[$slugKey] = $dataLocale['slug']; $data[$dataKey] = json_encode($dataLocale);
$data[$slugKey] = $dataLocale['slug'];
} else {
$data[$dataKey] = json_encode([]);
$data[$slugKey] = Slugify::create()->slugify($data['nomoffre']);
}
} }
} }
} }
} }
...@@ -126,22 +138,24 @@ class Data ...@@ -126,22 +138,24 @@ class Data
* @param $id * @param $id
* @return array * @return array
*/ */
private function getDataForLocale($locale, $id) { private function getDataForLocales($locale, $id)
if(!empty($defaultStream = $this->arrayStream[$locale])) { {
if (!empty($defaultStream = $this->arrayStream[$locale])) {
foreach ($defaultStream['value'] as $item) { foreach ($defaultStream['value'] as $item) {
if($item['SyndicObjectID'] === $id) { if ($item['SyndicObjectID'] === $id) {
$data = []; $data = [];
foreach ($item as $k => $v) { foreach ($item as $k => $v) {
$key = strtolower($k); $key = strtolower($k);
$data[$key] = $v; $data[$key] = $v;
} }
$data['title'] = $data['syndicobjectname']; $data['title'] = $data['syndicobjectname'];
$data['slug'] = Slugify::create()->slugify($data['nomoffre']); $data['slug'] = Slugify::create()->slugify($locale. '-' .$data['nomoffre']);
return $data; return $data;
} }
} }
} }
return [];
} }
} }
\ No newline at end of file
...@@ -26,10 +26,13 @@ class Parser ...@@ -26,10 +26,13 @@ class Parser
throw new \Exception('La configuration n\'est pas valide.'); throw new \Exception('La configuration n\'est pas valide.');
} }
$this->options = $options; $this->options = $options;
foreach ($options['keys'] as $locale => $key) { foreach ($options['keys'] as $locale => $key) {
$url = $options['url'].$options['ot_key'].'/'.$key.'/Objects?$format=JSON'; if(empty($key)) {
$this->getStreamAsArray($url, $locale); $this->arrayStream[$locale] = null;
} else {
$url = $options['url'].$options['ot_key'].'/'.$key.'/Objects?$format=JSON';
$this->getStreamAsArray($url, $locale);
}
} }
} }
...@@ -47,7 +50,6 @@ class Parser ...@@ -47,7 +50,6 @@ class Parser
} }
} }
} }
return $keys; return $keys;
} }
...@@ -62,15 +64,16 @@ class Parser ...@@ -62,15 +64,16 @@ class Parser
* @param string $url * @param string $url
* @param string $locale * @param string $locale
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
*/ */
protected function getStreamAsArray($url, $locale) { protected function getStreamAsArray($url, $locale)
{
$client = new Client(); $client = new Client();
$res = $client->request('GET', $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[$locale] = json_decode($res->getBody(), true); $this->arrayStream[$locale] = json_decode($res->getBody(), true);
} catch (\Exception $e) { } catch (\Exception $e) {
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Utils; namespace Bolt\Extension\Appolo\Tourinsoft\Utils;
use Bolt\Application; use Bolt\Application;
use Bolt\Provider\DatabaseSchemaServiceProvider;
use Bolt\Provider\StorageServiceProvider;
class Schema class Schema
{ {
...@@ -20,11 +22,31 @@ class Schema ...@@ -20,11 +22,31 @@ class Schema
$this->application = $application; $this->application = $application;
} }
public function update() { public function updateStep1()
{
$this->application['config']->initialize();
$this->application->register(new StorageServiceProvider());
$this->application->register(new DatabaseSchemaServiceProvider());
$output = $this->schemaManager()->update();
$this->session()->set('dbupdate_result', $output->getResponseStrings());
}
// $this->application['schema']->isUpdateRequired(); /**
// $this->application['schema']->check(); * @return \Bolt\Storage\Database\Schema\Manager
$output = $this->application['schema']->update(); */
$this->application['session']->set('dbupdate_result', $output->getResponseStrings()); protected function schemaManager()
{
return $this->application['schema'];
} }
/**
* Returns the session.
*
* @return \Symfony\Component\HttpFoundation\Session\Session
*/
protected function session()
{
return $this->application['session'];
}
} }
\ No newline at end of file
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