Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
anis
anis-server
Commits
acc50422
Commit
acc50422
authored
Jul 18, 2019
by
François Agneray
Browse files
Fixed
#32
parent
85738c47
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/Action/Search/SearchAction.php
View file @
acc50422
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment