Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
customQueries
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bolt Extensions
customQueries
Commits
f6800325
Commit
f6800325
authored
Dec 04, 2018
by
Van
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(all) : Add count results after save and refresh
parent
179d70d4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
76 deletions
+91
-76
CustomQueriesExtension.php
src/CustomQueriesExtension.php
+76
-73
app.css
web/css/app.css
+6
-0
app.js
web/js/app.js
+9
-3
No files found.
src/CustomQueriesExtension.php
View file @
f6800325
...
...
@@ -5,8 +5,11 @@ namespace Bolt\Extension\Appolo\CustomQueries;
use
Bolt\Asset\File\JavaScript
;
use
Bolt\Asset\File\Stylesheet
;
use
Bolt\Controller\Zone
;
use
Bolt\Events\StorageEvent
;
use
Bolt\Events\StorageEvents
;
use
Bolt\Extension\Appolo\CustomQueries\Controller\Backend\CustomQueriesController
;
use
Bolt\Extension\SimpleExtension
;
use
Silex\Application
;
/**
* CustomQueries extension class.
...
...
@@ -15,7 +18,44 @@ use Bolt\Extension\SimpleExtension;
*/
class
CustomQueriesExtension
extends
SimpleExtension
{
/**
* @param Application $app
*/
protected
function
registerServices
(
Application
$app
)
{
parent
::
registerServices
(
$app
);
$app
[
'storage'
]
->
getEventManager
()
->
addListener
(
StorageEvents
::
PRE_SAVE
,
[
$this
,
'updateContentEvent'
]);
}
public
function
updateContentEvent
(
StorageEvent
$event
)
{
/** @var \Bolt\Storage\Entity\Content $subject */
$subject
=
$event
->
getSubject
();
/** @var \Bolt\Storage\Mapping\ContentType $contentType */
$contentType
=
$subject
->
getContentType
();
$fields
=
$contentType
->
getFields
();
$customQueryFields
=
[];
foreach
(
$fields
as
$key
=>
$field
)
{
if
(
$field
[
'class'
]
===
'customQuery'
)
{
$customQueryFields
[
$key
]
=
true
;
break
;
}
}
if
(
!
empty
(
$customQueryFields
))
{
foreach
(
$customQueryFields
as
$field
=>
$customQueryField
)
{
$fieldJson
=
$subject
->
get
(
$field
);
if
(
$fieldJson
)
{
$jsonArray
=
\json_decode
(
$fieldJson
,
true
);
$jsonArray
[
'count'
]
=
$this
->
countGroupedByContent
(
$fieldJson
);
$newFieldJson
=
\json_encode
(
$jsonArray
);
$subject
->
set
(
$field
,
$newFieldJson
);
}
}
}
}
/**
* {@inheritdoc}
*/
...
...
@@ -72,44 +112,7 @@ class CustomQueriesExtension extends SimpleExtension
*/
public
function
getQueriesResults
(
$input
)
{
if
(
$input
==
''
)
{
return
''
;
}
$json
=
json_decode
(
$input
,
true
);
if
(
!
$json
||
empty
(
$json
))
{
return
''
;
}
$records
=
[];
$storage
=
$this
->
getContainer
()[
'storage'
];
foreach
(
$json
as
$contentType
=>
$fields
)
{
$query
=
false
;
if
(
$contentType
!=
'all'
){
$query
=
$this
->
_buildQuery
(
$json
,
$contentType
,
$fields
);
}
if
(
!
empty
(
$query
))
{
$rows
=
$this
->
getContainer
()[
'db'
]
->
fetchAll
(
$query
);
$ids
=
array_map
(
function
(
$rows
)
{
return
$rows
[
'id'
];
},
$rows
);
$contents
=
$storage
->
getContent
(
$contentType
,
[
'id'
=>
implode
(
' || '
,
$ids
),
'paging'
=>
true
,
'limit'
=>
99999
]);
if
(
is_array
(
$contents
))
{
$records
=
array_merge
(
$records
,
$contents
);
}
else
{
$records
=
array_merge
(
$records
,
[
$contents
]);
}
}
}
$records
=
$this
->
getResultsAsArray
(
$input
);
return
$this
->
getContainer
()[
'twig'
]
->
render
(
"custom_queries.twig"
,
[
'records'
=>
$records
]);
}
...
...
@@ -118,40 +121,28 @@ class CustomQueriesExtension extends SimpleExtension
*/
public
function
queriesArrayResults
(
$input
)
{
if
(
$input
==
''
)
{
return
''
;
}
$json
=
json_decode
(
$input
,
true
);
if
(
!
$json
||
empty
(
$json
))
{
return
''
;
}
$records
=
[];
$storage
=
$this
->
getContainer
()[
'storage'
];
foreach
(
$json
as
$contentType
=>
$fields
)
{
$query
=
$this
->
_buildQuery
(
$contentType
,
$fields
);
if
(
!
empty
(
$query
))
{
$rows
=
$this
->
getContainer
()[
'db'
]
->
fetchAll
(
$query
);
$ids
=
array_map
(
function
(
$rows
)
{
return
$rows
[
'id'
];
},
$rows
);
$contents
=
$storage
->
getContent
(
$contentType
,
[
'id'
=>
implode
(
' || '
,
$ids
),
'paging'
=>
true
,
'limit'
=>
99999
]);
if
(
is_array
(
$contents
))
{
$records
=
array_merge
(
$records
,
$contents
);
}
else
{
$records
=
array_merge
(
$records
,
[
$contents
]);
}
$records
=
$this
->
getResultsAsArray
(
$input
);
return
[
'records'
=>
$records
];
}
/**
* @param $input
* @return array
*/
public
function
countGroupedByContent
(
$input
)
{
$recordsCounted
=
[];
$records
=
$this
->
getResultsAsArray
(
$input
);
/** @var \Bolt\Legacy\Content $record */
foreach
(
$records
as
$record
)
{
$slug
=
$record
->
contenttype
[
'slug'
];
if
(
!
isset
(
$recordsCounted
[
$slug
]))
{
$recordsCounted
[
$slug
]
=
0
;
}
}
return
[
'records'
=>
$records
];
$recordsCounted
[
$slug
]
++
;
}
return
$recordsCounted
;
}
/**
...
...
@@ -159,11 +150,21 @@ class CustomQueriesExtension extends SimpleExtension
*/
public
function
countResults
(
$input
)
{
$records
=
$this
->
getResultsAsArray
(
$input
);
return
count
(
$records
);
}
/**
* @param $input
* @return array|string
*/
protected
function
getResultsAsArray
(
$input
)
{
if
(
$input
==
''
)
{
return
''
;
}
$json
=
json_decode
(
$input
,
true
);
if
(
!
$json
||
empty
(
$json
))
{
return
''
;
}
...
...
@@ -172,7 +173,10 @@ class CustomQueriesExtension extends SimpleExtension
$storage
=
$this
->
getContainer
()[
'storage'
];
foreach
(
$json
as
$contentType
=>
$fields
)
{
$query
=
$this
->
_buildQuery
(
$contentType
,
$fields
);
$query
=
false
;
if
(
$contentType
!=
'all'
&&
$contentType
!=
'count'
){
$query
=
$this
->
_buildQuery
(
$json
,
$contentType
,
$fields
);
}
if
(
!
empty
(
$query
))
{
...
...
@@ -184,15 +188,14 @@ class CustomQueriesExtension extends SimpleExtension
$contents
=
$storage
->
getContent
(
$contentType
,
[
'id'
=>
implode
(
' || '
,
$ids
),
'paging'
=>
true
,
'limit'
=>
99999
]);
if
(
is_array
(
$contents
))
{
$records
=
array_merge
(
$records
,
$contents
);
}
else
{
$records
=
array_merge
(
$records
,
[
$contents
]);
}
}
}
return
count
(
$records
)
;
return
$records
;
}
/**
...
...
web/css/app.css
View file @
f6800325
...
...
@@ -22,6 +22,12 @@
text-align
:
center
!important
;
}
.chooseContent
{
height
:
26px
!important
;
width
:
20em
!important
;
text-align
:
center
!important
;
}
.select2-container
{
text-align
:
left
;
}
...
...
web/js/app.js
View file @
f6800325
...
...
@@ -55,14 +55,13 @@ class CustomQueries {
/*##########################################
Load original content
##########################################*/
/**
* INIT JSON content for content type
*/
loadOriginalContent
()
{
let
content
=
this
.
json
;
for
(
let
index
in
content
)
{
if
(
index
!=
'all'
)
{
if
(
index
!=
'all'
&&
index
!=
'count'
)
{
this
.
createContentTypeBlock
(
index
);
this
.
loadOriginalField
(
index
,
content
[
index
],
content
)
}
...
...
@@ -115,7 +114,7 @@ class CustomQueries {
<div class="addContentTypeContainer addContentTypeContainer-
${
this
.
name
}
">
<button class="btn btn-primary" type="button" id="addContentType-
${
this
.
name
}
"><i class="fa fa-plus"></i> Ajouter un type de contenu</button>
<div class="form-inline" style="display: none;">
<select class="form-control"></select>
<select class="form-control
chooseContent
"></select>
<button type="button" class="btn btn-primary" id="validateContentType-
${
this
.
name
}
">Valider</button>
<button type="button" class="btn btn-danger" id="cancelContentType-
${
this
.
name
}
">Annuler</button>
</div>
...
...
@@ -133,6 +132,7 @@ class CustomQueries {
createContentTypeBlock
(
contentType
)
{
if
(
!
this
.
usedContentType
[
contentType
]
&&
contentType
)
{
this
.
usedContentType
[
contentType
]
=
[];
let
count
=
this
.
json
[
'count'
][
contentType
];
let
checked
=
""
;
if
(
jQuery
.
inArray
(
contentType
,
this
.
json
[
'all'
])
!==
-
1
)
{
checked
=
"checked"
;
...
...
@@ -144,7 +144,9 @@ class CustomQueries {
<div class="panel-heading text-right">
<div class="pull-left title">
${
contentType
}
<h6 style="float: right; margin-left: 1em">
${(
count
)
?
count
:
0
}
résultat
${(
count
>
1
?
's'
:
''
)}
</h6>
</div>
<div class="btn-group">
<div class="headField form-inline" style="display: none;">
<div>
...
...
@@ -301,6 +303,9 @@ class CustomQueries {
this
.
fillContentTypeSelector
(
index
);
}
}
setTimeout
(
function
()
{
$
(
'.chooseContent'
).
select2
();
},
1
);
$
(
this
.
selectors
.
selectContentType
).
parent
().
show
();
});
...
...
@@ -484,6 +489,7 @@ class CustomQueries {
/*console.log(json);*/
this
.
element
.
val
(
JSON
.
stringify
(
json
));
}
}
...
...
Write
Preview
Markdown
is supported
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