Commit a4a35e57 by Frédéric

Insert data

parent 3edcd00c
...@@ -5,6 +5,7 @@ namespace Bolt\Extension\Appolo\Tourinsoft\Synchronisation; ...@@ -5,6 +5,7 @@ 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\Utils\ContentTypeFile; 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\Parser;
use Bolt\Extension\Appolo\Tourinsoft\Utils\Schema; use Bolt\Extension\Appolo\Tourinsoft\Utils\Schema;
...@@ -49,6 +50,7 @@ class Synchronisation ...@@ -49,6 +50,7 @@ class Synchronisation
// Parse tourinsoft flux // Parse tourinsoft flux
$parser = new Parser($options); $parser = new Parser($options);
$keys = $parser->getKeys(); $keys = $parser->getKeys();
$arrayStream = $parser->getArrayStream();
// Write in contenttypes.yml file // Write in contenttypes.yml file
$file = new ContentTypeFile($this->application, $this->flux->getContentType(), $keys, $options); $file = new ContentTypeFile($this->application, $this->flux->getContentType(), $keys, $options);
...@@ -57,6 +59,10 @@ class Synchronisation ...@@ -57,6 +59,10 @@ class Synchronisation
// Sync database // Sync database
$schema = new Schema($this->application); $schema = new Schema($this->application);
$schema->update(); $schema->update();
// Add data
$data = new Data($this->application, $this->flux->getContentType(), $arrayStream);
$data->insert();
} }
/** /**
......
...@@ -74,16 +74,16 @@ class ContentTypeFile ...@@ -74,16 +74,16 @@ class ContentTypeFile
$fields = []; $fields = [];
foreach ($this->keys as $key) { foreach ($this->keys as $key) {
$fields[$key] = [ $fields[strtolower($key)] = [
'type' => 'text', 'type' => 'text',
'label' => $this->getFieldLabel($key) 'label' => $this->getFieldLabel($key),
'readonly' => true
]; ];
} }
$fields['slug'] = [ $fields['slug'] = [
'type' => 'slug', 'type' => 'slug',
'uses' => 'SyndicObjectID', 'uses' => ['syndicobjectid']
'group' => 'meta'
]; ];
return $fields; return $fields;
......
...@@ -2,7 +2,82 @@ ...@@ -2,7 +2,82 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Utils; namespace Bolt\Extension\Appolo\Tourinsoft\Utils;
use Bolt\Application;
use Bolt\Storage\Entity\Builder;
class Data class Data
{ {
/**
* @var Application
*/
private $application;
/**
* @var array
*/
private $arrayStream;
/**
* @var string
*/
private $contentType;
/**
* Data constructor.
* @param Application $application
* @param string $contentType
* @param array $arrayStream
*/
public function __construct(Application $application, $contentType = '', array $arrayStream = []) {
$this->application = $application;
$this->arrayStream = $arrayStream;
$this->contentType = $contentType;
}
/**
* Insert data in database
*/
public function insert() {
$this->deleteData();
$items = $this->getFormattedItems();
foreach ($items as $k => $item) {
$this->application['storage.request.save']->action($item, $this->getContentType(), null, null, null, null);
}
}
/**
* Delete data from contenttype table
*/
private function deleteData() {
$contentTypeRepository = $this->application['storage']->getRepository($this->contentType);
$deleteFieldQuery = $contentTypeRepository->createQueryBuilder()
->delete($contentTypeRepository->getTableName());
$deleteFieldQuery->execute();
}
/**
* @return mixed
*/
private function getContentType() {
return $this->application['storage']->getContentType($this->contentType);
}
/**
* @return array
*/
private function getFormattedItems() {
$items = [];
foreach ($this->arrayStream['value'] as $item) {
$data = [];
foreach ($item as $k => $v) {
$key = strtolower($k);
$data[$key] = $v;
}
$data['status'] = 'published';
$data['date_publish'] = new \DateTimeImmutable();
array_push($items, $data);
}
return $items;
}
} }
\ No newline at end of file
...@@ -52,6 +52,13 @@ class Parser ...@@ -52,6 +52,13 @@ class Parser
} }
/** /**
* @return array
*/
public function getArrayStream() {
return $this->arrayStream;
}
/**
* @throws \Exception * @throws \Exception
*/ */
protected function getStreamAsArray() { protected function getStreamAsArray() {
......
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