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