Commit 634082d8 authored by François Agneray's avatar François Agneray
Browse files

Merge branch '32-les-donnees-de-type-json-sont-mal-formatees' into 'develop'

Fixed #32

Closes #32

See merge request !28
parents 85738c47 acc50422
Pipeline #1338 passed with stages
in 9 minutes and 6 seconds
......@@ -16,6 +16,7 @@ use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use PDO;
use App\Utils\ActionTrait;
use App\Utils\DBALConnectionFactory;
......@@ -144,7 +145,7 @@ final class SearchAction
$listOfIds = explode(';', $queryParams['a']);
if ($searchType === 'data') {
$this->select($queryBuilder, $dataset, $listOfIds);
$attributes = $this->select($queryBuilder, $dataset, $listOfIds);
if (array_key_exists('o', $queryParams)) {
$this->order($queryBuilder, $dataset, explode(';', $queryParams['o']));
......@@ -154,10 +155,11 @@ final class SearchAction
$this->limit($queryBuilder, $queryParams['p']);
}
$result = $this->fetchAll($queryBuilder);
$result = $this->fetchAll($queryBuilder, $attributes);
} else if ($searchType === 'meta') {
$queryBuilder->select('COUNT(*) as nb');
$count = $this->fetchAll($queryBuilder);
$stmt = $queryBuilder->execute();
$count = $stmt->fetchAll();
$result = array();
$result['dataset_selected'] = $dataset->getLabel();
$attributesSelected = array();
......@@ -192,14 +194,18 @@ final class SearchAction
return $type;
}
private function select(QueryBuilder $queryBuilder, Dataset $dataset, array $listOfIds): void
private function select(QueryBuilder $queryBuilder, Dataset $dataset, array $listOfIds): array
{
$columns = array();
$attributes = array();
foreach ($listOfIds as $id) {
$attribute = $this->getAttribute($dataset, (int) $id);
$columns[] = $attribute->getTableName() . '.' . $attribute->getName() . ' as ' . $attribute->getLabel();
$attributes[] = $attribute;
}
$queryBuilder->select($columns);
return $attributes;
}
private function where(QueryBuilder $queryBuilder, Dataset $dataset, array $criteria): void
......@@ -253,10 +259,30 @@ final class SearchAction
->setMaxResults($limit);
}
private function fetchAll(QueryBuilder $queryBuilder): array
private function fetchAll(QueryBuilder $queryBuilder, array $attributes): array
{
$jsonAttributes = $this->getAttributesOfTypeJson($attributes);
$stmt = $queryBuilder->execute();
return $stmt->fetchAll();
$rows = $stmt->fetchAll();
foreach ($rows as &$row) {
foreach ($row as $key => &$column) {
if (array_search($key, $jsonAttributes)) {
if (!is_null($column)) {
$row[$key] = json_decode($column, true);
}
}
}
}
return $rows;
}
private function getAttributesOfTypeJson(array $attributes): array
{
return array_map(function($attribute) {
return $attribute->getLabel();
}, array_filter($attributes, function($attribute) {
return $attribute->getType() === 'json';
}));
}
private function getAttribute(Dataset $dataset, int $id): Attribute
......
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